chef-gen-flavors 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +2 -0
  3. data/.rubocop.yml +23 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock +242 -0
  7. data/Guardfile +15 -0
  8. data/History.md +10 -0
  9. data/LICENSE +15 -0
  10. data/Manifest.txt +46 -0
  11. data/README.md +311 -0
  12. data/Rakefile +85 -0
  13. data/chef-gen-flavors.gemspec +75 -0
  14. data/lib/chef_gen/flavor.rb +5 -0
  15. data/lib/chef_gen/flavor_base.rb +230 -0
  16. data/lib/chef_gen/flavors.rb +173 -0
  17. data/lib/chef_gen/snippet/attributes.rb +22 -0
  18. data/lib/chef_gen/snippet/chef_spec.rb +41 -0
  19. data/lib/chef_gen/snippet/cookbook_base.rb +64 -0
  20. data/lib/chef_gen/snippet/example_file.rb +23 -0
  21. data/lib/chef_gen/snippet/example_template.rb +25 -0
  22. data/lib/chef_gen/snippet/recipes.rb +22 -0
  23. data/lib/chef_gen/snippet/resource_provider.rb +24 -0
  24. data/lib/chef_gen/snippet/standard_ignore.rb +97 -0
  25. data/lib/chef_gen/snippet/style_rubocop.rb +14 -0
  26. data/lib/chef_gen/snippet/test_kitchen.rb +51 -0
  27. data/lib/chef_gen/snippets.rb +15 -0
  28. data/spec/lib/chef_gen/flavor_base_spec.rb +164 -0
  29. data/spec/lib/chef_gen/flavors_spec.rb +114 -0
  30. data/spec/lib/chef_gen/snippet/attributes_spec.rb +45 -0
  31. data/spec/lib/chef_gen/snippet/chef_spec_spec.rb +45 -0
  32. data/spec/lib/chef_gen/snippet/cookbook_base_spec.rb +37 -0
  33. data/spec/lib/chef_gen/snippet/example_file_spec.rb +45 -0
  34. data/spec/lib/chef_gen/snippet/example_template_spec.rb +45 -0
  35. data/spec/lib/chef_gen/snippet/recipes_spec.rb +45 -0
  36. data/spec/lib/chef_gen/snippet/resource_provider_spec.rb +45 -0
  37. data/spec/lib/chef_gen/snippet/standard_ignore_spec.rb +40 -0
  38. data/spec/lib/chef_gen/snippet/style_rubocop_spec.rb +36 -0
  39. data/spec/lib/chef_gen/snippet/test_kitchen_spec.rb +49 -0
  40. data/spec/spec_helper.rb +38 -0
  41. data/spec/support/fixtures/code_generator/metadata.rb +2 -0
  42. data/spec/support/fixtures/code_generator/recipes/cookbook.rb +1 -0
  43. data/spec/support/fixtures/code_generator_2/metadata.rb +2 -0
  44. data/spec/support/fixtures/code_generator_2/recipes/cookbook.rb +1 -0
  45. data/spec/support/fixtures/lib/chef_gen/flavor/bar.rb +21 -0
  46. data/spec/support/fixtures/lib/chef_gen/flavor/baz.rb +6 -0
  47. data/spec/support/fixtures/lib/chef_gen/flavor/foo.rb +9 -0
  48. metadata +317 -0
