command_exec 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/Gemfile +6 -2
  2. data/Gemfile.lock +42 -18
  3. data/README.md +707 -72
  4. data/RELEASE_NOTES.md +62 -0
  5. data/Rakefile +40 -9
  6. data/TODO.md +8 -2
  7. data/command_exec.gemspec +3 -2
  8. data/gemfiles/Gemfile.default +28 -0
  9. data/gemfiles/Gemfile.travis +16 -0
  10. data/gemfiles/Gemfile.travis.lock +48 -0
  11. data/lib/command_exec.rb +22 -2
  12. data/lib/command_exec/command.rb +307 -157
  13. data/lib/command_exec/exceptions.rb +16 -6
  14. data/lib/command_exec/field_helper.rb +263 -0
  15. data/lib/command_exec/formatter/array.rb +158 -0
  16. data/lib/command_exec/formatter/hash.rb +78 -0
  17. data/lib/command_exec/formatter/json.rb +22 -0
  18. data/lib/command_exec/formatter/string.rb +22 -0
  19. data/lib/command_exec/formatter/xml.rb +22 -0
  20. data/lib/command_exec/formatter/yaml.rb +22 -0
  21. data/lib/command_exec/logger.rb +9 -0
  22. data/lib/command_exec/process.rb +294 -0
  23. data/lib/command_exec/spec_helper_module.rb +52 -0
  24. data/lib/command_exec/version.rb +1 -1
  25. data/script/console +8 -0
  26. data/spec/command/command_spec.rb +413 -117
  27. data/spec/command/test_data/echo_test +3 -0
  28. data/spec/command/test_data/exit_status_test +2 -0
  29. data/spec/command/test_data/log_file_test +3 -0
  30. data/spec/command/test_data/logger_test +2 -0
  31. data/spec/command/test_data/not_raise_error_test +4 -0
  32. data/spec/command/test_data/not_throw_error_test +4 -0
  33. data/spec/command/test_data/output_test +6 -0
  34. data/spec/command/test_data/raise_error_test +6 -0
  35. data/spec/command/test_data/runner_open3_test +4 -0
  36. data/spec/command/test_data/runner_system_test +4 -0
  37. data/spec/command/test_data/stderr_test +4 -0
  38. data/spec/command/test_data/stdout_multiple_lines_test +4 -0
  39. data/spec/command/test_data/stdout_test +4 -0
  40. data/spec/command/test_data/throw_error_test +6 -0
  41. data/spec/command/test_data/true_test +2 -0
  42. data/spec/formatter/array_spec.rb +215 -0
  43. data/spec/formatter/hash_spec.rb +117 -0
  44. data/spec/formatter/json_spec.rb +21 -0
  45. data/spec/formatter/xml_spec.rb +33 -0
  46. data/spec/formatter/yaml_spec.rb +21 -0
  47. data/spec/process/process_spec.rb +329 -0
  48. data/spec/spec_helper.rb +15 -4
  49. metadata +79 -5
