Shamaoke
Shamaoke

Reputation: 6282

RSpec. How to color the piped or redirected output?

In versions of RSpec before 2.0 I could pipe the color output to less or redirect it to a file. In order to do it I simply have to set the RSPEC_COLOR environment variable to true. However, in the new main version of the framework this variable has stopped to define the output type (color or monchrome). Is there any way to pipe or redirect the color in RSpec 2.0 and higher?

Thanks.

Debian GNU/Linux 5.0.7;

Ruby 1.9.2;

RSpec 2.4.0.

Updated


I found the answer by myself.

One should use the tty configuration option to achieve the effect.

Here's the example:

# spec/spec_helper.rb

RSpec.configure do |config|
  config.tty = true
end

Upvotes: 5

Views: 1879

Answers (3)

Dorian
Dorian

Reputation: 23969

The answer in the question is the correct one:

# spec/spec_helper.rb

RSpec.configure do |config|
  config.tty = true
end

Then rspec | grep --color="never" something keeps the coloring.

Upvotes: 2

Sam Soffes
Sam Soffes

Reputation: 14935

It's as simple as:

# spec/spec_helper.rb

RSpec.configure do |config|
  config.color_enabled = true
end

Upvotes: 1

Oct
Oct

Reputation: 1525

By looking at the sources, it seems that the color_enabled configuration option is now in the Configuration module of RSpec. However, if the output is not done to a tty, color is disabled.

My suggestion would be to set color_enabled = true and to monkey patch the RSpec Configuration module so that is works even when not outputting to a tty:

module RSpec   
  module Core
    class Configuration
      def color_enabled
        true
      end
    end
  end
end

This is not the nicest way, though. This is also untested and I think that monkey patching rspec is not the easiest thing to do because usually tests are run via the dedicated command line tool.

Maybe you could open a bug report to the maintainer and ask for a force_color_enabled option ? It would probably be very quick to implement...

Good Luck and Happy Coding !

Upvotes: 1

Related Questions