Skip to content

Commit fb8899a

Browse files
committed
refactor: Rearrange context blocks in run_spec.rb
1 parent 8854ff9 commit fb8899a

File tree

1 file changed

+82
-86
lines changed

1 file changed

+82
-86
lines changed

spec/unit/run_spec.rb

Lines changed: 82 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"[\e[32m#{uuid}\e[0m] Running \e[33;1mecho hello\e[0m\n",
2727
"[\e[32m#{uuid}\e[0m] \thello\n",
2828
"[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 " \
29-
"(\e[32;1msuccessful\e[0m)\n"
29+
"(\e[32;1msuccessful\e[0m)\n"
3030
])
3131
end
3232

@@ -48,87 +48,6 @@
4848
])
4949
end
5050

51-
# TODO:
52-
# Move "with xxx option" context to the end of
53-
# the describe block to follow RSpec style guide.
54-
55-
context "with uuid option" do
56-
it "runs command successfully with logging without uuid set globally" do
57-
output = StringIO.new
58-
command = TTY::Command.new(output: output, uuid: false)
59-
60-
command.run(:echo, "hello")
61-
output.rewind
62-
63-
lines = output.readlines
64-
lines.last.gsub!(/\d+\.\d+/, "x")
65-
expect(lines).to eq(
66-
generic_colored_log_lines(prefix: nil)
67-
)
68-
end
69-
70-
it "runs command successfully with logging without uuid set locally" do
71-
output = StringIO.new
72-
command = TTY::Command.new(output: output)
73-
74-
command.run(:echo, "hello", uuid: false)
75-
output.rewind
76-
77-
lines = output.readlines
78-
lines.last.gsub!(/\d+\.\d+/, "x")
79-
expect(lines).to eq(
80-
generic_colored_log_lines(prefix: nil)
81-
)
82-
end
83-
end
84-
85-
context "with tag option" do
86-
it "prints the tag set globally" do
87-
output = StringIO.new
88-
tag = "task"
89-
command = TTY::Command.new(output: output, tag: tag)
90-
91-
command.run(:echo, "hello")
92-
93-
output.rewind
94-
lines = output.readlines
95-
lines.last.gsub!(/\d+\.\d+/, "x")
96-
expect(lines).to eq(
97-
generic_colored_log_lines(prefix: tag)
98-
)
99-
end
100-
101-
it "prints the tag set locally" do
102-
output = StringIO.new
103-
tag = "task"
104-
command = TTY::Command.new(output: output)
105-
106-
command.run(:echo, "hello", tag: tag)
107-
108-
output.rewind
109-
lines = output.readlines
110-
lines.last.gsub!(/\d+\.\d+/, "x")
111-
expect(lines).to eq(
112-
generic_colored_log_lines(prefix: tag)
113-
)
114-
end
115-
116-
it "prints the tag even if uuid is set to false" do
117-
output = StringIO.new
118-
tag = "task"
119-
command = TTY::Command.new(output: output, tag: tag, uuid: false)
120-
121-
command.run(:echo, "hello")
122-
123-
output.rewind
124-
lines = output.readlines
125-
lines.last.gsub!(/\d+\.\d+/, "x")
126-
expect(lines).to eq(
127-
generic_colored_log_lines(prefix: tag)
128-
)
129-
end
130-
end
131-
13251
it "runs command and fails with logging" do
13352
non_zero_exit = fixtures_path("non_zero_exit")
13453
output = StringIO.new
@@ -145,7 +64,7 @@
14564
"[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{non_zero_exit}\e[0m\n",
14665
"[\e[32m#{uuid}\e[0m] \tnooo\n",
14766
"[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 1 " \
148-
"(\e[31;1mfailed\e[0m)\n"
67+
"(\e[31;1mfailed\e[0m)\n"
14968
])
15069
end
15170

@@ -209,12 +128,12 @@
209128
"[\e[32m#{uuid}\e[0m] Running \e[33;1mruby #{phased_output}\e[0m\n",
210129
"[\e[32m#{uuid}\e[0m] \t..........\n",
211130
"[\e[32m#{uuid}\e[0m] Finished in x seconds with exit status 0 " \
212-
"(\e[32;1msuccessful\e[0m)\n"
131+
"(\e[32;1msuccessful\e[0m)\n"
213132
])
214133
end
215134

216135
it "does not persist environment variables",
217-
unless: RSpec::Support::OS.windows? do
136+
unless: RSpec::Support::OS.windows? do
218137
output = StringIO.new
219138
command = TTY::Command.new(output: output)
220139

@@ -234,14 +153,91 @@
234153
expect(lines[0]).to include("Running \e[33;1mecho hello\e[0m\n")
235154
end
236155

156+
context "with uuid option" do
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)
160+
161+
command.run(:echo, "hello")
162+
output.rewind
163+
164+
lines = output.readlines
165+
lines.last.gsub!(/\d+\.\d+/, "x")
166+
expect(lines).to eq(
167+
generic_colored_log_lines(prefix: nil)
168+
)
169+
end
170+
171+
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
177+
178+
lines = output.readlines
179+
lines.last.gsub!(/\d+\.\d+/, "x")
180+
expect(lines).to eq(
181+
generic_colored_log_lines(prefix: nil)
182+
)
183+
end
184+
end
185+
186+
context "with tag option" do
187+
it "prints the tag set globally" do
188+
output = StringIO.new
189+
tag = "task"
190+
command = TTY::Command.new(output: output, tag: tag)
191+
192+
command.run(:echo, "hello")
193+
194+
output.rewind
195+
lines = output.readlines
196+
lines.last.gsub!(/\d+\.\d+/, "x")
197+
expect(lines).to eq(
198+
generic_colored_log_lines(prefix: tag)
199+
)
200+
end
201+
202+
it "prints the tag set locally" do
203+
output = StringIO.new
204+
tag = "task"
205+
command = TTY::Command.new(output: output)
206+
207+
command.run(:echo, "hello", tag: tag)
208+
209+
output.rewind
210+
lines = output.readlines
211+
lines.last.gsub!(/\d+\.\d+/, "x")
212+
expect(lines).to eq(
213+
generic_colored_log_lines(prefix: tag)
214+
)
215+
end
216+
217+
it "prints the tag even if uuid is set to false" do
218+
output = StringIO.new
219+
tag = "task"
220+
command = TTY::Command.new(output: output, tag: tag, uuid: false)
221+
222+
command.run(:echo, "hello")
223+
224+
output.rewind
225+
lines = output.readlines
226+
lines.last.gsub!(/\d+\.\d+/, "x")
227+
expect(lines).to eq(
228+
generic_colored_log_lines(prefix: tag)
229+
)
230+
end
231+
end
232+
237233
# Generates the expected log lines in colored mode, with/without `[prefix]`
238234
def generic_colored_log_lines(prefix: nil)
239235
if prefix
240236
[
241237
"[\e[32m#{prefix}\e[0m] Running \e[33;1mecho hello\e[0m\n",
242238
"[\e[32m#{prefix}\e[0m] \thello\n",
243239
"[\e[32m#{prefix}\e[0m] Finished in x seconds with exit status 0 " \
244-
"(\e[32;1msuccessful\e[0m)\n"
240+
"(\e[32;1msuccessful\e[0m)\n"
245241
]
246242
else
247243
[

0 commit comments

Comments
 (0)