简体   繁体   English

使用单个命令为多个Ruby版本运行RSpec

[英]Run RSpec for more than one Ruby version using single command

I was looking for a way to run 'rake spec' for all ruby versions. 我一直在寻找一种对所有红宝石版本运行“ rake spec”的方法。

So far I wrote this code in Rakefile but it doesn't work: 到目前为止,我已经在Rakefile中编写了此代码,但是它不起作用:

RUBIES = ['ruby-2.0.0', 'ruby-2.1.1', 'ruby-2.2.1']

RUBIES.each do |ruby_v|
  sh "rvm use #{ruby_v}"
  RSpec::Core::RakeTask.new(:spec)
end

Any ideas how may I accomplish this? 有什么想法我该怎么做?

It would be great if I could pass down arguments specifying whether I want to run the tests for a single ruby version or for all. 如果我可以传递参数以指定是针对单个Ruby版本还是针对所有Ruby版本运行测试,那就太好了。

EDIT: 编辑:

Here's the Rakefile I used and the error I am getting. 这是我使用的Rakefile和我得到的错误。 It's not the same file which I mentioned at the top. 它与我在顶部提到的文件不同。 It uses rvm use #{ruby_v}; rspec spec 它使用rvm use #{ruby_v}; rspec spec rvm use #{ruby_v}; rspec spec as suggested by Keith in the answer. Keith在回答中建议的rvm use #{ruby_v}; rspec spec

rvm is a shell command and its Ruby setting applies only to the current instance of the shell. rvm是一个shell命令,其Ruby设置仅适用于该shell的当前实例。 So when you run sh "rvm use #{ruby_v}" you're changing the rvm version of the shell that you have invoked, and then exiting from that shell. 因此,当您运行sh "rvm use #{ruby_v}"您将更改已调用外壳的rvm版本,然后从该外壳退出。

In addition, your Ruby program is an operating system process running the Ruby virtual machine, so when you make the call to RSpec::Core::RakeTask.new(:spec) , you're still in that same OS process and Ruby version with which you started your script. 另外,您的Ruby程序是一个运行Ruby虚拟机的操作系统进程,因此,当您调用RSpec::Core::RakeTask.new(:spec) ,您仍处于同一OS进程和Ruby版本中通过它启动脚本。

You'll need to run the rspec tests in the shell that you invoke and change the Ruby version on. 您需要在调用的Shell中运行rspec测试并更改Ruby版本。 I thought something like this would work: 我认为这样会起作用:

`rvm use #{ruby_v}; rspec spec`

...but as you pointed out, it does not. ...但是正如您所指出的,事实并非如此。 You need to run the new shell as a "login shell" so that rvm is set up correctly. 您需要将新的外壳程序作为“登录外壳程序”运行,以便正确设置rvm。 In addition, the new shell must be told that the thing you're invoking is a shell command and not a script or binary executable. 此外,必须告知新的Shell,您正在调用的东西是Shell命令,而不是脚本或二进制可执行文件。 In other words: 换一种说法:

1) have your command explicitly invoke bash or zsh ( sh did not work for me on my Mac). 1)让您的命令显式调用bashzsh (在Mac上sh对我不起作用)。

2) specify (probably with -l ) that it is a login shell. 2)指定(可能是-l )它是一个登录shell。

3) specify (probably with -c ) that you are executing a shell command ( rvm ) and not a script or executable. 3)指定(可能使用-c )您正在执行shell命令( rvm ),而不是脚本或可执行文件。

I am using zsh as my shell, but you should be able to substitute bash in the code below and it should work (and of course put your rspec command in there): 我使用zsh作为我的外壳,但是您应该能够在下面的代码中替换bash并且它应该可以工作(当然,也可以将rspec命令放在其中):

2.3.0 :024 > puts `zsh -lc "rvm current"`
ruby-2.3.0
 => nil
2.3.0 :025 > puts `zsh -lc "rvm use jruby; rvm current"`
Using /Users/kbennett/.rvm/gems/jruby-9.0.5.0
jruby-9.0.5.0
 => nil
2.3.0 :026 > puts `zsh -lc "rvm current"`
ruby-2.3.0
 => nil

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM