File tree Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Expand file tree Collapse file tree 2 files changed +53
-0
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,21 @@ class World
29
29
def inspect
30
30
INSPECT
31
31
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
32
47
end # World
33
48
34
49
# Standalone configuration of a mutant execution.
Original file line number Diff line number Diff line change @@ -22,4 +22,42 @@ def apply
22
22
expect ( apply ) . to be ( apply )
23
23
end
24
24
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
25
63
end
You can’t perform that action at this time.
0 commit comments