From faa5dffbcc0a2649ee8f427905d45d7061975bcb Mon Sep 17 00:00:00 2001 From: Lin Jen-Shin Date: Thu, 13 Apr 2017 02:11:35 +0800 Subject: [PATCH] After Trace#limit, we seek to the next line in case of breaking ANSI sequence or Unicode --- lib/gitlab/ci/trace/stream.rb | 1 + spec/fixtures/trace/ansi-sequence-and-unicode | 5 +++++ spec/lib/gitlab/ci/trace/stream_spec.rb | 22 ++++++++++++++++++- spec/support/fixture_helpers.rb | 7 ++++-- 4 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/trace/ansi-sequence-and-unicode diff --git a/lib/gitlab/ci/trace/stream.rb b/lib/gitlab/ci/trace/stream.rb index 41dcf846fed..5e634bcac71 100644 --- a/lib/gitlab/ci/trace/stream.rb +++ b/lib/gitlab/ci/trace/stream.rb @@ -30,6 +30,7 @@ module Gitlab last_bytes = stream_size end stream.seek(-last_bytes, IO::SEEK_END) + stream.readline end def append(data, offset) diff --git a/spec/fixtures/trace/ansi-sequence-and-unicode b/spec/fixtures/trace/ansi-sequence-and-unicode new file mode 100644 index 00000000000..5d2466f0d0f --- /dev/null +++ b/spec/fixtures/trace/ansi-sequence-and-unicode @@ -0,0 +1,5 @@ +. +.. +😺 +ヾ(´༎ຶД༎ຶ`)ノ +許功蓋 diff --git a/spec/lib/gitlab/ci/trace/stream_spec.rb b/spec/lib/gitlab/ci/trace/stream_spec.rb index 2e57ccef182..61ac76401ea 100644 --- a/spec/lib/gitlab/ci/trace/stream_spec.rb +++ b/spec/lib/gitlab/ci/trace/stream_spec.rb @@ -21,7 +21,7 @@ describe Gitlab::Ci::Trace::Stream do end end - it 'if size is larger we start from beggining' do + it 'if size is larger we start from beginning' do stream.limit(10) expect(stream.tell).to eq(0) @@ -32,6 +32,26 @@ describe Gitlab::Ci::Trace::Stream do expect(stream.tell).to eq(6) end + + context 'when the trace contains ANSI sequence and Unicode' do + let(:stream) do + described_class.new do + File.open(expand_fixture_path('trace/ansi-sequence-and-unicode')) + end + end + + it 'forwards to the next linefeed, case 1' do + stream.limit(7) + + expect(stream.raw).to eq('') + end + + it 'forwards to the next linefeed, case 2' do + stream.limit(29) + + expect(stream.raw).to eq("\e[01;32m許功蓋\e[0m\n") + end + end end describe '#append' do diff --git a/spec/support/fixture_helpers.rb b/spec/support/fixture_helpers.rb index a05c9d18002..5515c355cea 100644 --- a/spec/support/fixture_helpers.rb +++ b/spec/support/fixture_helpers.rb @@ -1,8 +1,11 @@ module FixtureHelpers def fixture_file(filename) return '' if filename.blank? - file_path = File.expand_path(Rails.root.join('spec/fixtures/', filename)) - File.read(file_path) + File.read(expand_fixture_path(filename)) + end + + def expand_fixture_path(filename) + File.expand_path(Rails.root.join('spec/fixtures/', filename)) end end -- GitLab