From b3e15a55c9b1db54151dd4f4f1defb42ddce716e Mon Sep 17 00:00:00 2001 From: Paul Geraghty Date: Tue, 24 Sep 2019 18:40:56 +0200 Subject: [PATCH] Fix remaining Output module issues and disable debug output --- lib/ansible/ad_hoc.rb | 4 +-- lib/ansible/output.rb | 13 +++++----- spec/ansible/output_spec.rb | 50 +++++++++++++++++++++++++++++-------- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/lib/ansible/ad_hoc.rb b/lib/ansible/ad_hoc.rb index a00931f..bd1caa6 100644 --- a/lib/ansible/ad_hoc.rb +++ b/lib/ansible/ad_hoc.rb @@ -6,9 +6,7 @@ module Methods BIN = 'ansible' def one_off cmd - output = `#{config.to_s "#{BIN} #{cmd}"}` - puts output - output + `#{config.to_s "#{BIN} #{cmd}"}` end alias :[] :one_off diff --git a/lib/ansible/output.rb b/lib/ansible/output.rb index c571867..0b92daf 100644 --- a/lib/ansible/output.rb +++ b/lib/ansible/output.rb @@ -27,19 +27,20 @@ def self.to_html(line, stream='') styles << COLOR[colour] span = - if styles.empty? + # in case of invalid colours, although this may be impossible + if styles.compact.empty? %{} else %{} end stream << span + elsif s.scan(/\e\[0m/) + stream << %{} + elsif s.scan(/\e\[[^0]*m/) + stream << '' else - if s.scan(/\e\[0m/) - stream << %{} - else - stream << s.scan(/./m) - end + stream << s.scan(/./m) end end diff --git a/spec/ansible/output_spec.rb b/spec/ansible/output_spec.rb index afbae89..66a36c7 100644 --- a/spec/ansible/output_spec.rb +++ b/spec/ansible/output_spec.rb @@ -44,24 +44,52 @@ module Output expect(Output.to_html output).to eq "Bold Magenta" end - it 'ignores unsupported escape sequences' do - output = "\e[0;99Ignore me\e[0m" - expect(Output.to_html output).to eq "Ignore me" + it 'scrubs unsupported escape sequences' do + output = "\e[38;5;118mBright Green - unsupported\e[0m" + expect(Output.to_html output).to eq "Bright Green - unsupported" + end + + it 'handles some malformed cases (missing semicolon)' do + output = "\e[132mBright Green" + expect(Output.to_html output).to eq 'Bright Green' end # This code may be entirely unreachable as regexp appears to be very specific - pending 'handles situations where no style attribute should be added to the tag' do - # output = "\e[0;99Nothing\e[0m" - # expect(Output.to_html output).to eq "Nothing" - fail + it 'handles situations where no style attribute should be added to the tag' do + output = "\e[0;99Nothing\e[0m" + + s = instance_double("StringScanner", captures: []) + allow(s).to receive(:eos?).and_return(false, true) + allow(s).to receive(:scan).and_return(true, '') + + class_double("StringScanner", new: s).as_stubbed_const + + expect(Output.to_html output).to match // end - pending 'test against stream from playbook' do - fail + it 'correctly formats output of a streamed playbook' do + output = '' + Ansible.stream(['-i', 'localhost,', 'spec/fixtures/mock_playbook.yml']*' ') do |line| + output << Ansible::Output.to_html(line) + end + + expect(output).to match /ok=1/ end - pending 'returns to normal text after bold text' do - fail + context 'for a non-existent playbook' do + output = '' + + it 'raises an error' do + expect { + Ansible.stream(['-i', 'localhost,', 'does_not_exist.yml']*' ') do |line| + output << Ansible::Output.to_html(line) + end + }.to raise_error(Ansible::Playbook::Exception, /ERROR! the playbook: does_not_exist.yml could not be found/) + end + + it 'outputs an error message' do + expect(output).to match /ERROR! the playbook: does_not_exist.yml could not be found<\/span>/ + end end end end