summaryrefslogtreecommitdiff
path: root/lib/chef/cookbook_version.rb
diff options
context:
space:
mode:
authorDavid Duponchel <dduponchel@mediarithmics.com>2016-03-04 15:48:46 +0100
committerDavid Duponchel <dduponchel@mediarithmics.com>2016-03-07 20:44:35 +0100
commit4bf4b62d6631abb6453f055eb034eaea69cbf5bc (patch)
treea049e652524cfa010f1e9ceaec6f8b392efbe594 /lib/chef/cookbook_version.rb
parent60c8124366ab34b5992fc03c508b345218c90dfe (diff)
downloadchef-4bf4b62d6631abb6453f055eb034eaea69cbf5bc.tar.gz
preferred_manifest_record: fix pretty print.
The `preferred_manifest_record` function pretty prints the list of existing files when the asked file can't be found. To do that, the root path of the cookbook is removed from each existing file but the update is done on the `existing_files` variable, not on a copy. When called several times, this variable will eventually be equals to `[nil]`, leading to a NoMethodError. Without the fix, the new unit test fails with > expected Chef::Exceptions::FileNotFound, got #<NoMethodError: undefined method `[]' for nil:NilClass> with backtrace: > # ./lib/chef/cookbook_version.rb:321:in `block in preferred_manifest_record' > # ./lib/chef/cookbook_version.rb:319:in `map!' > # ./lib/chef/cookbook_version.rb:319:in `preferred_manifest_record' > # ./spec/unit/cookbook_version_file_specificity_spec.rb:318:in `block (3 levels) in <top (required)>' > # ./spec/unit/cookbook_version_file_specificity_spec.rb:317:in `block (2 levels) in <top (required)>' Fixes #2561.
Diffstat (limited to 'lib/chef/cookbook_version.rb')
-rw-r--r--lib/chef/cookbook_version.rb12
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/chef/cookbook_version.rb b/lib/chef/cookbook_version.rb
index 906b94baad..1e903608b5 100644
--- a/lib/chef/cookbook_version.rb
+++ b/lib/chef/cookbook_version.rb
@@ -316,11 +316,17 @@ class Chef
error_message << error_locations.join("\n")
existing_files = segment_filenames(segment)
# Strip the root_dir prefix off all files for readability
- existing_files.map! { |path| path[root_dir.length + 1..-1] } if root_dir
+ pretty_existing_files = existing_files.map { |path|
+ if root_dir
+ path[root_dir.length + 1..-1]
+ else
+ path
+ end
+ }
# Show the files that the cookbook does have. If the user made a typo,
# hopefully they'll see it here.
- unless existing_files.empty?
- error_message << "\n\nThis cookbook _does_ contain: ['#{existing_files.join("','")}']"
+ unless pretty_existing_files.empty?
+ error_message << "\n\nThis cookbook _does_ contain: ['#{pretty_existing_files.join("','")}']"
end
raise Chef::Exceptions::FileNotFound, error_message
else