summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Danna <steve@chef.io>2015-10-22 09:31:51 +0100
committerSteven Danna <steve@chef.io>2015-10-29 14:03:22 +0000
commit5a1eb3e0cba55068aeaec03e0d2271e19a7ad4e7 (patch)
tree79e15822e47ddabe832fba95577a65eacbd15ff6
parent571891bf67667d0240edd632c36fc84cd0fc8bbe (diff)
downloadchef-5a1eb3e0cba55068aeaec03e0d2271e19a7ad4e7.tar.gz
Improve detection of ChefFS-based commands in `knife rehash`ssd/rehash-cheffs
ChefFS-based commands have a superclass (Chef::ChefFS::Knife) which defines its own inherited method that calls super. This breaks our detection of where the subcommand is defined since the file with the definition is no longer at the top of the call stack. This commit special-cases subclasses with a superclass of Chef::ChefFS::Knife to account for this. Fixes #4089
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/chef/knife.rb15
-rw-r--r--spec/unit/knife_spec.rb8
3 files changed, 23 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 17608b32f8..2d434b3c28 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -48,6 +48,7 @@
* [pr#3597](https://github.com/chef/chef/pull/3597) print STDOUT from the powershell_script
* [pr#4091](https://github.com/chef/chef/pull/4091) Allow downloading of root_files in a chef repository
* [pr#4112](https://github.com/chef/chef/pull/4112) Update knife bootstrap command to honor --no-color flag in chef-client run that is part of the bootstrap process.
+* [pr#4090](https://github.com/chef/chef/pull/4090) Improve detection of ChefFS-based commands in `knife rehash`
## 12.5.1
diff --git a/lib/chef/knife.rb b/lib/chef/knife.rb
index 46e968827e..fed2ad4cfa 100644
--- a/lib/chef/knife.rb
+++ b/lib/chef/knife.rb
@@ -87,7 +87,16 @@ class Chef
def self.inherited(subclass)
unless subclass.unnamed?
subcommands[subclass.snake_case_name] = subclass
- subcommand_files[subclass.snake_case_name] += [caller[0].split(/:\d+/).first]
+ subcommand_files[subclass.snake_case_name] +=
+ if subclass.superclass.to_s == "Chef::ChefFS::Knife"
+ # ChefFS-based commands have a superclass that defines an
+ # inhereited method which calls super. This means that the
+ # top of the call stack is not the class definition for
+ # our subcommand. Try the second entry in the call stack.
+ [path_from_caller(caller[1])]
+ else
+ [path_from_caller(caller[0])]
+ end
end
end
@@ -221,6 +230,10 @@ class Chef
OFFICIAL_PLUGINS = %w[ec2 rackspace windows openstack terremark bluebox]
+ def self.path_from_caller(caller_line)
+ caller_line.split(/:\d+/).first
+ end
+
# :nodoc:
# Error out and print usage. probably because the arguments given by the
# user could not be resolved to a subcommand.
diff --git a/spec/unit/knife_spec.rb b/spec/unit/knife_spec.rb
index 022256f370..9e76ec59c4 100644
--- a/spec/unit/knife_spec.rb
+++ b/spec/unit/knife_spec.rb
@@ -117,6 +117,14 @@ describe Chef::Knife do
expect(Chef::Knife.subcommands["super_awesome_command"]).to eq(SuperAwesomeCommand)
end
+ it "records the location of ChefFS-based commands correctly" do
+ class AwesomeCheffsCommand < Chef::ChefFS::Knife
+ end
+
+ Chef::Knife.load_commands
+ expect(Chef::Knife.subcommand_files["awesome_cheffs_command"]).to eq([__FILE__])
+ end
+
it "guesses a category from a given ARGV" do
Chef::Knife.subcommands_by_category["cookbook"] << :cookbook
Chef::Knife.subcommands_by_category["cookbook site"] << :cookbook_site