chef-dk 0.15.9 → 0.15.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,169 +1,169 @@
1
- #
2
- # Copyright:: Copyright (c) 2014 Chef Software Inc.
3
- # License:: Apache License, Version 2.0
4
- #
5
- # Licensed under the Apache License, Version 2.0 (the "License");
6
- # you may not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing, software
12
- # distributed under the License is distributed on an "AS IS" BASIS,
13
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- # See the License for the specific language governing permissions and
15
- # limitations under the License.
16
- #
17
-
18
- require 'spec_helper'
19
- require 'shared/custom_generator_cookbook'
20
- require 'chef-dk/command/generator_commands/app'
21
-
22
- describe ChefDK::Command::GeneratorCommands::App do
23
-
24
- let(:argv) { %w[new_app] }
25
-
26
- let(:stdout_io) { StringIO.new }
27
- let(:stderr_io) { StringIO.new }
28
-
29
- let(:expected_cookbook_file_relpaths) do
30
- %w[
31
- .gitignore
32
- .kitchen.yml
33
- test
34
- test/integration
35
- test/integration/default
36
- test/integration/default/serverspec
37
- test/integration/default/serverspec/default_spec.rb
38
- test/integration/helpers/serverspec/spec_helper.rb
39
- README.md
40
- cookbooks/new_app/Berksfile
41
- cookbooks/new_app/chefignore
42
- cookbooks/new_app/metadata.rb
43
- cookbooks/new_app/recipes
44
- cookbooks/new_app/recipes/default.rb
45
- cookbooks/new_app/spec
46
- cookbooks/new_app/spec/spec_helper.rb
47
- cookbooks/new_app/spec/unit
48
- cookbooks/new_app/spec/unit/recipes
49
- cookbooks/new_app/spec/unit/recipes/default_spec.rb
50
- ]
51
- end
52
-
53
- let(:expected_cookbook_files) do
54
- expected_cookbook_file_relpaths.map do |relpath|
55
- File.join(tempdir, "new_app", relpath)
56
- end
57
- end
58
-
59
- subject(:cookbook_generator) { described_class.new(argv) }
60
-
61
- def generator_context
62
- ChefDK::Generator.context
63
- end
64
-
65
- before do
66
- ChefDK::Generator.reset
67
- end
68
-
69
- include_examples "custom generator cookbook" do
70
-
71
- let(:generator_arg) { "new_app" }
72
-
73
- let(:generator_name) { "app" }
74
-
75
- end
76
-
77
- context "when given the name of the cookbook to generate" do
78
-
79
- before do
80
- reset_tempdir
81
- end
82
-
83
- it "configures the generator context" do
84
- cookbook_generator.read_and_validate_params
85
- cookbook_generator.setup_context
86
- expect(generator_context.app_root).to eq(Dir.pwd)
87
- expect(generator_context.app_name).to eq("new_app")
88
- expect(generator_context.cookbook_root).to eq(File.join(Dir.pwd, "new_app", "cookbooks"))
89
- expect(generator_context.cookbook_name).to eq("new_app")
90
- expect(generator_context.recipe_name).to eq("default")
91
- end
92
-
93
- describe "generated files" do
94
- it "creates a new cookbook" do
95
- Dir.chdir(tempdir) do
96
- allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
97
- cookbook_generator.run
98
- end
99
- generated_files = Dir.glob(File.join(tempdir, "new_app", "**", "*"), File::FNM_DOTMATCH)
100
- expected_cookbook_files.each do |expected_file|
101
- expect(generated_files).to include(expected_file)
102
- end
103
- end
104
-
105
- shared_examples_for "a generated file" do |context_var|
106
- before do
107
- Dir.chdir(tempdir) do
108
- allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
109
- cookbook_generator.run
110
- end
111
- end
112
-
113
- it "should contain #{context_var} from the generator context" do
114
- expect(File.read(file)).to match line
115
- end
116
- end
117
-
118
- describe "README.md" do
119
- let(:file) { File.join(tempdir, "new_app", "README.md") }
120
-
121
- include_examples "a generated file", :cookbook_name do
122
- let(:line) { "# new_app" }
123
- end
124
- end
125
-
126
- describe ".kitchen.yml" do
127
- let(:file) { File.join(tempdir, "new_app", ".kitchen.yml") }
128
-
129
- include_examples "a generated file", :cookbook_name do
130
- let(:line) { /\s*- recipe\[new_app::default\]/ }
131
- end
132
- end
133
-
134
- describe "test/integration/default/serverspec/default_spec.rb" do
135
- let(:file) { File.join(tempdir, "new_app", "test", "integration", "default", "serverspec", "default_spec.rb") }
136
-
137
- include_examples "a generated file", :cookbook_name do
138
- let(:line) { "describe 'new_app::default' do" }
139
- end
140
- end
141
-
142
- describe "cookbooks/new_app/metadata.rb" do
143
- let(:file) { File.join(tempdir, "new_app", "cookbooks", "new_app", "metadata.rb") }
144
-
145
- include_examples "a generated file", :cookbook_name do
146
- let(:line) { /name\s+'new_app'/ }
147
- end
148
- end
149
-
150
- describe "cookbooks/new_app/recipes/default.rb" do
151
- let(:file) { File.join(tempdir, "new_app", "cookbooks", "new_app", "recipes", "default.rb") }
152
-
153
- include_examples "a generated file", :cookbook_name do
154
- let(:line) { "# Cookbook Name:: new_app" }
155
- end
156
- end
157
-
158
- describe "cookbooks/new_app/spec/unit/recipes/default_spec.rb" do
159
- let(:file) { File.join(tempdir, "new_app", "cookbooks", "new_app", "spec", "unit", "recipes", "default_spec.rb") }
160
-
161
- include_examples "a generated file", :cookbook_name do
162
- let(:line) { "describe \'new_app::default\' do" }
163
- end
164
- end
165
-
166
- end
167
- end
168
- end
169
-
1
+ #
2
+ # Copyright:: Copyright (c) 2014 Chef Software Inc.
3
+ # License:: Apache License, Version 2.0
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+
18
+ require 'spec_helper'
19
+ require 'shared/custom_generator_cookbook'
20
+ require 'chef-dk/command/generator_commands/app'
21
+
22
+ describe ChefDK::Command::GeneratorCommands::App do
23
+
24
+ let(:argv) { %w[new_app] }
25
+
26
+ let(:stdout_io) { StringIO.new }
27
+ let(:stderr_io) { StringIO.new }
28
+
29
+ let(:expected_cookbook_file_relpaths) do
30
+ %w[
31
+ .gitignore
32
+ .kitchen.yml
33
+ test
34
+ test/integration
35
+ test/integration/default
36
+ test/integration/default/serverspec
37
+ test/integration/default/serverspec/default_spec.rb
38
+ test/integration/helpers/serverspec/spec_helper.rb
39
+ README.md
40
+ cookbooks/new_app/Berksfile
41
+ cookbooks/new_app/chefignore
42
+ cookbooks/new_app/metadata.rb
43
+ cookbooks/new_app/recipes
44
+ cookbooks/new_app/recipes/default.rb
45
+ cookbooks/new_app/spec
46
+ cookbooks/new_app/spec/spec_helper.rb
47
+ cookbooks/new_app/spec/unit
48
+ cookbooks/new_app/spec/unit/recipes
49
+ cookbooks/new_app/spec/unit/recipes/default_spec.rb
50
+ ]
51
+ end
52
+
53
+ let(:expected_cookbook_files) do
54
+ expected_cookbook_file_relpaths.map do |relpath|
55
+ File.join(tempdir, "new_app", relpath)
56
+ end
57
+ end
58
+
59
+ subject(:cookbook_generator) { described_class.new(argv) }
60
+
61
+ def generator_context
62
+ ChefDK::Generator.context
63
+ end
64
+
65
+ before do
66
+ ChefDK::Generator.reset
67
+ end
68
+
69
+ include_examples "custom generator cookbook" do
70
+
71
+ let(:generator_arg) { "new_app" }
72
+
73
+ let(:generator_name) { "app" }
74
+
75
+ end
76
+
77
+ context "when given the name of the cookbook to generate" do
78
+
79
+ before do
80
+ reset_tempdir
81
+ end
82
+
83
+ it "configures the generator context" do
84
+ cookbook_generator.read_and_validate_params
85
+ cookbook_generator.setup_context
86
+ expect(generator_context.app_root).to eq(Dir.pwd)
87
+ expect(generator_context.app_name).to eq("new_app")
88
+ expect(generator_context.cookbook_root).to eq(File.join(Dir.pwd, "new_app", "cookbooks"))
89
+ expect(generator_context.cookbook_name).to eq("new_app")
90
+ expect(generator_context.recipe_name).to eq("default")
91
+ end
92
+
93
+ describe "generated files" do
94
+ it "creates a new cookbook" do
95
+ Dir.chdir(tempdir) do
96
+ allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
97
+ cookbook_generator.run
98
+ end
99
+ generated_files = Dir.glob(File.join(tempdir, "new_app", "**", "*"), File::FNM_DOTMATCH)
100
+ expected_cookbook_files.each do |expected_file|
101
+ expect(generated_files).to include(expected_file)
102
+ end
103
+ end
104
+
105
+ shared_examples_for "a generated file" do |context_var|
106
+ before do
107
+ Dir.chdir(tempdir) do
108
+ allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
109
+ cookbook_generator.run
110
+ end
111
+ end
112
+
113
+ it "should contain #{context_var} from the generator context" do
114
+ expect(File.read(file)).to match line
115
+ end
116
+ end
117
+
118
+ describe "README.md" do
119
+ let(:file) { File.join(tempdir, "new_app", "README.md") }
120
+
121
+ include_examples "a generated file", :cookbook_name do
122
+ let(:line) { "# new_app" }
123
+ end
124
+ end
125
+
126
+ describe ".kitchen.yml" do
127
+ let(:file) { File.join(tempdir, "new_app", ".kitchen.yml") }
128
+
129
+ include_examples "a generated file", :cookbook_name do
130
+ let(:line) { /\s*- recipe\[new_app::default\]/ }
131
+ end
132
+ end
133
+
134
+ describe "test/integration/default/serverspec/default_spec.rb" do
135
+ let(:file) { File.join(tempdir, "new_app", "test", "integration", "default", "serverspec", "default_spec.rb") }
136
+
137
+ include_examples "a generated file", :cookbook_name do
138
+ let(:line) { "describe 'new_app::default' do" }
139
+ end
140
+ end
141
+
142
+ describe "cookbooks/new_app/metadata.rb" do
143
+ let(:file) { File.join(tempdir, "new_app", "cookbooks", "new_app", "metadata.rb") }
144
+
145
+ include_examples "a generated file", :cookbook_name do
146
+ let(:line) { /name\s+'new_app'/ }
147
+ end
148
+ end
149
+
150
+ describe "cookbooks/new_app/recipes/default.rb" do
151
+ let(:file) { File.join(tempdir, "new_app", "cookbooks", "new_app", "recipes", "default.rb") }
152
+
153
+ include_examples "a generated file", :cookbook_name do
154
+ let(:line) { "# Cookbook Name:: new_app" }
155
+ end
156
+ end
157
+
158
+ describe "cookbooks/new_app/spec/unit/recipes/default_spec.rb" do
159
+ let(:file) { File.join(tempdir, "new_app", "cookbooks", "new_app", "spec", "unit", "recipes", "default_spec.rb") }
160
+
161
+ include_examples "a generated file", :cookbook_name do
162
+ let(:line) { "describe \'new_app::default\' do" }
163
+ end
164
+ end
165
+
166
+ end
167
+ end
168
+ end
169
+
@@ -17,10 +17,14 @@
17
17
 
