RSpec icon indicating copy to clipboard operation
RSpec copied to clipboard

Make it possible to configure the rspec command used by the build system

Open valo opened this issue 11 years ago • 32 comments
trafficstars

I am trying to use the build system on a rails 4.1 project. I am using RVM and the build system is failing with

[Errno 2] No such file or directory: 'rspec'
....
[path: /usr/bin:/bin:/usr/sbin:/sbin]
[Finished]

It will be nice to be able to override the cmd in the build system to use ./bin/rspec instead of just rspec.

valo avatar Nov 21 '14 13:11 valo

Hi @valo , since the build command itself is just a configuration that is used by Sublime directly, I can't think of a way to dynamically do it.

One easy way to work around it is to copy the build file and paste it as [Sublime]/Packages/User/RSpec/RSpec.sublime-build and modify it to meet your needs.

Hope this helps!

princemaple avatar Nov 22 '14 11:11 princemaple

Actually an idea just came up, I will do some research and see if there's any better way to write the build functionality.

princemaple avatar Nov 22 '14 11:11 princemaple

Thanks for the suggestion @princemaple! I looked at the Sublime docs briefly yesterday with hope it will be possible to use a config variable in the build file, but I am not sure this is possible. Is this really the case? If you can reference a variable from the RSpec config in the build file it will be possible to resolve that easily.

If you can't use such a variable it will be a real pity... This will mean that the build files are pretty much carved in stone...

valo avatar Nov 22 '14 11:11 valo

Normally I just config the build file to a very general setting and modify it in the project setting if there's anything special in the project. I think this is how Sublime works. Anyway, I'll look into a more configurable solution :)

princemaple avatar Nov 22 '14 11:11 princemaple

What do you think about the variants part of https://sublime-text-unofficial-documentation.readthedocs.org/en/latest/reference/build_systems.html ?

princemaple avatar Nov 22 '14 11:11 princemaple

Also, could you provide me with the cmd that should be run when using rvm?

princemaple avatar Nov 22 '14 11:11 princemaple

I will have a look at the variants later today. The command I use is ./bin/rspec $file from the project root. This I believe is the standard way to run rspec for the latest versions in rails.

valo avatar Nov 22 '14 11:11 valo

Another very widespread way to run your specs is with bundle exec rspec $file

valo avatar Nov 22 '14 11:11 valo

Right. My own RSpec build in User/RSpec is actually with bundle exec. :)

princemaple avatar Nov 22 '14 11:11 princemaple

Here is an example for build variants, but it is still far from perfect. It will be great if you can have access to ENV variables like HOME and also to auto-detect the variant for the default build command. The way you can execute them at the moment is with Cmd-Ship-P "Build:"

{
  "cmd": ["./bin/rspec", "-I ${file_path}", "$file"],
  "file_regex": "# ([A-Za-z:0-9_./ ]+rb):([0-9]+)",
  "working_dir": "${project_path:${folder:${file_path}}}",
  "selector": "source.ruby.rspec",

  "windows": {
    "cmd": ["rspec.bat", "-I ${file_path}", "$file"]
  },

  "variants": [
    {
      "name": "Run with RVM",
      "cmd": ["/Users/Valentin/.rvm/bin/rvm-auto-ruby", "./bin/rspec", "-I ${file_path}", "$file"],
      "shell": true
    },
    {
      "name": "Run global",
      "cmd": ["rspec", "-I ${file_path}", "$file"],
      "shell": true
    }
  ]
}

I think there could be a utility script that detects which rspec you have and use that. Something that probes first ./bin/rspec, then bundle exec rspec and finally just rspec. And for the RVM part, if it is possible to load the bash profile and execute commands in the project folder it will be done, but I can't find a way to load the environment for these build commands.

valo avatar Dec 07 '14 16:12 valo

Hey @valo thanks for sharing! I've been quite busy recently, will look into this again as soon as possible.

princemaple avatar Dec 09 '14 12:12 princemaple

I'm having trouble getting this to work, too. I use chruby by @postmodern and have tried many different combinations, with no luck.

Latest try:

{
  "env": {
    "PATH": "/usr/local/opt/chruby/bin:${PATH}"
  },
  "cmd": ["chruby-exec", "ruby", "2.2.0", "--", "bundle", "exec", "rspec", "-I ${file_path}", "$file"],
  "file_regex": "# ([A-Za-z:0-9_./ ]+rb):([0-9]+)",
  "working_dir": "${project_path:${folder:${file_path}}}",
  "selector": "source.ruby.rspec"
}

Results in:

