On a
previous post I’ve talk about how to get
Sublime Text 2 to honor the .rvmrc
files when executing both regular ruby and rspec.
The solution described there, as pointed out in in the comments, is non-optimal.
Correcting the old post seemed like a lot of work so I will prefer to post something new.
Ok, so you are using Sublime Text 2 and you can use Cmd-B
to execute ruby code, but that ruby code won’t be executed using under the environment dictated by the .rvmrc
in the project which is a pity. The same goes for RSpec, where you usually want to use bundle exec rspec
in addition to .rvmrc
.
I’m going to talk first on how to get regular ruby + RVM from Sublime Text 2 and then I’ll jump onto executing RSpec together with RVM and Bundler from Sublime Text 2.
Ruby
Well this one it’s very easy. You just need to update ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/Ruby/Ruby.sublime-build
to look like
{
"env":{
"PATH":"${HOME}/.rvm/bin:${PATH}"
},
"cmd": ["rvm-auto-ruby", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.ruby"
}
The env
part will add rvm-auto-ruby
into the $PATH
and the cmd
will execute rvm-auto-ruby yourfile.rb
when you press Cmd-B
. You and add options in between rvm-auto-ruby if you want, like this `“cmd”: [“rvm-auto-ruby”, “-S”, “$file”].
So that’s it. Now you can just Cmd-B
a file and it will use rvm-auto-ruby
to run it and that will use the settings in the .rvmrc
file, running the specific version/flavor of ruby of your choice (MRI-1.8.7, MRI-1.9.3, etc).
Rspec
If you are using
RVM, it’s very likely that you are using
bundler too. And then, executing your test from Sublime Text 2 won’t work as expected because you really need to execute bundle exec rspec
instead of just rspec
to make sure that all the gems versions specified in in your bundle (Gemfile) are visible in your enviroment prior to executing rspec.
In order to do that you need to do some modification to the package that you use to run the RSpecs
Sublime Package: RSpec
If you use
RSpec package (You can install it with
Package Manager) then you can run your rspec with Cmd-B
(asumming that you changed already the builder of your project to Rspec, with Tools/Build System/RSpec). But that will execute just rspec without RVM and without bundler. So to get RVM and bundler into play change the ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/RSpec/RSpec.sublime-build
like this
{
"env":{
"PATH":"${HOME}/.rvm/bin:${PATH}"
},
"cmd": ["rvm-auto-ruby","-S", "bundle", "exec", "rspec", "-I ${file_path}", "$file"],
"file_regex": "# ([A-Za-z:0-9_./ ]+rb):([0-9]+)",
"working_dir": "${project_path}",
"selector": "source.ruby",
"windows":
{
"cmd": ["rspec.bat", "-I ${file_path}", "$file"]
}
}
Just a matter of changing "cmd"
to run rvm-auto-ruby -S bundle exec rspec
instead of just rspec
. That will use the ruby from RVM to run bundle exec rspec
.
Sublime Package: RubyTest
I’m assuming that you already used
Package Control to install
RubyTest so that you can use Cmd-Shift-R
/ Cmd-Shift-T
to execute rspec test.
Open ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/RubyTest/RubyTest.sublime-settings
and edit it to look like this
{
"env":{
"PATH":"${HOME}/.rvm/bin:${PATH}"
},
"erb_verify_command": "erb -xT - {file_name} | ruby -c",
"ruby_verify_command": "ruby -c {file_name}",
"run_ruby_unit_command": "ruby -Itest {relative_path}",
"run_single_ruby_unit_command": "ruby -Itest {relative_path} -n '{test_name}'",
"run_cucumber_command": "rvm-auto-ruby -S bundle exec cucumber {relative_path}",
"run_single_cucumber_command": "rvm-auto-ruby -S bundle exec cucumber {relative_path} -l{line_number}",
"run_rspec_command": "rvm-auto-ruby -S bundle exec rspec {relative_path}",
"run_single_rspec_command": "rvm-auto-ruby -S bundle exec rspec {relative_path} -l{line_number}",
"ruby_unit_folder": "test",
"ruby_cucumber_folder": "features",
"ruby_rspec_folder": "spec",
"ruby_use_scratch" : false,
"save_on_run": false,
"ignored_directories": [".git", "vendor", "tmp"],
"hide_panel": false,
"before_callback": "",
"after_callback": ""
}
Here I’ve only edited the "run_rspec_command"
but you can use the same philosophy for the rest of the commands (run_ruby_unit_command
, etc) if you plan to use them.
The idea is that instead of running rspec {relative_path}
you run
rvm-auto-ruby -S bundle exec rspec {relative_path}
. the rvm-auto-ruby
part
loads the RVM and the bundle exec
part makes sure that you use bundler to
load your gems (including maybe a specific version of RSpec itself, if it’s
included in the Gemfile).