|
12 | 12 | end |
13 | 13 |
|
14 | 14 | it "runs command successfully with logging" do |
15 | | - output = StringIO.new |
16 | 15 | uuid = "xxxx" |
17 | 16 | allow(SecureRandom).to receive(:uuid).and_return(uuid) |
18 | | - command = TTY::Command.new(output: output) |
19 | 17 |
|
20 | | - command.run(:echo, "hello") |
| 18 | + lines = retrieve_log_lines do |output| |
| 19 | + command = TTY::Command.new(output: output) |
| 20 | + command.run(:echo, "hello") |
| 21 | + end |
21 | 22 |
|
22 | | - output.rewind |
23 | | - lines = output.readlines |
24 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
25 | 23 | expect(lines).to eq([ |
26 | 24 | "[\e[32m#{uuid}\e[0m] Running \e[33;1mecho hello\e[0m\n", |
27 | 25 | "[\e[32m#{uuid}\e[0m] \thello\n", |
|
31 | 29 | end |
32 | 30 |
|
33 | 31 | it "runs command successfully with logging without color" do |
34 | | - output = StringIO.new |
35 | 32 | uuid = "xxxx" |
36 | 33 | allow(SecureRandom).to receive(:uuid).and_return(uuid) |
37 | | - command = TTY::Command.new(output: output, color: false) |
38 | 34 |
|
39 | | - command.run(:echo, "hello") |
| 35 | + lines = retrieve_log_lines do |output| |
| 36 | + command = TTY::Command.new(output: output, color: false) |
| 37 | + command.run(:echo, "hello") |
| 38 | + end |
40 | 39 |
|
41 | | - output.rewind |
42 | | - lines = output.readlines |
43 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
44 | 40 | expect(lines).to eq([ |
45 | 41 | "[#{uuid}] Running echo hello\n", |
46 | 42 | "[#{uuid}] \thello\n", |
|
50 | 46 |
|
51 | 47 | it "runs command and fails with logging" do |
52 | 48 | non_zero_exit = fixtures_path("non_zero_exit") |
53 | | - output = StringIO.new |
54 | 49 | uuid = "xxxx" |
55 | 50 | allow(SecureRandom).to receive(:uuid).and_return(uuid) |
56 | | - command = TTY::Command.new(output: output) |
57 | 51 |
|
58 | | - command.run!("ruby #{non_zero_exit}") |
| 52 | + lines = retrieve_log_lines do |output| |
| 53 | + command = TTY::Command.new(output: output) |
| 54 | + command.run!("ruby #{non_zero_exit}") |
| 55 | + end |
59 | 56 |
|
60 | | - output.rewind |
61 | | - lines = output.readlines |
62 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
63 | 57 | expect(lines).to eq([ |
64 | 58 | "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{non_zero_exit}\e[0m\n", |
65 | 59 | "[\e[32m#{uuid}\e[0m] \tnooo\n", |
|
113 | 107 | phased_output = fixtures_path("phased_output") |
114 | 108 | uuid = "xxxx" |
115 | 109 | allow(SecureRandom).to receive(:uuid).and_return(uuid) |
116 | | - output = StringIO.new |
117 | | - cmd = TTY::Command.new(output: output) |
118 | 110 |
|
119 | | - out, err = cmd.run("ruby #{phased_output}") |
| 111 | + out = err = nil |
| 112 | + lines = retrieve_log_lines do |output| |
| 113 | + cmd = TTY::Command.new(output: output) |
| 114 | + out, err = cmd.run("ruby #{phased_output}") |
| 115 | + end |
120 | 116 |
|
121 | 117 | expect(out).to eq("." * 10) |
122 | 118 | expect(err).to eq("") |
123 | 119 |
|
124 | | - output.rewind |
125 | | - lines = output.readlines |
126 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
127 | 120 | expect(lines).to eq([ |
128 | 121 | "[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{phased_output}\e[0m\n", |
129 | 122 | "[\e[32m#{uuid}\e[0m] \t..........\n", |
|
155 | 148 |
|
156 | 149 | context "with uuid option" do |
157 | 150 | it "runs command successfully with logging without uuid set globally" do |
158 | | - output = StringIO.new |
159 | | - command = TTY::Command.new(output: output, uuid: false) |
| 151 | + lines = retrieve_log_lines do |output| |
| 152 | + command = TTY::Command.new(output: output, uuid: false) |
| 153 | + command.run(:echo, "hello") |
| 154 | + end |
160 | 155 |
|
161 | | - command.run(:echo, "hello") |
162 | | - output.rewind |
163 | | - |
164 | | - lines = output.readlines |
165 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
166 | 156 | expect(lines).to eq( |
167 | 157 | generic_colored_log_lines(prefix: nil) |
168 | 158 | ) |
169 | 159 | end |
170 | 160 |
|
171 | 161 | it "runs command successfully with logging without uuid set locally" do |
172 | | - output = StringIO.new |
173 | | - command = TTY::Command.new(output: output) |
| 162 | + lines = retrieve_log_lines do |output| |
| 163 | + command = TTY::Command.new(output: output) |
| 164 | + command.run(:echo, "hello", uuid: false) |
| 165 | + end |
174 | 166 |
|
175 | | - command.run(:echo, "hello", uuid: false) |
176 | | - output.rewind |
177 | | - |
178 | | - lines = output.readlines |
179 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
180 | 167 | expect(lines).to eq( |
181 | 168 | generic_colored_log_lines(prefix: nil) |
182 | 169 | ) |
|
185 | 172 |
|
186 | 173 | context "with tag option" do |
187 | 174 | it "prints the tag set globally" do |
188 | | - output = StringIO.new |
189 | 175 | tag = "task" |
190 | | - command = TTY::Command.new(output: output, tag: tag) |
191 | 176 |
|
192 | | - command.run(:echo, "hello") |
| 177 | + lines = retrieve_log_lines do |output| |
| 178 | + command = TTY::Command.new(output: output, tag: tag) |
| 179 | + command.run(:echo, "hello") |
| 180 | + end |
193 | 181 |
|
194 | | - output.rewind |
195 | | - lines = output.readlines |
196 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
197 | 182 | expect(lines).to eq( |
198 | 183 | generic_colored_log_lines(prefix: tag) |
199 | 184 | ) |
200 | 185 | end |
201 | 186 |
|
202 | 187 | it "prints the tag set locally" do |
203 | | - output = StringIO.new |
204 | 188 | tag = "task" |
205 | | - command = TTY::Command.new(output: output) |
206 | 189 |
|
207 | | - command.run(:echo, "hello", tag: tag) |
| 190 | + lines = retrieve_log_lines do |output| |
| 191 | + command = TTY::Command.new(output: output) |
| 192 | + command.run(:echo, "hello", tag: tag) |
| 193 | + end |
208 | 194 |
|
209 | | - output.rewind |
210 | | - lines = output.readlines |
211 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
212 | 195 | expect(lines).to eq( |
213 | 196 | generic_colored_log_lines(prefix: tag) |
214 | 197 | ) |
215 | 198 | end |
216 | 199 |
|
217 | 200 | it "prints the tag even if uuid is set to false" do |
218 | | - output = StringIO.new |
219 | 201 | tag = "task" |
220 | | - command = TTY::Command.new(output: output, tag: tag, uuid: false) |
221 | 202 |
|
222 | | - command.run(:echo, "hello") |
| 203 | + lines = retrieve_log_lines do |output| |
| 204 | + command = TTY::Command.new(output: output, tag: tag, uuid: false) |
| 205 | + command.run(:echo, "hello") |
| 206 | + end |
223 | 207 |
|
224 | | - output.rewind |
225 | | - lines = output.readlines |
226 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
227 | 208 | expect(lines).to eq( |
228 | 209 | generic_colored_log_lines(prefix: tag) |
229 | 210 | ) |
230 | 211 | end |
231 | 212 | end |
232 | 213 |
|
| 214 | + # Retrieves log lines from the output produced within the given block. |
| 215 | + # Also replaces the execution time portion in the output with `x`. |
| 216 | + def retrieve_log_lines |
| 217 | + output = StringIO.new |
| 218 | + yield(output) |
| 219 | + output.rewind |
| 220 | + lines = output.readlines |
| 221 | + lines.last.gsub!(/\d+\.\d+/, "x") |
| 222 | + lines |
| 223 | + end |
| 224 | + |
233 | 225 | # Generates the expected log lines in colored mode, with/without `[prefix]` |
234 | 226 | def generic_colored_log_lines(prefix: nil) |
235 | 227 | if prefix |
|
0 commit comments