Skip to content

Commit

Permalink
prevent #verbose_list from attempt to sort Array of Hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoblitt committed Sep 12, 2022
1 parent 10bc152 commit 286e698
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
9 changes: 8 additions & 1 deletion exe/foreman_envsync
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ def verbose_list(msg, items)
return unless @options[:verbose] && !items.nil?

printf(msg, items.count)
return if items.empty?

puts
puts "#{YAML.dump(items.sort)}\n" unless items.empty?
# do not attempt to sorry Array of Hashes
if items.is_a?(Array) && items.first.is_a?(Hash)
puts "#{YAML.dump(items)}\n"
else
puts "#{YAML.dump(items.sort)}\n"
end
end

def cert_file(file)
Expand Down
24 changes: 24 additions & 0 deletions spec/unit/foreman_envsync_verbose_list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

require "spec_helper"

describe ForemanEnvsync do
describe "#verbose_list" do
before { @options = { verbose: true } }

context "with array of hash" do
let(:aofh) { [{ a: 1 }, { b: 2 }] }
let(:foo_output) do
<<~FOO
foo
---
- :a: 1
- :b: 2
FOO
end

it { expect { verbose_list("foo", aofh) }.to output(foo_output).to_stdout }
end
end
end

0 comments on commit 286e698

Please sign in to comment.