2

In a ruby file:

When I do system("rspec file_spec.rb") I get a nice colored output.

When I do this:

result = `rspec file_spec.rb`
puts result

I get no color at all.

Is there any way to preserve the color?

btw, I'm on OSX with Terminal.app if that matters.

Kostas
  • 8,356
  • 11
  • 47
  • 63
  • Does `rspec -c file_spec.rb` work? I would guess rspec is automatically turning off ANSI color codes because it can't check whether its output supports them. – Edd Steel Feb 22 '11 at 16:43
  • I have `--color` in my rspec file in the rails app root, so the -c option does not matter. – Kostas Feb 22 '11 at 18:54
  • 1
    I don't know how to preserve the color, but it not preserving it makes sense - most of the time, you wouldn't want to parse out the color information that comes from a `` command. – Andrew Grimm Feb 22 '11 at 22:19
  • I think there's another question that asks something similar. – Andrew Grimm Feb 23 '11 at 21:52
  • @Andrew -- yep, see my answer for the link – zetetic Feb 23 '11 at 23:10

1 Answers1

6

From reading the code it looks like RSpec calls IO#isatty on the output stream to decide whether or not to colorize the output. The backquote method must work differently from system in this respect.

EDIT

This works if you add the option --tty to the rspec command:

`rspec --color --tty file_spec.rb`

as mentioned in this SO question.

Community
  • 1
  • 1
zetetic
  • 47,184
  • 10
  • 111
  • 119