From 286e69845d7ab04effae9320ddd9e6d904e36cf3 Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Mon, 12 Sep 2022 13:32:03 -0700 Subject: [PATCH] prevent #verbose_list from attempt to sort Array of Hashes --- exe/foreman_envsync | 9 ++++++- .../unit/foreman_envsync_verbose_list_spec.rb | 24 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 spec/unit/foreman_envsync_verbose_list_spec.rb 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