Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Avoid override existing delivery configuration #1162

Merged
merged 2 commits into from
Feb 14, 2017
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
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
18 changes: 11 additions & 7 deletions lib/chef-dk/skeletons/code_generator/recipes/build_cookbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -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')
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions spec/unit/command/generator_commands/build_cookbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down