zsh:1: command not found: chruby
[Finished in 0.1s with exit code 127]
[cmd: ['chruby-exec', 'ruby', '2.2.0', '--', 'bundle', 'exec', 'rspec', '-I /Users/jh/Code/Rails/project/spec/controllers', '/Users/jh/Code/Rails/project/spec/controllers/products_controller_spec.rb']]
[dir: /Users/jh/Code/Rails/project]
[path: /usr/local/opt/chruby/bin:${PATH}]

Any ideas? Wish I could just spin up my normal shell environment easily and run bundle exec rspec <file>.

jamonholmgren avatar Jan 19 '15 20:01 jamonholmgren

Might have to go with a python command, but will lose many nice things the build system has...

On Tue, 20 Jan 2015 07:05 Jamon Holmgren [email protected] wrote:

I'm having trouble getting this to work, too. I use chruby https://github.com/postmodern/chruby by @postmodern https://github.com/postmodern and have tried many different combinations, with no luck.

Latest try:

{ "env": { "PATH": "/usr/local/opt/chruby/bin:${PATH}" }, "cmd": ["chruby-exec", "ruby", "2.2.0", "--", "bundle", "exec", "rspec", "-I ${file_path}", "$file"],

"file_regex": "# ([A-Za-z:0-9_./ ]+rb):([0-9]+)", "working_dir": "${project_path:${folder:${file_path}}}", "selector": "source.ruby.rspec" }

Results in:

zsh:1: command not found: chruby [Finished in 0.1s with exit code 127] [cmd: ['chruby-exec', 'ruby', '2.2.0', '--', 'bundle', 'exec', 'rspec', '-I /Users/jh/Code/Rails/project/spec/controllers', '/Users/jh/Code/Rails/project/spec/controllers/products_controller_spec.rb']] [dir: /Users/jh/Code/Rails/project] [path: /usr/local/opt/chruby/bin:${PATH}]

Any ideas? Wish I could just spin up my normal shell environment easily and run bundle exec rspec .

— Reply to this email directly or view it on GitHub https://github.com/SublimeText/RSpec/issues/50#issuecomment-70552356.

princemaple avatar Jan 19 '15 21:01 princemaple

I figured out a way to do this in chruby using the RubyTest package instead.

For those of you interested:

Keyboard Shortcuts

[
  { "keys": ["alt+shift+r"], "command": "run_all_ruby_test",
      "context": [ { "key": "selector", "operator": "equal",
         "operand": "source.ruby, source.rspec, text.gherkin.feature"
                } ]
  }, // test file

  { "keys": ["super+shift+t"], "command": "reopen_last_file" }, // un-hijack this key combo
]

RubyTest.sublime-settings

{
  "run_rspec_command": "/Users/jh/rspec_env.sh bundle exec spring rspec {relative_path}",
  "run_single_rspec_command": "/Users/jh/rspec_env.sh bundle exec spring rspec {relative_path}:{line_number}",
  "save_on_run": true
}

Make sure to change the path to match your own user.

~/rspec_env.sh

This file loads the environment. I'm using zsh.

#!/usr/bin/env zsh
source ./.zshrc
$@

I could be doing this in a boneheaded way, but it's working for me, so hopefully it'll help someone else.

jamonholmgren avatar Jan 19 '15 23:01 jamonholmgren

That's basically what I meant by having to go the python command way. Might try and make a cleaner implementation / interface.

On Tue, 20 Jan 2015 10:44 Jamon Holmgren [email protected] wrote:

I figured out a way to do this in chruby using the RubyTest package instead.

For those of you interested:

Keyboard Shortcuts

