Skip to content

Commit

Permalink
Add the Cucumber::Formatter::Interceptor::Pipe class for transparentl…
Browse files Browse the repository at this point in the history
…y intercepting writes to pipes

This will ultimately be used to consume $stderr and $stdout for formatters
  • Loading branch information
R. Tyler Croy committed Apr 24, 2012
1 parent 143d2b8 commit 4d1bccd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/cucumber/formatter/interceptor.rb
@@ -0,0 +1,23 @@

module Cucumber
module Formatter
module Interceptor
class Pipe
attr_reader :pipe, :buffer
def initialize(pipe)
@pipe = pipe
@buffer = []
end

def write(str)
@buffer << str
return @pipe.write(str)
end

def method_missing(method, *args, &blk)
@pipe.send(method, *args, &blk)
end
end
end
end
end
38 changes: 38 additions & 0 deletions spec/cucumber/formatter/interceptor_spec.rb
@@ -0,0 +1,38 @@
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 '#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

0 comments on commit 4d1bccd

Please sign in to comment.