Setting vim-rspec to use Spring when available

Hi guys, using vim-rspec, I’ve set let g:rspec_command = '!spring rspec {spec}' in my vimrc to use spring to run my tests. I also have some gem projects I work on, and I’d like to be able to use the plugin to run my tests there. Problem is, I obviously get errors saying Spring doesn’t exist.

I’d like to work out a way to tell if spring can be run in the current project, any ideas?

Thanks!

Hey @aaronmcadam, what happens when you type spring rspec at the terminal?

@geoffharcourt What I’d expect, when I’m on my gem’s gemset, I get command not found: spring. On my Rails project it works of course.

Ah, are you using RVM? Zero experience there, sorry.

@geoffharcourt I think you might have misunderstood my question. I’m trying to figure out how I could check for spring. Maybe something like “if command -v spring isn’t an empty string, then use spring, otherwise use bundle exec”. It’s more a question of how you’d do it in vimscript/vimrc

Ok, so I’ve got a zsh function that works standalone, but now my vim can’t see the function. I’m using stock vim with vundle, copying some settings from GitHub - thoughtbot/dotfiles: A set of vim, zsh, git, and tmux configuration files.. I can run the function with the vim-run-interactive plugin though.

I use Spring’s binstubs to make this seamless. Here’s what you can do:

  1. Add ./bin to your PATH. We do this safely in our dotfiles.
  2. Run spring binstub rspec in your project.
  3. Just run rspec instead of spring rspec everywhere.

You’ll hit the binstub in projects which have one, and you’ll hit the regular rspec command on projects which don’t.

5 Likes

Ah thanks @jferris! I was hoping something like that would work. Really smart way of adding “trusted” repos too!

I use a similar approach to @jferris but with direnv which is mentioned on the Spring README.

I’m confused about one thing though, I’ve been using GitHub - jonleighton/spring-commands-rspec on all my Rails 4 projects so far - what’s the difference between that and just running ‘spring binstub rspec’?

@andyw8 Are you inferring by that that you can spring binstub any gem’s executable (that boots Rails of course)?

Regards your question, looking at one of the commit messages: “Generate binstub for rspec-core instead of rspec

You can’t run spring binstub rspec without also having spring-commands-rspec. Without the gem, you’ll get this error:

The ‘rspec’ command is not known to spring.

The difference is:

  • The spring-commands-rspec gem tell spring how to use RSpec
  • The spring binstub rspec command creates an executable file that runs RSpec and preloads using Spring

@jferris and others, do any of you have a problem with the spring RSpec binstub where if you add arguments to RSpec (like --profile) without specifying a directory that no tests get run? This isn’t a problem when you test from vim, but I find if I want to change options from the command line either with profiling or failing fast that I have to specify the path. It’s not clear to me from the binstub code why this is.

Great solution @jferris.
I’m having trouble with RVM + oh-my-zsh, because it changes PATH and the only solution I found to override is by running source ~/.zshrc.
When I open a new terminal, it loads:

PATH=/Users/thiagoborges/.rvm/gems/ruby-2.1.1/bin:/Users/thiagoborges/.rvm/rubies/ruby-2.1.1/bin:/Users/thiagoborges/.rvm/bin:/usr/local/heroku/bin:.git/safe/../../bin:

After source ~/.zshrc it is:

PATH=.git/safe/../../bin:/Applications/Postgres.app/Contents/Versions/9.3/bin: 

and RVM becomes the last one.

@thiagoborges If you’re having trouble, you can try direnv as an alternative, check out my zshrc and my envrc.

I solved my RVM issue.
My RVM was loaded on ~/.zlogin, so I inserted export PATH=".git/safe/../../bin:$PATH" after RVM load.

@aaronmcadam I prefer @jferris approach. Thanks anyway.

I’m having trouble with vim-rspec and spring. In an app generated by suspenders, when running specs, bin/rspecgets called and spring gets started, no problem here, but when I change to other app, when running the specs does not starts spring, even though I have created the .git/safe folder and the :Rake command effectively uses spring.
The only solution I have right now is to use call Send_to_Tmux and explicitly use bin/rspec command.
I can’t figure out why is not working, any ideas?