@@ -0,0 +1,3 @@
1
+ #!/bin/bash
2
+ echo "test"
3
+ exit 0
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ exit $1
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ exit 0
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ exit 1
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $stdout.puts "no error"
4
+ exit 0
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $stdout.puts "no error"
4
+ exit 0
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $stderr.puts "this is output on stderr"
4
+ $stdout.puts "this is output on stdout"
5
+
6
+ exit 128
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $stderr.puts "this is output on stderr"
4
+ $stdout.puts "this is output on stdout"
5
+
6
+ exit 128
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $stdout.puts "no error"
4
+ exit 0
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #$stdout.puts "no error"
4
+ exit 0
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $stderr.puts "error. execution failed"
4
+ exit 0
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ puts "error. execution failed\nerror. execution failed"
4
+ exit 0
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $stdout.puts "error, huh, what goes on"
4
+ exit 0
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $stderr.puts "this is output on stderr"
4
+ $stdout.puts "this is output on stdout"
5
+
6
+ exit 128
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ exit 0
@@ -0,0 +1,215 @@
1
+ #encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Formatter::Array do
6
+
7
+ before(:each) do
8
+ @formatter = Formatter::Array.new
9
+ end
10
+
11
+ context :private_api do
12
+
13
+ it "formats headers (plain)" do
14
+ expect(@formatter.send(:format_header,:reason_for_failure)).to eq("===== REASON FOR FAILURE =====")
15
+ expect(@formatter.send(:format_header,:status)).to eq("===== STATUS =====")
16
+ end
17
+
18
+ it "formats headers and modifies prefix" do
19
+ expect(@formatter.send(:format_header,:status, prefix: '-' * 5 )).to eq("----- STATUS =====")
20
+ end
21
+
22
+ it "formats headers and modifies suffix" do
23
+ expect(@formatter.send(:format_header,:status, suffix: '-' * 5 )).to eq("===== STATUS -----")
24
+ end
25
+
26
+ it "formats headers and modifies suffix/prefix" do
27
+ expect(@formatter.send(:format_header,:status, prefix: '#' * 5, suffix: '-' * 5 )).to eq("##### STATUS -----")
28
+ end
29
+
30
+ it "leaves out nil prefix/suffix" do
31
+ expect(@formatter.send(:format_header,:status, prefix: nil , suffix: nil)).to eq(" STATUS ")
32
+ end
33
+
34
+ it "finds the longest header names' length" do
35
+ expect(@formatter.send(:max_header_length)).to eq(18)
36
+ end
37
+
38
+ it "centers header names" do
39
+ expect(@formatter.send(:halign, '012' , 10 , :center)).to eq(' 012 ')
40
+ expect(@formatter.send(:halign, '0123' , 10 , :center)).to eq(' 0123 ')
41
+ expect(@formatter.send(:halign, '0123456789' , 10 , :center)).to eq('0123456789')
42
+ expect(@formatter.send(:halign, '012' , 11 , :center)).to eq(' 012 ')
43
+ expect(@formatter.send(:halign, '0123' , 11 , :center)).to eq(' 0123 ')
44
+ expect(@formatter.send(:halign, '01234567891' , 11 , :center)).to eq('01234567891')
45
+
46
+ #default = center
47
+ expect(@formatter.send(:halign, '01234567891' , 11 , :unknown)).to eq('01234567891')
48
+ end
49
+
50
+ it "leftify header names" do
51
+ expect(@formatter.send(:halign, '012' , 10 , :left)).to eq('012 ')
52
+ expect(@formatter.send(:halign, '0123' , 10 , :left)).to eq('0123 ')
53
+ expect(@formatter.send(:halign, '0123456789' , 10 , :left)).to eq('0123456789')
54
+ expect(@formatter.send(:halign, '012' , 11 , :left)).to eq('012 ')
55
+ expect(@formatter.send(:halign, '0123' , 11 , :left)).to eq('0123 ')
56
+ expect(@formatter.send(:halign, '01234567891' , 11 , :left)).to eq('01234567891')
57
+ end
58
+
59
+ it "justify header names right" do
60
+ expect(@formatter.send(:halign, '012' , 10 , :right)).to eq(' 012')
61
+ expect(@formatter.send(:halign, '0123' , 10 , :right)).to eq(' 0123')
62
+ expect(@formatter.send(:halign, '0123456789' , 10 , :right)).to eq('0123456789')
63
+ expect(@formatter.send(:halign, '012' , 11 , :right)).to eq(' 012')
64
+ expect(@formatter.send(:halign, '0123' , 11 , :right)).to eq(' 0123')
65
+ expect(@formatter.send(:halign, '01234567891' , 11 , :right)).to eq('01234567891')
66
+ end
67
+
68
+ end
69
+
70
+ context :public_api do
71
+
72
+ it "outputs stderr with header" do
73
+ expect(@formatter.stderr("output of stderr")).to eq(["output of stderr"])
74
+ end
75
+
76
+ it "supports arrays as well" do
77
+ expect(@formatter.stderr(["output of stderr"])).to eq(["output of stderr"])
78
+ end
79
+
80
+ it "outputs multiple values if called multiple times (but only with one header)" do
81
+ 2.times do
82
+ @formatter.stderr(["output of stderr"])
83
+ end
84
+ expect(@formatter.output(:stderr)).to eq(["===== STDERR =====", "output of stderr", "output of stderr"])
85
+ end
86
+
87
+ it "outputs stdout" do
88
+ expect(@formatter.stdout("output of stdout")).to eq(["output of stdout"])
89
+ end
90
+
91
+ it "outputs log file" do
92
+ expect(@formatter.log_file("output of log file")).to eq(["output of log file"])
93
+ end
94
+
95
+ it "outputs return code" do
96
+ expect(@formatter.return_code("output of return code")).to eq(["output of return code"])
97
+ end
98
+
99
+ it "outputs status" do
100
+ expect(@formatter.status(:failed)).to eq(["\e[1;31mFAILED\e[0m"])
101
+ expect(@formatter.status(:success)).to eq(["\e[1;32mOK\e[0m"])
102
+ expect(@formatter.status(:unknown)).to eq(["\e[1;31mFAILED\e[0m"])
103
+ end
104
+
105
+ it "outputs status as single value (no data is appended)" do
106
+ @formatter.status(:success)
107
+ @formatter.status(:failed)
108
+ expect(@formatter.output(:status)).to eq(["===== STATUS =====", "\e[1;31mFAILED\e[0m"])
109
+ end
110
+
111
+ it "supports status as string as well" do
112
+ expect(@formatter.status('failed')).to eq(["\e[1;31mFAILED\e[0m"])
113
+ expect(@formatter.status('success')).to eq([ "\e[1;32mOK\e[0m"])
114
+ end
115
+
116
+ it "supports blank headers" do
117
+ formatter = Formatter::Array.new(headers: { names: { return_code: "" } } )
118
+ formatter.return_code("output of return code")
119
+ expect(formatter.output(:return_code)).to eq(["" , "output of return code"])
120
+ end
121
+
122
+ it "suppresses headers if nil" do
123
+ expect(@formatter.return_code("output of return code")).to eq(["output of return code"])
124
+ end
125
+
126
+ it "output only wanted values" do
127
+ @formatter.stderr(["output of stderr"])
128
+ @formatter.stdout("output of stdout")
129
+ @formatter.log_file("output of log file")
130
+ @formatter.return_code("output of return code")
131
+ @formatter.status(:failed)
132
+ @formatter.pid(4711)
133
+ @formatter.reason_for_failure('great an error occured')
134
+ @formatter.executable('/usr/bin/true')
135
+ start_time = Time.now
136
+ end_time= start_time + 2.seconds
137
+ @formatter.start_time(start_time)
138
+ @formatter.end_time(end_time)
139
+
140
+ expect(@formatter.output(:stderr)).to eq([
141
+ "===== STDERR =====",
142
+ "output of stderr",
143
+ ])
144
+ expect(@formatter.output).to eq([
145
+ "===== STATUS =====",
146
+ "\e[1;31mFAILED\e[0m",
147
+ "===== RETURN CODE =====",
148
+ "output of return code",
149
+ "===== STDERR =====",
150
+ "output of stderr",
151
+ "===== STDOUT =====",
152
+ "output of stdout",
153
+ "===== LOG FILE =====",
154
+ "output of log file",
155
+ "===== PID =====",
156
+ '4711',
157
+ "===== REASON FOR FAILURE =====",
158
+ 'great an error occured',
159
+ "===== EXECUTABLE =====",
160
+ "/usr/bin/true",
161
+ "===== START TIME =====",
162
+ start_time,
163
+ "===== END TIME =====",
164
+ end_time,
165
+ ])
166
+ expect(@formatter.output(:stdout,:stderr)).to eq([
167
+ "===== STDOUT =====",
168
+ "output of stdout",
169
+ "===== STDERR =====",
170
+ "output of stderr",
171
+ ])
172
+ end
173
+
174
+ it "accepts a reason for a failure" do
175
+ expect(@formatter.reason_for_failure('error in stdout found')).to eq([ "error in stdout found" ])
176
+ end
177
+
178
+ it "outputs only wanted values (given as array)" do
179
+ @formatter.stderr(["output of stderr"])
180
+ @formatter.stdout("output of stdout")
181
+ @formatter.log_file("output of log file")
182
+ @formatter.return_code("output of return code")
183
+ @formatter.status(:failed)
184
+
185
+ expect(@formatter.output([:stdout,:stderr])).to eq([
186
+ "===== STDOUT =====",
187
+ "output of stdout",
188
+ "===== STDERR =====",
189
+ "output of stderr"
190
+ ])
191
+ end
192
+
193
+ it "outputs only wanted values in the given order)" do
194
+ @formatter.stderr(["output of stderr"])
195
+ @formatter.stdout("output of stdout")
196
+ @formatter.log_file("output of log file")
197
+ @formatter.return_code("output of return code")
198
+ @formatter.status(:failed)
199
+
200
+ expect(@formatter.output([:stdout,:stderr])).to eq([
201
+ "===== STDOUT =====",
202
+ "output of stdout",
203
+ "===== STDERR =====",
204
+ "output of stderr",
205
+ ])
206
+
207
+ expect(@formatter.output([:stderr,:stdout])).to eq([
208
+ "===== STDERR =====",
209
+ "output of stderr",
210
+ "===== STDOUT =====",
211
+ "output of stdout",
212
+ ])
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,117 @@
1
+ #encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Formatter::Hash do
6
+
7
+ before :each do
8
+ @formatter = Formatter::Hash.new
9
+ end
10
+
11
+ context :private_api do
12
+
13
+ it "prepares output for all fields (default)" do
14
+ @formatter.stderr("output of stderr")
15
+ expect(@formatter.send(:prepare_output)).to eq({:status=>[], :pid => [], :return_code=>[], :stderr=>["output of stderr"], :stdout=>[], :log_file=>[], :reason_for_failure=>[], :executable => [], :start_time => [], :end_time => []})
16
+ end
17
+
18
+ it "prepares output for given fields" do
19
+ @formatter.stderr("output of stderr")
20
+ expect(@formatter.send(:prepare_output, [ :stderr ])).to eq({:stderr=>["output of stderr"]})
21
+ end
22
+
23
+ end
24
+
25
+ context :public_api do
26
+
27
+ it "outputs stderr with header" do
28
+ expect(@formatter.stderr("output of stderr")).to eq(["output of stderr"])
29
+ end
30
+
31
+ it "supports arrays as well" do
32
+ expect(@formatter.stderr(["output of stderr"])).to eq(["output of stderr"])
33
+ end
34
+
35
+ it "outputs multiple values if called multiple times (but only with one header)" do
36
+ 2.times do
37
+ @formatter.stderr(["output of stderr"])
38
+ end
39
+ expect(@formatter.output(:stderr)).to eq(stderr: ["output of stderr", "output of stderr"])
40
+ end
41
+
42
+ it "outputs stdout" do
43
+ expect(@formatter.stdout("output of stdout")).to eq(["output of stdout"])
44
+ end
45
+
46
+ it "outputs log file" do
47
+ expect(@formatter.log_file("output of log file")).to eq(["output of log file"])
48
+ end
49
+
50
+ it "outputs return code" do
51
+ expect(@formatter.return_code("output of return code")).to eq(["output of return code"])
52
+ end
53
+
54
+ it "outputs status" do
55
+ expect(@formatter.status(:failed)).to eq([ "FAILED"])
56
+ expect(@formatter.status(:success)).to eq(["OK"])
57
+ expect(@formatter.status(:unknown)).to eq([ "FAILED"])
58
+ end
59
+
60
+ it "outputs status as single value (no data is appended)" do
61
+ @formatter.status(:success)
62
+ @formatter.status(:failed)
63
+ expect(@formatter.output(:status)).to eq(status: ["FAILED"])
64
+ end
65
+
66
+ it "supports status as string as well" do
67
+ expect(@formatter.status('failed')).to eq(["FAILED"])
68
+ expect(@formatter.status('success')).to eq([ "OK"])
69
+ end
70
+
71
+ it "accepts a reason for a failure" do
72
+ expect(@formatter.reason_for_failure('error in stdout found')).to eq([ "error in stdout found" ])
73
+ end
74
+
75
+ it "output only wanted values" do
76
+ @formatter.stderr(["output of stderr"])
77
+ @formatter.stdout("output of stdout")
78
+ @formatter.log_file("output of log file")
79
+ @formatter.return_code("output of return code")
80
+ @formatter.status(:failed)
81
+ @formatter.pid(4711)
82
+ @formatter.reason_for_failure('great an error occured')
83
+ @formatter.executable('/usr/bin/true')
84
+
85
+ expect(@formatter.output(:stderr)).to eq(stderr: [ "output of stderr" ])
86
+ expect(@formatter.output).to eq(status: [ "FAILED"],
87
+ return_code: [ "output of return code"],
88
+ stderr: [ "output of stderr"],
89
+ stdout: [ "output of stdout"],
90
+ log_file: [ "output of log file"],
91
+ pid: [ "4711" ],
92
+ reason_for_failure: [ 'great an error occured'],
93
+ executable: [ '/usr/bin/true'],
94
+ start_time: [],
95
+ end_time: [],
96
+ )
97
+ expect(@formatter.output(:stdout,:stderr)).to eq(
98
+ stdout: [ "output of stdout"],
99
+ stderr: [ "output of stderr"],
100
+ )
101
+ end
102
+
103
+ it "output only wanted values (given as array)" do
104
+ @formatter.stderr(["output of stderr"])
105
+ @formatter.stdout("output of stdout")
106
+ @formatter.log_file("output of log file")
107
+ @formatter.return_code("output of return code")
108
+ @formatter.status(:failed)
109
+ @formatter.executable('/usr/bin/true')
110
+
111
+ expect(@formatter.output([:stdout,:stderr])).to eq(
112
+ stdout: [ "output of stdout"],
113
+ stderr: [ "output of stderr"],
114
+ )
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,21 @@
1
+ #encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Formatter::JSON do
6
+ before :each do
7
+ @formatter = Formatter::JSON.new
8
+ end
9
+
10
+ it "outputs data as json string" do
11
+ @formatter.stderr(["output of stderr"])
12
+ @formatter.stdout("output of stdout")
13
+ @formatter.log_file("output of log file")
14
+ @formatter.return_code("output of return code")
15
+ @formatter.pid(4711)
16
+ @formatter.status(:failed)
17
+ @formatter.executable('/usr/bin/true')
18
+
19
+ expect(@formatter.output(:stdout,:stderr)).to eq("{\"stdout\":[\"output of stdout\"],\"stderr\":[\"output of stderr\"]}")
20
+ end
21
+ end