Skip to content

Commit

Permalink
Fix remaining Output module issues and disable debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
pgeraghty committed Sep 24, 2019
1 parent 7c1fbd6 commit b3e15a5
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
4 changes: 1 addition & 3 deletions lib/ansible/ad_hoc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 7 additions & 6 deletions lib/ansible/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
%{<span>}
else
%{<span style="#{styles*'; '};">}
end

stream << span
elsif s.scan(/\e\[0m/)
stream << %{</span>}
elsif s.scan(/\e\[[^0]*m/)
stream << '<span>'
else
if s.scan(/\e\[0m/)
stream << %{</span>}
else
stream << s.scan(/./m)
end
stream << s.scan(/./m)
end
end

Expand Down
50 changes: 39 additions & 11 deletions spec/ansible/output_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,52 @@ module Output
expect(Output.to_html output).to eq "<span style=\"font-weight: bold; color: magenta;\">Bold Magenta</span>"
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 "<span>Bright Green - unsupported</span>"
end

it 'handles some malformed cases (missing semicolon)' do
output = "\e[132mBright Green"
expect(Output.to_html output).to eq '<span style="font-weight: bold; color: green;">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 "<span>Nothing</span>"
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 /<span>/
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 /<span style="color: green;">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 /<span style="color: red;">ERROR! the playbook: does_not_exist.yml could not be found<\/span>/
end
end
end
end
Expand Down

0 comments on commit b3e15a5

Please sign in to comment.