summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/cookbook/metadata.rb34
-rw-r--r--lib/chef/version_constraint.rb11
-rw-r--r--spec/unit/cookbook/metadata_spec.rb24
-rw-r--r--spec/unit/environment_spec.rb6
-rw-r--r--spec/unit/version_constraint_spec.rb16
5 files changed, 70 insertions, 21 deletions
diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb
index 32597490d3..7da1ae70de 100644
--- a/lib/chef/cookbook/metadata.rb
+++ b/lib/chef/cookbook/metadata.rb
@@ -242,8 +242,8 @@ class Chef
# versions<Array>:: Returns the list of versions for the platform
def supports(platform, *version_args)
version = new_args_format(:supports, platform, version_args)
- validate_version_constraint(:supports, platform, version)
- @platforms[platform] = version
+ normalized_version = normalize_version_constraint(:supports, platform, version)
+ @platforms[platform] = normalized_version
@platforms[platform]
end
@@ -259,8 +259,8 @@ class Chef
# versions<Array>:: Returns the list of versions for the platform
def depends(cookbook, *version_args)
version = new_args_format(:depends, cookbook, version_args)
- validate_version_constraint(:depends, cookbook, version)
- @dependencies[cookbook] = version
+ normalized_version = normalize_version_constraint(:depends, cookbook, version)
+ @dependencies[cookbook] = normalized_version
@dependencies[cookbook]
end
@@ -276,8 +276,8 @@ class Chef
# versions<Array>:: Returns the list of versions for the platform
def recommends(cookbook, *version_args)
version = new_args_format(:recommends, cookbook, version_args)
- validate_version_constraint(:recommends, cookbook, version)
- @recommendations[cookbook] = version
+ normalized_version = normalize_version_constraint(:recommends, cookbook, version)
+ @recommendations[cookbook] = normalized_version
@recommendations[cookbook]
end
@@ -293,8 +293,8 @@ class Chef
# versions<Array>:: Returns the list of versions for the platform
def suggests(cookbook, *version_args)
version = new_args_format(:suggests, cookbook, version_args)
- validate_version_constraint(:suggests, cookbook, version)
- @suggestions[cookbook] = version
+ normalized_version = normalize_version_constraint(:suggests, cookbook, version)
+ @suggestions[cookbook] = normalized_version
@suggestions[cookbook]
end
@@ -310,8 +310,8 @@ class Chef
# versions<Array>:: Returns the list of versions for the platform
def conflicts(cookbook, *version_args)
version = new_args_format(:conflicts, cookbook, version_args)
- validate_version_constraint(:conflicts, cookbook, version)
- @conflicting[cookbook] = version
+ normalized_version = normalize_version_constraint(:conflicts, cookbook, version)
+ @conflicting[cookbook] = normalized_version
@conflicting[cookbook]
end
@@ -331,8 +331,8 @@ class Chef
# versions<Array>:: Returns the list of versions for the platform
def provides(cookbook, *version_args)
version = new_args_format(:provides, cookbook, version_args)
- validate_version_constraint(:provides, cookbook, version)
- @providing[cookbook] = version
+ normalized_version = normalize_version_constraint(:provides, cookbook, version)
+ @providing[cookbook] = normalized_version
@providing[cookbook]
end
@@ -347,8 +347,8 @@ class Chef
# versions<Array>:: Returns the list of versions for the platform
def replaces(cookbook, *version_args)
version = new_args_format(:replaces, cookbook, version_args)
- validate_version_constraint(:replaces, cookbook, version)
- @replacing[cookbook] = version
+ normalized_version = normalize_version_constraint(:replaces, cookbook, version)
+ @replacing[cookbook] = normalized_version
@replacing[cookbook]
end
@@ -532,6 +532,12 @@ Called from:
INVALID
raise Exceptions::InvalidVersionConstraint, msg
end
+
+ def normalize_version_constraint(caller_name, dep_name, constraint_str)
+ version_constraint = validate_version_constraint(caller_name, dep_name, constraint_str)
+ "#{version_constraint.op} #{version_constraint.raw_version}"
+ end
+
# Verify that the given array is an array of strings
#
# Raise an exception if the members of the array are not Strings
diff --git a/lib/chef/version_constraint.rb b/lib/chef/version_constraint.rb
index cc213abb3d..7bfde41e74 100644
--- a/lib/chef/version_constraint.rb
+++ b/lib/chef/version_constraint.rb
@@ -21,10 +21,10 @@ class Chef
DEFAULT_CONSTRAINT = ">= 0.0.0"
STANDARD_OPS = %w(< > <= >=)
OPS = %w(< > = <= >= ~>)
- PATTERN = /^(#{OPS.join('|')}) (.+)$/
+ PATTERN = /^(#{OPS.join('|')}) *([0-9].*)$/
VERSION_CLASS = Chef::Version
- attr_reader :op, :version
+ attr_reader :op, :version, :raw_version
def initialize(constraint_spec=DEFAULT_CONSTRAINT)
case constraint_spec
@@ -99,12 +99,13 @@ class Chef
@missing_patch_level = false
if str.index(" ").nil? && str =~ /^[0-9]/
# try for lone version, implied '='
- @version = self.class::VERSION_CLASS.new(str)
+ @raw_version = str
+ @version = self.class::VERSION_CLASS.new(@raw_version)
@op = "="
elsif PATTERN.match str
@op = $1
- raw_version = $2
- @version = self.class::VERSION_CLASS.new(raw_version)
+ @raw_version = $2
+ @version = self.class::VERSION_CLASS.new(@raw_version)
if raw_version.split('.').size <= 2
@missing_patch_level = true
end
diff --git a/spec/unit/cookbook/metadata_spec.rb b/spec/unit/cookbook/metadata_spec.rb
index 88c4a1a5f5..8e98610183 100644
--- a/spec/unit/cookbook/metadata_spec.rb
+++ b/spec/unit/cookbook/metadata_spec.rb
@@ -178,6 +178,7 @@ describe Chef::Cookbook::Metadata do
end
describe "describing dependencies" do
+
dep_types = {
:depends => [ :dependencies, "foo::bar", "> 0.2" ],
:recommends => [ :recommendations, "foo::bar", ">= 0.2" ],
@@ -199,6 +200,28 @@ describe Chef::Cookbook::Metadata do
end
end
+ dep_types = {
+ :depends => [ :dependencies, "foo::bar", ">0.2", "> 0.2" ],
+ :recommends => [ :recommendations, "foo::bar", ">=0.2", ">= 0.2" ],
+ :suggests => [ :suggestions, "foo::bar", ">0.2", "> 0.2" ],
+ :conflicts => [ :conflicting, "foo::bar", "~>0.2", "~> 0.2" ],
+ :provides => [ :providing, "foo::bar", "<=0.2", "<= 0.2" ],
+ :replaces => [ :replacing, "foo::bar", "=0.2.1", "= 0.2.1" ],
+ }
+ dep_types.sort { |a,b| a.to_s <=> b.to_s }.each do |dep, dep_args|
+ check_with = dep_args.shift
+ normalized_version = dep_args.pop
+ describe dep do
+ it "should be set-able and normalized via #{dep}" do
+ @meta.send(dep, *dep_args).should == normalized_version
+ end
+ it "should be get-able and normalized via #{check_with}" do
+ @meta.send(dep, *dep_args)
+ @meta.send(check_with).should == { dep_args[0] => normalized_version }
+ end
+ end
+ end
+
describe "in the obsoleted format" do
dep_types = {
@@ -546,6 +569,7 @@ describe Chef::Cookbook::Metadata do
@meta.long_description "I have a long arm!"
@meta.supports :ubuntu, "> 8.04"
@meta.depends "bobo", "= 1.0"
+ @meta.depends "bubu", "=1.0"
@meta.depends "bobotclown", "= 1.1"
@meta.recommends "snark", "< 3.0"
@meta.suggests "kindness", "> 2.0"
diff --git a/spec/unit/environment_spec.rb b/spec/unit/environment_spec.rb
index ebbcdd9861..0e230e46ed 100644
--- a/spec/unit/environment_spec.rb
+++ b/spec/unit/environment_spec.rb
@@ -263,7 +263,9 @@ describe Chef::Environment do
describe "self.validate_cookbook_version" do
it "should validate correct version numbers" do
Chef::Environment.validate_cookbook_version("= 1.2.3").should == true
+ Chef::Environment.validate_cookbook_version("=1.2.3").should == true
Chef::Environment.validate_cookbook_version(">= 0.0.3").should == true
+ Chef::Environment.validate_cookbook_version(">=0.0.3").should == true
# A lone version is allowed, interpreted as implicit '='
Chef::Environment.validate_cookbook_version("1.2.3").should == true
end
@@ -271,9 +273,13 @@ describe Chef::Environment do
it "should return false when an invalid version is given" do
Chef::Environment.validate_cookbook_version(Chef::CookbookVersion.new("meta")).should == false
Chef::Environment.validate_cookbook_version("= 1.2.3a").should == false
+ Chef::Environment.validate_cookbook_version("=1.2.3a").should == false
Chef::Environment.validate_cookbook_version("= 1").should == false
+ Chef::Environment.validate_cookbook_version("=1").should == false
Chef::Environment.validate_cookbook_version("= a").should == false
+ Chef::Environment.validate_cookbook_version("=a").should == false
Chef::Environment.validate_cookbook_version("= 1.2.3.4").should == false
+ Chef::Environment.validate_cookbook_version("=1.2.3.4").should == false
end
describe "in solo mode" do
diff --git a/spec/unit/version_constraint_spec.rb b/spec/unit/version_constraint_spec.rb
index a49b082d9d..4184689e2b 100644
--- a/spec/unit/version_constraint_spec.rb
+++ b/spec/unit/version_constraint_spec.rb
@@ -20,8 +20,8 @@ require 'chef/version_constraint'
describe Chef::VersionConstraint do
describe "validation" do
- bad_version = ["> >", ">= 1.2.z", "> 1.2.3 < 5.0", "> 1.2.3, < 5.0"]
- bad_op = ["<3.0.1", ">$ 1.2.3", "! 3.4"]
+ bad_version = [">= 1.2.z", "> 1.2.3 < 5.0", "> 1.2.3, < 5.0"]
+ bad_op = ["> >", ">$ 1.2.3", "! 3.4"]
o_error = Chef::Exceptions::InvalidVersionConstraint
v_error = Chef::Exceptions::InvalidCookbookVersion
bad_version.each do |s|
@@ -64,6 +64,18 @@ describe Chef::VersionConstraint do
vc.version.should be_an_instance_of(Chef::Version)
end
+ it "should allow ops without space separator" do
+ Chef::VersionConstraint.new("=1.2.3").should eql(Chef::VersionConstraint.new("= 1.2.3"))
+ Chef::VersionConstraint.new(">1.2.3").should eql(Chef::VersionConstraint.new("> 1.2.3"))
+ Chef::VersionConstraint.new("<1.2.3").should eql(Chef::VersionConstraint.new("< 1.2.3"))
+ Chef::VersionConstraint.new(">=1.2.3").should eql(Chef::VersionConstraint.new(">= 1.2.3"))
+ Chef::VersionConstraint.new("<=1.2.3").should eql(Chef::VersionConstraint.new("<= 1.2.3"))
+ end
+
+ it "should allow ops with multiple spaces" do
+ Chef::VersionConstraint.new("= 1.2.3").should eql(Chef::VersionConstraint.new("= 1.2.3"))
+ end
+
describe "include?" do
describe "handles various input data types" do
before do