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

CHEF-1533 Add check for co-located generator and cookbook #225

Merged
merged 5 commits into from
Jun 19, 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
13 changes: 12 additions & 1 deletion lib/chef-cli/command/generator_commands/cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ def cookbook_root
end

def cookbook_full_path
File.expand_path(cookbook_name_or_path, Dir.pwd)
if !cookbook_name_or_path.nil? && !cookbook_name_or_path.empty?
File.expand_path(cookbook_name_or_path, Dir.pwd)
else
""
end
end

def policy_mode?
Expand All @@ -205,6 +209,13 @@ def read_and_validate_params
msg("Hyphens are discouraged in cookbook names as they may cause problems with custom resources. See https://docs.chef.io/workstation/ctl_chef/#chef-generate-cookbook for more information.")
end

if !generator_cookbook_path.empty? &&
!cookbook_full_path.empty? &&
File.identical?(Pathname.new(cookbook_full_path).parent, generator_cookbook_path)
err("The generator and the cookbook cannot be in the same directory. Please specify a cookbook directory that is different from the generator's parent.")
@params_valid = false
end

if config[:berks] && config[:policy]
err("Berkshelf and Policyfiles are mutually exclusive. Please specify only one.")
@params_valid = false
Expand Down
39 changes: 30 additions & 9 deletions spec/shared/custom_generator_cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
end
end

before do
it "configures the generator context" do
reset_tempdir
code_generator.read_and_validate_params
allow(code_generator.config_loader).to receive(:load)
end

it "configures the generator context" do
code_generator.setup_context
expect(generator_context.cookbook_name).to eq(generator_arg)
expect(generator_context.cookbook_name).to eq(File.basename(generator_arg))
expect(code_generator.chef_runner.cookbook_path).to eq(tempdir)
expect(code_generator.chef_runner.run_list).to eq(["recipe[a_generator_cookbook::#{generator_name}]"])
end
Expand All @@ -55,19 +53,33 @@
end

before do
reset_tempdir
code_generator.read_and_validate_params
allow(code_generator.config_loader).to receive(:load)
allow(code_generator).to receive(:chefcli_config).and_return(chefcli_config)
end

it "configures the generator context" do
code_generator.setup_context
expect(generator_context.cookbook_name).to eq(generator_arg)
expect(code_generator.chef_runner.cookbook_path).to eq(tempdir)
expect(code_generator.chef_runner.run_list).to eq(["recipe[a_generator_cookbook::#{generator_name}]"])
expect(generator_context.cookbook_name).to eq(File.basename(generator_arg))
expect(code_generator.chef_runner.cookbook_path).to eq(File.expand_path("lib/chef-cli/skeletons", project_root))
expect(code_generator.chef_runner.run_list).to eq(["recipe[code_generator::#{generator_name}]"])
end
end

context "with an invalid generator-cookbook path" do

let(:argv) { ["new_cookbook", "--generator-cookbook", "#{tempdir}/nested/a_generator_cookbook"] }

before do
reset_tempdir
FileUtils.mkdir_p("#{tempdir}/nested")
FileUtils.cp_r(default_generator_cookbook_path, "#{tempdir}/nested/")

code_generator.read_and_validate_params
allow(code_generator.config_loader).to receive(:load)
end

it "fails with an informative error" do
Dir.chdir(tempdir) do
allow(code_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
Expand All @@ -88,14 +100,18 @@
let(:metadata_file) { File.join(generator_cookbook_path, "metadata.rb") }

before do
reset_tempdir
code_generator.read_and_validate_params
allow(code_generator.config_loader).to receive(:load)

FileUtils.cp_r(default_generator_cookbook_path, generator_cookbook_path)

# have to update metadata with the correct name
IO.binwrite(metadata_file, "name 'a_generator_cookbook'")
end

it "creates the new files" do
expect(code_generator.chef_runner.cookbook_path).to eq(tempdir)
expect(code_generator.chef_runner.cookbook_path).to eq("#{tempdir}")
expect(code_generator.chef_runner.run_list).to eq(["recipe[a_generator_cookbook::#{generator_name}]"])

Dir.chdir(tempdir) do
Expand All @@ -108,11 +124,16 @@

context "with a generator-cookbook path to a directory containing a 'code_generator' cookbook" do

let(:argv) { ["#{tempdir}/new_cookbook", "--generator-cookbook", generator_cookbook_path] }

before do
reset_tempdir
FileUtils.mkdir_p(generator_cookbook_path)
FileUtils.cp_r(default_generator_cookbook_path, generator_cookbook_path)

allow(code_generator).to receive(:stderr).and_return(stderr_io)
code_generator.read_and_validate_params
allow(code_generator.config_loader).to receive(:load)
end

it "creates the new_files (and warns about deprecated usage)" do
Expand All @@ -121,7 +142,7 @@
Dir.chdir(tempdir) do
code_generator.run
end
generated_files = Dir.glob("#{tempdir}/#{generator_arg}/**/*", File::FNM_DOTMATCH)
generated_files = Dir.glob("#{tempdir}/new_cookbook/**/*", File::FNM_DOTMATCH)
expected_cookbook_files.each do |expected_file|
expect(generated_files).to include(expected_file)
end
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/command/generator_commands/cookbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ def with_argv(argv)
expect(stderr_io.string).to include(message)
end

it "errors if cookbook parent folder is same as generator parent folder" do
expect(with_argv(%w{ my_cookbook -g my_generator }).run).to eq(1)
message = "The generator and the cookbook cannot be in the same directory. Please specify a cookbook directory that is different from the generator's parent."
expect(stderr_io.string).to include(message)
end

it "warns if a hyphenated cookbook name is passed" do
expect(with_argv(%w{my-cookbook}).run).to eq(0)
message = "Hyphens are discouraged in cookbook names as they may cause problems with custom resources. See https://docs.chef.io/workstation/ctl_chef/#chef-generate-cookbook for more information."
Expand Down