@@ -0,0 +1,114 @@
1
+ require 'chef_gen/flavors'
2
+
3
+ # sample plugins
4
+ require 'support/fixtures/lib/chef_gen/flavor/foo'
5
+ require 'support/fixtures/lib/chef_gen/flavor/bar'
6
+ require 'support/fixtures/lib/chef_gen/flavor/baz'
7
+
8
+ RSpec.describe ChefGen::Flavors do
9
+ before do
10
+ ChefGen::Flavors.clear_plugins
11
+ ENV.delete('CHEFGEN_FLAVOR')
12
+ ENV.delete('CHEFDK_FLAVOR')
13
+ @orig_stdout = $stdout
14
+ $stdout = File.open(File::NULL, 'w')
15
+ end
16
+
17
+ after do
18
+ $stdout = @orig_stdout
19
+ end
20
+
21
+ it 'should be able to load plugins' do
22
+ expect(ChefGen::Flavors.plugins).to be_a(Hash)
23
+ end
24
+
25
+ it 'should load the expected plugins' do
26
+ ChefGen::Flavors.disregard_plugin :amazing, :awesome
27
+ expect(ChefGen::Flavors.plugins.keys).to(
28
+ contain_exactly(:foo, :bar, :baz)
29
+ )
30
+ end
31
+
32
+ it 'allows for the plugin list to be make explicit' do
33
+ ChefGen::Flavors.plugin :foo
34
+ expect(ChefGen::Flavors.plugins).to include(:foo)
35
+ expect(ChefGen::Flavors.plugins).not_to include(:bar)
36
+ end
37
+
38
+ it 'allows for plugins to be blacklisted' do
39
+ ChefGen::Flavors.disregard_plugin :foo, :baz
40
+ expect(ChefGen::Flavors.plugins).not_to include(:foo)
41
+ expect(ChefGen::Flavors.plugins).to include(:bar)
42
+ end
43
+
44
+ it 'should not prompt for a plugin if only one is available' do
45
+ ChefGen::Flavors.plugin :foo
46
+ expect(ChefGen::Flavors).not_to receive(:prompt_for_plugin)
47
+ ChefGen::Flavors.path
48
+ end
49
+
50
+ it 'should not prompt for a plugin if the env var matches one' do
51
+ ChefGen::Flavors.disregard_plugin :baz
52
+ ENV['CHEFGEN_FLAVOR'] = 'Foo'
53
+ expect(ChefGen::Flavors).not_to receive(:prompt_for_plugin)
54
+ ChefGen::Flavors.path
55
+ end
56
+
57
+ it 'should be case insensitive when selecting the plugin via env var' do
58
+ ChefGen::Flavors.disregard_plugin :baz
59
+ ENV['CHEFGEN_FLAVOR'] = 'foo'
60
+ expect(ChefGen::Flavors).not_to receive(:prompt_for_plugin)
61
+ ChefGen::Flavors.path
62
+ ENV.delete('CHEFGEN_FLAVOR')
63
+ end
64
+
65
+ it 'should prompt for a plugin if the env var does not match' do
66
+ ChefGen::Flavors.disregard_plugin :baz
67
+ ENV['CHEFGEN_FLAVOR'] = 'Gzonk'
68
+ expect(ChefGen::Flavors)
69
+ .to receive(:prompt_for_plugin)
70
+ .and_return(:foo)
71
+ ChefGen::Flavors.path
72
+ end
73
+
74
+ it 'should prompt for a plugin if the env var is not set' do
75
+ ChefGen::Flavors.disregard_plugin :baz
76
+ expect(ChefGen::Flavors)
77
+ .to receive(:prompt_for_plugin)
78
+ .and_return(:bar)
79
+ ChefGen::Flavors.path
80
+ end
81
+
82
+ it 'should raise an error if there are no plugins available' do
83
+ ChefGen::Flavors.disregard_plugin :foo, :bar, :baz,
84
+ :amazing, :awesome
85
+ expect { ChefGen::Flavors.path }.to raise_error
86
+ end
87
+
88
+ it 'should offer the builtin flavors as an option with the env var set' do
89
+ ENV['CHEFDK_FLAVOR'] = 'true'
90
+ ChefGen::Flavors.disregard_plugin :foo, :bar, :baz,
91
+ :amazing, :awesome
92
+ expect(ChefGen::Flavors).not_to receive(:prompt_for_plugin)
93
+ ChefGen::Flavors.path
94
+ end
95
+
96
+ it 'should raise an error if the plugin has no description' do
97
+ ENV['CHEFGEN_FLAVOR'] = 'Baz'
98
+ expect { ChefGen::Flavors.path }.to raise_error
99
+ end
100
+
101
+ it 'should default the code_generator path' do
102
+ ChefGen::Flavors.disregard_plugin :baz
103
+ ENV['CHEFGEN_FLAVOR'] = 'Foo'
104
+ expect(ChefGen::Flavors.path)
105
+ .to match(%r{spec/support/fixtures/code_generator$})
106
+ end
107
+
108
+ it 'should respect an overridden code_generator path' do
109
+ ChefGen::Flavors.disregard_plugin :baz
110
+ ENV['CHEFGEN_FLAVOR'] = 'Bar'
111
+ expect(ChefGen::Flavors.path)
112
+ .to match(%r{spec/support/fixtures/code_generator_2$})
113
+ end
114
+ end
@@ -0,0 +1,45 @@
1
+ require 'chef_gen/flavor_base'
2
+ require 'chef_gen/snippets'
3
+
4
+ module ChefGen
5
+ module Flavor
6
+ class Awesome < FlavorBase
7
+ include ChefGen::Snippet::Attributes
8
+
9
+ class << self
10
+ # :nocov:
11
+ def description
12
+ 'my awesome template'
13
+ end
14
+ # :nocov:
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ # rubocop:disable Style/RegexpLiteral
21
+ RSpec.describe ChefGen::Snippet::Attributes do
22
+ before do
23
+ @ctx = double('ChefDK generator context')
24
+ allow(@ctx).to receive(:cookbook_root).and_return('/nonexistent')
25
+ allow(@ctx).to receive(:cookbook_name).and_return('foo')
26
+ allow(ChefDK::Generator).to receive(:context).and_return(@ctx)
27
+ @recipe = double('Chef recipe').as_null_object
28
+ end
29
+
30
+ %w(attributes).each do |dname|
31
+ it "should create the directory #{dname}" do
32
+ expect(@recipe).to receive(:directory).with(%r{#{dname}$})
33
+ template = ChefGen::Flavor::Awesome.new(@recipe)
34
+ template.generate
35
+ end
36
+ end
37
+
38
+ %w(attributes/default.rb).each do |fname|
39
+ it "should add a template for #{fname}" do
40
+ expect(@recipe).to receive(:template).with(%r{#{fname}$})
41
+ template = ChefGen::Flavor::Awesome.new(@recipe)
42
+ template.generate
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,45 @@
1
+ require 'chef_gen/flavor_base'
2
+ require 'chef_gen/snippets'
3
+
4
+ module ChefGen
5
+ module Flavor
6
+ class Awesome < FlavorBase
7
+ include ChefGen::Snippet::ChefSpec
8
+
9
+ class << self
10
+ # :nocov:
11
+ def description
12
+ 'my awesome template'
13
+ end
14
+ # :nocov:
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ # rubocop:disable Style/RegexpLiteral
21
+ RSpec.describe ChefGen::Snippet::ChefSpec do
22
+ before do
23
+ @ctx = double('ChefDK generator context')
24
+ allow(@ctx).to receive(:cookbook_root).and_return('/nonexistent')
25
+ allow(@ctx).to receive(:cookbook_name).and_return('foo')
26
+ allow(ChefDK::Generator).to receive(:context).and_return(@ctx)
27
+ @recipe = double('Chef recipe').as_null_object
28
+ end
29
+
30
+ %w(spec spec/recipes).each do |dname|
31
+ it "should create the directory #{dname}" do
32
+ expect(@recipe).to receive(:directory).with(%r{#{dname}$})
33
+ template = ChefGen::Flavor::Awesome.new(@recipe)
34
+ template.generate
35
+ end
36
+ end
37
+
38
+ %w(.rspec spec/spec_helper.rb spec/recipes/default_spec.rb).each do |fname|
39
+ it "should add a template for #{fname}" do
40
+ expect(@recipe).to receive(:template).with(%r{#{fname}$})
41
+ template = ChefGen::Flavor::Awesome.new(@recipe)
42
+ template.generate
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,37 @@
1
+ require 'chef_gen/flavor_base'
2
+ require 'chef_gen/snippets'
3
+
4
+ module ChefGen
5
+ module Flavor
6
+ class Awesome < FlavorBase
7
+ include ChefGen::Snippet::CookbookBase
8
+
9
+ class << self
10
+ # :nocov:
11
+ def description
12
+ 'my awesome template'
13
+ end
14
+ # :nocov:
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ RSpec.describe ChefGen::Snippet::CookbookBase do
21
+ before do
22
+ @ctx = double('ChefDK generator context')
23
+ allow(@ctx).to receive(:cookbook_root).and_return('/nonexistent')
24
+ allow(@ctx).to receive(:cookbook_name).and_return('foo')
25
+ allow(ChefDK::Generator).to receive(:context).and_return(@ctx)
26
+ @recipe = double('Chef recipe').as_null_object
27
+ end
28
+
29
+ %w(Gemfile Rakefile Berksfile Guardfile README.md
30
+ CHANGELOG.md metadata.rb).each do |fname|
31
+ it "should add a template for #{fname}" do
32
+ expect(@recipe).to receive(:template).with(/#{fname}$/)
33
+ template = ChefGen::Flavor::Awesome.new(@recipe)
34
+ template.generate
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,45 @@
1
+ require 'chef_gen/flavor_base'
2
+ require 'chef_gen/snippets'
3
+
4
+ module ChefGen
5
+ module Flavor
6
+ class Awesome < FlavorBase
7
+ include ChefGen::Snippet::ExampleFile
8
+
9
+ class << self
10
+ # :nocov:
11
+ def description
12
+ 'my awesome template'
13
+ end
14
+ # :nocov:
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ # rubocop:disable Style/RegexpLiteral
21
+ RSpec.describe ChefGen::Snippet::ExampleFile do
22
+ before do
23
+ @ctx = double('ChefDK generator context')
24
+ allow(@ctx).to receive(:cookbook_root).and_return('/nonexistent')
25
+ allow(@ctx).to receive(:cookbook_name).and_return('foo')
26
+ allow(ChefDK::Generator).to receive(:context).and_return(@ctx)
27
+ @recipe = double('Chef recipe').as_null_object
28
+ end
29
+
30
+ %w(files files/default).each do |dname|
31
+ it "should create the directory #{dname}" do
32
+ expect(@recipe).to receive(:directory).with(%r{#{dname}$})
33
+ template = ChefGen::Flavor::Awesome.new(@recipe)
34
+ template.generate
35
+ end
36
+ end
37
+
38
+ %w(files/default/example.conf).each do |fname|
39
+ it "should add a template for #{fname}" do
40
+ expect(@recipe).to receive(:cookbook_file).with(%r{#{fname}$})
41
+ template = ChefGen::Flavor::Awesome.new(@recipe)
42
+ template.generate
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,45 @@
1
+ require 'chef_gen/flavor_base'
2
+ require 'chef_gen/snippets'
3
+
4
+ module ChefGen
5
+ module Flavor
6
+ class Awesome < FlavorBase
7
+ include ChefGen::Snippet::ExampleTemplate
8
+
9
+ class << self
10
+ # :nocov:
11
+ def description
12
+ 'my awesome template'
13
+ end
14
+ # :nocov:
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ # rubocop:disable Style/RegexpLiteral
21
+ RSpec.describe ChefGen::Snippet::ExampleTemplate do
22
+ before do
23
+ @ctx = double('ChefDK generator context')
24
+ allow(@ctx).to receive(:cookbook_root).and_return('/nonexistent')
25
+ allow(@ctx).to receive(:cookbook_name).and_return('foo')
26
+ allow(ChefDK::Generator).to receive(:context).and_return(@ctx)
27
+ @recipe = double('Chef recipe').as_null_object
28
+ end
29
+
30
+ %w(templates templates/default).each do |dname|
31
+ it "should create the directory #{dname}" do
32
+ expect(@recipe).to receive(:directory).with(%r{#{dname}$})
33
+ template = ChefGen::Flavor::Awesome.new(@recipe)
34
+ template.generate
35
+ end
36
+ end
37
+
38
+ %w(templates/default/example.conf.erb).each do |fname|
39
+ it "should add a template for #{fname}" do
40
+ expect(@recipe).to receive(:cookbook_file).with(%r{#{fname}$})
41
+ template = ChefGen::Flavor::Awesome.new(@recipe)
42
+ template.generate
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,45 @@
1
+ require 'chef_gen/flavor_base'
2
+ require 'chef_gen/snippets'
3
+
4
+ module ChefGen
5
+ module Flavor
6
+ class Awesome < FlavorBase
7
+ include ChefGen::Snippet::Recipes
8
+
9
+ class << self
10
+ # :nocov:
11
+ def description
12
+ 'my awesome template'
13
+ end
14
+ # :nocov:
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ # rubocop:disable Style/RegexpLiteral
21
+ RSpec.describe ChefGen::Snippet::Recipes do
22
+ before do
23
+ @ctx = double('ChefDK generator context')
24
+ allow(@ctx).to receive(:cookbook_root).and_return('/nonexistent')
25
+ allow(@ctx).to receive(:cookbook_name).and_return('foo')
26
+ allow(ChefDK::Generator).to receive(:context).and_return(@ctx)
27
+ @recipe = double('Chef recipe').as_null_object
28
+ end
29
+
30
+ %w(recipes).each do |dname|
31
+ it "should create the directory #{dname}" do
32
+ expect(@recipe).to receive(:directory).with(%r{#{dname}$})
33
+ template = ChefGen::Flavor::Awesome.new(@recipe)
34
+ template.generate
35
+ end
36
+ end
37
+
38
+ %w(recipes/default.rb).each do |fname|
39
+ it "should add a template for #{fname}" do
40
+ expect(@recipe).to receive(:template).with(%r{#{fname}$})
41
+ template = ChefGen::Flavor::Awesome.new(@recipe)
42
+ template.generate
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,45 @@
1
+ require 'chef_gen/flavor_base'
2
+ require 'chef_gen/snippets'
3
+
4
+ module ChefGen
5
+ module Flavor
6
+ class Awesome < FlavorBase
7
+ include ChefGen::Snippet::ResourceProvider
8
+
9
+ class << self
10
+ # :nocov:
11
+ def description
12
+ 'my awesome template'
13
+ end
14
+ # :nocov:
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ # rubocop:disable Style/RegexpLiteral
21
+ RSpec.describe ChefGen::Snippet::ResourceProvider do
22
+ before do
23
+ @ctx = double('ChefDK generator context')
24
+ allow(@ctx).to receive(:cookbook_root).and_return('/nonexistent')
25
+ allow(@ctx).to receive(:cookbook_name).and_return('foo')
26
+ allow(ChefDK::Generator).to receive(:context).and_return(@ctx)
27
+ @recipe = double('Chef recipe').as_null_object
28
+ end
29
+
30
+ %w(resources providers).each do |dname|
31
+ it "should create the directory #{dname}" do
32
+ expect(@recipe).to receive(:directory).with(%r{#{dname}$})
33
+ template = ChefGen::Flavor::Awesome.new(@recipe)
34
+ template.generate
35
+ end
36
+ end
37
+
38
+ %w(resources/default.rb providers/default.rb).each do |fname|
39
+ it "should add a template for #{fname}" do
40
+ expect(@recipe).to receive(:template).with(%r{#{fname}$})
41
+ template = ChefGen::Flavor::Awesome.new(@recipe)
42
+ template.generate
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,40 @@
1
+ require 'chef_gen/flavor_base'
2
+ require 'chef_gen/snippets'
3
+
4
+ module ChefGen
5
+ module Flavor
6
+ class Awesome < FlavorBase
7
+ include ChefGen::Snippet::StandardIgnore
8
+
9
+ class << self
10
+ # :nocov:
11
+ def description
12
+ 'my awesome template'
13
+ end
14
+ # :nocov:
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ RSpec.describe ChefGen::Snippet::StandardIgnore do
21
+ before do
22
+ @ctx = double('ChefDK generator context')
23
+ allow(@ctx).to receive(:cookbook_root).and_return('/nonexistent')
24
+ allow(@ctx).to receive(:cookbook_name).and_return('foo')
25
+ allow(ChefDK::Generator).to receive(:context).and_return(@ctx)
26
+ @recipe = double('Chef recipe').as_null_object
27
+ end
28
+
29
+ it 'should create a chefignore file' do
30
+ expect(@recipe).to receive(:file).with(/chefignore$/)
31
+ template = ChefGen::Flavor::Awesome.new(@recipe)
32
+ template.generate
33
+ end
34
+
35
+ it 'should create a .gitignore file' do
36
+ expect(@recipe).to receive(:file).with(/\.gitignore$/)
37
+ template = ChefGen::Flavor::Awesome.new(@recipe)
38
+ template.generate
39
+ end
40
+ end