Skip to content

Commit

Permalink
Merge pull request #18 from chef/nikhil/CHEF-617-invalid-cookbook-upl…
Browse files Browse the repository at this point in the history
…oading

Fix cookbook dependency version validation against chef's required format to berks upload
  • Loading branch information
nikhil2611 authored Jun 12, 2023
2 parents c4ae9de + fd088a7 commit f2a57d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/berkshelf/validator.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'chef/version_class'

module Berkshelf
module Validator
class << self
Expand Down Expand Up @@ -25,10 +27,19 @@ def validate_files(cookbooks)
base, name = Pathname.new(cookbook.path.to_s).split

files = Dir.glob("#{name}/**/*.rb", base: base.to_s).select { |f| f =~ /[[:space:]]/ }
validate_versions(cookbook)

raise InvalidCookbookFiles.new(cookbook, files) unless files.empty?
end
end

def validate_versions(cookbook)
cookbook_dependencies = cookbook.dependencies
cookbook_dependencies.each do |cookbook_name, cookbook_version|
version = cookbook_version.gsub(/[^\d,\.]/, '')
Chef::Version.new(version)
end
end
end
end
end
16 changes: 16 additions & 0 deletions spec/unit/berkshelf/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,29 @@

it "raises an error when the cookbook has spaces in the files" do
allow(Dir).to receive(:glob).and_return(["/there are/spaces/in this/recipes/default.rb"])
allow(cookbook).to receive(:dependencies).and_return({"cookbook" => "1.0.0"})
expect do
subject.validate_files(cookbook)
end.to raise_error(Berkshelf::InvalidCookbookFiles)
end

it "does not raise an error when the cookbook is valid" do
allow(Dir).to receive(:glob).and_return(["/there-are/no-spaces/in-this/recipes/default.rb"])
allow(cookbook).to receive(:dependencies).and_return({"cookbook" => "1.0.0"})
expect do
subject.validate_files(cookbook)
end.to_not raise_error
end

it "raises an error when the cookbook version is not valid" do
allow(cookbook).to receive(:dependencies).and_return({"cookbook" => "1"})
expect do
subject.validate_files(cookbook)
end.to raise_error
end

it "does not raise an error when the cookbook version is valid" do
allow(cookbook).to receive(:dependencies).and_return({"cookbook" => "1.0"})
expect do
subject.validate_files(cookbook)
end.to_not raise_error
Expand Down

0 comments on commit f2a57d5

Please sign in to comment.