diff options
-rw-r--r-- | lib/chef/cookbook/cookbook_version_loader.rb | 30 | ||||
-rw-r--r-- | spec/unit/cookbook/cookbook_version_loader_spec.rb | 4 |
2 files changed, 34 insertions, 0 deletions
diff --git a/lib/chef/cookbook/cookbook_version_loader.rb b/lib/chef/cookbook/cookbook_version_loader.rb index 04be8c725c..74ef9f9fcb 100644 --- a/lib/chef/cookbook/cookbook_version_loader.rb +++ b/lib/chef/cookbook/cookbook_version_loader.rb @@ -43,6 +43,7 @@ class Chef @relative_path = /#{Regexp.escape(@cookbook_path)}\/(.+)$/ @metadata_loaded = false @cookbook_settings = { + :all_unignored_files => {}, :attribute_filenames => {}, :definition_filenames => {}, :recipe_filenames => {}, @@ -78,6 +79,16 @@ class Chef # re-raise any exception that occurred when reading the metadata raise_metadata_error! + load_all_unignored_files + + # TODO: + # 1. Can we safely pick out the segment files with fnmatch against the + # file names in all_unignored_files (?) + # 2. If so, can we avoid running chefignore twice by running it against + # all_unignored_files before segmenting? + # + # See: https://shane.io/2014/07/13/sobbing-with-ruby-file-globbing.html + load_as(:attribute_filenames, "attributes", "*.rb") load_as(:definition_filenames, "definitions", "*.rb") load_as(:recipe_filenames, "recipes", "*.rb") @@ -121,6 +132,7 @@ class Chef return nil if empty? Chef::CookbookVersion.new(cookbook_name, *cookbook_paths).tap do |c| + c.all_unignored_files = cookbook_settings[:all_unignored_files].values c.attribute_filenames = cookbook_settings[:attribute_filenames].values c.definition_filenames = cookbook_settings[:definition_filenames].values c.recipe_filenames = cookbook_settings[:recipe_filenames].values @@ -212,6 +224,24 @@ class Chef @chefignore ||= Chefignore.new(File.basename(cookbook_path)) end + def load_all_unignored_files + # List all directories that are not dotdirs, then list everything in + # those, *including* dotfiles and dotdirs. + # Finally, list all the _files_ at the root, including dotfiles, and + # merge those in. + Dir.glob(File.join(Chef::Util::PathHelper.escape_glob(cookbook_path), "*")).each do |dir| + dir = Chef::Util::PathHelper.cleanpath(dir) + next unless File.directory?(dir) + + Dir.glob(File.join(Chef::Util::PathHelper.escape_glob(dir), "**/*"), File::FNM_DOTMATCH).each do |file| + name = Chef::Util::PathHelper.relative_path_from(@cookbook_path, file) + cookbook_settings[:all_unignored_files][name] = file + end + end + load_root_files + cookbook_settings[:all_unignored_files].merge!(cookbook_settings[:root_filenames]) + end + def load_root_files Dir.glob(File.join(Chef::Util::PathHelper.escape_glob(cookbook_path), "*"), File::FNM_DOTMATCH).each do |file| file = Chef::Util::PathHelper.cleanpath(file) diff --git a/spec/unit/cookbook/cookbook_version_loader_spec.rb b/spec/unit/cookbook/cookbook_version_loader_spec.rb index a32eb052af..161b3d0f3d 100644 --- a/spec/unit/cookbook/cookbook_version_loader_spec.rb +++ b/spec/unit/cookbook/cookbook_version_loader_spec.rb @@ -73,6 +73,10 @@ describe Chef::Cookbook::CookbookVersionLoader do expect(loaded_cookbook.file_filenames).to include(full_path("/files/default/remotedir/.a_dotdir/.a_dotfile_in_a_dotdir")) end + it "loads all unignored files, even if they don't match a segment type" do + expect(loaded_cookbook.all_unignored_files).to include(full_path("/spec/spec_helper.rb")) + end + it "should load the metadata for the cookbook" do expect(loaded_cookbook.metadata.name.to_s).to eq("openldap") expect(loaded_cookbook.metadata).to be_a_kind_of(Chef::Cookbook::Metadata) |