From 828d81ee1f6aaaf6ab9de1ed0c675ff961831c17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Thu, 6 Apr 2017 16:20:27 +0000 Subject: Optimise trace handling code to use streaming instead of full read --- spec/lib/ci/ansi2html_spec.rb | 110 +++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 43 deletions(-) (limited to 'spec/lib/ci') diff --git a/spec/lib/ci/ansi2html_spec.rb b/spec/lib/ci/ansi2html_spec.rb index 0762fd7e56a..a5dfb49478a 100644 --- a/spec/lib/ci/ansi2html_spec.rb +++ b/spec/lib/ci/ansi2html_spec.rb @@ -1,159 +1,160 @@ require 'spec_helper' describe Ci::Ansi2html, lib: true do - subject { Ci::Ansi2html } + subject { described_class } it "prints non-ansi as-is" do - expect(subject.convert("Hello")[:html]).to eq('Hello') + expect(convert_html("Hello")).to eq('Hello') end it "strips non-color-changing controll sequences" do - expect(subject.convert("Hello \e[2Kworld")[:html]).to eq('Hello world') + expect(convert_html("Hello \e[2Kworld")).to eq('Hello world') end it "prints simply red" do - expect(subject.convert("\e[31mHello\e[0m")[:html]).to eq('Hello') + expect(convert_html("\e[31mHello\e[0m")).to eq('Hello') end it "prints simply red without trailing reset" do - expect(subject.convert("\e[31mHello")[:html]).to eq('Hello') + expect(convert_html("\e[31mHello")).to eq('Hello') end it "prints simply yellow" do - expect(subject.convert("\e[33mHello\e[0m")[:html]).to eq('Hello') + expect(convert_html("\e[33mHello\e[0m")).to eq('Hello') end it "prints default on blue" do - expect(subject.convert("\e[39;44mHello")[:html]).to eq('Hello') + expect(convert_html("\e[39;44mHello")).to eq('Hello') end it "prints red on blue" do - expect(subject.convert("\e[31;44mHello")[:html]).to eq('Hello') + expect(convert_html("\e[31;44mHello")).to eq('Hello') end it "resets colors after red on blue" do - expect(subject.convert("\e[31;44mHello\e[0m world")[:html]).to eq('Hello world') + expect(convert_html("\e[31;44mHello\e[0m world")).to eq('Hello world') end it "performs color change from red/blue to yellow/blue" do - expect(subject.convert("\e[31;44mHello \e[33mworld")[:html]).to eq('Hello world') + expect(convert_html("\e[31;44mHello \e[33mworld")).to eq('Hello world') end it "performs color change from red/blue to yellow/green" do - expect(subject.convert("\e[31;44mHello \e[33;42mworld")[:html]).to eq('Hello world') + expect(convert_html("\e[31;44mHello \e[33;42mworld")).to eq('Hello world') end it "performs color change from red/blue to reset to yellow/green" do - expect(subject.convert("\e[31;44mHello\e[0m \e[33;42mworld")[:html]).to eq('Hello world') + expect(convert_html("\e[31;44mHello\e[0m \e[33;42mworld")).to eq('Hello world') end it "ignores unsupported codes" do - expect(subject.convert("\e[51mHello\e[0m")[:html]).to eq('Hello') + expect(convert_html("\e[51mHello\e[0m")).to eq('Hello') end it "prints light red" do - expect(subject.convert("\e[91mHello\e[0m")[:html]).to eq('Hello') + expect(convert_html("\e[91mHello\e[0m")).to eq('Hello') end it "prints default on light red" do - expect(subject.convert("\e[101mHello\e[0m")[:html]).to eq('Hello') + expect(convert_html("\e[101mHello\e[0m")).to eq('Hello') end it "performs color change from red/blue to default/blue" do - expect(subject.convert("\e[31;44mHello \e[39mworld")[:html]).to eq('Hello world') + expect(convert_html("\e[31;44mHello \e[39mworld")).to eq('Hello world') end it "performs color change from light red/blue to default/blue" do - expect(subject.convert("\e[91;44mHello \e[39mworld")[:html]).to eq('Hello world') + expect(convert_html("\e[91;44mHello \e[39mworld")).to eq('Hello world') end it "prints bold text" do - expect(subject.convert("\e[1mHello")[:html]).to eq('Hello') + expect(convert_html("\e[1mHello")).to eq('Hello') end it "resets bold text" do - expect(subject.convert("\e[1mHello\e[21m world")[:html]).to eq('Hello world') - expect(subject.convert("\e[1mHello\e[22m world")[:html]).to eq('Hello world') + expect(convert_html("\e[1mHello\e[21m world")).to eq('Hello world') + expect(convert_html("\e[1mHello\e[22m world")).to eq('Hello world') end it "prints italic text" do - expect(subject.convert("\e[3mHello")[:html]).to eq('Hello') + expect(convert_html("\e[3mHello")).to eq('Hello') end it "resets italic text" do - expect(subject.convert("\e[3mHello\e[23m world")[:html]).to eq('Hello world') + expect(convert_html("\e[3mHello\e[23m world")).to eq('Hello world') end it "prints underlined text" do - expect(subject.convert("\e[4mHello")[:html]).to eq('Hello') + expect(convert_html("\e[4mHello")).to eq('Hello') end it "resets underlined text" do - expect(subject.convert("\e[4mHello\e[24m world")[:html]).to eq('Hello world') + expect(convert_html("\e[4mHello\e[24m world")).to eq('Hello world') end it "prints concealed text" do - expect(subject.convert("\e[8mHello")[:html]).to eq('Hello') + expect(convert_html("\e[8mHello")).to eq('Hello') end it "resets concealed text" do - expect(subject.convert("\e[8mHello\e[28m world")[:html]).to eq('Hello world') + expect(convert_html("\e[8mHello\e[28m world")).to eq('Hello world') end it "prints crossed-out text" do - expect(subject.convert("\e[9mHello")[:html]).to eq('Hello') + expect(convert_html("\e[9mHello")).to eq('Hello') end it "resets crossed-out text" do - expect(subject.convert("\e[9mHello\e[29m world")[:html]).to eq('Hello world') + expect(convert_html("\e[9mHello\e[29m world")).to eq('Hello world') end it "can print 256 xterm fg colors" do - expect(subject.convert("\e[38;5;16mHello")[:html]).to eq('Hello') + expect(convert_html("\e[38;5;16mHello")).to eq('Hello') end it "can print 256 xterm fg colors on normal magenta background" do - expect(subject.convert("\e[38;5;16;45mHello")[:html]).to eq('Hello') + expect(convert_html("\e[38;5;16;45mHello")).to eq('Hello') end it "can print 256 xterm bg colors" do - expect(subject.convert("\e[48;5;240mHello")[:html]).to eq('Hello') + expect(convert_html("\e[48;5;240mHello")).to eq('Hello') end it "can print 256 xterm bg colors on normal magenta foreground" do - expect(subject.convert("\e[48;5;16;35mHello")[:html]).to eq('Hello') + expect(convert_html("\e[48;5;16;35mHello")).to eq('Hello') end it "prints bold colored text vividly" do - expect(subject.convert("\e[1;31mHello\e[0m")[:html]).to eq('Hello') + expect(convert_html("\e[1;31mHello\e[0m")).to eq('Hello') end it "prints bold light colored text correctly" do - expect(subject.convert("\e[1;91mHello\e[0m")[:html]).to eq('Hello') + expect(convert_html("\e[1;91mHello\e[0m")).to eq('Hello') end it "prints <" do - expect(subject.convert("<")[:html]).to eq('<') + expect(convert_html("<")).to eq('<') end it "replaces newlines with line break tags" do - expect(subject.convert("\n")[:html]).to eq('
') + expect(convert_html("\n")).to eq('
') end it "groups carriage returns with newlines" do - expect(subject.convert("\r\n")[:html]).to eq('
') + expect(convert_html("\r\n")).to eq('
') end describe "incremental update" do shared_examples 'stateable converter' do - let(:pass1) { subject.convert(pre_text) } - let(:pass2) { subject.convert(pre_text + text, pass1[:state]) } + 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) } it "to returns html to append" do - expect(pass2[:append]).to be_truthy - expect(pass2[:html]).to eq(html) - expect(pass1[:text] + pass2[:text]).to eq(pre_text + text) - expect(pass1[:html] + pass2[:html]).to eq(pre_html + html) + expect(pass2.append).to be_truthy + expect(pass2.html).to eq(html) + expect(pass1.html + pass2.html).to eq(pre_html + html) end end @@ -193,4 +194,27 @@ describe Ci::Ansi2html, lib: true do it_behaves_like 'stateable converter' end end + + 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 end -- cgit v1.2.1