diff options
author | andy-dufour <adufour@chef.io> | 2016-09-21 00:11:14 -0400 |
---|---|---|
committer | andy-dufour <adufour@chef.io> | 2016-09-21 00:11:14 -0400 |
commit | 3d2a2e2e8cf340523a6bb223f9419689557c118e (patch) | |
tree | 4c2802118fc0886f008fbde84c9e450e30e85941 | |
parent | ce694677fdc808143a28457dc44d56e97930b9ec (diff) | |
download | chef-3d2a2e2e8cf340523a6bb223f9419689557c118e.tar.gz |
Fixing metadata.json validation, adding spec test to ensure we're actually validating the JSON passed to usad/3888
-rw-r--r-- | lib/chef/cookbook/metadata.rb | 4 | ||||
-rw-r--r-- | spec/unit/knife/cookbook_metadata_spec.rb | 12 |
2 files changed, 13 insertions, 3 deletions
diff --git a/lib/chef/cookbook/metadata.rb b/lib/chef/cookbook/metadata.rb index ab83da9e55..3ef0d4528f 100644 --- a/lib/chef/cookbook/metadata.rb +++ b/lib/chef/cookbook/metadata.rb @@ -641,8 +641,8 @@ class Chef VERSION_CONSTRAINTS.each do |dependency_type, hash_key| if dependency_group = o[hash_key] dependency_group.each do |cb_name, constraints| - if metadata.respond_to?(method_name) - metadata.public_send(method_name, cb_name, *Array(constraints)) + if metadata.respond_to?(dependency_type) + metadata.public_send(dependency_type, cb_name, *Array(constraints)) end end end diff --git a/spec/unit/knife/cookbook_metadata_spec.rb b/spec/unit/knife/cookbook_metadata_spec.rb index 4b405d0842..ec4d343846 100644 --- a/spec/unit/knife/cookbook_metadata_spec.rb +++ b/spec/unit/knife/cookbook_metadata_spec.rb @@ -24,6 +24,7 @@ describe Chef::Knife::CookbookMetadata do @knife.name_args = ["foobar"] @cookbook_dir = Dir.mktmpdir @json_data = '{ "version": "1.0.0" }' + @invalid_json_data = '{ "version": "1.0.0", {ImInvalid}}' @stdout = StringIO.new @stderr = StringIO.new allow(@knife.ui).to receive(:stdout).and_return(@stdout) @@ -109,7 +110,7 @@ describe Chef::Knife::CookbookMetadata do @json_file_mock = double("json_file") end - it "should generate the metatdata json from metatdata.rb" do + it "should generate the metadata json from metadata.rb" do allow(Chef::Cookbook::Metadata).to receive(:new).and_return(@metadata_mock) expect(@metadata_mock).to receive(:name).with("foobar") expect(@metadata_mock).to receive(:from_file).with("#{@cookbook_dir}/foobar/metadata.rb") @@ -174,6 +175,15 @@ describe Chef::Knife::CookbookMetadata do expect(@stderr.string).to match /#{description} blah/im end end + it "should raise a syntax error on invalid JSON" do + expect(File).to receive(:exist?).with("#{@cookbook_dir}/foobar/metadata.json"). + and_return(true) + expect(IO).to receive(:read).with("#{@cookbook_dir}/foobar/metadata.json"). + and_return(@invalid_json_data) + expect do + @knife.validate_metadata_json(@cookbook_dir, "foobar") + end.to raise_error(Chef::Exceptions::JSON::ParseError) + end end end |