2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
describe Gitlab::Ci::Ansi2html do
|
2017-08-17 22:00:37 +05:30
|
|
|
subject { described_class }
|
2015-09-25 12:07:36 +05:30
|
|
|
|
|
|
|
it "prints non-ansi as-is" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("Hello")).to eq('<span>Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
it "strips non-color-changing control sequences" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("Hello \e[2Kworld")).to eq('<span>Hello world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints simply red" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[31mHello\e[0m")).to eq('<span class="term-fg-red">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints simply red without trailing reset" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[31mHello")).to eq('<span class="term-fg-red">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints simply yellow" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[33mHello\e[0m")).to eq('<span class="term-fg-yellow">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints default on blue" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[39;44mHello")).to eq('<span class="term-bg-blue">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints red on blue" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[31;44mHello")).to eq('<span class="term-fg-red term-bg-blue">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "resets colors after red on blue" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\e[31;44mHello\e[0m world")).to eq('<span class="term-fg-red term-bg-blue">Hello</span><span> world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "performs color change from red/blue to yellow/blue" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[31;44mHello \e[33mworld")).to eq('<span class="term-fg-red term-bg-blue">Hello </span><span class="term-fg-yellow term-bg-blue">world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "performs color change from red/blue to yellow/green" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[31;44mHello \e[33;42mworld")).to eq('<span class="term-fg-red term-bg-blue">Hello </span><span class="term-fg-yellow term-bg-green">world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "performs color change from red/blue to reset to yellow/green" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\e[31;44mHello\e[0m \e[33;42mworld")).to eq('<span class="term-fg-red term-bg-blue">Hello</span><span> </span><span class="term-fg-yellow term-bg-green">world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores unsupported codes" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\e[51mHello\e[0m")).to eq('<span>Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints light red" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[91mHello\e[0m")).to eq('<span class="term-fg-l-red">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints default on light red" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[101mHello\e[0m")).to eq('<span class="term-bg-l-red">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "performs color change from red/blue to default/blue" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[31;44mHello \e[39mworld")).to eq('<span class="term-fg-red term-bg-blue">Hello </span><span class="term-bg-blue">world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "performs color change from light red/blue to default/blue" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[91;44mHello \e[39mworld")).to eq('<span class="term-fg-l-red term-bg-blue">Hello </span><span class="term-bg-blue">world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints bold text" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[1mHello")).to eq('<span class="term-bold">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "resets bold text" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\e[1mHello\e[21m world")).to eq('<span class="term-bold">Hello</span><span> world</span>')
|
|
|
|
expect(convert_html("\e[1mHello\e[22m world")).to eq('<span class="term-bold">Hello</span><span> world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints italic text" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[3mHello")).to eq('<span class="term-italic">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "resets italic text" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\e[3mHello\e[23m world")).to eq('<span class="term-italic">Hello</span><span> world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints underlined text" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[4mHello")).to eq('<span class="term-underline">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "resets underlined text" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\e[4mHello\e[24m world")).to eq('<span class="term-underline">Hello</span><span> world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints concealed text" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[8mHello")).to eq('<span class="term-conceal">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "resets concealed text" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\e[8mHello\e[28m world")).to eq('<span class="term-conceal">Hello</span><span> world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints crossed-out text" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[9mHello")).to eq('<span class="term-cross">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "resets crossed-out text" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\e[9mHello\e[29m world")).to eq('<span class="term-cross">Hello</span><span> world</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "can print 256 xterm fg colors" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[38;5;16mHello")).to eq('<span class="xterm-fg-16">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "can print 256 xterm fg colors on normal magenta background" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[38;5;16;45mHello")).to eq('<span class="xterm-fg-16 term-bg-magenta">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "can print 256 xterm bg colors" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[48;5;240mHello")).to eq('<span class="xterm-bg-240">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
it "can print 256 xterm fg bold colors" do
|
|
|
|
expect(convert_html("\e[38;5;16;1mHello")).to eq('<span class="xterm-fg-16 term-bold">Hello</span>')
|
|
|
|
end
|
|
|
|
|
2015-09-25 12:07:36 +05:30
|
|
|
it "can print 256 xterm bg colors on normal magenta foreground" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[48;5;16;35mHello")).to eq('<span class="term-fg-magenta xterm-bg-16">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints bold colored text vividly" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[1;31mHello\e[0m")).to eq('<span class="term-fg-l-red term-bold">Hello</span>')
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints bold light colored text correctly" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(convert_html("\e[1;91mHello\e[0m")).to eq('<span class="term-fg-l-red term-bold">Hello</span>')
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "prints <" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("<")).to eq('<span><</span>')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "replaces newlines with line break tags" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\n")).to eq('<span><br/></span>')
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it "groups carriage returns with newlines" do
|
2019-09-30 21:07:59 +05:30
|
|
|
expect(convert_html("\r\n")).to eq('<span><br/></span>')
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
describe "incremental update" do
|
|
|
|
shared_examples 'stateable converter' do
|
2017-08-17 22:00:37 +05:30
|
|
|
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) }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
it "to returns html to append" do
|
2017-08-17 22:00:37 +05:30
|
|
|
expect(pass2.append).to be_truthy
|
|
|
|
expect(pass2.html).to eq(html)
|
|
|
|
expect(pass1.html + pass2.html).to eq(pre_html + html)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with split word" do
|
|
|
|
let(:pre_text) { "\e[1mHello" }
|
|
|
|
let(:pre_html) { "<span class=\"term-bold\">Hello</span>" }
|
|
|
|
let(:text) { "\e[1mWorld" }
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:html) { "<span class=\"term-bold\">World</span>" }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
it_behaves_like 'stateable converter'
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with split sequence" do
|
|
|
|
let(:pre_text) { "\e[1m" }
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:pre_html) { "" }
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:text) { "Hello" }
|
|
|
|
let(:html) { "<span class=\"term-bold\">Hello</span>" }
|
|
|
|
|
|
|
|
it_behaves_like 'stateable converter'
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with partial sequence" do
|
|
|
|
let(:pre_text) { "Hello\e" }
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:pre_html) { "<span>Hello</span>" }
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:text) { "[1m World" }
|
|
|
|
let(:html) { "<span class=\"term-bold\"> World</span>" }
|
|
|
|
|
|
|
|
it_behaves_like 'stateable converter'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with new line' do
|
|
|
|
let(:pre_text) { "Hello\r" }
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:pre_html) { "<span>Hello\r</span>" }
|
2016-06-02 11:05:42 +05:30
|
|
|
let(:text) { "\nWorld" }
|
2019-09-30 21:07:59 +05:30
|
|
|
let(:html) { "<span><br/>World</span>" }
|
2016-06-02 11:05:42 +05:30
|
|
|
|
|
|
|
it_behaves_like 'stateable converter'
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
context "with section markers" do
|
|
|
|
let(:section_name) { 'test_section' }
|
|
|
|
let(:section_start_time) { Time.new(2017, 9, 20).utc }
|
|
|
|
let(:section_duration) { 3.seconds }
|
|
|
|
let(:section_end_time) { section_start_time + section_duration }
|
|
|
|
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"}
|
|
|
|
let(:section_start_html) do
|
2019-10-12 21:52:04 +05:30
|
|
|
'<div class="js-section-start section-start fa fa-caret-down pr-2 cursor-pointer"' \
|
2019-09-04 21:01:54 +05:30
|
|
|
" data-timestamp=\"#{section_start_time.to_i}\" data-section=\"#{class_name(section_name)}\"" \
|
|
|
|
' role="button"></div>'
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
let(:section_end_html) do
|
2019-09-04 21:01:54 +05:30
|
|
|
"<div class=\"section-end\" data-section=\"#{class_name(section_name)}\"></div>"
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'forbidden char in section_name' do
|
|
|
|
it 'ignores sections' do
|
|
|
|
text = "#{section_start}Some text#{section_end}"
|
2019-09-04 21:01:54 +05:30
|
|
|
class_name_start = section_start.gsub("\033[0K", '').gsub('<', '<')
|
|
|
|
class_name_end = section_end.gsub("\033[0K", '').gsub('<', '<')
|
2019-09-30 21:07:59 +05:30
|
|
|
html = %{<span>#{class_name_start}Some text#{class_name_end}</span>}
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
expect(convert_html(text)).to eq(html)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'a legit section' do
|
|
|
|
let(:text) { "#{section_start}Some text#{section_end}" }
|
|
|
|
|
|
|
|
it 'prints light red' do
|
2019-09-30 21:07:59 +05:30
|
|
|
text = "#{section_start}\e[91mHello\e[0m\nLine 1\nLine 2\nLine 3\n#{section_end}"
|
2019-10-12 21:52:04 +05:30
|
|
|
header = %{<span class="term-fg-l-red section js-section-header section-header cursor-pointer js-s-#{class_name(section_name)}">Hello</span>}
|
|
|
|
line_break = %{<span class="section js-section-header section-header cursor-pointer js-s-#{class_name(section_name)}"><br/></span>}
|
2019-09-30 21:07:59 +05:30
|
|
|
output_line = %{<span class="section line js-s-#{class_name(section_name)}">Line 1<br/>Line 2<br/>Line 3<br/></span>}
|
|
|
|
html = "#{section_start_html}#{header}#{line_break}#{output_line}#{section_end_html}"
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
expect(convert_html(text)).to eq(html)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'begins with a section_start html marker' do
|
|
|
|
expect(convert_html(text)).to start_with(section_start_html)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ends with a section_end html marker' do
|
|
|
|
expect(convert_html(text)).to end_with(section_end_html)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'a legit section'
|
|
|
|
|
|
|
|
context 'section name includes $' do
|
|
|
|
let(:section_name) { 'my_$ection'}
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden char in section_name'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'section name includes <' do
|
|
|
|
let(:section_name) { '<a_tag>'}
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden char in section_name'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'section name contains .-_' do
|
|
|
|
let(:section_name) { 'a.Legit-SeCtIoN_namE' }
|
|
|
|
|
|
|
|
it_behaves_like 'a legit section'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'do not allow XSS injections' do
|
|
|
|
text = "#{section_start}section_end:1:2<script>alert('XSS Hack!');</script>#{section_end}"
|
|
|
|
|
|
|
|
expect(convert_html(text)).not_to include('<script>')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
describe "truncates" do
|
|
|
|
let(:text) { "Hello World" }
|
|
|
|
let(:stream) { StringIO.new(text) }
|
|
|
|
let(:subject) { described_class.convert(stream) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stream.seek(3, IO::SEEK_SET)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns truncated output" do
|
|
|
|
expect(subject.truncated).to be_truthy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not append output" do
|
|
|
|
expect(subject.append).to be_falsey
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_html(data)
|
|
|
|
stream = StringIO.new(data)
|
|
|
|
subject.convert(stream).html
|
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
def class_name(section)
|
|
|
|
subject::Converter.new.section_to_class_name(section)
|
|
|
|
end
|
2015-09-25 12:07:36 +05:30
|
|
|
end
|