diff options
-rw-r--r-- | lib/chef/cookbook/metadata.rb | 54 | ||||
-rw-r--r-- | spec/unit/cookbook/metadata_spec.rb | 72 |
2 files changed, 117 insertions, 9 deletions
diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb index 9822920a7d..b7d61bd09e 100644 --- a/lib/chef/cookbook/metadata.rb +++ b/lib/chef/cookbook/metadata.rb @@ -55,19 +55,23 @@ class Chef SOURCE_URL = 'source_url'.freeze ISSUES_URL = 'issues_url'.freeze PRIVACY = 'privacy'.freeze + CHEF_VERSIONS = 'chef_versions'.freeze + OHAI_VERSIONS = 'ohai_versions'.freeze COMPARISON_FIELDS = [ :name, :description, :long_description, :maintainer, :maintainer_email, :license, :platforms, :dependencies, :recommendations, :suggestions, :conflicting, :providing, :replacing, :attributes, :groupings, :recipes, :version, - :source_url, :issues_url, :privacy ] + :source_url, :issues_url, :privacy, :chef_versions, :ohai_versions ] - VERSION_CONSTRAINTS = {:depends => DEPENDENCIES, - :recommends => RECOMMENDATIONS, - :suggests => SUGGESTIONS, - :conflicts => CONFLICTING, - :provides => PROVIDING, - :replaces => REPLACING } + VERSION_CONSTRAINTS = {:depends => DEPENDENCIES, + :recommends => RECOMMENDATIONS, + :suggests => SUGGESTIONS, + :conflicts => CONFLICTING, + :provides => PROVIDING, + :replaces => REPLACING, + :chef_version => CHEF_VERSIONS, + :ohai_version => OHAI_VERSIONS } include Chef::Mixin::ParamsValidate include Chef::Mixin::FromFile @@ -83,6 +87,8 @@ class Chef attr_reader :groupings attr_reader :recipes attr_reader :version + attr_reader :chef_versions + attr_reader :ohai_versions # Builds a new Chef::Cookbook::Metadata object. # @@ -118,6 +124,8 @@ class Chef @source_url = '' @issues_url = '' @privacy = false + @chef_versions = [] + @ohai_versions = [] @errors = [] end @@ -386,6 +394,18 @@ class Chef @replacing[cookbook] end + # FIXME + def chef_version(*version_args) + @chef_versions << Gem::Dependency.new('chef', *version_args) + @chef_versions + end + + # FIXME + def ohai_version(*version_args) + @ohai_versions << Gem::Dependency.new('ohai', *version_args) + @ohai_versions + end + # Adds a description for a recipe. # # === Parameters @@ -481,6 +501,20 @@ class Chef @groupings[name] end + def gem_requirements_to_hash(*deps) + deps.map do |dep| + dep.requirement.requirements.map do |op, version| + "#{op} #{version}" + end.sort + end + end + + def gem_requirements_from_hash(what, hash) + hash.map do |dep| + Gem::Dependency.new(what, *dep) + end + end + def to_hash { NAME => self.name, @@ -502,7 +536,9 @@ class Chef VERSION => self.version, SOURCE_URL => self.source_url, ISSUES_URL => self.issues_url, - PRIVACY => self.privacy + PRIVACY => self.privacy, + CHEF_VERSIONS => gem_requirements_to_hash(*self.chef_versions), + OHAI_VERSIONS => gem_requirements_to_hash(*self.ohai_versions) } end @@ -537,6 +573,8 @@ class Chef @source_url = o[SOURCE_URL] if o.has_key?(SOURCE_URL) @issues_url = o[ISSUES_URL] if o.has_key?(ISSUES_URL) @privacy = o[PRIVACY] if o.has_key?(PRIVACY) + @chef_versions = gem_requirements_from_hash("chef", o[CHEF_VERSIONS]) if o.has_key?(CHEF_VERSIONS) + @ohai_versions = gem_requirements_from_hash("ohai", o[OHAI_VERSIONS]) if o.has_key?(OHAI_VERSIONS) self end diff --git a/spec/unit/cookbook/metadata_spec.rb b/spec/unit/cookbook/metadata_spec.rb index 1b30286f51..ce8fb1ff26 100644 --- a/spec/unit/cookbook/metadata_spec.rb +++ b/spec/unit/cookbook/metadata_spec.rb @@ -30,7 +30,7 @@ describe Chef::Cookbook::Metadata do :maintainer_email, :license, :platforms, :dependencies, :recommendations, :suggestions, :conflicting, :providing, :replacing, :attributes, :groupings, :recipes, :version, - :source_url, :issues_url, :privacy ] + :source_url, :issues_url, :privacy, :ohai_versions, :chef_versions ] end it "does not depend on object identity for equality" do @@ -326,6 +326,60 @@ describe Chef::Cookbook::Metadata do end end + describe "chef_version" do + def expect_chef_version_works(*args) + ret = [] + args.each do |arg| + metadata.send(:chef_version, *arg) + ret << Gem::Dependency.new("chef", *arg) + end + expect(metadata.send(:chef_versions)).to eql(ret) + end + + it "should work with a single simple constraint" do + expect_chef_version_works(["~> 12"]) + end + + it "should work with a single complex constraint" do + expect_chef_version_works([">= 12.0.1", "< 12.5.1"]) + end + + it "should work with multiple simple constraints" do + expect_chef_version_works(["~> 12.5.1"],["~> 11.18.10"]) + end + + it "should work with multiple complex constraints" do + expect_chef_version_works([">= 11.14.2", "< 11.18.10"],[">= 12.2.1", "< 12.5.1"]) + end + end + + describe "ohai_version" do + def expect_ohai_version_works(*args) + ret = [] + args.each do |arg| + metadata.send(:ohai_version, *arg) + ret << Gem::Dependency.new("ohai", *arg) + end + expect(metadata.send(:ohai_versions)).to eql(ret) + end + + it "should work with a single simple constraint" do + expect_ohai_version_works(["~> 12"]) + end + + it "should work with a single complex constraint" do + expect_ohai_version_works([">= 12.0.1", "< 12.5.1"]) + end + + it "should work with multiple simple constraints" do + expect_ohai_version_works(["~> 12.5.1"],["~> 11.18.10"]) + end + + it "should work with multiple complex constraints" do + expect_ohai_version_works([">= 11.14.2", "< 11.18.10"],[">= 12.2.1", "< 12.5.1"]) + end + end + describe "attribute groupings" do it "should allow you set a grouping" do group = { @@ -684,9 +738,14 @@ describe Chef::Cookbook::Metadata do metadata.attribute "bizspark/has_login", :display_name => "You have nothing" metadata.version "1.2.3" + metadata.chef_version ">= 11.14.2", "< 11.18.10" + metadata.chef_version ">= 12.2.1", "< 12.5.1" + metadata.ohai_version ">= 7.1.0", "< 7.5.0" + metadata.ohai_version ">= 8.0.1", "< 8.6.0" end it "should produce the same output from to_json and Chef::JSONCompat" do + # XXX: fairly certain this is testing ruby method dispatch expect(metadata.to_json).to eq(Chef::JSONCompat.to_json(metadata)) end @@ -723,6 +782,15 @@ describe Chef::Cookbook::Metadata do expect(deserialized_metadata[t]).to eq(metadata.send(t.to_sym)) end end + + %w{ + ohai_versions + chef_versions + }.each do |t| + it "should include '#{t}'" do + expect(deserialized_metadata[t]).to eq(metadata.gem_requirements_to_hash(*metadata.send(t.to_sym))) + end + end end describe "deserialize" do @@ -754,6 +822,8 @@ describe Chef::Cookbook::Metadata do source_url issues_url privacy + chef_versions + ohai_versions }.each do |t| it "should match '#{t}'" do expect(deserialized_metadata.send(t.to_sym)).to eq(metadata.send(t.to_sym)) |