gitlab-org--gitlab-foss/spec/lib/gitlab/ci/trace_reader_spec.rb
Lin Jen-Shin 050103f209 Make sure TraceReader uses Encoding.default_external
Encoding.default_external was chosen over
Encoding.default_internal because File.read is
returning Encoding.default_external, therefore
we should align with it. Alternatively, we could
force both of them to be Encoding.default_internal.

However, ideally this should be determined by different
projects. For example, some projects might want to use
an encoding different to what GitLab is using.

This might not happen soon though.

Closes #27052
2017-01-26 18:29:44 +08:00

52 lines
1.3 KiB
Ruby

require 'spec_helper'
describe Gitlab::Ci::TraceReader do
let(:path) { __FILE__ }
let(:lines) { File.readlines(path) }
let(:bytesize) { lines.sum(&:bytesize) }
it 'returns last few lines' do
10.times do
subject = build_subject
last_lines = random_lines
expected = lines.last(last_lines).join
result = subject.read(last_lines: last_lines)
expect(result).to eq(expected)
expect(result.encoding).to eq(Encoding.default_external)
end
end
it 'returns everything if trying to get too many lines' do
result = build_subject.read(last_lines: lines.size * 2)
expect(result).to eq(lines.join)
expect(result.encoding).to eq(Encoding.default_external)
end
it 'returns all contents if last_lines is not specified' do
result = build_subject.read
expect(result).to eq(lines.join)
expect(result.encoding).to eq(Encoding.default_external)
end
it 'raises an error if not passing an integer for last_lines' do
expect do
build_subject.read(last_lines: lines)
end.to raise_error(ArgumentError)
end
def random_lines
Random.rand(lines.size) + 1
end
def random_buffer
Random.rand(bytesize) + 1
end
def build_subject
described_class.new(__FILE__, buffer_size: random_buffer)
end
end