1

I have the following Rakefile

desc "Runs tests"
namespace :test do
    task :api do
        `mocha`
    end
end

When I run the command rake test:api, I don't get the nice output of dots that I would if I just ran the command mocha.

How do I print the output of a command real-time in a rake task?

Vega
  • 27,856
  • 27
  • 95
  • 103
Shane Stillwell
  • 3,198
  • 5
  • 31
  • 53

1 Answers1

6

You can just put the output:

puts `mocha`

The backticks ` are calling the command mocha and return the output of the command.

You may also use %x{}:

puts %x{mocha}

Or you use system:

system('mocha')

Or you store the output for later use in a variable:

output = `mocha`
puts output
knut
  • 27,320
  • 6
  • 84
  • 112
  • puts `mocha` did the job. Thanks. – Shane Stillwell Dec 13 '12 at 18:20
  • 1
    One thing to keep in mind is that `puts \`mocha\`` won't print in real time - it'll wait until the entire command is done, and then print the final result. Also, loss of color. For those, you should use `system('mocha')` – Cody Poll Dec 01 '15 at 18:02
  • Thanks @CodyPoll, didn't know of the difference between the backticks and system call – abhijit Dec 19 '17 at 00:46