diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 9a0f43c1e..f6f2990b4 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -4,3 +4,6 @@ Build cookbooks generated via `chef generate build-cookbook` will no longer depend on the delivery_build or delivery-base cookbook. Instead, the Test Kitchen instance will use ChefDK as per the standard Workflow Runner setup. + +Also the build cookbook generator will not overwrite your `config.json` or +`project.toml` if they exist already on your project. diff --git a/lib/chef-dk/skeletons/code_generator/recipes/build_cookbook.rb b/lib/chef-dk/skeletons/code_generator/recipes/build_cookbook.rb index 7ff66cb40..414eb1986 100644 --- a/lib/chef-dk/skeletons/code_generator/recipes/build_cookbook.rb +++ b/lib/chef-dk/skeletons/code_generator/recipes/build_cookbook.rb @@ -3,18 +3,22 @@ delivery_project_dir = context.delivery_project_dir pipeline = context.pipeline dot_delivery_dir = File.join(delivery_project_dir, '.delivery') +config_json = File.join(dot_delivery_dir, 'config.json') +project_toml = File.join(dot_delivery_dir, 'project.toml') generator_desc('Ensuring delivery configuration') directory dot_delivery_dir -cookbook_file File.join(dot_delivery_dir, 'config.json') do +cookbook_file config_json do source 'delivery-config.json' + not_if { File.exist?(config_json) } end # Adding a new prototype file for delivery-cli local commands -cookbook_file File.join(dot_delivery_dir, 'project.toml') do +cookbook_file project_toml do source 'delivery-project.toml' + not_if { File.exist?(project_toml) } end generator_desc('Ensuring correct delivery build cookbook content') @@ -124,7 +128,7 @@ command('git add .delivery/config.json') cwd delivery_project_dir - only_if 'git status --porcelain |grep "."' + only_if 'git status -u --porcelain |grep ".delivery/config.json"' end # Adding the new prototype file to the feature branch @@ -133,14 +137,14 @@ command('git add .delivery/project.toml') cwd delivery_project_dir - only_if 'git status --porcelain |grep "."' + only_if 'git status -u --porcelain |grep ".delivery/project.toml"' end execute('git-commit-delivery-config') do command('git commit -m "Add generated delivery configuration"') cwd delivery_project_dir - only_if 'git status --porcelain |grep "."' + only_if 'git status -u --porcelain | egrep "config.json|project.toml"' end generator_desc('Adding build cookbook to feature branch') @@ -149,14 +153,14 @@ command('git add .delivery') cwd delivery_project_dir - only_if 'git status --porcelain |grep "."' + only_if 'git status -u --porcelain |grep ".delivery"' end execute('git-commit-delivery-build-cookbook') do command('git commit -m "Add generated delivery build cookbook"') cwd delivery_project_dir - only_if 'git status --porcelain |grep "."' + only_if 'git status -u --porcelain |grep ".delivery"' end execute("git-return-to-#{pipeline}-branch") do diff --git a/spec/unit/command/generator_commands/build_cookbook_spec.rb b/spec/unit/command/generator_commands/build_cookbook_spec.rb index 6bb2de89d..f9fc6ba0f 100644 --- a/spec/unit/command/generator_commands/build_cookbook_spec.rb +++ b/spec/unit/command/generator_commands/build_cookbook_spec.rb @@ -322,6 +322,40 @@ def git!(cmd) end end + + context "when the delivery project has already a config.json and project.toml" do + + let(:dot_delivery) { File.join(project_dir, ".delivery") } + let(:config_json) { File.join(dot_delivery, "config.json") } + let(:project_toml) { File.join(dot_delivery, "project.toml") } + + def git!(cmd) + Mixlib::ShellOut.new("git #{cmd}", cwd: project_dir).tap do |c| + c.run_command + c.error! + end + end + + before do + FileUtils.mkdir_p(dot_delivery) + FileUtils.touch(config_json) + FileUtils.touch(project_toml) + + git!("init .") + git!("add .") + git!("commit -m \"initial commit\"") + + Dir.chdir(tempdir) do + allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io) + expect(cookbook_generator.run).to eq(0) + end + end + + it "does not overwrite the delivery config" do + expect(git!("log").stdout).to_not include("Add generated delivery configuration") + end + + end end context "when given a path including the .delivery directory" do