diff options
-rw-r--r-- | lib/chef/cookbook/cookbook_version_loader.rb | 29 | ||||
-rw-r--r-- | lib/chef/cookbook/file_system_file_vendor.rb | 11 | ||||
-rw-r--r-- | lib/chef/cookbook/metadata.rb | 11 | ||||
-rw-r--r-- | lib/chef/cookbook/syntax_check.rb | 8 | ||||
-rw-r--r-- | lib/chef/cookbook_uploader.rb | 2 | ||||
-rw-r--r-- | lib/chef/cookbook_version.rb | 16 | ||||
-rw-r--r-- | spec/data/cookbooks/not-nginx/attributes/default.rb | 1 | ||||
-rw-r--r-- | spec/data/cookbooks/not-nginx/metadata.rb | 1 | ||||
-rw-r--r-- | spec/data/kitchen/no-really-not-nginx/attributes/default.rb | 1 | ||||
-rw-r--r-- | spec/data/kitchen/no-really-not-nginx/metadata.rb | 1 | ||||
-rw-r--r-- | spec/unit/cookbook/metadata_spec.rb | 106 | ||||
-rw-r--r-- | spec/unit/cookbook_loader_spec.rb | 23 |
12 files changed, 77 insertions, 133 deletions
diff --git a/lib/chef/cookbook/cookbook_version_loader.rb b/lib/chef/cookbook/cookbook_version_loader.rb index 44782bd7bf..e98da77d7f 100644 --- a/lib/chef/cookbook/cookbook_version_loader.rb +++ b/lib/chef/cookbook/cookbook_version_loader.rb @@ -18,14 +18,15 @@ class Chef :provider_filenames] - attr_reader :cookbook_pathname + attr_reader :cookbook_name attr_reader :cookbook_settings attr_reader :metadata_filenames def initialize(path, chefignore=nil) @cookbook_path = File.expand_path( path ) - @cookbook_pathname = File.basename( path ) + @cookbook_name = File.basename( path ) @chefignore = chefignore + @metadata = Hash.new @relative_path = /#{Regexp.escape(@cookbook_path)}\/(.+)$/ @cookbook_settings = { :attribute_filenames => {}, @@ -64,18 +65,13 @@ class Chef 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 - def cookbook_name - metadata.name || @cookbook_pathname - end - def cookbook_version return nil if empty? - Chef::CookbookVersion.new(@cookbook_pathname.to_sym).tap do |c| + Chef::CookbookVersion.new(@cookbook_name.to_sym).tap do |c| c.root_dir = @cookbook_path c.attribute_filenames = cookbook_settings[:attribute_filenames].values c.definition_filenames = cookbook_settings[:definition_filenames].values @@ -87,16 +83,12 @@ 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 = load_metadata(c) + c.metadata = metadata(c) end end - def metadata - @metadata ||= load_metadata(nil) - end - # Generates the Cookbook::Metadata object - def load_metadata(cookbook_version) + def metadata(cookbook_version) @metadata = Chef::Cookbook::Metadata.new(cookbook_version) @metadata_filenames.each do |metadata_file| case metadata_file @@ -108,7 +100,6 @@ class Chef raise RuntimeError, "Invalid metadata file: #{metadata_file} for cookbook: #{cookbook_version}" end end - Chef::Log.warn "Inferring cookbook name from directory name (#@cookbook_pathname) is deprecated, please set a name in the metadata." unless @metadata.name @metadata end @@ -159,18 +150,18 @@ class Chef def apply_ruby_metadata(file) begin - metadata.from_file(file) + @metadata.from_file(file) rescue JSON::ParserError - Chef::Log.error("Error evaluating metadata.rb for #{cookbook_name} in " + file) + Chef::Log.error("Error evaluating metadata.rb for #@cookbook_name in " + file) raise end end def apply_json_metadata(file) begin - metadata.from_json(IO.read(file)) + @metadata.from_json(IO.read(file)) rescue JSON::ParserError - Chef::Log.error("Couldn't parse cookbook metadata JSON for #{cookbook_name} in " + file) + Chef::Log.error("Couldn't parse cookbook metadata JSON for #@cookbook_name in " + file) raise end end diff --git a/lib/chef/cookbook/file_system_file_vendor.rb b/lib/chef/cookbook/file_system_file_vendor.rb index 41c6691e02..8896e3ed30 100644 --- a/lib/chef/cookbook/file_system_file_vendor.rb +++ b/lib/chef/cookbook/file_system_file_vendor.rb @@ -37,17 +37,14 @@ class Chef raise ArgumentError, "You must specify at least one repo path" if @repo_paths.empty? end - def cookbooks - @cookbooks ||= Chef::CookbookLoader.new(@repo_paths).load_cookbooks - end - # Implements abstract base's requirement. It looks in the # Chef::Config.cookbook_path file hierarchy for the requested # file. def get_filename(filename) - if cookbooks.has_key?(@cookbook_name) - location = File.join(cookbooks[@cookbook_name].root_dir, filename) - location = nil unless File.exist?(location) + location = @repo_paths.inject(nil) do |memo, basepath| + candidate_location = File.join(basepath, @cookbook_name, filename) + memo = candidate_location if File.exist?(candidate_location) + memo end raise "File #{filename} does not exist for cookbook #{@cookbook_name}" unless location diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb index 0c03b25b00..18368bd99f 100644 --- a/lib/chef/cookbook/metadata.rb +++ b/lib/chef/cookbook/metadata.rb @@ -91,7 +91,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 : nil + @name = cookbook ? cookbook.name : "" @long_description = "" self.maintainer(maintainer) self.maintainer_email(maintainer_email) @@ -468,15 +468,6 @@ class Chef @groupings = o[GROUPINGS] if o.has_key?(GROUPINGS) @recipes = o[RECIPES] if o.has_key?(RECIPES) @version = o[VERSION] if o.has_key?(VERSION) - # - # XXX: When a client (berkshelf in particular) uploads metadata without a name field, the hosted chef - # ruby server will subsequently return empty string ("") for the metadata.name. In erchef uploading - # metadata without a name field will simply not store or retrieve a name field so we get @name = nil. - # During the migration from ruby to erchef, metadata.name of "" was migrated faithfully even though it - # fails validation, and erchef will return this, so we still have to handle this case unless the - # database is sanitized to remove metadata.name == "". - # - @name = nil if !@name.nil? && @name.empty? self end diff --git a/lib/chef/cookbook/syntax_check.rb b/lib/chef/cookbook/syntax_check.rb index 4a06c88cbc..0e757074e3 100644 --- a/lib/chef/cookbook/syntax_check.rb +++ b/lib/chef/cookbook/syntax_check.rb @@ -85,14 +85,14 @@ class Chef # validated. attr_reader :validated_files - # Creates a new SyntaxCheck given the +cookbook_pathname+ and a +cookbook_path+. + # Creates a new SyntaxCheck given the +cookbook_name+ and a +cookbook_path+. # If no +cookbook_path+ is given, +Chef::Config.cookbook_path+ is used. - def self.for_cookbook(cookbook_pathname, cookbook_path=nil) + def self.for_cookbook(cookbook_name, cookbook_path=nil) cookbook_path ||= Chef::Config.cookbook_path unless cookbook_path - raise ArgumentError, "Cannot find cookbook #{cookbook_pathname} unless Chef::Config.cookbook_path is set or an explicit cookbook path is given" + raise ArgumentError, "Cannot find cookbook #{cookbook_name} unless Chef::Config.cookbook_path is set or an explicit cookbook path is given" end - new(File.join(cookbook_path, cookbook_pathname.to_s)) + new(File.join(cookbook_path, cookbook_name.to_s)) end # Create a new SyntaxCheck object diff --git a/lib/chef/cookbook_uploader.rb b/lib/chef/cookbook_uploader.rb index 456c8b6c07..9ba5b2bd8b 100644 --- a/lib/chef/cookbook_uploader.rb +++ b/lib/chef/cookbook_uploader.rb @@ -159,7 +159,7 @@ class Chef def validate_cookbooks cookbooks.each do |cb| - syntax_checker = Chef::Cookbook::SyntaxCheck.for_cookbook(cb.pathname, @user_cookbook_path) + syntax_checker = Chef::Cookbook::SyntaxCheck.for_cookbook(cb.name, @user_cookbook_path) Chef::Log.info("Validating ruby files") exit(1) unless syntax_checker.validate_ruby_files Chef::Log.info("Validating templates") diff --git a/lib/chef/cookbook_version.rb b/lib/chef/cookbook_version.rb index 31ccbabe59..4e8b2a048e 100644 --- a/lib/chef/cookbook_version.rb +++ b/lib/chef/cookbook_version.rb @@ -50,7 +50,7 @@ class Chef attr_accessor :resource_filenames attr_accessor :provider_filenames attr_accessor :root_filenames - attr_accessor :pathname + attr_accessor :name attr_accessor :metadata attr_accessor :metadata_filenames attr_accessor :status @@ -83,8 +83,8 @@ class Chef # # === Returns # object<Chef::CookbookVersion>:: Duh. :) - def initialize(pathname) - @pathname = pathname + def initialize(name) + @name = name @frozen = false @attribute_filenames = Array.new @definition_filenames = Array.new @@ -104,10 +104,6 @@ class Chef @metadata = Chef::Cookbook::Metadata.new end - def name - metadata.name || @pathname - end - def version metadata.version end @@ -602,11 +598,11 @@ class Chef specificity = "default" if segment == :root_files - matcher = segment_file.match(".+/#{Regexp.escape(pathname.to_s)}/(.+)") + matcher = segment_file.match(".+/#{Regexp.escape(name.to_s)}/(.+)") file_name = matcher[1] path = file_name elsif segment == :templates || segment == :files - matcher = segment_file.match("/#{Regexp.escape(pathname.to_s)}/(#{Regexp.escape(segment.to_s)}/(.+?)/(.+))") + matcher = segment_file.match("/#{Regexp.escape(name.to_s)}/(#{Regexp.escape(segment.to_s)}/(.+?)/(.+))") unless matcher Chef::Log.debug("Skipping file #{segment_file}, as it isn't in any of the proper directories (platform-version, platform or default)") Chef::Log.debug("You probably need to move #{segment_file} into the 'default' sub-directory") @@ -616,7 +612,7 @@ class Chef specificity = matcher[2] file_name = matcher[3] else - matcher = segment_file.match("/#{Regexp.escape(pathname.to_s)}/(#{Regexp.escape(segment.to_s)}/(.+))") + matcher = segment_file.match("/#{Regexp.escape(name.to_s)}/(#{Regexp.escape(segment.to_s)}/(.+))") path = matcher[1] file_name = matcher[2] end diff --git a/spec/data/cookbooks/not-nginx/attributes/default.rb b/spec/data/cookbooks/not-nginx/attributes/default.rb deleted file mode 100644 index be06213478..0000000000 --- a/spec/data/cookbooks/not-nginx/attributes/default.rb +++ /dev/null @@ -1 +0,0 @@ -default[:cheese] = 'mr whiskers' diff --git a/spec/data/cookbooks/not-nginx/metadata.rb b/spec/data/cookbooks/not-nginx/metadata.rb deleted file mode 100644 index 85e7200e2b..0000000000 --- a/spec/data/cookbooks/not-nginx/metadata.rb +++ /dev/null @@ -1 +0,0 @@ -name "nginx" diff --git a/spec/data/kitchen/no-really-not-nginx/attributes/default.rb b/spec/data/kitchen/no-really-not-nginx/attributes/default.rb deleted file mode 100644 index 7987157ec2..0000000000 --- a/spec/data/kitchen/no-really-not-nginx/attributes/default.rb +++ /dev/null @@ -1 +0,0 @@ -default[:wine] = 'captain socks' diff --git a/spec/data/kitchen/no-really-not-nginx/metadata.rb b/spec/data/kitchen/no-really-not-nginx/metadata.rb deleted file mode 100644 index 85e7200e2b..0000000000 --- a/spec/data/kitchen/no-really-not-nginx/metadata.rb +++ /dev/null @@ -1 +0,0 @@ -name "nginx" diff --git a/spec/unit/cookbook/metadata_spec.rb b/spec/unit/cookbook/metadata_spec.rb index 2463333e6f..2757f92506 100644 --- a/spec/unit/cookbook/metadata_spec.rb +++ b/spec/unit/cookbook/metadata_spec.rb @@ -7,9 +7,9 @@ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at -# +# # http://www.apache.org/licenses/LICENSE-2.0 -# +# # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -20,7 +20,7 @@ require 'spec_helper' require 'chef/cookbook/metadata' -describe Chef::Cookbook::Metadata do +describe Chef::Cookbook::Metadata do before(:each) do @cookbook = Chef::CookbookVersion.new('test_cookbook') @meta = Chef::Cookbook::Metadata.new(@cookbook) @@ -86,7 +86,7 @@ describe Chef::Cookbook::Metadata do it "should return a Chef::Cookbook::Metadata object" do @meta.should be_a_kind_of(Chef::Cookbook::Metadata) end - + it "should allow a cookbook as the first argument" do lambda { Chef::Cookbook::Metadata.new(@cookbook) }.should_not raise_error end @@ -96,7 +96,7 @@ describe Chef::Cookbook::Metadata do end it "should set the maintainer name from the second argument" do - md = Chef::Cookbook::Metadata.new(@cookbook, 'Bobo T. Clown') + md = Chef::Cookbook::Metadata.new(@cookbook, 'Bobo T. Clown') md.maintainer.should == 'Bobo T. Clown' end @@ -105,7 +105,7 @@ describe Chef::Cookbook::Metadata do end it "should set the maintainer email from the third argument" do - md = Chef::Cookbook::Metadata.new(@cookbook, 'Bobo T. Clown', 'bobo@clown.co') + md = Chef::Cookbook::Metadata.new(@cookbook, 'Bobo T. Clown', 'bobo@clown.co') md.maintainer_email.should == 'bobo@clown.co' end @@ -114,10 +114,10 @@ describe Chef::Cookbook::Metadata do end it "should set the license from the fourth argument" do - md = Chef::Cookbook::Metadata.new(@cookbook, 'Bobo T. Clown', 'bobo@clown.co', 'Clown License v1') + md = Chef::Cookbook::Metadata.new(@cookbook, 'Bobo T. Clown', 'bobo@clown.co', 'Clown License v1') md.license.should == 'Clown License v1' end - end + end describe "cookbook" do it "should return the cookbook we were initialized with" do @@ -133,7 +133,7 @@ describe Chef::Cookbook::Metadata do describe "platforms" do it "should return the current platform hash" do - @meta.platforms.should be_a_kind_of(Hash) + @meta.platforms.should be_a_kind_of(Hash) end end @@ -235,7 +235,7 @@ describe Chef::Cookbook::Metadata do end end end - + describe "attribute groupings" do it "should allow you set a grouping" do group = { @@ -312,7 +312,7 @@ describe Chef::Cookbook::Metadata do @meta.attribute("db/mysql/databases", {}) @meta.attributes["db/mysql/databases"][:choice].should == [] end - + it "should let calculated be true or false" do lambda { @meta.attribute("db/mysql/databases", :calculated => true) @@ -324,7 +324,7 @@ describe Chef::Cookbook::Metadata do @meta.attribute("db/mysql/databases", :calculated => Hash.new) }.should raise_error(ArgumentError) end - + it "should set calculated to false by default" do @meta.attribute("db/mysql/databases", {}) @meta.attributes["db/mysql/databases"][:calculated].should == false @@ -350,7 +350,7 @@ describe Chef::Cookbook::Metadata do @meta.attribute("db/mysql/databases", :type => "symbol") }.should_not raise_error(ArgumentError) end - + it "should let type be hash (backwards compatability only)" do lambda { @meta.attribute("db/mysql/databases", :type => "hash") @@ -375,7 +375,7 @@ describe Chef::Cookbook::Metadata do }.should_not raise_error(ArgumentError) #attrib = @meta.attributes["db/mysql/databases"][:required].should == "required" end - + it "should convert required false to optional" do lambda { @meta.attribute("db/mysql/databases", :required => false) @@ -387,7 +387,7 @@ describe Chef::Cookbook::Metadata do @meta.attribute("db/mysql/databases", {}) @meta.attributes["db/mysql/databases"][:required].should == 'optional' end - + it "should make sure recipes is an array" do lambda { @meta.attribute("db/mysql/databases", :recipes => []) @@ -399,7 +399,7 @@ describe Chef::Cookbook::Metadata do it "should set recipes to an empty array by default" do @meta.attribute("db/mysql/databases", {}) - @meta.attributes["db/mysql/databases"][:recipes].should == [] + @meta.attributes["db/mysql/databases"][:recipes].should == [] end it "should allow the default value to be a string, array, or hash" do @@ -438,14 +438,14 @@ describe Chef::Cookbook::Metadata do lambda { attrs = { :choice => [ "a", "b", "c"], - :default => "b" + :default => "b" } @meta.attribute("db/mysql/databases", attrs) }.should_not raise_error(ArgumentError) lambda { attrs = { :choice => [ "a", "b", "c", "d", "e"], - :default => ["b", "d"] + :default => ["b", "d"] } @meta.attribute("db/mysql/databases", attrs) }.should_not raise_error(ArgumentError) @@ -455,7 +455,7 @@ describe Chef::Cookbook::Metadata do lambda { attrs = { :choice => [ "a", "b", "c"], - :default => "d" + :default => "d" } @meta.attribute("db/mysql/databases", attrs) }.should raise_error(ArgumentError) @@ -470,11 +470,11 @@ describe Chef::Cookbook::Metadata do end describe "recipes" do - before(:each) do + before(:each) do @cookbook.recipe_files = [ "default.rb", "enlighten.rb" ] @meta = Chef::Cookbook::Metadata.new(@cookbook) end - + it "should have the names of the recipes" do @meta.recipes["test_cookbook"].should == "" @meta.recipes["test_cookbook::enlighten"].should == "" @@ -493,7 +493,7 @@ describe Chef::Cookbook::Metadata do end describe "json" do - before(:each) do + before(:each) do @cookbook.recipe_files = [ "default.rb", "enlighten.rb" ] @meta = Chef::Cookbook::Metadata.new(@cookbook) @meta.version "1.0" @@ -509,8 +509,8 @@ describe Chef::Cookbook::Metadata do @meta.provides "foo(:bar, :baz)" @meta.replaces "snarkitron" @meta.recipe "test_cookbook::enlighten", "is your buddy" - @meta.attribute "bizspark/has_login", - :display_name => "You have nothing" + @meta.attribute "bizspark/has_login", + :display_name => "You have nothing" @meta.version "1.2.3" end @@ -524,23 +524,23 @@ describe Chef::Cookbook::Metadata do end %w{ - name - description - long_description - maintainer - maintainer_email + name + description + long_description + maintainer + maintainer_email license - platforms - dependencies - suggestions - recommendations - conflicting + platforms + dependencies + suggestions + recommendations + conflicting providing - replacing - attributes + replacing + attributes recipes version - }.each do |t| + }.each do |t| it "should include '#{t}'" do @serial[t].should == @meta.send(t.to_sym) end @@ -557,23 +557,23 @@ describe Chef::Cookbook::Metadata do end %w{ - name - description - long_description - maintainer - maintainer_email + name + description + long_description + maintainer + maintainer_email license - platforms - dependencies - suggestions - recommendations - conflicting + platforms + dependencies + suggestions + recommendations + conflicting providing - replacing - attributes + replacing + attributes recipes version - }.each do |t| + }.each do |t| it "should match '#{t}'" do @deserial.send(t.to_sym).should == @meta.send(t.to_sym) end @@ -585,12 +585,6 @@ describe Chef::Cookbook::Metadata do @hash = @meta.to_hash end - it "should convert a name field containing the empty string to nil" do - @hash['name'] = "" - deserial = Chef::Cookbook::Metadata.from_hash(@hash) - deserial.name.should be_nil - end - [:dependencies, :recommendations, :suggestions, diff --git a/spec/unit/cookbook_loader_spec.rb b/spec/unit/cookbook_loader_spec.rb index fd37aa1f08..c3303dd09a 100644 --- a/spec/unit/cookbook_loader_spec.rb +++ b/spec/unit/cookbook_loader_spec.rb @@ -68,8 +68,7 @@ describe Chef::CookbookLoader do seen[2].should == "borken" seen[3].should == "ignorken" seen[4].should == "java" - seen[5].should == "nginx" - seen[6].should == "openldap" + seen[5].should == "openldap" end end @@ -156,26 +155,6 @@ describe Chef::CookbookLoader do end @cookbook_loader.load_cookbooks end - - it "should index cookbooks by name, not pathname" do - @cookbook_loader.should_not have_key(:'not-nginx') - @cookbook_loader.should_not have_key(:'no-really-not-nginx') - @cookbook_loader.should have_key(:nginx) - end - - it "should shadow cookbooks by name, not pathname" do - @cookbook_loader[:nginx].attribute_filenames.detect { |f| - f =~ /cookbooks\/not-nginx\/attributes\/default.rb/ - }.should_not eql(nil) - @cookbook_loader[:nginx].attribute_filenames.detect { |f| - f =~ /kitchen\/no-really-not-nginx\/attributes\/default.rb/ - }.should eql(nil) - end - - it "should emit deprecation warning if name is not in metadata" do - Chef::Log.should_receive(:warn).at_least(:once).with(/Inferring cookbook name from directory name \([^)]+\) is deprecated, please set a name in the metadata./) - @cookbook_loader.load_cookbooks - end end # load_cookbooks end # loading all cookbooks |