18
18
  require 'spec_helper'
19
19
  require 'shared/custom_generator_cookbook'
20
+ require 'shared/setup_git_committer_config'
20
21
  require 'chef-dk/command/generator_commands/build_cookbook'
22
+ require 'mixlib/shellout'
21
23
 
22
24
  describe ChefDK::Command::GeneratorCommands::BuildCookbook do
23
25
 
26
+ include_context("setup_git_committer_config")
27
+
24
28
  let(:argv) { %w[delivery_project] }
25
29
 
26
30
  let(:stdout_io) { StringIO.new }
@@ -125,7 +129,7 @@ describe ChefDK::Command::GeneratorCommands::BuildCookbook do
125
129
  it "creates a build cookbook" do
126
130
  Dir.chdir(tempdir) do
127
131
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
128
- cookbook_generator.run
132
+ expect(cookbook_generator.run).to eq(0)
129
133
  end
130
134
  generated_files = Dir.glob("#{tempdir}/delivery_project/**/*", File::FNM_DOTMATCH)
131
135
  expected_cookbook_files.each do |expected_file|
@@ -137,7 +141,7 @@ describe ChefDK::Command::GeneratorCommands::BuildCookbook do
137
141
  before do
138
142
  Dir.chdir(tempdir) do
139
143
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
140
- cookbook_generator.run
144
+ expect(cookbook_generator.run).to eq(0)
141
145
  end
