Commit 11663a5b authored by Yorick Peterse's avatar Yorick Peterse

Handle empty chat output better

If the output was empty we would end up running `''.split("\n")`,
producing an empty Array. Slicing an empty Array using a range such as
1..-1 would then produce a nil value, instead of an empty Array. This in
turn would cause a NoMethodError since "join" is not defined for
NilClass.
parent 9a1c93fe
......@@ -59,7 +59,18 @@ module Gitlab
#
# output - A `String` containing the output of a trace section.
def without_executed_command_line(output)
output.split("\n")[1..-1].join("\n")
# If `output.split("\n")` produces an empty Array then the slicing that
# follows it will produce a nil. For example:
#
# "\n".split("\n") # => []
# "\n".split("\n")[1..-1] # => nil
#
# To work around this we only "join" if we're given an Array.
if (converted = output.split("\n")[1..-1])
converted.join("\n")
else
''
end
end
# Returns the trace section for the given name, or `nil` if the section
......
......@@ -71,6 +71,14 @@ describe Gitlab::Chat::Output do
expect(output.without_executed_command_line("hello\nworld"))
.to eq('world')
end
it 'returns an empty String when the input is empty' do
expect(output.without_executed_command_line('')).to eq('')
end
it 'returns an empty String when the input consits of a single newline' do
expect(output.without_executed_command_line("\n")).to eq('')
end
end
describe '#find_build_trace_section' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment