summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/cookbook/cookbook_version_loader.rb60
-rw-r--r--lib/chef/cookbook/metadata.rb35
-rw-r--r--lib/chef/cookbook_version.rb12
-rw-r--r--spec/data/cookbooks/name-mismatch-versionnumber/README.md4
-rw-r--r--spec/data/cookbooks/name-mismatch-versionnumber/metadata.rb8
-rw-r--r--spec/data/cookbooks/name-mismatch-versionnumber/recipes/default.rb8
-rw-r--r--spec/unit/cookbook/cookbook_version_loader_spec.rb28
-rw-r--r--spec/unit/cookbook_loader_spec.rb3
8 files changed, 134 insertions, 24 deletions
diff --git a/lib/chef/cookbook/cookbook_version_loader.rb b/lib/chef/cookbook/cookbook_version_loader.rb
index fac8c80993..bff17dc4a4 100644
--- a/lib/chef/cookbook/cookbook_version_loader.rb
+++ b/lib/chef/cookbook/cookbook_version_loader.rb
@@ -18,7 +18,6 @@ class Chef
UPLOADED_COOKBOOK_VERSION_FILE = ".uploaded-cookbook-version.json".freeze
- attr_reader :cookbook_name
attr_reader :cookbook_settings
attr_reader :cookbook_paths
attr_reader :metadata_filenames
@@ -29,10 +28,13 @@ class Chef
@cookbook_path = File.expand_path( path ) # cookbook_path from which this was loaded
# We keep a list of all cookbook paths that have been merged in
@cookbook_paths = [ @cookbook_path ]
+
+ # TODO: Add a "strict mode" setting, use this when not in strict mode
@cookbook_name = File.basename( path )
@chefignore = chefignore
- @metadata = Hash.new
+ @metadata = nil
@relative_path = /#{Regexp.escape(@cookbook_path)}\/(.+)$/
+ @metadata_loaded = false
@cookbook_settings = {
:attribute_filenames => {},
:definition_filenames => {},
@@ -48,7 +50,16 @@ class Chef
@metadata_filenames = []
end
- def load_cookbooks
+ def load!
+ file_paths_map = load
+
+ if empty?
+ raise Exceptions::CookbookNotFoundInRepo, "The directory #{@cookbook_path} does not contain a cookbook"
+ end
+ file_paths_map
+ end
+
+ def load
load_as(:attribute_filenames, 'attributes', '*.rb')
load_as(:definition_filenames, 'definitions', '*.rb')
load_as(:recipe_filenames, 'recipes', '*.rb')
@@ -61,6 +72,16 @@ class Chef
remove_ignored_files
+ if empty?
+ Chef::Log.warn "found a directory #{cookbook_name} in the cookbook path, but it contains no cookbook files. skipping."
+ end
+ @cookbook_settings
+ end
+
+ alias :load_cookbooks :load
+
+ def metadata_filenames
+ return @metadata_filenames unless @metadata_filenames.empty?
if File.exists?(File.join(@cookbook_path, UPLOADED_COOKBOOK_VERSION_FILE))
@uploaded_cookbook_version_file = File.join(@cookbook_path, UPLOADED_COOKBOOK_VERSION_FILE)
end
@@ -75,17 +96,13 @@ class Chef
# Set frozen based on .uploaded-cookbook-version.json
set_frozen
-
- if empty?
- Chef::Log.warn "found a directory #{cookbook_name} in the cookbook path, but it contains no cookbook files. skipping."
- end
- @cookbook_settings
+ @metadata_filenames
end
def cookbook_version
return nil if empty?
- Chef::CookbookVersion.new(@cookbook_name.to_sym, *@cookbook_paths).tap do |c|
+ Chef::CookbookVersion.new(cookbook_name, *@cookbook_paths).tap do |c|
c.attribute_filenames = cookbook_settings[:attribute_filenames].values
c.definition_filenames = cookbook_settings[:definition_filenames].values
c.recipe_filenames = cookbook_settings[:recipe_filenames].values
@@ -96,15 +113,23 @@ class Chef
c.provider_filenames = cookbook_settings[:provider_filenames].values
c.root_filenames = cookbook_settings[:root_filenames].values
c.metadata_filenames = @metadata_filenames
- c.metadata = metadata(c)
+ c.metadata = metadata
+
c.freeze_version if @frozen
end
end
+ def cookbook_name
+ (metadata.name || @cookbook_name).to_sym
+ end
+
# Generates the Cookbook::Metadata object
- def metadata(cookbook_version)
- @metadata = Chef::Cookbook::Metadata.new(cookbook_version)
- @metadata_filenames.each do |metadata_file|
+ def metadata
+ return @metadata unless @metadata.nil?
+
+ @metadata = Chef::Cookbook::Metadata.new
+
+ metadata_filenames.each do |metadata_file|
case metadata_file
when /\.rb$/
apply_ruby_metadata(metadata_file)
@@ -116,6 +141,13 @@ class Chef
raise RuntimeError, "Invalid metadata file: #{metadata_file} for cookbook: #{cookbook_version}"
end
end
+
+ # Compatibility if metadata is missing the name attribute:
+ # TODO: probably should live elsewhere.
+ if @metadata.name.nil?
+ @metadata.name(@cookbook_name)
+ end
+
@metadata
end
@@ -131,6 +163,8 @@ class Chef
@metadata_filenames.concat(other_cookbook_loader.metadata_filenames)
@cookbook_paths += other_cookbook_loader.cookbook_paths
@frozen = true if other_cookbook_loader.frozen
+ @metadata = nil # reset metadata so it gets reloaded and all metadata files applied.
+ self
end
def chefignore
diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb
index 8d3f8b84aa..c7e542e44b 100644
--- a/lib/chef/cookbook/metadata.rb
+++ b/lib/chef/cookbook/metadata.rb
@@ -92,7 +92,7 @@ class Chef
# metadata<Chef::Cookbook::Metadata>
def initialize(cookbook=nil, maintainer='YOUR_COMPANY_NAME', maintainer_email='YOUR_EMAIL', license='none')
@cookbook = cookbook
- @name = cookbook ? cookbook.name : ""
+ @name = cookbook ? cookbook.name : nil
@long_description = ""
self.maintainer(maintainer)
self.maintainer_email(maintainer_email)
@@ -110,12 +110,7 @@ class Chef
@recipes = Mash.new
@version = Version.new "0.0.0"
if cookbook
- @recipes = cookbook.fully_qualified_recipe_names.inject({}) do |r, e|
- e = self.name.to_s if e =~ /::default$/
- r[e] ||= ""
- self.provides e
- r
- end
+ recipes_from_cookbook_version(cookbook)
end
end
@@ -365,6 +360,32 @@ class Chef
@recipes[name] = description
end
+ # Sets the cookbook's recipes to the list of recipes in the given
+ # +cookbook+. Any recipe that already has a description (if set by the
+ # #recipe method) will not be updated.
+ #
+ # === Parameters
+ # cookbook<CookbookVersion>:: CookbookVersion object representing the cookbook
+ # description<String>:: The description of the recipe
+ #
+ # === Returns
+ # recipe_unqualified_names<Array>:: An array of the recipe names given by the cookbook
+ def recipes_from_cookbook_version(cookbook)
+ cookbook.fully_qualified_recipe_names.map do |recipe_name|
+ unqualified_name =
+ if recipe_name =~ /::default$/
+ self.name.to_s
+ else
+ recipe_name
+ end
+
+ @recipes[unqualified_name] ||= ""
+ provides(unqualified_name)
+
+ unqualified_name
+ end
+ end
+
# Adds an attribute )hat a user needs to configure for this cookbook. Takes
# a name (with the / notation for a nested attribute), followed by any of
# these options
diff --git a/lib/chef/cookbook_version.rb b/lib/chef/cookbook_version.rb
index 4482f778c1..76e6d152b2 100644
--- a/lib/chef/cookbook_version.rb
+++ b/lib/chef/cookbook_version.rb
@@ -51,10 +51,14 @@ class Chef
attr_accessor :provider_filenames
attr_accessor :root_filenames
attr_accessor :name
- attr_accessor :metadata
attr_accessor :metadata_filenames
attr_accessor :status
+ # A Chef::Cookbook::Metadata object. It has a setter that fixes up the
+ # metadata to add descriptions of the recipes contained in this
+ # CookbookVersion.
+ attr_reader :metadata
+
# attribute_filenames also has a setter that has non-default
# functionality.
attr_reader :attribute_filenames
@@ -195,6 +199,12 @@ class Chef
attribute_filenames
end
+ def metadata=(metadata)
+ @metadata = metadata
+ @metadata.recipes_from_cookbook_version(self)
+ @metadata
+ end
+
## BACKCOMPAT/DEPRECATED - Remove these and fix breakage before release [DAN - 5/20/2010]##
alias :attribute_files :attribute_filenames
alias :attribute_files= :attribute_filenames=
diff --git a/spec/data/cookbooks/name-mismatch-versionnumber/README.md b/spec/data/cookbooks/name-mismatch-versionnumber/README.md
new file mode 100644
index 0000000000..a61dc9a390
--- /dev/null
+++ b/spec/data/cookbooks/name-mismatch-versionnumber/README.md
@@ -0,0 +1,4 @@
+# name-mismatch
+
+TODO: Enter the cookbook description here.
+
diff --git a/spec/data/cookbooks/name-mismatch-versionnumber/metadata.rb b/spec/data/cookbooks/name-mismatch-versionnumber/metadata.rb
new file mode 100644
index 0000000000..81775bdcc8
--- /dev/null
+++ b/spec/data/cookbooks/name-mismatch-versionnumber/metadata.rb
@@ -0,0 +1,8 @@
+name 'name-mismatch'
+maintainer ''
+maintainer_email ''
+license ''
+description 'Installs/Configures name-mismatch'
+long_description 'Installs/Configures name-mismatch'
+version '0.1.0'
+
diff --git a/spec/data/cookbooks/name-mismatch-versionnumber/recipes/default.rb b/spec/data/cookbooks/name-mismatch-versionnumber/recipes/default.rb
new file mode 100644
index 0000000000..01d302f043
--- /dev/null
+++ b/spec/data/cookbooks/name-mismatch-versionnumber/recipes/default.rb
@@ -0,0 +1,8 @@
+#
+# Cookbook Name:: name-mismatch
+# Recipe:: default
+#
+# Copyright (C) 2014
+#
+#
+#
diff --git a/spec/unit/cookbook/cookbook_version_loader_spec.rb b/spec/unit/cookbook/cookbook_version_loader_spec.rb
index 3e69a37d54..f60248a3b2 100644
--- a/spec/unit/cookbook/cookbook_version_loader_spec.rb
+++ b/spec/unit/cookbook/cookbook_version_loader_spec.rb
@@ -20,7 +20,7 @@ require 'spec_helper'
describe Chef::Cookbook::CookbookVersionLoader do
- describe "loading a simple cookbook" do
+ describe "loading a cookbook" do
let(:chefignore) { nil }
@@ -29,7 +29,7 @@ describe Chef::Cookbook::CookbookVersionLoader do
let(:cookbook_loader) { Chef::Cookbook::CookbookVersionLoader.new(cookbook_path, chefignore) }
let(:loaded_cookbook) do
- cookbook_loader.load_cookbooks
+ cookbook_loader.load!
cookbook_loader.cookbook_version
end
@@ -84,6 +84,30 @@ describe Chef::Cookbook::CookbookVersionLoader do
end
+ context "when the given path is not actually a cookbook" do
+
+ let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "cookbooks/NOTHING_HERE_FOLKS") }
+
+ it "raises an error when loading with #load!" do
+ expect { cookbook_loader.load! }.to raise_error(Chef::Exceptions::CookbookNotFoundInRepo)
+ end
+
+ it "skips the cookbook when called with #load" do
+ expect { cookbook_loader.load }.to_not raise_error
+ end
+
+ end
+
+ context "when a cookbook has a metadata name different than directory basename" do
+
+ let(:cookbook_path) { File.join(CHEF_SPEC_DATA, "cookbooks/name-mismatch-versionnumber") }
+
+ it "prefers the metadata name to the directory basename" do
+ expect(loaded_cookbook.name).to eq(:"name-mismatch")
+ end
+
+ end
+
end
end
diff --git a/spec/unit/cookbook_loader_spec.rb b/spec/unit/cookbook_loader_spec.rb
index 03272af851..713347af7b 100644
--- a/spec/unit/cookbook_loader_spec.rb
+++ b/spec/unit/cookbook_loader_spec.rb
@@ -72,7 +72,8 @@ describe Chef::CookbookLoader do
seen[2].should == "borken"
seen[3].should == "ignorken"
seen[4].should == "java"
- seen[5].should == "openldap"
+ seen[5].should == "name-mismatch"
+ seen[6].should == "openldap"
end
end