Commit d4290995 authored by Shinya Maeda's avatar Shinya Maeda

Add tests for Trace::Stream

parent b94c84e5
......@@ -24,7 +24,7 @@ module Ci
raw_data
else
raise 'Unsupported data store'
end
end&.force_encoding(Encoding::BINARY)
end
def set_data(value)
......
require 'spec_helper'
describe Gitlab::Ci::Trace::Stream do
describe Gitlab::Ci::Trace::Stream, :clean_gitlab_redis_cache do
set(:job) { create(:ci_build, :running) }
before do
stub_feature_flags(ci_enable_live_trace: true)
end
describe 'delegates' do
subject { described_class.new { nil } }
......@@ -15,12 +21,7 @@ describe Gitlab::Ci::Trace::Stream do
end
describe '#limit' do
let(:stream) do
described_class.new do
StringIO.new((1..8).to_a.join("\n"))
end
end
shared_examples_for 'limits' do
it 'if size is larger we start from beginning' do
stream.limit(20)
......@@ -70,21 +71,32 @@ describe Gitlab::Ci::Trace::Stream do
end
end
describe '#append' do
let(:tempfile) { Tempfile.new }
context 'when stream is StringIO' do
let(:stream) do
described_class.new do
StringIO.new((1..8).to_a.join("\n"))
end
end
it_behaves_like 'limits'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
tempfile.write("12345678")
tempfile.rewind
tempfile
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write((1..8).to_a.join("\n"))
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
after do
tempfile.unlink
it_behaves_like 'limits'
end
end
describe '#append' do
shared_examples_for 'appends' do
it "truncates and append content" do
stream.append("89", 4)
stream.seek(0)
......@@ -105,13 +117,40 @@ describe Gitlab::Ci::Trace::Stream do
end
end
describe '#set' do
context 'when stream is StringIO' do
let(:tempfile) { Tempfile.new }
let(:stream) do
described_class.new do
StringIO.new("12345678")
tempfile.write("12345678")
tempfile.rewind
tempfile
end
end
after do
tempfile.unlink
end
it_behaves_like 'appends'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write('12345678')
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'appends'
end
end
describe '#set' do
shared_examples_for 'sets' do
before do
stream.set("8901")
end
......@@ -124,15 +163,32 @@ describe Gitlab::Ci::Trace::Stream do
end
end
describe '#raw' do
let(:path) { __FILE__ }
let(:lines) { File.readlines(path) }
context 'when stream is StringIO' do
let(:stream) do
described_class.new do
File.open(path)
StringIO.new("12345678")
end
end
it_behaves_like 'sets'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write('12345678')
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'sets'
end
end
describe '#raw' do
shared_examples_for 'sets' do
it 'returns all contents if last_lines is not specified' do
result = stream.raw
......@@ -163,13 +219,33 @@ describe Gitlab::Ci::Trace::Stream do
end
end
describe '#html_with_state' do
let(:path) { __FILE__ }
let(:lines) { File.readlines(path) }
context 'when stream is File' do
let(:stream) do
described_class.new do
StringIO.new("1234")
File.open(path)
end
end
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write(File.binread(path))
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'sets'
end
end
describe '#html_with_state' do
shared_examples_for 'html_with_states' do
it 'returns html content with state' do
result = stream.html_with_state
......@@ -193,13 +269,32 @@ describe Gitlab::Ci::Trace::Stream do
end
end
describe '#html' do
context 'when stream is StringIO' do
let(:stream) do
described_class.new do
StringIO.new("12\n34\n56")
StringIO.new("1234")
end
end
it_behaves_like 'html_with_states'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write("1234")
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'html_with_states'
end
end
describe '#html' do
shared_examples_for 'htmls' do
it "returns html" do
expect(stream.html).to eq("12<br>34<br>56")
end
......@@ -209,15 +304,32 @@ describe Gitlab::Ci::Trace::Stream do
end
end
describe '#extract_coverage' do
context 'when stream is StringIO' do
let(:stream) do
described_class.new do
StringIO.new(data)
StringIO.new("12\n34\n56")
end
end
subject { stream.extract_coverage(regex) }
it_behaves_like 'htmls'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write("12\n34\n56")
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'htmls'
end
end
describe '#extract_coverage' do
shared_examples_for 'extract_coverages' do
context 'valid content & regex' do
let(:data) { 'Coverage 1033 / 1051 LOC (98.29%) covered' }
let(:regex) { '\(\d+.\d+\%\) covered' }
......@@ -344,4 +456,30 @@ describe Gitlab::Ci::Trace::Stream do
end
end
end
subject { stream.extract_coverage(regex) }
context 'when stream is StringIO' do
let(:stream) do
described_class.new do
StringIO.new(data)
end
end
it_behaves_like 'extract_coverages'
end
context 'when stream is ChunkedIO' do
let(:stream) do
described_class.new do
Gitlab::Ci::Trace::ChunkedIO.new(job).tap do |chunked_io|
chunked_io.write(data)
chunked_io.seek(0, IO::SEEK_SET)
end
end
end
it_behaves_like 'extract_coverages'
end
end
end
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