diff --git a/lib/gitlab/downtime_check/message.rb b/lib/gitlab/downtime_check/message.rb index 4446e921e0df989d2cd0b7a54dafd84388becfe9..40a4815a9a02ba320d8ab71c4e075ffe96be9f20 100644 --- a/lib/gitlab/downtime_check/message.rb +++ b/lib/gitlab/downtime_check/message.rb @@ -1,10 +1,10 @@ module Gitlab class DowntimeCheck class Message - attr_reader :path, :offline, :reason + attr_reader :path, :offline - OFFLINE = "\e[32moffline\e[0m" - ONLINE = "\e[31monline\e[0m" + OFFLINE = "\e[31moffline\e[0m" + ONLINE = "\e[32monline\e[0m" # path - The file path of the migration. # offline - When set to `true` the migration will require downtime. @@ -19,10 +19,21 @@ def to_s label = offline ? OFFLINE : ONLINE message = "[#{label}]: #{path}" - message += ": #{reason}" if reason + + if reason? + message += ":\n\n#{reason}\n\n" + end message end + + def reason? + @reason.present? + end + + def reason + @reason.strip.lines.map(&:strip).join("\n") + end end end end diff --git a/spec/lib/gitlab/downtime_check/message_spec.rb b/spec/lib/gitlab/downtime_check/message_spec.rb index 93094cda776f03f3e7b31a629c8b59f20192fc5d..a5a398abf78b73d8b0d7fd62a470958cfc791553 100644 --- a/spec/lib/gitlab/downtime_check/message_spec.rb +++ b/spec/lib/gitlab/downtime_check/message_spec.rb @@ -5,13 +5,35 @@ it 'returns an ANSI formatted String for an offline migration' do message = described_class.new('foo.rb', true, 'hello') - expect(message.to_s).to eq("[\e[32moffline\e[0m]: foo.rb: hello") + expect(message.to_s).to eq("[\e[31moffline\e[0m]: foo.rb:\n\nhello\n\n") end it 'returns an ANSI formatted String for an online migration' do message = described_class.new('foo.rb') - expect(message.to_s).to eq("[\e[31monline\e[0m]: foo.rb") + expect(message.to_s).to eq("[\e[32monline\e[0m]: foo.rb") + end + end + + describe '#reason?' do + it 'returns false when no reason is specified' do + message = described_class.new('foo.rb') + + expect(message.reason?).to eq(false) + end + + it 'returns true when a reason is specified' do + message = described_class.new('foo.rb', true, 'hello') + + expect(message.reason?).to eq(true) + end + end + + describe '#reason' do + it 'strips excessive whitespace from the returned String' do + message = described_class.new('foo.rb', true, " hello\n world\n\n foo") + + expect(message.reason).to eq("hello\nworld\n\nfoo") end end end