Skip to content

Commit d3f0d41

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

File tree

1 file changed

+48
-56
lines changed

1 file changed

+48
-56
lines changed

spec/unit/run_spec.rb

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@
1212
end
1313

1414
it "runs command successfully with logging" do
15-
output = StringIO.new
1615
uuid = "xxxx"
1716
allow(SecureRandom).to receive(:uuid).and_return(uuid)
18-
command = TTY::Command.new(output: output)
1917

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
2122

22-
output.rewind
23-
lines = output.readlines
24-
lines.last.gsub!(/\d+\.\d+/, "x")
2523
expect(lines).to eq([
2624
"[\e[32m#{uuid}\e[0m] Running \e[33;1mecho hello\e[0m\n",
2725
"[\e[32m#{uuid}\e[0m] \thello\n",
@@ -31,16 +29,14 @@
3129
end
3230

3331
it "runs command successfully with logging without color" do
34-
output = StringIO.new
3532
uuid = "xxxx"
3633
allow(SecureRandom).to receive(:uuid).and_return(uuid)
37-
command = TTY::Command.new(output: output, color: false)
3834

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
4039

41-
output.rewind
42-
lines = output.readlines
43-
lines.last.gsub!(/\d+\.\d+/, "x")
4440
expect(lines).to eq([
4541
"[#{uuid}] Running echo hello\n",
4642
"[#{uuid}] \thello\n",
@@ -50,16 +46,14 @@
5046

5147
it "runs command and fails with logging" do
5248
non_zero_exit = fixtures_path("non_zero_exit")
53-
output = StringIO.new
5449
uuid = "xxxx"
5550
allow(SecureRandom).to receive(:uuid).and_return(uuid)
56-
command = TTY::Command.new(output: output)
5751

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
5956

60-
output.rewind
61-
lines = output.readlines
62-
lines.last.gsub!(/\d+\.\d+/, "x")
6357
expect(lines).to eq([
6458
"[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{non_zero_exit}\e[0m\n",
6559
"[\e[32m#{uuid}\e[0m] \tnooo\n",
@@ -113,17 +107,16 @@
113107
phased_output = fixtures_path("phased_output")
114108
uuid = "xxxx"
115109
allow(SecureRandom).to receive(:uuid).and_return(uuid)
116-
output = StringIO.new
117-
cmd = TTY::Command.new(output: output)
118110

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
120116

121117
expect(out).to eq("." * 10)
122118
expect(err).to eq("")
123119

124-
output.rewind
125-
lines = output.readlines
126-
lines.last.gsub!(/\d+\.\d+/, "x")
127120
expect(lines).to eq([
128121
"[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{phased_output}\e[0m\n",
129122
"[\e[32m#{uuid}\e[0m] \t..........\n",
@@ -155,28 +148,22 @@
155148

156149
context "with uuid option" do
157150
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
160155

161-
command.run(:echo, "hello")
162-
output.rewind
163-
164-
lines = output.readlines
165-
lines.last.gsub!(/\d+\.\d+/, "x")
166156
expect(lines).to eq(
167157
generic_colored_log_lines(prefix: nil)
168158
)
169159
end
170160

171161
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
174166

175-
command.run(:echo, "hello", uuid: false)
176-
output.rewind
177-
178-
lines = output.readlines
179-
lines.last.gsub!(/\d+\.\d+/, "x")
180167
expect(lines).to eq(
181168
generic_colored_log_lines(prefix: nil)
182169
)
@@ -185,51 +172,56 @@
185172

186173
context "with tag option" do
187174
it "prints the tag set globally" do
188-
output = StringIO.new
189175
tag = "task"
190-
command = TTY::Command.new(output: output, tag: tag)
191176

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
193181

194-
output.rewind
195-
lines = output.readlines
196-
lines.last.gsub!(/\d+\.\d+/, "x")
197182
expect(lines).to eq(
198183
generic_colored_log_lines(prefix: tag)
199184
)
200185
end
201186

202187
it "prints the tag set locally" do
203-
output = StringIO.new
204188
tag = "task"
205-
command = TTY::Command.new(output: output)
206189

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
208194

209-
output.rewind
210-
lines = output.readlines
211-
lines.last.gsub!(/\d+\.\d+/, "x")
212195
expect(lines).to eq(
213196
generic_colored_log_lines(prefix: tag)
214197
)
215198
end
216199

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

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
223207

224-
output.rewind
225-
lines = output.readlines
226-
lines.last.gsub!(/\d+\.\d+/, "x")
227208
expect(lines).to eq(
228209
generic_colored_log_lines(prefix: tag)
229210
)
230211
end
231212
end
232213

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

0 commit comments

Comments
 (0)