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 t开发者_如何学运维here 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
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.
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 !
It's as simple as:
# spec/spec_helper.rb
RSpec.configure do |config|
config.color_enabled = true
end
精彩评论