debian-mirror-gitlab/spec/lib/gitlab/ci/ansi2json_spec.rb

594 lines
18 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Ci::Ansi2json do
2019-12-21 20:55:43 +05:30
subject { described_class }
describe 'lines' do
it 'prints non-ansi as-is' do
2022-11-25 23:54:43 +05:30
expect(convert_json('Hello')).to eq([{ offset: 0, content: [{ text: 'Hello' }] }])
2019-12-21 20:55:43 +05:30
end
2019-12-26 22:10:19 +05:30
context 'new lines' do
it 'adds new line when encountering \n' do
2022-11-25 23:54:43 +05:30
expect(convert_json("Hello\nworld")).to eq(
[
{ offset: 0, content: [{ text: 'Hello' }] },
{ offset: 6, content: [{ text: 'world' }] }
])
2019-12-26 22:10:19 +05:30
end
it 'adds new line when encountering \r\n' do
2022-11-25 23:54:43 +05:30
expect(convert_json("Hello\r\nworld")).to eq(
[
{ offset: 0, content: [{ text: 'Hello' }] },
{ offset: 7, content: [{ text: 'world' }] }
])
2019-12-26 22:10:19 +05:30
end
2022-07-16 23:28:13 +05:30
it 'ignores empty newlines' do
2022-11-25 23:54:43 +05:30
expect(convert_json("Hello\n\nworld")).to eq(
[
{ offset: 0, content: [{ text: 'Hello' }] },
{ offset: 7, content: [{ text: 'world' }] }
])
expect(convert_json("Hello\r\n\r\nworld")).to eq(
[
{ offset: 0, content: [{ text: 'Hello' }] },
{ offset: 9, content: [{ text: 'world' }] }
])
2022-07-16 23:28:13 +05:30
end
2019-12-26 22:10:19 +05:30
it 'replace the current line when encountering \r' do
2022-11-25 23:54:43 +05:30
expect(convert_json("Hello\rworld")).to eq([{ offset: 0, content: [{ text: 'world' }] }])
2019-12-26 22:10:19 +05:30
end
2019-12-21 20:55:43 +05:30
end
it 'recognizes color changing ANSI sequences' do
2022-11-25 23:54:43 +05:30
expect(convert_json("\e[31mHello\e[0m")).to eq(
[
{ offset: 0, content: [{ text: 'Hello', style: 'term-fg-red' }] }
])
2019-12-21 20:55:43 +05:30
end
it 'recognizes color changing ANSI sequences across multiple lines' do
2022-11-25 23:54:43 +05:30
expect(convert_json("\e[31mHello\nWorld\e[0m")).to eq(
[
{ offset: 0, content: [{ text: 'Hello', style: 'term-fg-red' }] },
{ offset: 11, content: [{ text: 'World', style: 'term-fg-red' }] }
])
2019-12-21 20:55:43 +05:30
end
it 'recognizes background and foreground colors' do
2022-11-25 23:54:43 +05:30
expect(convert_json("\e[31;44mHello")).to eq(
[
{ offset: 0, content: [{ text: 'Hello', style: 'term-fg-red term-bg-blue' }] }
])
2019-12-21 20:55:43 +05:30
end
it 'recognizes style changes within the same line' do
2022-11-25 23:54:43 +05:30
expect(convert_json("\e[31;44mHello\e[0m world")).to eq(
[
{ offset: 0, content: [
{ text: 'Hello', style: 'term-fg-red term-bg-blue' },
{ text: ' world' }
] }
])
2019-12-21 20:55:43 +05:30
end
context 'with section markers' do
let(:section_name) { 'prepare-script' }
let(:section_duration) { 63.seconds }
let(:section_start_time) { Time.new(2019, 9, 17).utc }
let(:section_end_time) { section_start_time + section_duration }
2022-08-27 11:52:29 +05:30
let(:section_start) { "section_start:#{section_start_time.to_i}:#{section_name}\r\033[0K" }
let(:section_end) { "section_end:#{section_end_time.to_i}:#{section_name}\r\033[0K" }
2019-12-21 20:55:43 +05:30
it 'marks the first line of the section as header' do
2022-11-25 23:54:43 +05:30
expect(convert_json("Hello#{section_start}world!")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello' }]
},
{
offset: 5,
content: [{ text: 'world!' }],
section: 'prepare-script',
section_header: true
}
])
2019-12-21 20:55:43 +05:30
end
it 'does not marks the other lines of the section as header' do
2022-11-25 23:54:43 +05:30
expect(convert_json("outside section#{section_start}Hello\nworld!")).to eq(
[
{
offset: 0,
content: [{ text: 'outside section' }]
},
{
offset: 15,
content: [{ text: 'Hello' }],
section: 'prepare-script',
section_header: true
},
{
offset: 65,
content: [{ text: 'world!' }],
section: 'prepare-script'
}
])
2019-12-21 20:55:43 +05:30
end
it 'marks the last line of the section as footer' do
2022-11-25 23:54:43 +05:30
expect(convert_json("#{section_start}Good\nmorning\nworld!#{section_end}")).to eq(
[
{
offset: 0,
content: [{ text: 'Good' }],
section: 'prepare-script',
section_header: true
},
{
offset: 49,
content: [{ text: 'morning' }],
section: 'prepare-script'
},
{
offset: 57,
content: [{ text: 'world!' }],
section: 'prepare-script'
},
{
offset: 63,
content: [],
section_duration: '01:03',
section: 'prepare-script'
}
])
2019-12-21 20:55:43 +05:30
end
it 'marks the first line as header and footer if is the only line in the section' do
2022-11-25 23:54:43 +05:30
expect(convert_json("#{section_start}Hello world!#{section_end}")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello world!' }],
section: 'prepare-script',
section_header: true
},
{
offset: 56,
content: [],
section: 'prepare-script',
section_duration: '01:03'
}
])
2019-12-21 20:55:43 +05:30
end
it 'does not add sections attribute to lines after the section is closed' do
2022-11-25 23:54:43 +05:30
expect(convert_json("#{section_start}Hello#{section_end}world")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello' }],
section: 'prepare-script',
section_header: true
},
{
offset: 49,
content: [],
section: 'prepare-script',
section_duration: '01:03'
},
{
offset: 91,
content: [{ text: 'world' }]
}
])
2019-12-21 20:55:43 +05:30
end
it 'ignores section_end marker if no section_start exists' do
2022-11-25 23:54:43 +05:30
expect(convert_json("Hello #{section_end}world")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello world' }]
}
])
2019-12-21 20:55:43 +05:30
end
context 'when section name contains .-_ and capital letters' do
let(:section_name) { 'a.Legit-SeCtIoN_namE' }
it 'sanitizes the section name' do
2022-11-25 23:54:43 +05:30
expect(convert_json("Hello#{section_start}world!")).to eq(
[
{
offset: 0,
content: [{ text: 'Hello' }]
},
{
offset: 5,
content: [{ text: 'world!' }],
section: 'a-legit-section-name',
section_header: true
}
])
2019-12-21 20:55:43 +05:30
end
end
context 'when section name includes $' do
let(:section_name) { 'my_$ection' }
it 'ignores the section' do
2022-11-25 23:54:43 +05:30
expect(convert_json("#{section_start}hello")).to eq(
[
{
offset: 0,
content: [{ text: 'hello' }]
}
])
2019-12-21 20:55:43 +05:30
end
end
context 'when section name includes <' do
let(:section_name) { '<a_tag>' }
it 'ignores the section' do
2022-11-25 23:54:43 +05:30
expect(convert_json("#{section_start}hello")).to eq(
[
{
offset: 0,
content: [{ text: 'hello' }]
}
])
2019-12-21 20:55:43 +05:30
end
end
2019-12-26 22:10:19 +05:30
it 'prints HTML tags as is' do
trace = "#{section_start}section_end:1:2<div>hello</div>#{section_end}"
2022-11-25 23:54:43 +05:30
expect(convert_json(trace)).to eq(
[
{
offset: 0,
content: [{ text: 'section_end:1:2<div>hello</div>' }],
section: 'prepare-script',
section_header: true
},
{
offset: 75,
content: [],
section: 'prepare-script',
section_duration: '01:03'
}
])
2019-12-21 20:55:43 +05:30
end
context 'with nested section' do
let(:nested_section_name) { 'prepare-script-nested' }
let(:nested_section_duration) { 2.seconds }
let(:nested_section_start_time) { Time.new(2019, 9, 17).utc }
let(:nested_section_end_time) { nested_section_start_time + nested_section_duration }
2022-08-27 11:52:29 +05:30
let(:nested_section_start) { "section_start:#{nested_section_start_time.to_i}:#{nested_section_name}\r\033[0K" }
let(:nested_section_end) { "section_end:#{nested_section_end_time.to_i}:#{nested_section_name}\r\033[0K" }
2019-12-21 20:55:43 +05:30
it 'adds multiple sections to the lines inside the nested section' do
trace = "Hello#{section_start}foo#{nested_section_start}bar#{nested_section_end}baz#{section_end}world"
2022-11-25 23:54:43 +05:30
expect(convert_json(trace)).to eq(
[
2019-12-21 20:55:43 +05:30
{
offset: 0,
content: [{ text: 'Hello' }]
},
{
offset: 5,
content: [{ text: 'foo' }],
section: 'prepare-script',
section_header: true
},
{
offset: 52,
content: [{ text: 'bar' }],
section: 'prepare-script-nested',
section_header: true
},
{
offset: 106,
content: [],
section: 'prepare-script-nested',
section_duration: '00:02'
},
{
2019-12-26 22:10:19 +05:30
offset: 155,
2019-12-21 20:55:43 +05:30
content: [{ text: 'baz' }],
section: 'prepare-script'
},
{
offset: 158,
content: [],
section: 'prepare-script',
section_duration: '01:03'
},
{
2019-12-26 22:10:19 +05:30
offset: 200,
2019-12-21 20:55:43 +05:30
content: [{ text: 'world' }]
}
])
end
it 'adds multiple sections to the lines inside the nested section and closes all sections together' do
trace = "Hello#{section_start}\e[91mfoo\e[0m#{nested_section_start}bar#{nested_section_end}#{section_end}"
2022-11-25 23:54:43 +05:30
expect(convert_json(trace)).to eq(
[
2019-12-21 20:55:43 +05:30
{
offset: 0,
content: [{ text: 'Hello' }]
},
{
offset: 5,
content: [{ text: 'foo', style: 'term-fg-l-red' }],
section: 'prepare-script',
section_header: true
},
{
offset: 61,
content: [{ text: 'bar' }],
section: 'prepare-script-nested',
section_header: true
},
{
offset: 115,
content: [],
section: 'prepare-script-nested',
section_duration: '00:02'
},
{
2019-12-26 22:10:19 +05:30
offset: 164,
2019-12-21 20:55:43 +05:30
content: [],
section: 'prepare-script',
section_duration: '01:03'
}
])
end
end
2021-01-03 14:25:43 +05:30
context 'with section options' do
2022-08-27 11:52:29 +05:30
let(:option_section_start) { "section_start:#{section_start_time.to_i}:#{section_name}[collapsed=true,unused_option=123]\r\033[0K" }
2021-01-03 14:25:43 +05:30
it 'provides section options when set' do
trace = "#{option_section_start}hello#{section_end}"
2022-11-25 23:54:43 +05:30
expect(convert_json(trace)).to eq(
[
{
offset: 0,
content: [{ text: 'hello' }],
section: 'prepare-script',
section_header: true,
section_options: {
'collapsed' => 'true',
'unused_option' => '123'
}
},
{
offset: 83,
content: [],
section: 'prepare-script',
section_duration: '01:03'
2021-01-03 14:25:43 +05:30
}
2022-11-25 23:54:43 +05:30
])
2021-01-03 14:25:43 +05:30
end
end
2019-12-21 20:55:43 +05:30
end
describe 'incremental updates' do
let(:pass1_stream) { StringIO.new(pre_text) }
let(:pass2_stream) { StringIO.new(pre_text + text) }
let(:pass1) { subject.convert(pass1_stream) }
let(:pass2) { subject.convert(pass2_stream, pass1.state) }
context 'with split word' do
let(:pre_text) { "\e[1mHello " }
2021-01-03 14:25:43 +05:30
let(:text) { 'World' }
2019-12-21 20:55:43 +05:30
let(:lines) do
[
{ offset: 0, content: [{ text: 'Hello World', style: 'term-bold' }] }
]
end
it 'returns the full line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_falsey
end
end
context 'with split word on second line' do
let(:pre_text) { "Good\nmorning " }
2021-01-03 14:25:43 +05:30
let(:text) { 'World' }
2019-12-21 20:55:43 +05:30
let(:lines) do
[
{ offset: 5, content: [{ text: 'morning World' }] }
]
end
it 'returns all lines since last partially processed line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_truthy
end
end
context 'with split sequence across multiple lines' do
let(:pre_text) { "\e[1mgood\nmorning\n" }
let(:text) { "\e[3mworld" }
let(:lines) do
[
{ offset: 17, content: [{ text: 'world', style: 'term-bold term-italic' }] }
]
end
2019-12-26 22:10:19 +05:30
it 'returns the line since last partially processed line' do
2019-12-21 20:55:43 +05:30
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_truthy
end
end
context 'with split partial sequence' do
let(:pre_text) { "hello\e" }
let(:text) { "[1m world" }
let(:lines) do
[
{ offset: 0, content: [
{ text: 'hello' },
{ text: ' world', style: 'term-bold' }
] }
]
end
2019-12-26 22:10:19 +05:30
it 'returns the line since last partially processed line' do
2019-12-21 20:55:43 +05:30
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_falsey
end
end
context 'with split new line' do
let(:pre_text) { "hello\r" }
let(:text) { "\nworld" }
let(:lines) do
[
{ offset: 0, content: [{ text: 'hello' }] },
{ offset: 7, content: [{ text: 'world' }] }
]
end
2019-12-26 22:10:19 +05:30
it 'returns a blank line and the next line' do
2019-12-21 20:55:43 +05:30
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_falsey
end
end
context 'with split section' do
let(:section_name) { 'prepare-script' }
let(:section_duration) { 63.seconds }
let(:section_start_time) { Time.new(2019, 9, 17).utc }
let(:section_end_time) { section_start_time + section_duration }
2022-08-27 11:52:29 +05:30
let(:section_start) { "section_start:#{section_start_time.to_i}:#{section_name}\r\033[0K" }
let(:section_end) { "section_end:#{section_end_time.to_i}:#{section_name}\r\033[0K" }
2019-12-21 20:55:43 +05:30
context 'with split section body' do
let(:pre_text) { "#{section_start}this is a header\nand " }
let(:text) { "this\n is a body" }
let(:lines) do
[
{
offset: 61,
content: [{ text: 'and this' }],
section: 'prepare-script'
},
{
offset: 70,
content: [{ text: ' is a body' }],
section: 'prepare-script'
}
]
end
it 'returns the full line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_truthy
end
end
context 'with split section where header is also split' do
let(:pre_text) { "#{section_start}this is " }
let(:text) { "a header\nand body" }
let(:lines) do
[
{
offset: 0,
content: [{ text: 'this is a header' }],
section: 'prepare-script',
section_header: true
},
{
offset: 61,
content: [{ text: 'and body' }],
section: 'prepare-script'
}
]
end
it 'returns the full line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_falsey
end
end
context 'with split section end' do
let(:pre_text) { "#{section_start}this is a header\nthe" }
let(:text) { " body\nthe end#{section_end}" }
let(:lines) do
[
{
offset: 61,
content: [{ text: 'the body' }],
section: 'prepare-script'
},
{
offset: 70,
content: [{ text: 'the end' }],
section: 'prepare-script'
},
{
offset: 77,
content: [],
section: 'prepare-script',
section_duration: '01:03'
}
]
end
it 'returns the full line' do
expect(pass2.lines).to eq(lines)
expect(pass2.append).to be_truthy
end
end
end
end
describe 'trucates' do
2021-01-03 14:25:43 +05:30
let(:text) { 'Hello World' }
2019-12-21 20:55:43 +05:30
let(:stream) { StringIO.new(text) }
let(:subject) { described_class.convert(stream) }
before do
stream.seek(3, IO::SEEK_SET)
end
2021-01-03 14:25:43 +05:30
it 'returns truncated output' do
2019-12-21 20:55:43 +05:30
expect(subject.truncated).to be_truthy
end
2021-01-03 14:25:43 +05:30
it 'does not append output' do
2019-12-21 20:55:43 +05:30
expect(subject.append).to be_falsey
end
end
def convert_json(data)
stream = StringIO.new(data)
subject.convert(stream).lines
end
end
end