diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-09-21 09:42:07 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-21 09:42:07 -0700 |
commit | a83ebd9424ee1053d6c453317e874ed0dfbfbfe0 (patch) | |
tree | 55589e4ef6c115cc4d2d6dd13ad037eb3e2c9e56 | |
parent | ce694677fdc808143a28457dc44d56e97930b9ec (diff) | |
parent | 50e78d61d00eb0a984d760d5c2035aa8cf3ff291 (diff) | |
download | chef-a83ebd9424ee1053d6c453317e874ed0dfbfbfe0.tar.gz |
Merge pull request #5351 from chef/lcg/node-presenter-tweaks
fix method_access and array handling in node presenter
-rw-r--r-- | lib/chef/knife/core/generic_presenter.rb | 28 | ||||
-rw-r--r-- | spec/unit/knife/core/ui_spec.rb | 23 |
2 files changed, 34 insertions, 17 deletions
diff --git a/lib/chef/knife/core/generic_presenter.rb b/lib/chef/knife/core/generic_presenter.rb index f273cb5bca..3f5c0712d0 100644 --- a/lib/chef/knife/core/generic_presenter.rb +++ b/lib/chef/knife/core/generic_presenter.rb @@ -175,23 +175,19 @@ class Chef def extract_nested_value(data, nested_value_spec) nested_value_spec.split(".").each do |attr| - if data.nil? - nil # don't get no method error on nil - # Must check :[] before attr because spec can include - # `keys` - want the key named `keys`, not a list of - # available keys. - elsif data.respond_to?(:[]) && data.has_key?(attr) - data = data[attr] - elsif data.respond_to?(attr.to_sym) - data = data.send(attr.to_sym) - else - data = begin - data.send(attr.to_sym) - rescue NoMethodError - nil - end - end + data = + if data.is_a?(Array) + data[attr.to_i] + elsif data.respond_to?(:[], false) && data.key?(attr) + data[attr] + elsif data.respond_to?(attr.to_sym, false) + # handles -a chef_environment and other things that hang of the node and aren't really attributes + data.public_send(attr.to_sym) + else + nil + end end + # necessary (?) for coercing objects (the run_list object?) to hashes ( !data.kind_of?(Array) && data.respond_to?(:to_hash) ) ? data.to_hash : data end diff --git a/spec/unit/knife/core/ui_spec.rb b/spec/unit/knife/core/ui_spec.rb index 9f525f22f0..be77fd8501 100644 --- a/spec/unit/knife/core/ui_spec.rb +++ b/spec/unit/knife/core/ui_spec.rb @@ -377,12 +377,33 @@ EOM end it "should return the name attribute" do - allow_any_instance_of(Chef::Node).to receive(:name).and_return("chef.localdomain") input = Chef::Node.new + input.name("chef.localdomain") @ui.config[:attribute] = "name" expect(@ui.format_for_display(input)).to eq( { "chef.localdomain" => { "name" => "chef.localdomain" } }) end + it "should return a 'class' attribute and not the node.class" do + input = Chef::Node.new + input.default["class"] = "classy!" + @ui.config[:attribute] = "class" + expect(@ui.format_for_display(input)).to eq( { nil => { "class" => "classy!" } } ) + end + + it "should return the chef_environment attribute" do + input = Chef::Node.new + input.chef_environment = "production-partner-load-integration-preview-testing" + @ui.config[:attribute] = "chef_environment" + expect(@ui.format_for_display(input)).to eq( { nil => { "chef_environment" => "production-partner-load-integration-preview-testing" } } ) + end + + it "works with arrays" do + input = Chef::Node.new + input.default["array"] = %w{zero one two} + @ui.config[:attribute] = "array.1" + expect(@ui.format_for_display(input)).to eq( { nil => { "array.1" => "one" } } ) + end + it "returns nil when given an attribute path that isn't a name or attribute" do input = { "keys" => { "keys" => "values" }, "hi" => "ho", "id" => "sample-data-bag-item" } non_existing_path = "nope.nada.nothingtoseehere" |