[ { "keys": ["alt+shift+r"], "command": "run_all_ruby_test", "context": [ { "key": "selector", "operator": "equal", "operand": "source.ruby, source.rspec, text.gherkin.feature" } ] }, // test file

{ "keys": ["super+shift+t"], "command": "reopen_last_file" }, // un-hijack this key combo ]

RubyTest.sublime-settings

{ "run_rspec_command": "/Users/jh/rspec_env.sh bundle exec spring rspec {relative_path}", "run_single_rspec_command": "/Users/jh/rspec_env.sh bundle exec spring rspec {relative_path}:{line_number}", "save_on_run": true }

Make sure to change the path to match your own user.

~/rspec_env.sh

This file loads the environment. I'm using zsh.

#!/usr/bin/env zsh source ./.zshrc $@

I could be doing this in a boneheaded way, but it's working for me, so hopefully it'll help someone else.

— Reply to this email directly or view it on GitHub https://github.com/SublimeText/RSpec/issues/50#issuecomment-70580869.

princemaple avatar Jan 20 '15 00:01 princemaple

Cool, thanks @princemaple. I like this package better than the RubyTest one, so definitely update here if there's a good way to do it in RSpec ST package.

jamonholmgren avatar Jan 20 '15 00:01 jamonholmgren

Update: I'm interested to see what new build system Jon is going to bring us.

princemaple avatar Feb 03 '15 12:02 princemaple

@jamonholmgren and @valo , just FYI, rbenv could make your life much easier.

princemaple avatar Mar 14 '15 02:03 princemaple

@princemaple Thanks for the advice. I'm pretty happy with chruby.

jamonholmgren avatar Mar 14 '15 20:03 jamonholmgren

I'm having the similar problem, but everything worked great until I've installed LiveReload package. Then suddenly after using the build command "Ctrl + B" it gave me this error. I've tried everything (unistalled LiveReload first of course) and can't seem to make it work again like it used to. Any help? I'm using RVM as well.

ristovskiv avatar Mar 26 '15 10:03 ristovskiv

With the new build system that ST3 rolled out recently, I think we can solve this problem by having multiple build files that target the same file type. So everyone can use whatever suits them the best.

@ristovskiv can you tell me what you would have to run manually to get spec running?

princemaple avatar Mar 26 '15 11:03 princemaple

@princemaple Sorry if I didn't understand you properly, you mean what I need to run on the CLI so that the specs would run?

ristovskiv avatar Mar 26 '15 11:03 ristovskiv

Yes, please

On Thu, Mar 26, 2015 at 10:50 PM, Vlatko Ristovski <[email protected]

wrote:

Sorry if I didn't understand you properly, you mean what I need to run on the CLI so that the specs would run?

— Reply to this email directly or view it on GitHub https://github.com/SublimeText/RSpec/issues/50#issuecomment-86479915.

Regards,

Po Chen

princemaple avatar Mar 26 '15 12:03 princemaple

I'm using just "rspec spec/" or just "rspec" for all specs

ristovskiv avatar Mar 26 '15 12:03 ristovskiv

So no special commands for rvm? sorry I use rbenv, am not very familiar how things are done with rvm. rbenv doesn't need any special commands (too), but I've seen people saying you have to run special commands for rvm.

On Thu, Mar 26, 2015 at 11:53 PM, Vlatko Ristovski <[email protected]

wrote:

I'm using just "rspec spec/" or just "rspec" for all specs

— Reply to this email directly or view it on GitHub https://github.com/SublimeText/RSpec/issues/50#issuecomment-86501335.

Regards,

Po Chen

princemaple avatar Mar 26 '15 13:03 princemaple

Nope, at least I've never used a special command to run the specs nor the tests. Anyway one funny thing. I didn't know that there was a new version of ST3 released today. So I've upgraded ST3 to Build 3083 and now everything works fine... I don't have a simple explanation how this happened. I've wasted a lot of time on this but I'm a really stubborn guy, but the problem is here that I've wasted your time as well. So my apologies for that . If something goes wrong I'll update here.

ristovskiv avatar Mar 26 '15 13:03 ristovskiv

Thanks a lot for the update. I think I will spend some time to update the build for rspec package anyway, to take advantage of the new functionalities of ST3.

On Fri, 27 Mar 2015 00:11 Vlatko Ristovski [email protected] wrote:

Nope, at least I've never used a special command to run the specs nor the tests. Anyway one funny thing. I didn't know that there was a new version of ST3 released today. So I've upgraded ST3 to Build 3083 and now everything works fine... I don't have a simple explanation how this happened. I've wasted a lot of time on this but I'm a really stubborn guy, but the problem is here that I've wasted your time as well. So my apologies for that . If something goes wrong I'll update here.

— Reply to this email directly or view it on GitHub https://github.com/SublimeText/RSpec/issues/50#issuecomment-86507459.

princemaple avatar Mar 26 '15 13:03 princemaple

+1 for that. thanks!

ristovskiv avatar Mar 26 '15 13:03 ristovskiv

Any update on this? I'm aiming to leverage spring to run my tests -- at the end of the day basically just means prepending spring to my tests. Any suggested workarounds would be much appreciated.

dylandrop avatar Nov 04 '15 17:11 dylandrop

@dylandrop some time ago I added https://github.com/SublimeText/RSpec/blob/master/RSpec%20-%20Bundle%20Exec.sublime-build to utilize ST3's new build system. I can simply add another one for spring rspec $file, if that is what you want. With spring, I recommend using guard to continuously watch your files and rebuild as soon as anything changes, it's fast with spring in play.

So yeah, send a PR if you want, or just tell me to add another build file for spring rspec.

princemaple avatar Nov 05 '15 00:11 princemaple