forked from cucumber/cucumber-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request cucumber#259 from rtyler/feature/junit-with-sysout
Include the output $stderr and $stdout in JUnit formatted XML
- Loading branch information
Showing
4 changed files
with
238 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
module Cucumber | ||
module Formatter | ||
module Interceptor | ||
class Pipe | ||
attr_reader :pipe, :buffer | ||
def initialize(pipe) | ||
@pipe = pipe | ||
@buffer = [] | ||
@wrapped = true | ||
end | ||
|
||
def write(str) | ||
@buffer << str if @wrapped | ||
return @pipe.write(str) | ||
end | ||
|
||
def unwrap! | ||
@wrapped = false | ||
@pipe | ||
end | ||
|
||
def method_missing(method, *args, &blk) | ||
@pipe.send(method, *args, &blk) | ||
end | ||
|
||
def self.validate_pipe(pipe) | ||
unless [:stdout, :stderr].include? pipe | ||
raise ArgumentError, '#wrap only accepts :stderr or :stdout' | ||
end | ||
end | ||
|
||
def self.unwrap!(pipe) | ||
validate_pipe pipe | ||
wrapped = nil | ||
case pipe | ||
when :stdout | ||
wrapped = $stdout | ||
$stdout = wrapped.unwrap! | ||
when :stderr | ||
wrapped = $stderr | ||
$stderr = wrapped.unwrap! | ||
end | ||
wrapped | ||
end | ||
|
||
def self.wrap(pipe) | ||
validate_pipe pipe | ||
|
||
case pipe | ||
when :stderr | ||
$stderr = self.new($stderr) | ||
return $stderr | ||
when :stdout | ||
$stdout = self.new($stdout) | ||
return $stdout | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
require 'spec_helper' | ||
require 'cucumber/formatter/interceptor' | ||
|
||
module Cucumber::Formatter | ||
describe Interceptor::Pipe do | ||
let(:pipe) do | ||
pipe = double('original pipe') | ||
pipe.stub(:instance_of?).and_return(true) | ||
pipe | ||
end | ||
|
||
describe '#wrap!' do | ||
it 'should raise an ArgumentError if its not passed :stderr/:stdout' do | ||
expect { | ||
Interceptor::Pipe.wrap(:nonsense) | ||
}.to raise_error(ArgumentError) | ||
|
||
end | ||
|
||
context 'when passed :stderr' do | ||
before :each do | ||
@stderr = $stdout | ||
end | ||
|
||
it 'should wrap $stderr' do | ||
wrapped = Interceptor::Pipe.wrap(:stderr) | ||
$stderr.should be_instance_of Interceptor::Pipe | ||
$stderr.should be wrapped | ||
end | ||
|
||
after :each do | ||
$stderr = @stderr | ||
end | ||
end | ||
|
||
context 'when passed :stdout' do | ||
before :each do | ||
@stdout = $stdout | ||
end | ||
|
||
it 'should wrap $stdout' do | ||
wrapped = Interceptor::Pipe.wrap(:stdout) | ||
$stdout.should be_instance_of Interceptor::Pipe | ||
$stdout.should be wrapped | ||
end | ||
|
||
after :each do | ||
$stdout = @stdout | ||
end | ||
end | ||
end | ||
|
||
describe '#unwrap!' do | ||
before :each do | ||
@stdout = $stdout | ||
@wrapped = Interceptor::Pipe.wrap(:stdout) | ||
end | ||
|
||
it 'should raise an ArgumentError if it wasn\'t passed :stderr/:stdout' do | ||
expect { | ||
Interceptor::Pipe.unwrap!(:nonsense) | ||
}.to raise_error(ArgumentError) | ||
end | ||
|
||
it 'should reset $stdout when #unwrap! is called' do | ||
interceptor = Interceptor::Pipe.unwrap! :stdout | ||
interceptor.should be_instance_of Interceptor::Pipe | ||
$stdout.should_not be interceptor | ||
end | ||
|
||
it 'should disable the pipe bypass' do | ||
buffer = '(::)' | ||
Interceptor::Pipe.unwrap! :stdout | ||
|
||
@wrapped.should_receive(:write).with(buffer) | ||
@wrapped.buffer.should_not_receive(:<<) | ||
@wrapped.write(buffer) | ||
end | ||
|
||
after :each do | ||
$stdout = @stdout | ||
end | ||
end | ||
|
||
describe '#write' do | ||
let(:buffer) { 'Some stupid buffer' } | ||
let(:pi) { Interceptor::Pipe.new(pipe) } | ||
|
||
it 'should write arguments to the original pipe' do | ||
pipe.should_receive(:write).with(buffer).and_return(buffer.size) | ||
pi.write(buffer).should == buffer.size | ||
end | ||
|
||
it 'should add the buffer to its stored output' do | ||
pipe.stub(:write) | ||
pi.write(buffer) | ||
pi.buffer.should_not be_empty | ||
pi.buffer.first.should == buffer | ||
end | ||
end | ||
|
||
describe '#method_missing' do | ||
let(:pi) { Interceptor::Pipe.new(pipe) } | ||
|
||
it 'should pass #tty? to the original pipe' do | ||
pipe.should_receive(:tty?).and_return(true) | ||
pi.tty?.should be true | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.