thoughtbot / vim-rspec

Run Rspec specs from Vim
https://robots.thoughtbot.com
MIT License
657 stars 108 forks source link

Cannot Load spec/controllers/spec ????? #40

Closed spinlock99 closed 10 years ago

spinlock99 commented 10 years ago

I have vim-rspec setup and working in "my" projects but I just joined a project that was set up before I go there and I'm getting this strange error when I try to run specs from vim:

/Users/spinlock/.rvm/gems/ruby-2.0.0-p247@hoplu/gems/rspec-core-2.14.7/lib/rspec   /core/configuration.rb:896:in `load': cannot load such file -- /Users/spinlock/rails/hoplu/spec/controllers/spec (LoadError)
from /Users/spinlock/.rvm/gems/ruby-2.0.0-p247@hoplu/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /Users/spinlock/.rvm/gems/ruby-2.0.0-p247@hoplu/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `each'
from /Users/spinlock/.rvm/gems/ruby-2.0.0-p247@hoplu/gems/rspec-core-2.14.7/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /Users/spinlock/.rvm/gems/ruby-2.0.0-p247@hoplu/gems/rspec-core-2.14.7/lib/rspec/core/command_line.rb:22:in `run'
from /Users/spinlock/.rvm/gems/ruby-2.0.0-p247@hoplu/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:80:in `run'
from /Users/spinlock/.rvm/gems/ruby-2.0.0-p247@hoplu/gems/rspec-core-2.14.7/lib/rspec/core/runner.rb:17:in `block in autorun'

Any ideas where to start tracking this down? I can run the specs from the command line but not from withing vim (even with :!rspec).

Any help would be appreciated. Thanks!

gylaz commented 10 years ago

@spinlock99 It looks like vim thinks you're in /Users/spinlock/rails/hoplu/spec/controllers when running rspec. Can you make sure :pwd command within vim prints out `/Users/spinlock/rails/hoplu before triggering the spec run.

spinlock99 commented 10 years ago

@gylaz that's exactly right. I modified a configuration file so that, when I open nerdtree, it opens in the current directory. Any ideas on updating the g:rspec_command to work around that? I've currently got:

let g:rspec_command = "!bundle exec rspec -f d -c {spec}"

I've also noticed that rake spec SPEC... will work so I'm thinking of changing g:rspec_command to use rake but I ran into the same problem of the directory not being correct.

gylaz commented 10 years ago

Any ideas on updating the g:rspec_command to work around that?

I don't have much experience with nerdtree, unfortunately. The only thing that comes to mind is to set some variable as the root directory of the project. Something along the lines of:

let g:rspec_command = "bundle exec rspec -f d -c -I$ROOT_DIR/spec $ROOT_DIR/**/user_spec.rb"
dlynam commented 10 years ago

Running into the same issue. I change my current directory in vimrc for the default NERDtree directory and when I run rspec-vim, the path to the test is one directory too high. @spinlock99 did you ever figure this out?

jferris commented 10 years ago

One option to fix this issue would be to run specs using the full path to the spec. If we change uses of @% with expand("%:p"), it will run rspec /full/path/to/project/spec/whatever_spec.rb instead of just rspec spec/whatever_spec.rb. The command looks a little uglier, but seems to work fine from any directory.

spinlock99 commented 10 years ago

@dlynam I'm making progress but no luck getting this working yet.

@jferris That seems to be working to find the spec but the paths to spec_helper are still not right so it fails to find any files that you require in your spec. Any ideas?

Thanks!

spinlock99 commented 10 years ago

I think this is beginning to make sense to me. If I run a single spec or single file, I'm failing with this error:

1:in `require': cannot load such file -- spec_helper (LoadError)

I can recreate this by cd'ing into spec/ and running rspec my_spec.rb. So, this isn't related to how vim-rspec is running the specs but because rspec doesn't have the current directory in it's load path. Changing require 'spec_helper' to require './spec_helper' fixes this issue.

If I try to run all of the specs, however, I fail with this error:

lib/rspec/core/configuration.rb:896:in `load': cannot load such file -- /<path-to-specs>/spec/spec (LoadError)

This one is related to how vim-rspec is calling the specs:

function! RunAllSpecs()
   let l:spec = "spec"
   call SetLastSpecCommand(l:spec)
   call RunSpecs(l:spec)
endfunction

So, changing let l:spec = "spec" to let l:spec = "." gets all of the specs to run (i.e. we need to run rspec . because we're already in spec/.

jferris commented 10 years ago

The first issue ("cannot load such file -- spec_helper") is because rspec expects you to run specs from the project root. If you want to run them from anywhere else, you'll need to expand the path.

If we expand the path to the actual spec within Vim, that will allow you to run specs regardless of cwd for Vim, but the working directory for the terminal where you run specs will need to be the project root.

spinlock99 commented 10 years ago

@jferris I managed to get the first problem fixed by editing my g:rspec_command in my .vimrc:

let g:rspec_command = "!bundle exec rspec -I . -f d -c {spec}"

-I . adds the current directory to rsepcs include path.

jferris commented 10 years ago

Cool. I think that would be harmless to include in the defaults, since rspec attempts to do that itself from the project root anyway. So:

This will make it so that you can run specs regardless of your working directory.

@gylaz thoughts?

gylaz commented 10 years ago

Sounds good. I'll take a crack at adding this.

gylaz commented 10 years ago

I'm closing this. If rspec is ran anywhere other than the root directory there will need to be proper path's added to RSpec's load path. We don't feel it should be vim-rspec's concern to solve that.

spinlock99 commented 10 years ago

Just sent a pull request with my workaround in it. The key to getting vim-rspec to work if you have autochdir set is to add the cwd to rspec's include path:

let g:rspec_command = "!bundle exec rspec -I . {rspec}"

HTH, Andrew

Edit: fixed typo

lsaffie commented 10 years ago

I have my models living in an engine outside Rails.root When running my tests via vim-rspec, it thinks Rails.root is the engine root understandably since the file lives in there. I've changed the path manually and ensured :pwd returns the actual root of the project but no luck. Also I've done the above, and no luck. I'll post my findings When I get a fix (if)

Let me know if you have any pointers.

L

lsaffie commented 10 years ago

Actually, I think I've found it. It was actually working correctly and the complains were due to bundle exec not being present.

This worked let g:rspec_command = "!bundle exec rspec -I . {rspec}" Thanks @spinlock99 (fixed the typo in this command "g:rsepc_command")

jondavidjohn commented 9 years ago

For anyone that is wanting to run specs from anywhere in your project (not dependant on your cwd being in the root, I'm using this with success. Obviously depends on git to get to the project root.

let g:rspec_command = "SPEC="$PWD/{spec}" && (cd `git rev-parse --show-toplevel` && bundle exec rspec "$SPEC" && unset SPEC)"

It uses a subshell to move to the appropriate directory before running the specs, this does not effect your cwd in vim.

phallguy commented 7 years ago

I ended up with this command that searches for the root spec/ folder and then runs from it's parent.

let g:rspec_command = "!clear; ( cd $(find `( SPEC='{spec}'; CP=${SPEC\\%/*}; while [ -n \"$CP\" ] ; do echo $CP; CP=${CP\\%/*}; done; echo / ) ` -mindepth 1 -maxdepth 1 -type d -name spec)/..; bin/rspec {spec})"

Credit to 'Zarick Lau' for the parent search method http://stackoverflow.com/a/7614803/76456