5

I have a ruby script (Guardfile) that executes a rake command.

guard :shell do

  watch(%r{^manifests\/.+\.pp$}) do |m|
    spec = `rake spec`
    retval = $?.to_i
    case retval
    when 0
       if spec.length > 0 then
          puts spec
          n "#{m[0]} Tests Failed!", 'Rake Spec', :pending         
       else
          puts spec
          n "#{m[0]} Tests Passed!", 'Rake Spec', :pending

       end
    end
end

When I run a 'rake spec' from the command line, outputs are colorized. How could I make it so the output of the ruby script is also colorized?

From command line:

enter image description here

From ruby script:

enter image description here

Update

I was able to sort-of work around the problem by using script

bash command preserve color when piping

spec = `script -q /dev/null rake spec`

This still has the downside of not scrolling the text in real time. While it does preserve the colors, it does not output anything until the very end.

Is there a more native way to do this that will allow for scrolling?

3
  • Try using spec = `rake spec --color` Commented Sep 25, 2014 at 21:01
  • good idea, I get this error: invalid option: --color Commented Sep 25, 2014 at 21:09
  • Does creating a file named ".rspec" with the contents of, "--color" work? Commented Sep 26, 2014 at 1:49

2 Answers 2

2

First, rake spec --color won't work, because you're passing --color to rake, and not rspec.

Jay Mitchell's suggestion for color should work - by putting this in your .rspec file:

--color

As for having "live" output, guard-shell has an eager command for this:

https://github.com/guard/guard-shell/blob/master/lib/guard/shell.rb#L37-L51

Unfortunately, guard-shell has 2 important shortcomings:

  • it doesn't give you access to the exit code

  • it doesn't properly report failures in Guard (which causes other tasks to run)

So the eager method of Guard::Shell is useless for our needs here.

Instead, the following should work:

# a version of Guard::Shell's 'eager()' which returns the result
class InPty
  require 'pty'

  def self.run(command)
    PTY.spawn(command) do |r, w, pid|
      begin
        $stdout.puts
        r.each {|line| $stdout.print line }
      rescue Errno::EIO
      end
      Process.wait(pid)
    end

    $?.success?
  rescue PTY::ChildExited
  end
end

# A hack so that Guard::Shell properly throws :task_has_failed
class ProperGuardPluginFailure
  def to_s
    throw :task_has_failed
  end
end

guard :shell, any_return: true do
  watch(%r{^manifests\/.+\.pp$}) do |m|
    ok = InPty.run('rake spec')
    status, type = ok ? ['Passed', :success] : ['Failed', :failed]
    n "#{m[0]} Tests #{status}!", 'Rake Spec', type
    ok ? nil : ProperGuardPluginFailure.new
  end
end

The above looks ideal for a new guard plugin - good idea?

Sign up to request clarification or add additional context in comments.

Comments

-1

I am unfamiliar with Guardfiles. Can you use gems? The colorize gem is great.

https://github.com/fazibear/colorize

Install it:

$ sudo gem install colorize

Use it:

require 'colorize'

puts "Tests failed!".red
puts "Tests passed!".green

1 Comment

Doesn't answer OP's question but it brought me to the colorize gem, which is what I wanted. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.