summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordanielsdeleo <dan@getchef.com>2014-08-07 13:44:40 -0700
committerdanielsdeleo <dan@getchef.com>2014-08-12 11:03:10 -0700
commit89427a59886b0724a5a1101dde7ea1a1def1c67a (patch)
tree502cccbf5f09aa9b359cf1e0c22867220599a98c
parent13c67e0dfbde2d366f0000ef036a68f374e4cef9 (diff)
downloadchef-89427a59886b0724a5a1101dde7ea1a1def1c67a.tar.gz
Add validation to Metadata
exposes #valid? and #errors methods to check validity.
-rw-r--r--lib/chef/cookbook/metadata.rb34
-rw-r--r--spec/unit/cookbook/metadata_spec.rb32
2 files changed, 66 insertions, 0 deletions
diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb
index bd3a400e10..c1fe20e748 100644
--- a/lib/chef/cookbook/metadata.rb
+++ b/lib/chef/cookbook/metadata.rb
@@ -110,6 +110,8 @@ class Chef
@groupings = Mash.new
@recipes = Mash.new
@version = Version.new("0.0.0")
+
+ @errors = []
end
def ==(other)
@@ -118,6 +120,32 @@ class Chef
end
end
+ # Whether this metadata is valid. In order to be valid, all required
+ # fields must be set. Chef's validation implementation checks the content
+ # of a given field when setting (and raises an error if the content does
+ # not meet the criteria), so the content of the fields is not considered
+ # when checking validity.
+ #
+ # === Returns
+ # valid<Boolean>:: Whether this metadata object is valid
+ def valid?
+ run_validation
+ @errors.empty?
+ end
+
+ # A list of validation errors for this metadata object. See #valid? for
+ # comments about the validation criteria.
+ #
+ # If there are any validation errors, one or more error strings will be
+ # returned. Otherwise an empty array is returned.
+ #
+ # === Returns
+ # error messages<Array>:: Whether this metadata object is valid
+ def errors
+ run_validation
+ @errors
+ end
+
# Sets the cookbooks maintainer, or returns it.
#
# === Parameters
@@ -516,6 +544,12 @@ class Chef
private
+ def run_validation
+ if name.nil?
+ @errors = ["The `name' attribute is required in cookbook metadata"]
+ end
+ end
+
def new_args_format(caller_name, dep_name, version_constraints)
if version_constraints.empty?
">= 0.0.0"
diff --git a/spec/unit/cookbook/metadata_spec.rb b/spec/unit/cookbook/metadata_spec.rb
index 0352c17350..e61c85b42b 100644
--- a/spec/unit/cookbook/metadata_spec.rb
+++ b/spec/unit/cookbook/metadata_spec.rb
@@ -142,6 +142,38 @@ describe Chef::Cookbook::Metadata do
end
+ describe "validation" do
+
+ context "when no required fields are set" do
+
+ it "is not valid" do
+ metadata.should_not be_valid
+ end
+
+ it "has a list of validation errors" do
+ expected_errors = ["The `name' attribute is required in cookbook metadata"]
+ metadata.errors.should eq(expected_errors)
+ end
+
+ end
+
+ context "when all required fields are set" do
+ before do
+ metadata.name "a-valid-name"
+ end
+
+ it "is valid" do
+ metadata.should be_valid
+ end
+
+ it "has no validation errors" do
+ metadata.errors.should be_empty
+ end
+
+ end
+
+ end
+
describe "adding a supported platform" do
it "should support adding a supported platform with a single expression" do
metadata.supports("ubuntu", ">= 8.04")