Skip to content

Commit 30ba8be

Browse files
committed
refactor: DRY up command output retrieval logic in run_spec.rb
1 parent fb8899a commit 30ba8be

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

spec/unit/run_spec.rb

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -155,28 +155,22 @@
155155

156156
context "with uuid option" do
157157
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+
}
160162

161-
command.run(:echo, "hello")
162-
output.rewind
163-
164-
lines = output.readlines
165-
lines.last.gsub!(/\d+\.\d+/, "x")
166163
expect(lines).to eq(
167164
generic_colored_log_lines(prefix: nil)
168165
)
169166
end
170167

171168
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+
}
177173

178-
lines = output.readlines
179-
lines.last.gsub!(/\d+\.\d+/, "x")
180174
expect(lines).to eq(
181175
generic_colored_log_lines(prefix: nil)
182176
)
@@ -185,51 +179,56 @@
185179

186180
context "with tag option" do
187181
it "prints the tag set globally" do
188-
output = StringIO.new
189182
tag = "task"
190-
command = TTY::Command.new(output: output, tag: tag)
191183

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+
}
193188

194-
output.rewind
195-
lines = output.readlines
196-
lines.last.gsub!(/\d+\.\d+/, "x")
197189
expect(lines).to eq(
198190
generic_colored_log_lines(prefix: tag)
199191
)
200192
end
201193

202194
it "prints the tag set locally" do
203-
output = StringIO.new
204195
tag = "task"
205-
command = TTY::Command.new(output: output)
206196

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+
}
208201

209-
output.rewind
210-
lines = output.readlines
211-
lines.last.gsub!(/\d+\.\d+/, "x")
212202
expect(lines).to eq(
213203
generic_colored_log_lines(prefix: tag)
214204
)
215205
end
216206

217207
it "prints the tag even if uuid is set to false" do
218-
output = StringIO.new
219208
tag = "task"
220-
command = TTY::Command.new(output: output, tag: tag, uuid: false)
221209

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+
}
223214

224-
output.rewind
225-
lines = output.readlines
226-
lines.last.gsub!(/\d+\.\d+/, "x")
227215
expect(lines).to eq(
228216
generic_colored_log_lines(prefix: tag)
229217
)
230218
end
231219
end
232220

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+
233232
# Generates the expected log lines in colored mode, with/without `[prefix]`
234233
def generic_colored_log_lines(prefix: nil)
235234
if prefix

0 commit comments

Comments
 (0)