142
146
  end
143
147
 
@@ -155,7 +159,7 @@ describe ChefDK::Command::GeneratorCommands::BuildCookbook do
155
159
  before do
156
160
  Dir.chdir(tempdir) do
157
161
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
158
- cookbook_generator.run
162
+ expect(cookbook_generator.run).to eq(0)
159
163
  end
160
164
  end
161
165
 
@@ -207,7 +211,7 @@ METADATA
207
211
  before do
208
212
  Dir.chdir(tempdir) do
209
213
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
210
- cookbook_generator.run
214
+ expect(cookbook_generator.run).to eq(0)
211
215
  end
212
216
  end
213
217
 
@@ -264,7 +268,7 @@ METADATA
264
268
  before do
265
269
  Dir.chdir(tempdir) do
266
270
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
267
- cookbook_generator.run
271
+ expect(cookbook_generator.run).to eq(0)
268
272
  end
269
273
  end
270
274
 
@@ -289,6 +293,36 @@ METADATA
289
293
  end
290
294
 
291
295
  end
296
+
297
+ context "when the delivery project is a git repo" do
298
+
299
+ let(:readme) { File.join(project_dir, "README.md") }
300
+
301
+ def git!(cmd)
302
+ Mixlib::ShellOut.new("git #{cmd}", cwd: project_dir).tap do |c|
303
+ c.run_command
304
+ c.error!
305
+ end
306
+ end
307
+
308
+ before do
309
+ FileUtils.touch(readme)
310
+
311
+ git!("init .")
312
+ git!("add .")
313
+ git!("commit -m \"initial commit\"")
314
+
315
+ Dir.chdir(tempdir) do
316
+ allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
317
+ expect(cookbook_generator.run).to eq(0)
318
+ end
319
+ end
320
+
321
+ it "creates delivery config in a feature branch and merges it" do
322
+ expect(git!("log").stdout).to include("Merge branch 'add-delivery-configuration'")
323
+ end
324
+
325
+ end
292
326
  end
