summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShinya Maeda <gitlab.shinyamaeda@gmail.com>2017-05-10 02:40:34 +0900
committerShinya Maeda <gitlab.shinyamaeda@gmail.com>2017-05-24 20:10:14 +0900
commit3d11b7d8e25ca39b347e61e12100c7745b6ea085 (patch)
tree521f2659d3f3704ede9314ae25facdf235a8f140
parentddf67424ec5d84ff373e5e39195b5012c84eb920 (diff)
downloadgitlab-ce-3d11b7d8e25ca39b347e61e12100c7745b6ea085.tar.gz
Fix reverse_line from chunk based
-rw-r--r--lib/gitlab/ci/trace/stream.rb19
-rw-r--r--spec/lib/gitlab/ci/trace/stream_spec.rb22
2 files changed, 39 insertions, 2 deletions
diff --git a/lib/gitlab/ci/trace/stream.rb b/lib/gitlab/ci/trace/stream.rb
index 72a0d669de0..546f6788a78 100644
--- a/lib/gitlab/ci/trace/stream.rb
+++ b/lib/gitlab/ci/trace/stream.rb
@@ -119,16 +119,31 @@ module Gitlab
def reverse_line
pos = BUFFER_SIZE
max = stream.size
+ debris = ''
while pos < max
stream.seek(-pos, IO::SEEK_END)
- yield(stream.read(BUFFER_SIZE))
+ stream.read(BUFFER_SIZE).tap do |buf|
+ buf = buf + debris
+ lines = buf.split("\n")
+ lines.reverse.each do |line|
+ yield(line)
+ end
+ debris = lines.count > 0 ? lines[0] : ''
+ end
pos += BUFFER_SIZE
end
# Reached the head, read only left
stream.seek(0)
- yield(stream.read(BUFFER_SIZE - (pos - max)))
+ last = (max > BUFFER_SIZE) ? (max % BUFFER_SIZE) : max
+ stream.read(last).tap do |buf|
+ buf = buf + debris
+ lines = buf.split("\n")
+ lines.reverse.each do |line|
+ yield(line)
+ end
+ end
end
end
end
diff --git a/spec/lib/gitlab/ci/trace/stream_spec.rb b/spec/lib/gitlab/ci/trace/stream_spec.rb
index a607f000431..d90ccf4f799 100644
--- a/spec/lib/gitlab/ci/trace/stream_spec.rb
+++ b/spec/lib/gitlab/ci/trace/stream_spec.rb
@@ -254,6 +254,28 @@ describe Gitlab::Ci::Trace::Stream do
end
end
+ context 'when BUFFER_SIZE is smaller than stream.size' do
+ let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered\n' }
+ let(:regex) { '\(\d+.\d+\%\) covered' }
+
+ before do
+ stub_const('Gitlab::Ci::Trace::Stream::BUFFER_SIZE', 5)
+ end
+
+ it { is_expected.to eq("98.29") }
+ end
+
+ context 'when BUFFER_SIZE is equal to stream.size' do
+ let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered\n' }
+ let(:regex) { '\(\d+.\d+\%\) covered' }
+
+ before do
+ stub_const('Gitlab::Ci::Trace::Stream::BUFFER_SIZE', data.length)
+ end
+
+ it { is_expected.to eq("98.29") }
+ end
+
context 'using a regex capture' do
let(:data) { 'TOTAL 9926 3489 65%' }
let(:regex) { 'TOTAL\s+\d+\s+\d+\s+(\d{1,3}\%)' }