|
3 | 3 | require 'spec_helper'
|
4 | 4 | require 'cucumber/formatter/interceptor'
|
5 | 5 |
|
6 |
| -module Cucumber |
7 |
| - module Formatter |
8 |
| - describe Interceptor::Pipe do |
9 |
| - let(:pipe) { instance_spy(IO) } |
10 |
| - |
11 |
| - describe '#wrap!' do |
12 |
| - it 'raises an ArgumentError if its not passed :stderr/:stdout' do |
13 |
| - expect { described_class.wrap(:nonsense) }.to raise_error(ArgumentError) |
14 |
| - end |
15 |
| - |
16 |
| - context 'when passed :stderr' do |
17 |
| - before :each do |
18 |
| - @stderr = $stderr |
19 |
| - end |
20 |
| - |
21 |
| - after :each do |
22 |
| - $stderr = @stderr |
23 |
| - end |
24 |
| - |
25 |
| - it 'wraps $stderr' do |
26 |
| - wrapped = described_class.wrap(:stderr) |
27 |
| - |
28 |
| - expect($stderr).to be_instance_of described_class |
29 |
| - expect($stderr).to be wrapped |
30 |
| - end |
31 |
| - end |
32 |
| - |
33 |
| - context 'when passed :stdout' do |
34 |
| - before :each do |
35 |
| - @stdout = $stdout |
36 |
| - end |
37 |
| - |
38 |
| - after :each do |
39 |
| - $stdout = @stdout |
40 |
| - end |
41 |
| - |
42 |
| - it 'wraps $stdout' do |
43 |
| - wrapped = described_class.wrap(:stdout) |
44 |
| - |
45 |
| - expect($stdout).to be_instance_of described_class |
46 |
| - expect($stdout).to be wrapped |
47 |
| - end |
48 |
| - end |
| 6 | +describe Cucumber::Formatter::Interceptor::Pipe do |
| 7 | + let(:pipe) { instance_spy(IO) } |
| 8 | + |
| 9 | + describe '#wrap!' do |
| 10 | + it 'raises an ArgumentError if its not passed :stderr/:stdout' do |
| 11 | + expect { described_class.wrap(:nonsense) }.to raise_error(ArgumentError) |
| 12 | + end |
| 13 | + |
| 14 | + context 'when passed :stderr' do |
| 15 | + before :each do |
| 16 | + @stderr = $stderr |
49 | 17 | end
|
50 | 18 |
|
51 |
| - describe '#unwrap!' do |
52 |
| - before :each do |
53 |
| - @stdout = $stdout |
54 |
| - $stdout = pipe |
55 |
| - @wrapped = described_class.wrap(:stdout) |
56 |
| - end |
| 19 | + after :each do |
| 20 | + $stderr = @stderr |
| 21 | + end |
57 | 22 |
|
58 |
| - after :each do |
59 |
| - $stdout = @stdout |
60 |
| - end |
| 23 | + it 'wraps $stderr' do |
| 24 | + wrapped = described_class.wrap(:stderr) |
61 | 25 |
|
62 |
| - it "raises an ArgumentError if it wasn't passed :stderr/:stdout" do |
63 |
| - expect { described_class.unwrap!(:nonsense) }.to raise_error(ArgumentError) |
64 |
| - end |
| 26 | + expect($stderr).to be_instance_of described_class |
| 27 | + expect($stderr).to be wrapped |
| 28 | + end |
| 29 | + end |
65 | 30 |
|
66 |
| - it 'resets $stdout when #unwrap! is called' do |
67 |
| - interceptor = described_class.unwrap! :stdout |
| 31 | + context 'when passed :stdout' do |
| 32 | + before :each do |
| 33 | + @stdout = $stdout |
| 34 | + end |
68 | 35 |
|
69 |
| - expect(interceptor).to be_instance_of described_class |
70 |
| - expect($stdout).not_to be interceptor |
71 |
| - end |
| 36 | + after :each do |
| 37 | + $stdout = @stdout |
| 38 | + end |
72 | 39 |
|
73 |
| - it 'noops if $stdout or $stderr has been overwritten' do |
74 |
| - $stdout = StringIO.new |
75 |
| - pipe = described_class.unwrap! :stdout |
76 |
| - expect(pipe).to eq $stdout |
| 40 | + it 'wraps $stdout' do |
| 41 | + wrapped = described_class.wrap(:stdout) |
77 | 42 |
|
78 |
| - $stderr = StringIO.new |
79 |
| - pipe = described_class.unwrap! :stderr |
80 |
| - expect(pipe).to eq $stderr |
81 |
| - end |
| 43 | + expect($stdout).to be_instance_of described_class |
| 44 | + expect($stdout).to be wrapped |
| 45 | + end |
| 46 | + end |
| 47 | + end |
82 | 48 |
|
83 |
| - it 'disables the pipe bypass' do |
84 |
| - buffer = '(::)' |
85 |
| - described_class.unwrap! :stdout |
| 49 | + describe '#unwrap!' do |
| 50 | + before :each do |
| 51 | + @stdout = $stdout |
| 52 | + $stdout = pipe |
| 53 | + @wrapped = described_class.wrap(:stdout) |
| 54 | + @stderr = $stderr |
| 55 | + end |
86 | 56 |
|
87 |
| - @wrapped.write(buffer) |
| 57 | + after :each do |
| 58 | + $stdout = @stdout |
| 59 | + $stderr = @stderr |
| 60 | + end |
88 | 61 |
|
89 |
| - expect(@wrapped.buffer_string).not_to end_with(buffer) |
90 |
| - end |
91 |
| - end |
| 62 | + it "raises an ArgumentError if it wasn't passed :stderr/:stdout" do |
| 63 | + expect { described_class.unwrap!(:nonsense) }.to raise_error(ArgumentError) |
| 64 | + end |
92 | 65 |
|
93 |
| - describe '#write' do |
94 |
| - let(:buffer) { 'Some stupid buffer' } |
95 |
| - let(:pi) { described_class.new(pipe) } |
| 66 | + it 'resets $stdout when #unwrap! is called' do |
| 67 | + interceptor = described_class.unwrap! :stdout |
96 | 68 |
|
97 |
| - it 'writes arguments to the original pipe' do |
98 |
| - expect(pipe).to receive(:write).with(buffer) { buffer.size } |
99 |
| - expect(pi.write(buffer)).to eq buffer.size |
100 |
| - end |
| 69 | + expect(interceptor).to be_instance_of described_class |
| 70 | + expect($stdout).not_to be interceptor |
| 71 | + end |
101 | 72 |
|
102 |
| - it 'adds the buffer to its stored output' do |
103 |
| - expect(pipe).to receive(:write).with(buffer) |
| 73 | + it 'noops if $stdout or $stderr has been overwritten' do |
| 74 | + $stdout = StringIO.new |
| 75 | + pipe = described_class.unwrap! :stdout |
| 76 | + expect(pipe).to eq($stdout) |
104 | 77 |
|
105 |
| - pi.write(buffer) |
| 78 | + $stderr = StringIO.new |
| 79 | + pipe = described_class.unwrap! :stderr |
| 80 | + expect(pipe).to eq($stderr) |
| 81 | + end |
106 | 82 |
|
107 |
| - expect(pi.buffer_string).not_to be_empty |
108 |
| - expect(pi.buffer_string).to eq buffer |
109 |
| - end |
110 |
| - end |
| 83 | + it 'disables the pipe bypass' do |
| 84 | + buffer = '(::)' |
| 85 | + described_class.unwrap! :stdout |
| 86 | + @wrapped.write(buffer) |
111 | 87 |
|
112 |
| - describe '#method_missing' do |
113 |
| - let(:pi) { described_class.new(pipe) } |
| 88 | + expect(@wrapped.buffer_string).not_to end_with(buffer) |
| 89 | + end |
| 90 | + end |
114 | 91 |
|
115 |
| - it 'passes #tty? to the original pipe' do |
116 |
| - expect(pipe).to receive(:tty?).and_return(true) |
117 |
| - expect(pi.tty?).to be true |
118 |
| - end |
119 |
| - end |
| 92 | + describe '#write' do |
| 93 | + let(:buffer) { 'Some stupid buffer' } |
| 94 | + let(:pi) { described_class.new(pipe) } |
120 | 95 |
|
121 |
| - describe '#respond_to' do |
122 |
| - let(:pi) { described_class.wrap(:stderr) } |
| 96 | + it 'writes arguments to the original pipe' do |
| 97 | + expect(pipe).to receive(:write).with(buffer) { buffer.size } |
| 98 | + expect(pi.write(buffer)).to eq(buffer.length) |
| 99 | + end |
123 | 100 |
|
124 |
| - it 'responds to all methods $stderr has' do |
125 |
| - $stderr.methods.each { |m| expect(pi.respond_to?(m)).to be true } |
126 |
| - end |
127 |
| - end |
| 101 | + it 'adds the buffer to its stored output' do |
| 102 | + expect(pipe).to receive(:write).with(buffer) |
128 | 103 |
|
129 |
| - describe 'when calling `methods` on the stream' do |
130 |
| - it 'does not raise errors' do |
131 |
| - allow($stderr).to receive(:puts) |
| 104 | + pi.write(buffer) |
132 | 105 |
|
133 |
| - described_class.wrap(:stderr) |
134 |
| - expect { $stderr.puts('Oh, hi here !') }.not_to raise_exception(NoMethodError) |
135 |
| - end |
| 106 | + expect(pi.buffer_string).not_to be_empty |
| 107 | + expect(pi.buffer_string).to eq(buffer) |
| 108 | + end |
| 109 | + end |
136 | 110 |
|
137 |
| - it 'does not shadow errors when method do not exist on the stream' do |
138 |
| - described_class.wrap(:stderr) |
139 |
| - expect { $stderr.not_really_puts('Oh, hi here !') }.to raise_exception(NoMethodError) |
140 |
| - end |
141 |
| - end |
| 111 | + describe '#method_missing' do |
| 112 | + before :each do |
| 113 | + @stdout = $stdout |
| 114 | + $stdout = pipe |
| 115 | + @wrapped = described_class.wrap(:stdout) |
| 116 | + @stderr = $stderr |
| 117 | + end |
| 118 | + |
| 119 | + after :each do |
| 120 | + $stdout = @stdout |
| 121 | + $stderr = @stderr |
| 122 | + end |
| 123 | + |
| 124 | + let(:pi) { described_class.new(pipe) } |
| 125 | + |
| 126 | + it 'passes #tty? to the original pipe' do |
| 127 | + expect(pipe).to receive(:tty?).and_return(true) |
| 128 | + expect(pi.tty?).to be true |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + describe '#respond_to' do |
| 133 | + before :each do |
| 134 | + @stdout = $stdout |
| 135 | + $stdout = pipe |
| 136 | + @wrapped = described_class.wrap(:stdout) |
| 137 | + @stderr = $stderr |
| 138 | + end |
| 139 | + |
| 140 | + after :each do |
| 141 | + $stdout = @stdout |
| 142 | + $stderr = @stderr |
| 143 | + end |
| 144 | + |
| 145 | + let(:pi) { described_class.wrap(:stderr) } |
| 146 | + |
| 147 | + it 'responds to all methods $stderr has' do |
| 148 | + expect(pi).to respond_to(*$stderr.methods) |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + describe 'when calling `methods` on the stream' do |
| 153 | + it 'does not raise errors' do |
| 154 | + allow($stderr).to receive(:puts) |
| 155 | + described_class.wrap(:stderr) |
| 156 | + |
| 157 | + expect { $stderr.puts('Oh, hi here !') }.not_to raise_error |
| 158 | + end |
| 159 | + |
| 160 | + it 'does not shadow errors when method do not exist on the stream' do |
| 161 | + described_class.wrap(:stderr) |
| 162 | + |
| 163 | + expect { $stderr.not_really_puts('Oh, hi here !') }.to raise_error(NoMethodError) |
142 | 164 | end
|
143 | 165 | end
|
144 | 166 | end
|
0 commit comments