293
327
 
294
328
  context "when given a path including the .delivery directory" do
@@ -17,10 +17,13 @@
17
17
 
18
18
  require 'spec_helper'
19
19
  require 'shared/custom_generator_cookbook'
20
+ require 'shared/setup_git_committer_config'
20
21
  require 'chef-dk/command/generator_commands/cookbook'
21
22
 
22
23
  describe ChefDK::Command::GeneratorCommands::Cookbook do
23
24
 
25
+ include_context("setup_git_committer_config")
26
+
24
27
  let(:argv) { %w[new_cookbook] }
25
28
 
26
29
  let(:stdout_io) { StringIO.new }
@@ -59,6 +62,7 @@ describe ChefDK::Command::GeneratorCommands::Cookbook do
59
62
  subject(:cookbook_generator) do
60
63
  g = described_class.new(argv)
61
64
  allow(g).to receive(:cookbook_path_in_git_repo?).and_return(false)
65
+ allow(g).to receive(:stdout).and_return(stdout_io)
62
66
  g
63
67
  end
64
68
 
@@ -129,7 +133,7 @@ describe ChefDK::Command::GeneratorCommands::Cookbook do
129
133
  it "creates a new cookbook" do
130
134
  Dir.chdir(tempdir) do
131
135
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
132
- cookbook_generator.run
136
+ expect(cookbook_generator.run).to eq(0)
133
137
  end
