当从Ruby脚本作为命令%x [rspec]运行时,如何将RSpec输出设置为控制台?

时间:2021-12-02 01:10:35

I have a class with an instance method that runs RSpec using the %x[] notation:

我有一个类使用%x []表示法运行RSpec的实例方法:

class TestRunner
  def run_rspec
    # do stuff
    %x[rspec spec -c -f documentation]
    # do more stuff
  end
end

When I do this:

我这样做的时候:

> tr = TestRunner.new
> tr.run_rspec

The documentation (group and example names) does not appear in the console.

文档(组和示例名称)不会出现在控制台中。

To contrast, when I run rspec straight from the command line I get this:

相比之下,当我直接从命令行运行rspec时,我得到了这个:

$ rspec spec -c -f documentation

  a group name
    an example
    another example
    ...

I don't want to do this:

我不想这样做:

puts %x[rspec spec -c -f documentation

Because then the output all spits out in one huge clump at the very end. I want it to run in "real time," with each example showing up as each test is run.

因为那时输出都会在最后一个巨大的丛中吐出来。我希望它能够“实时”运行,每个示例都会在每个测试运行时显示出来。

Is there a way, with the setup I have, to get RSpec to announce what it's doing, as it's doing it (as it does when run normally from the command line)?

有没有办法,通过我的设置,让RSpec宣布它在做什么,就像它正在做的那样(正如从命令行正常运行时那样)?

1 个解决方案

#1


3  

I've been advised that system() and the other shell methods can be dangerous to use, so I've opted to switch to the even-better approach of using RSpec itself:

我被告知system()和其他shell方法使用起来很危险,所以我选择转向使用RSpec本身的更好的方法:

RSpec::Core::Runner.run(['spec', '-c', '-f', 'documentation'])

rather than calling it via shell from my Ruby script.

而不是从我的Ruby脚本通过shell调用它。


Ruby offers several options for running programs from the command line. I was using %x[], the wrong choice for my use case.

Ruby提供了几种从命令行运行程序的选项。我使用的是%x [],这是我用例的错误选择。

Solution: Use system(), not %x[] -- rspec will write to STDOUT in real-time when I call it with system('rspec spec').

解决方案:使用system(),而不是%x [] - 当我用system('rspec spec')调用它时,rspec将实时写入STDOUT。


Some background in case it's helpful to anyone who stumbles upon this question:

一些背景,如果它对任何偶然发现这个问题的人有帮助:

Consider the differences between Ruby's command-line options:

考虑Ruby的命令行选项之间的差异:

  • %x[command] accumulates the result of command and returns it, in one chunk.
  • %x [command]累积命令的结果并将其返回到一个块中。

  • exec('command') will output command as command runs, but will replace whatever process called it -- i.e., if you use exec in your Ruby script, your Ruby script won't finish.
  • exec('command')将在命令运行时输出命令,但将替换任何称为它的进程 - 即,如果在Ruby脚本中使用exec,则Ruby脚本将无法完成。

  • system('command') executes command in a subshell, and returns to the calling script.
  • system('command')在子shell中执行命令,并返回到调用脚本。

This is why system was the choice for my script.

这就是为什么系统是我的脚本的选择。

#1


3  

I've been advised that system() and the other shell methods can be dangerous to use, so I've opted to switch to the even-better approach of using RSpec itself:

我被告知system()和其他shell方法使用起来很危险,所以我选择转向使用RSpec本身的更好的方法:

RSpec::Core::Runner.run(['spec', '-c', '-f', 'documentation'])

rather than calling it via shell from my Ruby script.

而不是从我的Ruby脚本通过shell调用它。


Ruby offers several options for running programs from the command line. I was using %x[], the wrong choice for my use case.

Ruby提供了几种从命令行运行程序的选项。我使用的是%x [],这是我用例的错误选择。

Solution: Use system(), not %x[] -- rspec will write to STDOUT in real-time when I call it with system('rspec spec').

解决方案:使用system(),而不是%x [] - 当我用system('rspec spec')调用它时,rspec将实时写入STDOUT。


Some background in case it's helpful to anyone who stumbles upon this question:

一些背景,如果它对任何偶然发现这个问题的人有帮助:

Consider the differences between Ruby's command-line options:

考虑Ruby的命令行选项之间的差异:

  • %x[command] accumulates the result of command and returns it, in one chunk.
  • %x [command]累积命令的结果并将其返回到一个块中。

  • exec('command') will output command as command runs, but will replace whatever process called it -- i.e., if you use exec in your Ruby script, your Ruby script won't finish.
  • exec('command')将在命令运行时输出命令,但将替换任何称为它的进程 - 即,如果在Ruby脚本中使用exec,则Ruby脚本将无法完成。

  • system('command') executes command in a subshell, and returns to the calling script.
  • system('command')在子shell中执行命令,并返回到调用脚本。

This is why system was the choice for my script.

这就是为什么系统是我的脚本的选择。