Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cookbook dependency version validation against chef's required format to berks upload #18

Merged
merged 1 commit into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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