134
138
  generated_files = Dir.glob("#{tempdir}/new_cookbook/**/*", File::FNM_DOTMATCH)
135
139
  expected_cookbook_files.each do |expected_file|
@@ -141,7 +145,7 @@ describe ChefDK::Command::GeneratorCommands::Cookbook do
141
145
  before do
142
146
  Dir.chdir(tempdir) do
143
147
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
144
- cookbook_generator.run
148
+ expect(cookbook_generator.run).to eq(0)
145
149
  end
146
150
  end
147
151
 
@@ -164,17 +168,21 @@ describe ChefDK::Command::GeneratorCommands::Cookbook do
164
168
 
165
169
  describe "Generating Test Kitchen and integration testing files" do
166
170
 
167
- before do
168
- Dir.chdir(tempdir) do
169
- allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
170
- cookbook_generator.run
171
+ describe "generating kitchen config" do
172
+
173
+ before do
174
+ Dir.chdir(tempdir) do
175
+ allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
176
+ expect(cookbook_generator.run).to eq(0)
177
+ end
171
178
  end
172
- end
173
179
 
174
- let(:file) { File.join(tempdir, "new_cookbook", ".kitchen.yml") }
180
+ let(:file) { File.join(tempdir, "new_cookbook", ".kitchen.yml") }
181
+
182
+ it "creates a .kitchen.yml with the expected content" do
183
+ expect(IO.read(file)).to eq(expected_kitchen_yml_content)
184
+ end
175
185
 
176
- it "creates a .kitchen.yml with the expected content" do
177
- expect(IO.read(file)).to eq(expected_kitchen_yml_content)
178
186
  end
179
187
 
180
188
  describe "test/integration/default/serverspec/default_spec.rb" do
@@ -196,7 +204,7 @@ describe ChefDK::Command::GeneratorCommands::Cookbook do
196
204
  before do
197
205
  Dir.chdir(tempdir) do
198
206
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
199
- cookbook_generator.run
207
+ expect(cookbook_generator.run).to eq(0)
200
208
  end
201
209
  end
202
210
 
@@ -242,7 +250,7 @@ POLICYFILE_RB
242
250
  before do
243
251
  Dir.chdir(tempdir) do
244
252
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
245
- cookbook_generator.run
253
+ expect(cookbook_generator.run).to eq(0)
246
254
  end
247
255
  end
248
256
 
@@ -326,7 +334,7 @@ POLICYFILE_RB
326
334
  before do
327
335
  Dir.chdir(tempdir) do
328
336
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
329
- cookbook_generator.run
337
+ expect(cookbook_generator.run).to eq(0)
330
338
  end
331
339
  end
332
340
 
@@ -389,7 +397,7 @@ SPEC_HELPER
389
397
  before do
390
398
  Dir.chdir(tempdir) do
391
399
  allow(cookbook_generator.chef_runner).to receive(:stdout).and_return(stdout_io)
392
- cookbook_generator.run
400
+ expect(cookbook_generator.run).to eq(0)
393
401
  end
394
402
  end
395
403