|
155 | 155 |
|
156 | 156 | context "with uuid option" do |
157 | 157 | it "runs command successfully with logging without uuid set globally" do |
158 | | - output = StringIO.new |
159 | | - command = TTY::Command.new(output: output, uuid: false) |
| 158 | + lines = retrieve_log_lines { |output| |
| 159 | + command = TTY::Command.new(output: output, uuid: false) |
| 160 | + command.run(:echo, "hello") |
| 161 | + } |
160 | 162 |
|
161 | | - command.run(:echo, "hello") |
162 | | - output.rewind |
163 | | - |
164 | | - lines = output.readlines |
165 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
166 | 163 | expect(lines).to eq( |
167 | 164 | generic_colored_log_lines(prefix: nil) |
168 | 165 | ) |
169 | 166 | end |
170 | 167 |
|
171 | 168 | it "runs command successfully with logging without uuid set locally" do |
172 | | - output = StringIO.new |
173 | | - command = TTY::Command.new(output: output) |
174 | | - |
175 | | - command.run(:echo, "hello", uuid: false) |
176 | | - output.rewind |
| 169 | + lines = retrieve_log_lines { |output| |
| 170 | + command = TTY::Command.new(output: output) |
| 171 | + command.run(:echo, "hello", uuid: false) |
| 172 | + } |
177 | 173 |
|
178 | | - lines = output.readlines |
179 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
180 | 174 | expect(lines).to eq( |
181 | 175 | generic_colored_log_lines(prefix: nil) |
182 | 176 | ) |
|
185 | 179 |
|
186 | 180 | context "with tag option" do |
187 | 181 | it "prints the tag set globally" do |
188 | | - output = StringIO.new |
189 | 182 | tag = "task" |
190 | | - command = TTY::Command.new(output: output, tag: tag) |
191 | 183 |
|
192 | | - command.run(:echo, "hello") |
| 184 | + lines = retrieve_log_lines { |output| |
| 185 | + command = TTY::Command.new(output: output, tag: tag) |
| 186 | + command.run(:echo, "hello") |
| 187 | + } |
193 | 188 |
|
194 | | - output.rewind |
195 | | - lines = output.readlines |
196 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
197 | 189 | expect(lines).to eq( |
198 | 190 | generic_colored_log_lines(prefix: tag) |
199 | 191 | ) |
200 | 192 | end |
201 | 193 |
|
202 | 194 | it "prints the tag set locally" do |
203 | | - output = StringIO.new |
204 | 195 | tag = "task" |
205 | | - command = TTY::Command.new(output: output) |
206 | 196 |
|
207 | | - command.run(:echo, "hello", tag: tag) |
| 197 | + lines = retrieve_log_lines { |output| |
| 198 | + command = TTY::Command.new(output: output) |
| 199 | + command.run(:echo, "hello", tag: tag) |
| 200 | + } |
208 | 201 |
|
209 | | - output.rewind |
210 | | - lines = output.readlines |
211 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
212 | 202 | expect(lines).to eq( |
213 | 203 | generic_colored_log_lines(prefix: tag) |
214 | 204 | ) |
215 | 205 | end |
216 | 206 |
|
217 | 207 | it "prints the tag even if uuid is set to false" do |
218 | | - output = StringIO.new |
219 | 208 | tag = "task" |
220 | | - command = TTY::Command.new(output: output, tag: tag, uuid: false) |
221 | 209 |
|
222 | | - command.run(:echo, "hello") |
| 210 | + lines = retrieve_log_lines { |output| |
| 211 | + command = TTY::Command.new(output: output, tag: tag, uuid: false) |
| 212 | + command.run(:echo, "hello") |
| 213 | + } |
223 | 214 |
|
224 | | - output.rewind |
225 | | - lines = output.readlines |
226 | | - lines.last.gsub!(/\d+\.\d+/, "x") |
227 | 215 | expect(lines).to eq( |
228 | 216 | generic_colored_log_lines(prefix: tag) |
229 | 217 | ) |
230 | 218 | end |
231 | 219 | end |
232 | 220 |
|
| 221 | + # Retrieves log lines from the output produced within the given block. |
| 222 | + # Also replaces the execution time portion in the output with `x`. |
| 223 | + def retrieve_log_lines |
| 224 | + output = StringIO.new |
| 225 | + yield(output) |
| 226 | + output.rewind |
| 227 | + lines = output.readlines |
| 228 | + lines.last.gsub!(/\d+\.\d+/, "x") |
| 229 | + lines |
| 230 | + end |
| 231 | + |
233 | 232 | # Generates the expected log lines in colored mode, with/without `[prefix]` |
234 | 233 | def generic_colored_log_lines(prefix: nil) |
235 | 234 | if prefix |
|
0 commit comments