Skip to content
This repository was archived by the owner on Oct 27, 2021. It is now read-only.

Commit 3457a3a

Browse files
tpopedchelimsky
authored andcommitted
Remove special case output to STDOUT
The spec formatter checks that the output is a tty to decide whether to use colourized formatting but then bypasses this check if STDOUT is used. This means spec > output.log has undesirable escape sequences. The only purpose for this special casing seems to be preventing the closure of STDOUT. Localize this special case to #close so #output_to_tty? works as expected.
1 parent 4ce2b04 commit 3457a3a

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

lib/spec/runner/formatter/base_text_formatter.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ def initialize(options, where)
1515
super
1616
if where.is_a?(String)
1717
@output = File.open(where, 'w')
18-
elsif where == STDOUT
19-
@output = Kernel
20-
def @output.flush
21-
STDOUT.flush
22-
end
2318
else
2419
@output = where
2520
end
@@ -81,7 +76,7 @@ def dump_pending
8176
end
8277

8378
def close
84-
if IO === @output
79+
if IO === @output && @output != STDOUT
8580
@output.close
8681
end
8782
end
@@ -112,7 +107,7 @@ def colour(text, colour_code)
112107

113108
def output_to_tty?
114109
begin
115-
@output == Kernel || @output.tty?
110+
@output.tty?
116111
rescue NoMethodError
117112
false
118113
end

spec/spec/runner/formatter/story/plain_text_formatter_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,26 @@ module Story
320320
@out.string.should include('When I say hey (SKIPPED)')
321321
end
322322

323+
it "should use colour codes on a tty" do
324+
@out.stub!(:tty?).and_return(true)
325+
@options.stub!(:colour).and_return(true)
326+
327+
@formatter.scenario_started('','')
328+
@formatter.step_succeeded(:given, 'a context')
329+
330+
@out.string.should =~ /\e/m
331+
end
332+
333+
it "should not use colour codes on a non-tty" do
334+
@out.stub!(:tty?).and_return(false)
335+
@options.stub!(:colour).and_return(true)
336+
337+
@formatter.scenario_started('','')
338+
@formatter.step_succeeded(:given, 'a context')
339+
340+
@out.string.should_not =~ /\e/m
341+
end
342+
323343
it "should print steps which succeeded in green" do
324344
@out.stub!(:tty?).and_return(true)
325345
@options.stub!(:colour).and_return(true)

0 commit comments

Comments
 (0)