diff --git a/exe/foreman_envsync b/exe/foreman_envsync index 0d22661..35cd23a 100755 --- a/exe/foreman_envsync +++ b/exe/foreman_envsync @@ -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) diff --git a/spec/unit/foreman_envsync_verbose_list_spec.rb b/spec/unit/foreman_envsync_verbose_list_spec.rb new file mode 100644 index 0000000..368971f --- /dev/null +++ b/spec/unit/foreman_envsync_verbose_list_spec.rb @@ -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