Skip to content

Commit 8854ff9

Browse files
committed
refactor: DRY up duplicated expected log lines in run_spec.rb
1 parent 9971c07 commit 8854ff9

File tree

1 file changed

+33
-28
lines changed

1 file changed

+33
-28
lines changed

spec/unit/run_spec.rb

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@
6262

6363
lines = output.readlines
6464
lines.last.gsub!(/\d+\.\d+/, "x")
65-
expect(lines).to eq([
66-
"Running \e[33;1mecho hello\e[0m\n",
67-
"\thello\n",
68-
"Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
69-
])
65+
expect(lines).to eq(
66+
generic_colored_log_lines(prefix: nil)
67+
)
7068
end
7169

7270
it "runs command successfully with logging without uuid set locally" do
@@ -78,11 +76,9 @@
7876

7977
lines = output.readlines
8078
lines.last.gsub!(/\d+\.\d+/, "x")
81-
expect(lines).to eq([
82-
"Running \e[33;1mecho hello\e[0m\n",
83-
"\thello\n",
84-
"Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
85-
])
79+
expect(lines).to eq(
80+
generic_colored_log_lines(prefix: nil)
81+
)
8682
end
8783
end
8884

@@ -97,12 +93,9 @@
9793
output.rewind
9894
lines = output.readlines
9995
lines.last.gsub!(/\d+\.\d+/, "x")
100-
expect(lines).to eq([
101-
"[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n",
102-
"[\e[32m#{tag}\e[0m] \thello\n",
103-
"[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \
104-
"(\e[32;1msuccessful\e[0m)\n"
105-
])
96+
expect(lines).to eq(
97+
generic_colored_log_lines(prefix: tag)
98+
)
10699
end
107100

108101
it "prints the tag set locally" do
@@ -115,12 +108,9 @@
115108
output.rewind
116109
lines = output.readlines
117110
lines.last.gsub!(/\d+\.\d+/, "x")
118-
expect(lines).to eq([
119-
"[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n",
120-
"[\e[32m#{tag}\e[0m] \thello\n",
121-
"[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \
122-
"(\e[32;1msuccessful\e[0m)\n"
123-
])
111+
expect(lines).to eq(
112+
generic_colored_log_lines(prefix: tag)
113+
)
124114
end
125115

126116
it "prints the tag even if uuid is set to false" do
@@ -133,12 +123,9 @@
133123
output.rewind
134124
lines = output.readlines
135125
lines.last.gsub!(/\d+\.\d+/, "x")
136-
expect(lines).to eq([
137-
"[\e[32m#{tag}\e[0m] Running \e[33;1mecho hello\e[0m\n",
138-
"[\e[32m#{tag}\e[0m] \thello\n",
139-
"[\e[32m#{tag}\e[0m] Finished in x seconds with exit status 0 " \
140-
"(\e[32;1msuccessful\e[0m)\n"
141-
])
126+
expect(lines).to eq(
127+
generic_colored_log_lines(prefix: tag)
128+
)
142129
end
143130
end
144131

@@ -246,4 +233,22 @@
246233
lines = output.readlines
247234
expect(lines[0]).to include("Running \e[33;1mecho hello\e[0m\n")
248235
end
236+
237+
# Generates the expected log lines in colored mode, with/without `[prefix]`
238+
def generic_colored_log_lines(prefix: nil)
239+
if prefix
240+
[
241+
"[\e[32m#{prefix}\e[0m] Running \e[33;1mecho hello\e[0m\n",
242+
"[\e[32m#{prefix}\e[0m] \thello\n",
243+
"[\e[32m#{prefix}\e[0m] Finished in x seconds with exit status 0 " \
244+
"(\e[32;1msuccessful\e[0m)\n"
245+
]
246+
else
247+
[
248+
"Running \e[33;1mecho hello\e[0m\n",
249+
"\thello\n",
250+
"Finished in x seconds with exit status 0 (\e[32;1msuccessful\e[0m)\n"
251+
]
252+
end
253+
end
249254
end

0 commit comments

Comments
 (0)