I have a class with an instance method that runs RSpec using the %x[] notation:
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 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)?
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:rather than calling it via shell from my Ruby script.
Ruby offers several options for running programs from the command line. I was using
%x[], the wrong choice for my use case.Solution: Use
system(), not%x[]—rspecwill write toSTDOUTin real-time when I call it withsystem('rspec spec').Some background in case it’s helpful to anyone who stumbles upon this question:
Consider the differences between Ruby’s command-line options:
%x[command]accumulates the result ofcommandand returns it, in one chunk.exec('command')will outputcommandascommandruns, but will replace whatever process called it — i.e., if you useexecin your Ruby script, your Ruby script won’t finish.system('command')executescommandin a subshell, and returns to the calling script.This is why
systemwas the choice for my script.