Skip to content

Commit 03bc179

Browse files
committedApr 1, 2012
Search Gemfile.lock instead of Gemfile.
1 parent c7b2afc commit 03bc179

File tree

3 files changed

+119
-22
lines changed

3 files changed

+119
-22
lines changed
 

‎README.md

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
runspec.vim
22
===========
33

4-
A simple Vim plugin to run specs: if the current file ends in `_spec.rb`, run it; if not, guess where the associated spec file is and run that.
4+
A simple Vim plugin to run specs: if the current file ends in `_spec.rb` or
5+
`_test.rb`, run it; if not, guess where the associated spec file is and run
6+
that.
57

6-
The plugin will attempt to automatically discover whether you are using [RSpec](https://www.relishapp.com/rspec) or [minitest/spec](http://docs.seattlerb.org/minitest/MiniTest/Spec.html) and use [Bundler](http://gembundler.com/) (and binstubs) if appropriate.
8+
The plugin will attempt to automatically discover whether you are using
9+
[RSpec](https://www.relishapp.com/rspec) or
10+
[minitest/spec](http://docs.seattlerb.org/minitest/MiniTest/Spec.html) and use
11+
[Bundler](http://gembundler.com/) (and binstubs) if appropriate.
712

813
Installation
914
------------
1015

11-
I recommend using [Vundle](https://github.com/gmarik/vundle) and then you can install the plugin by simply adding the following line to your `.vimrc`:
16+
I recommend using [Vundle](https://github.com/gmarik/vundle) and then you can
17+
install the plugin by simply adding the following line to your `.vimrc`:
1218

1319
```vim
1420
Bundle 'mudge/runspec.vim'
@@ -17,8 +23,16 @@ Bundle 'mudge/runspec.vim'
1723
Usage
1824
-----
1925

20-
By default, the plugin will bind to `<Leader>t` but you can manually map to `<Plug>RunSpecRun` like so:
26+
By default, the plugin will bind to `<Leader>t` if it is not already mapped but
27+
you can manually map to `<Plug>RunSpecRun` like so:
2128

2229
```vim
2330
map <Leader>r <Plug>RunSpecRun
2431
```
32+
33+
Dependencies
34+
------------
35+
36+
In order to detect your test runner accurately, the plugin requires that you are
37+
using Bundler and have a valid `Gemfile.lock` (so that it can work with
38+
`Gemfile`s that only specify `gemspec`).

‎doc/runspec.txt

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@ If the current file is a spec, run it; if not, guess where the spec file is and
44
run that.
55

66
This plugin will attempt to detect whether RSpec or a library like minitest is
7-
being used and run the appropriate command.
7+
being used and run the appropriate command either using binstubs if present or
8+
bundle exec.
89

9-
A very simple mapping is made between a file and its associated spec: the file
10-
name is changed to end in '_spec.rb' and prefixed with 'spec/'. If the path
11-
starts with 'app/' (as it would in a Rails application), this is first stripped.
10+
If the current file is not a spec or test, the plugin will attempt to find the
11+
most appropriate spec or test by taking the current path, changing it to end in
12+
either _spec.rb or _test.rb (depending on the presence of a spec or test
13+
directory) and then searching for that. If nothing comes up, then the plugin
14+
will remove the leading directory and try again, removing directories until only
15+
the file name is left.
16+
17+
e.g. given app/models/user.rb and a spec directory, the plugin will first try
18+
looking for app/models/user_spec.rb anywhere in the current directory. Then it
19+
will try models/user_spec.rb which will successfully find
20+
spec/models/user_spec.rb.
21+
22+
The plugin assumes you are using Bundler to manage dependencies and relies on
23+
searching a valid Gemfile.lock for gems (so it should work even if your Gemfile
24+
only contains "gemspec").
1225

1326
Mappings:
1427
<Leader>t or <Plug>RunSpecRun

‎plugin/runspec.vim

+84-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
1-
" If the current file is a spec, run it; if not, guess where the spec file is
2-
" and run that.
3-
"
4-
" e.g.
5-
" spec/models/person_spec.rb => bin/rspec --no-color spec/models/person_spec.rb
6-
" lib/something.rb => bin/rspec --no-color spec/lib/something_spec.rb
7-
" app/models/person.rb => bin/rspec --no-color spec/models/person_spec.rb
1+
" Vim global plugin for running specs appropriate to the current file.
2+
" Maintainer: Paul Mucur (http://mudge.name)
83

94
if exists('g:loaded_runspec')
105
finish
116
endif
127
let g:loaded_runspec = 1
138

14-
" Attempt to find a test or spec for the given path.
15-
" First take the path, replace the end with a given extension and use findfile() to
16-
" locate it. If it's not found, try stripping directories one-by-one from the
17-
" front of the path until a match is found.
9+
" Internal: Attempt to find a test or spec for the given path. Do this by
10+
" first taking the path, replacing the end with a given extension and using
11+
" findfile() to locate it. If it's not found, try stripping directories one-by-one
12+
" from the front of the path until a match is found.
13+
"
14+
" path - The String path of the file whose spec needs to be found.
15+
" extension - The String extension of the spec (either _spec.rb or _test.rb)
16+
" to find.
17+
"
18+
" Examples
19+
"
20+
" s:Hunt('app/models/user.rb', '_spec.rb')
21+
" # => 'spec/models/user_spec.rb'
22+
"
23+
" s:Hunt('lib/admin/lock.rb', '_test.rb')
24+
" # => 'test/lock_test.rb'
25+
"
26+
" Returns the String path of the matching spec or 0 is none was found.
1827
function s:Hunt(path, extension)
1928
let path = substitute(a:path, '\.rb$', a:extension, '')
2029
let test_path = findfile(path, '**')
@@ -32,14 +41,30 @@ function s:Hunt(path, extension)
3241
return test_path
3342
endfunction
3443

44+
" Internal: Attempt to find a spec for the given path.
45+
"
46+
" path - The String path of the file whose spec needs to be found.
47+
"
48+
" Returns the String path of the matching spec or 0 if none was found.
3549
function s:HuntSpec(path)
3650
return s:Hunt(a:path, '_spec.rb')
3751
endfunction
3852

53+
" Internal: Attempt to find a test for the given path.
54+
"
55+
" path - The String path of the file whose test needs to be found.
56+
"
57+
" Returns the String path of the matching test or 0 if none was found.
3958
function s:HuntTest(path)
4059
return s:Hunt(a:path, '_test.rb')
4160
endfunction
4261

62+
" Internal: The path of the test file relevant to the current path. If this
63+
" file is a test or spec, return that immediately, else go looking for it.
64+
"
65+
" path - The String path of the file whose spec needs to be found.
66+
"
67+
" Returns the String path of the matching spec or 0 if none was found.
4368
function s:SpecPath(path)
4469
let path = a:path
4570

@@ -54,6 +79,16 @@ function s:SpecPath(path)
5479
return path
5580
endfunction
5681

82+
" Internal: Any flags that need passing to ruby to set up the load path. Will
83+
" look for lib, spec and test directories and add them as -I flags when
84+
" appropriate.
85+
"
86+
" Examples
87+
"
88+
" s:LoadPath()
89+
" # => ' -Ilib -Ispec'
90+
"
91+
" Returns the String flags to be passed to ruby.
5792
function s:LoadPath()
5893
let load_path = ''
5994

@@ -72,19 +107,48 @@ function s:LoadPath()
72107
return load_path
73108
endfunction
74109

110+
" Internal: Whether or not Bundler is being used on the current project.
111+
"
112+
" Returns truthy if Gemfile.lock exists, false if not.
75113
function s:HasBundler()
76-
return filereadable('Gemfile')
114+
return filereadable('Gemfile.lock')
77115
endfunction
78116

117+
" Internal: Whether or not a given gem is installed in the current project.
118+
"
119+
" gem - The String gem name to search for.
120+
"
121+
" Examples
122+
"
123+
" s:HasGem('rspec')
124+
" # => 40
125+
"
126+
" s:HasGem('nonesuch')
127+
" # => 0
128+
"
129+
" Returns a truthy value if the gem is present, false if not.
79130
function s:HasGem(gem)
80131
if s:HasBundler()
81-
let gems = join(readfile('Gemfile'))
82-
return match(gems, a:gem) != -1
132+
let gems = join(readfile('Gemfile.lock'))
133+
return match(gems, '\<' . a:gem . '\>') != -1
83134
else
84135
return 0
85136
endif
86137
endfunction
87138

139+
" Internal: The appropriate command to run the tests with. If rspec is
140+
" present, use that (either via Bundler's binstubs or using bundle exec) or
141+
" fall back to calling ruby directly with an appropriate load path.
142+
"
143+
" Examples
144+
"
145+
" s:SpecCommand()
146+
" # => 'bin/rspec --no-color'
147+
"
148+
" s:SpecCommand()
149+
" # => 'ruby -Ilib -Ispec'
150+
"
151+
" Returns the String command.
88152
function s:SpecCommand()
89153
let spec_command = 'ruby' . s:LoadPath()
90154

@@ -101,6 +165,12 @@ function s:SpecCommand()
101165
return spec_command
102166
endfunction
103167

168+
" Public: Save the current file and run its specs or tests. If the file is
169+
" already a spec or test, run it using the appropriate command (rspec through
170+
" Bundler, ruby directly, etc.); if not, find the most appropriate spec or
171+
" test and run that.
172+
"
173+
" Returns nothing.
104174
function s:RunSpec()
105175
write
106176
let path = s:SpecPath(expand('%'))

0 commit comments

Comments
 (0)
Please sign in to comment.