← Back to Upcase

Setting vim-rspec to use Spring when available


(Aaron Mc Adam) #1

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!


(Geoff Harcourt) #2

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


(Aaron Mc Adam) #3

@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.


(Geoff Harcourt) #4

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


(Aaron Mc Adam) #5

@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


(Aaron Mc Adam) #6

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 https://github.com/thoughtbot/dotfiles. I can run the function with the vim-run-interactive plugin though.


(Joe Ferris) #7

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.


(Aaron Mc Adam) #8

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


(Andy Waite) #9

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 https://github.com/jonleighton/spring-commands-rspec on all my Rails 4 projects so far - what’s the difference between that and just running ‘spring binstub rspec’?


(Aaron Mc Adam) #10

@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


(Joe Ferris) #11

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

(Geoff Harcourt) #12

@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.


(Thiago Gabriel B. da Silva) #13

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.


(Aaron Mc Adam) #14

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


(Thiago Gabriel B. da Silva) #15

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.


(Sebastián Vera) #16

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?