Skip to content

Commit c4de647

Browse files
authored
Merge pull request #931 from mbj/add/world-capture-stdout
Add Mutant::World#capture_stdout
2 parents 0b8c948 + c6391cb commit c4de647

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

lib/mutant/config.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ class World
2929
def inspect
3030
INSPECT
3131
end
32+
33+
# Capture stdout of a command
34+
#
35+
# @param [Array<String>] command
36+
#
37+
# @return [Either<String,String>]
38+
def capture_stdout(command)
39+
stdout, status = open3.capture2(*command, binmode: true)
40+
41+
if status.success?
42+
Either::Right.new(stdout)
43+
else
44+
Either::Left.new("Command #{command} failed!")
45+
end
46+
end
3247
end # World
3348

3449
# Standalone configuration of a mutant execution.

spec/unit/mutant/world_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,42 @@ def apply
2222
expect(apply).to be(apply)
2323
end
2424
end
25+
26+
describe '#capture_stdout' do
27+
def apply
28+
subject.capture_stdout(command)
29+
end
30+
31+
let(:open3) { class_double(Open3) }
32+
let(:stdout) { instance_double(String, :stdout) }
33+
let(:subject) { super().with(open3: open3) }
34+
let(:command) { %w[foo bar baz] }
35+
36+
let(:process_status) do
37+
instance_double(
38+
Process::Status,
39+
success?: success?
40+
)
41+
end
42+
43+
before do
44+
allow(open3).to receive_messages(capture2: [stdout, process_status])
45+
end
46+
47+
context 'when process exists successful' do
48+
let(:success?) { true }
49+
50+
it 'returns stdout' do
51+
expect(apply).to eql(Mutant::Either::Right.new(stdout))
52+
end
53+
end
54+
55+
context 'when process exists unsuccessful' do
56+
let(:success?) { false }
57+
58+
it 'returns stdout' do
59+
expect(apply).to eql(Mutant::Either::Left.new("Command #{command.inspect} failed!"))
60+
end
61+
end
62+
end
2563
end

0 commit comments

Comments
 (0)