From 4b13d59c30e48e48ef25c0245c8352fcda04680b Mon Sep 17 00:00:00 2001 From: Michael Campagnaro Date: Wed, 24 Oct 2012 02:51:26 -0400 Subject: [PATCH] Add test script and some slight changes to vimrc --- .vimrc | 66 ++++++++++++++++++++++++------------------------- scripts/test.sh | 36 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 34 deletions(-) create mode 100644 scripts/test.sh diff --git a/.vimrc b/.vimrc index c9ebece..0751f92 100644 --- a/.vimrc +++ b/.vimrc @@ -1,5 +1,3 @@ -let mapleader="," - """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " PLUGINS """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" @@ -188,7 +186,7 @@ function! AlternateForCurrentFile() let new_file = current_file let in_spec = match(current_file, '^spec/') != -1 let going_to_spec = !in_spec - let in_app = match(current_file, '\') != -1 || match(current_file, '\') != -1 || match(current_file, '\') != -1 + let in_app = match(current_file, '\') != -1 || match(current_file, '\') != -1 || match(current_file, '\') != -1 || match(current_file, '\') != -1 if going_to_spec if in_app let new_file = substitute(new_file, '^app/', '', '') @@ -209,32 +207,11 @@ nnoremap . :call OpenTestAlternate() """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" " RUNNING TESTS """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" -function! RunTests(filename) - " Write the file and run tests for the given filename - :w - :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo - :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo - :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo - :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo - :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo - :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo - if match(a:filename, '\.feature$') != -1 - exec ":!script/features " . a:filename - else - if filereadable("script/test") - exec ":!script/test " . a:filename - elseif filereadable("Gemfile") - exec ":!bundle exec rspec --color " . a:filename - else - exec ":!rspec --color " . a:filename - end - end -endfunction - -function! SetTestFile() - " Set the spec file that tests will be run for. - let t:grb_test_file=@% -endfunction +map t :call RunTestFile() +map T :call RunNearestTest() +map a :call RunTests('') +map c :w\|:!script/features +map w :w\|:!script/features --profile wip function! RunTestFile(...) if a:0 @@ -258,8 +235,29 @@ function! RunNearestTest() call RunTestFile(":" . spec_line_number . " -b") endfunction -map t :call RunTestFile() -map T :call RunNearestTest() -map a :call RunTests('') -map c :w\|:!script/features -map w :w\|:!script/features --profile wip +function! SetTestFile() + " Set the spec file that tests will be run for. + let t:grb_test_file=@% +endfunction + +function! RunTests(filename) + " Write the file and run tests for the given filename + :w + :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo + :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo + :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo + :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo + :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo + :silent !echo;echo;echo;echo;echo;echo;echo;echo;echo;echo + if match(a:filename, '\.feature$') != -1 + exec ":!script/features " . a:filename + else + if filereadable("script/test") + exec ":!script/test " . a:filename + elseif filereadable("Gemfile") + exec ":!bundle exec rspec --color " . a:filename + else + exec ":!rspec --color " . a:filename + end + end +endfunction diff --git a/scripts/test.sh b/scripts/test.sh new file mode 100644 index 0000000..75ab3f0 --- /dev/null +++ b/scripts/test.sh @@ -0,0 +1,36 @@ +#!/bin/bash +# +# From Destroy All Software screencast #10, at: +# http://destroyallsoftware.com/screencasts/catalog/fast-tests-with-and-without-rails +# +# Released under the MIT license: http://opensource.org/licenses/MIT +# +# Put this in the script/ directory of your Rails app, then run it with a spec +# filename. If the spec uses spec_helper, it'll be run inside Bundler. +# Otherwise, it'll be run directly with whatever `rspec` executable is on the +# path. + +set -e + +need_rails=1 + +if [ $# -gt 0 ]; then # we have args + filename=$1 + # Remove trailing line numbers from filename, e.g. spec/my_spec.rb:33 + grep_filename=`echo $1 | sed 's/:.*$//g'` + + (set +e; grep -r '\bspec_helper\b' $grep_filename) > /dev/null + if [ $? -eq 1 ]; then # no match; we have a stand-alone spec + need_rails='' + fi +else # we have no args + filename='spec' +fi + +command='rspec' + +if [ $need_rails ]; then + command="ruby -S bundle exec $command" +fi + +RAILS_ENV=test $command $filename \ No newline at end of file