Skip to content

Commit

Permalink
Assign error streams before running config options
Browse files Browse the repository at this point in the history
- this ensures that the output stream is assigned before win32console
  swaps out $stdout and/or $stderr
- Closes #143.
  • Loading branch information
dchelimsky committed Oct 6, 2010
1 parent 248a2e9 commit 686cad4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/rspec/core/command_line.rb
Expand Up @@ -12,9 +12,9 @@ def initialize(options, configuration=RSpec::configuration, world=RSpec::world)
end

def run(err, out)
@options.configure(@configuration)
@configuration.error_stream = err
@configuration.output_stream ||= out
@options.configure(@configuration)
@configuration.load_spec_files
@configuration.configure_mock_framework
@world.announce_inclusion_filter
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/core/configuration.rb
Expand Up @@ -136,11 +136,11 @@ def color_enabled=(bool)
settings[:color_enabled] = true
if bool && ::RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
using_stdout = settings[:output_stream] == $stdout
using_stderr = settings[:error_stream] == $stderr
using_stderr = settings[:error_stream] == $stderr
begin
require 'Win32/Console/ANSI'
settings[:output_stream] = $stdout if using_stdout
settings[:error_stream] = $stderr if using_stderr
settings[:error_stream] = $stderr if using_stderr
rescue LoadError
warn "You must 'gem install win32console' to use colour on Windows"
settings[:color_enabled] = false
Expand Down
16 changes: 13 additions & 3 deletions spec/rspec/core/command_line_spec.rb
Expand Up @@ -39,20 +39,29 @@ module RSpec::Core
RSpec::Core::Configuration.new
end

let(:err) { ::StringIO.new }
let(:out) { ::StringIO.new }

before do
config.stub(:run_hook)
end

it "configures streams before command line options" do
# this is necessary to ensure that color works correctly on windows
config.should_receive(:error_stream=).ordered
config.should_receive(:output_stream=).ordered
config.should_receive(:color_enabled=).ordered
command_line.run(err, out) rescue nil
end

it "runs before suite hooks" do
config.should_receive(:run_hook).with(:before, :suite)
command_line.run(out, out)
command_line.run(err, out)
end

it "runs after suite hooks" do
config.should_receive(:run_hook).with(:after, :suite)
command_line.run(out, out)
command_line.run(err, out)
end

it "runs after suite hooks even after an error" do
Expand All @@ -66,10 +75,11 @@ module RSpec::Core
end
end
expect do
command_line.run(out, out)
command_line.run(err, out)
end.to raise_error
after_suite_called.should be_true
end

end

describe "#run with custom output" do
Expand Down

0 comments on commit 686cad4

Please sign in to comment.