puppet-retrospec 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +52 -0
  3. data/.gitlab-ci.yml +1 -0
  4. data/.overcommit.yml +33 -0
  5. data/.rubocop.yml +17 -3
  6. data/.rubocop_todo.yml +744 -0
  7. data/CHANGELOG.md +3 -1
  8. data/Gemfile +1 -4
  9. data/Gemfile.lock +34 -62
  10. data/README.md +2 -2
  11. data/Rakefile +3 -21
  12. data/lib/retrospec/plugins/v1/plugin/generators/fact_generator.rb +5 -5
  13. data/lib/retrospec/plugins/v1/plugin/generators/function_generator.rb +62 -18
  14. data/lib/retrospec/plugins/v1/plugin/generators/parsers/native_function.rb +58 -0
  15. data/lib/retrospec/plugins/v1/plugin/puppet.rb +1 -1
  16. data/lib/retrospec/plugins/v1/plugin/version.rb +1 -1
  17. data/puppet-retrospec.gemspec +5 -1648
  18. data/spec/fixtures/fixture_modules/sample_module/lib/puppet/functions/awesome_parser.rb +14 -0
  19. data/spec/fixtures/functions/abs.pp +4 -0
  20. data/spec/unit/generators/acceptance_generator_spec.rb +10 -14
  21. data/spec/unit/generators/definition_generator_spec.rb +4 -8
  22. data/spec/unit/generators/fact_generater_spec.rb +6 -7
  23. data/spec/unit/generators/function_generator_spec.rb +83 -17
  24. data/spec/unit/generators/function_spec.rb +1 -3
  25. data/spec/unit/generators/hostclass_generator_spec.rb +7 -10
  26. data/spec/unit/generators/node_generator_spec.rb +4 -7
  27. data/spec/unit/generators/parsers/fact_spec.rb +7 -10
  28. data/spec/unit/generators/parsers/provider_spec.rb +5 -5
  29. data/spec/unit/generators/parsers/type_spec.rb +8 -9
  30. data/spec/unit/generators/provider_generator_spec.rb +1 -2
  31. data/spec/unit/generators/report_generator_spec.rb +5 -7
  32. data/spec/unit/generators/resource_base_generator_spec.rb +3 -5
  33. data/spec/unit/generators/schema_generator_spec.rb +28 -32
  34. data/spec/unit/generators/serializers/rspec_dumper_spec.rb +35 -38
  35. data/spec/unit/generators/type_generator_spec.rb +116 -116
  36. data/spec/unit/plugin_spec.rb +15 -22
  37. data/spec/unit/puppet-retrospec_spec.rb +3 -5
  38. data/spec/unit/spec_object_spec.rb +17 -20
  39. metadata +9 -4
@@ -1,5 +1,19 @@
1
1
  # extremely helpful documentation
2
2
  # https://github.com/puppetlabs/puppet-specifications/blob/master/language/func-api.md#the-4x-api
3
+
4
+ # Example Documention for function using puppet strings
5
+ # https://github.com/puppetlabs/puppetlabs-strings
6
+ # When given two numbers, returns the one that is larger.
7
+ # You could have a several line description here if you wanted,
8
+ # but I don't have much to say about this function.
9
+ #
10
+ # @example using two integers
11
+ # $bigger_int = max(int_one, int_two)
12
+ #
13
+ # @return [Integer] the larger of the two parameters
14
+ #
15
+ # @param num_a [Integer] the first number to be compared
16
+ # @param num_b [Integer] the second number to be compared
3
17
  Puppet::Functions.create_function(:awesome_parser) do
4
18
  # the function below is called by puppet and and must match
5
19
  # the name of the puppet function above. You can set your
@@ -0,0 +1,4 @@
1
+ function abs(Integer $x) {
2
+ if $x < 0 { $x * -1 }
3
+ else { $x }
4
+ }
@@ -1,17 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Retrospec::Puppet::Generators::AcceptanceGenerator do
4
-
5
4
  after(:each) do
6
- FileUtils.rm_rf(spec_files_path) if File.exists?(spec_files_path)
5
+ FileUtils.rm_rf(spec_files_path) if File.exist?(spec_files_path)
7
6
  end
8
7
 
9
8
  let(:sample_file) do
10
- File.join(module_path, 'manifests','one_define.pp')
9
+ File.join(module_path, 'manifests', 'one_define.pp')
11
10
  end
12
11
 
13
12
  let(:generator_opts) do
14
- {:manifest_file => sample_file, :template_dir => retrospec_templates_path}
13
+ { :manifest_file => sample_file, :template_dir => retrospec_templates_path }
15
14
  end
16
15
 
17
16
  let(:spec_files_path) do
@@ -31,14 +30,13 @@ describe Retrospec::Puppet::Generators::AcceptanceGenerator do
31
30
  end
32
31
 
33
32
  describe 'valid context' do
34
-
35
33
  describe 'define' do
36
34
  let(:spec_file) do
37
- path = File.join(module_path, 'spec', 'acceptance', 'classes','one_define_spec.rb')
35
+ path = File.join(module_path, 'spec', 'acceptance', 'classes', 'one_define_spec.rb')
38
36
  end
39
37
 
40
38
  let(:sample_file) do
41
- File.join(module_path, 'manifests','one_define.pp')
39
+ File.join(module_path, 'manifests', 'one_define.pp')
42
40
  end
43
41
  let(:context) do
44
42
  generator.load_context_data
@@ -65,7 +63,7 @@ describe Retrospec::Puppet::Generators::AcceptanceGenerator do
65
63
 
66
64
  it 'should create spec file' do
67
65
  expect(generator.run).to eq(spec_file)
68
- expect(File.exists?(spec_file)).to eq(true)
66
+ expect(File.exist?(spec_file)).to eq(true)
69
67
  end
70
68
 
71
69
  it 'should produce correct file name' do
@@ -76,15 +74,14 @@ describe Retrospec::Puppet::Generators::AcceptanceGenerator do
76
74
  data = "require 'spec_helper_acceptance'\n\ndescribe 'one_resource::one_define one_resource::one_define' do\n describe 'running puppet code' do\n it 'should work with no errors' do\n pp = <<-EOS\n one_resource::one_define{'some_value':\n #:one => \"one_value\",\n\n }\n EOS\n\n # Run it twice and test for idempotency\n apply_manifest(pp, :catch_failures => true)\n apply_manifest(pp, :catch_changes => true)\n end\n\n end\nend\n"
77
75
  expect(spec_file_contents).to eq(data)
78
76
  end
79
-
80
77
  end
81
78
  describe 'class' do
82
79
  let(:spec_file) do
83
- path = File.join(module_path, 'spec', 'acceptance', 'classes','another_resource_spec.rb')
80
+ path = File.join(module_path, 'spec', 'acceptance', 'classes', 'another_resource_spec.rb')
84
81
  end
85
82
 
86
83
  let(:sample_file) do
87
- File.join(module_path, 'manifests','another_resource.pp')
84
+ File.join(module_path, 'manifests', 'another_resource.pp')
88
85
  end
89
86
  let(:context) do
90
87
  generator.load_context_data
@@ -109,7 +106,7 @@ describe Retrospec::Puppet::Generators::AcceptanceGenerator do
109
106
 
110
107
  it 'should create spec file' do
111
108
  expect(generator.run).to eq(spec_file)
112
- expect(File.exists?(spec_file)).to eq(true)
109
+ expect(File.exist?(spec_file)).to eq(true)
113
110
  end
114
111
 
115
112
  it 'should produce correct file name' do
@@ -121,7 +118,6 @@ describe Retrospec::Puppet::Generators::AcceptanceGenerator do
121
118
  expect(spec_file_contents).to eq(data)
122
119
  end
123
120
  end
124
-
125
121
  end
126
122
 
127
123
  describe 'spec files' do
@@ -131,7 +127,7 @@ describe Retrospec::Puppet::Generators::AcceptanceGenerator do
131
127
  File.join(spec_files_path, 'inherits_params_spec.rb'),
132
128
  File.join(spec_files_path, 'one_define_spec.rb'),
133
129
  File.join(spec_files_path, 'one_resource_spec.rb'),
134
- File.join(spec_files_path, 'params_spec.rb'),
130
+ File.join(spec_files_path, 'params_spec.rb')
135
131
 
136
132
  ]
137
133
  end
@@ -1,17 +1,16 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Retrospec::Puppet::Generators::DefinitionGenerator do
4
-
5
4
  after(:each) do
6
- FileUtils.rm(spec_file) if File.exists?(spec_file)
5
+ FileUtils.rm(spec_file) if File.exist?(spec_file)
7
6
  end
8
7
 
9
8
  let(:generator_opts) do
10
- {:manifest_file => sample_file, :template_dir => retrospec_templates_path}
9
+ { :manifest_file => sample_file, :template_dir => retrospec_templates_path }
11
10
  end
12
11
 
13
12
  let(:sample_file) do
14
- File.join(module_path, 'manifests','one_define.pp')
13
+ File.join(module_path, 'manifests', 'one_define.pp')
15
14
  end
16
15
 
17
16
  let(:context) do
@@ -40,7 +39,7 @@ describe Retrospec::Puppet::Generators::DefinitionGenerator do
40
39
 
41
40
  it 'should create spec file' do
42
41
  expect(generator.run).to eq(spec_file)
43
- expect(File.exists?(spec_file)).to eq(true)
42
+ expect(File.exist?(spec_file)).to eq(true)
44
43
  end
45
44
 
46
45
  it 'should produce correct file name' do
@@ -79,10 +78,8 @@ describe Retrospec::Puppet::Generators::DefinitionGenerator do
79
78
  expect(spec_file_contents).to match(data)
80
79
  expect(spec_file_contents).to match(/#:one => "one_value",/)
81
80
  end
82
-
83
81
  end
84
82
 
85
-
86
83
  describe 'spec files' do
87
84
  let(:generated_files) do
88
85
  [File.join(spec_files_path, 'one_define_spec.rb')]
@@ -92,5 +89,4 @@ describe Retrospec::Puppet::Generators::DefinitionGenerator do
92
89
  expect(files).to eq(generated_files)
93
90
  end
94
91
  end
95
-
96
92
  end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "fact generator" do
4
-
3
+ describe 'fact generator' do
5
4
  before :all do
6
5
  initialize_templates
7
6
  end
@@ -23,11 +22,11 @@ describe "fact generator" do
23
22
  end
24
23
 
25
24
  let(:context) do
26
- {:name => 'datacenter', :template_dir => retrospec_templates_path }
25
+ { :name => 'datacenter', :template_dir => retrospec_templates_path }
27
26
  end
28
27
 
29
28
  let(:generator) do
30
- Retrospec::Puppet::Generators::FactGenerator.new(module_path, context )
29
+ Retrospec::Puppet::Generators::FactGenerator.new(module_path, context)
31
30
  end
32
31
 
33
32
  describe :datacenter do
@@ -37,7 +36,7 @@ describe "fact generator" do
37
36
  end
38
37
 
39
38
  let(:context) do
40
- {:name => 'datacenter', :template_dir => retrospec_templates_path}
39
+ { :name => 'datacenter', :template_dir => retrospec_templates_path }
41
40
  end
42
41
  it 'returns facter dir' do
43
42
  expect(generator.facter_dir).to eq(fixtures_facts_path)
@@ -53,7 +52,7 @@ describe "fact generator" do
53
52
 
54
53
  it 'can generate a fact file' do
55
54
  expect(generator.generate_fact_file.count).to eq(6)
56
- expect(File.exists?(File.join(generator.facter_dir, "#{generator.fact_name}.rb")))
55
+ expect(File.exist?(File.join(generator.facter_dir, "#{generator.fact_name}.rb")))
57
56
  end
58
57
 
59
58
  it 'can generate a spec file' do
@@ -68,7 +67,7 @@ describe "fact generator" do
68
67
  end
69
68
 
70
69
  let(:context) do
71
- {:name => 'oracle_controls',:template_dir => retrospec_templates_path}
70
+ { :name => 'oracle_controls', :template_dir => retrospec_templates_path }
72
71
  end
73
72
 
74
73
  it 'can generate a spec file' do
@@ -28,6 +28,10 @@ describe 'function_generator' do
28
28
  retrospec_templates_path
29
29
  end
30
30
 
31
+ let(:native_function_file) do
32
+ File.join(fixtures_path, 'functions', 'abs.pp')
33
+ end
34
+
31
35
  let(:function_name) do
32
36
  'awesome_parser'
33
37
  end
@@ -63,11 +67,9 @@ describe 'function_generator' do
63
67
 
64
68
  it 'generate spec files' do
65
69
  files = [File.join(generator.v3_spec_dir, 'defined_spec.rb'),
66
- File.join(generator.v3_spec_dir, 'sha1_spec.rb'),
67
- File.join(generator.v4_spec_dir, 'reduce_spec.rb'),
68
- File.join(generator.v4_spec_dir, 'awesome_parser_spec.rb')
69
-
70
- ]
70
+ File.join(generator.v3_spec_dir, 'sha1_spec.rb'),
71
+ File.join(generator.v4_spec_dir, 'reduce_spec.rb'),
72
+ File.join(generator.v4_spec_dir, 'awesome_parser_spec.rb')]
71
73
  expect(generator.generate_spec_files).to match_array(files)
72
74
  end
73
75
 
@@ -77,7 +79,7 @@ describe 'function_generator' do
77
79
  File.join(module_path, 'lib', 'puppet', 'functions', 'reduce.rb'),
78
80
  File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'bad_sha1.rb'),
79
81
  File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'defined.rb'),
80
- File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'sha1.rb'),
82
+ File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'sha1.rb')
81
83
  ]
82
84
  expect(generator.discovered_functions).to match_array(files)
83
85
  end
@@ -118,14 +120,19 @@ describe 'function_generator' do
118
120
 
119
121
  it 'return true when v3 function' do
120
122
  path = File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'bad_sha1.rb')
121
- expect(generator.is_v3_function?(path)).to eq (true)
123
+ expect(generator.v3_function?(path)).to eq true
122
124
  end
123
125
 
124
126
  it 'return v3 template path when context is changed' do
125
127
  generator.template_dir # uses v4
126
128
  generator.context.function_type = 'v3'
127
129
  expect(generator.template_dir).to match(/functions\/v3/)
130
+ end
128
131
 
132
+ it 'return false when v3 function' do
133
+ path = File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'reduce.rb')
134
+ expect(generator.v4_function?(path)).to eq false
135
+ expect(generator.native_function?(path)).to eq false
129
136
  end
130
137
  end
131
138
 
@@ -140,7 +147,8 @@ describe 'function_generator' do
140
147
 
141
148
  it 'return false when v4 function' do
142
149
  path = File.join(module_path, 'lib', 'puppet', 'functions', 'reduce.rb')
143
- expect(generator.is_v3_function?(path)).to eq (false)
150
+ expect(generator.v3_function?(path)).to eq false
151
+ expect(generator.native_function?(path)).to eq false
144
152
  end
145
153
 
146
154
  it 'return v4 template path' do
@@ -190,11 +198,9 @@ describe 'function_generator' do
190
198
 
191
199
  it 'generate spec files' do
192
200
  files = [File.join(generator.v3_spec_dir, 'defined_spec.rb'),
193
- File.join(generator.v3_spec_dir, 'sha1_spec.rb'),
194
- File.join(generator.v4_spec_dir, 'reduce_spec.rb'),
195
- File.join(generator.v4_spec_dir, 'awesome_parser_spec.rb')
196
-
197
- ]
201
+ File.join(generator.v3_spec_dir, 'sha1_spec.rb'),
202
+ File.join(generator.v4_spec_dir, 'reduce_spec.rb'),
203
+ File.join(generator.v4_spec_dir, 'awesome_parser_spec.rb')]
198
204
  expect(generator.generate_spec_files).to match_array(files)
199
205
  end
200
206
 
@@ -204,7 +210,7 @@ describe 'function_generator' do
204
210
  File.join(module_path, 'lib', 'puppet', 'functions', 'reduce.rb'),
205
211
  File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'bad_sha1.rb'),
206
212
  File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'defined.rb'),
207
- File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'sha1.rb'),
213
+ File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'sha1.rb')
208
214
  ]
209
215
  expect(generator.discovered_functions).to match_array(files)
210
216
  end
@@ -245,7 +251,7 @@ describe 'function_generator' do
245
251
 
246
252
  it 'return true when v3 function' do
247
253
  path = File.join(module_path, 'lib', 'puppet', 'parser', 'functions', 'bad_sha1.rb')
248
- expect(generator.is_v3_function?(path)).to eq (true)
254
+ expect(generator.v3_function?(path)).to eq true
249
255
  end
250
256
 
251
257
  it 'return v3 template path when context is changed' do
@@ -266,7 +272,7 @@ describe 'function_generator' do
266
272
 
267
273
  it 'return false when v4 function' do
268
274
  path = File.join(module_path, 'lib', 'puppet', 'functions', 'reduce.rb')
269
- expect(generator.is_v3_function?(path)).to eq (false)
275
+ expect(generator.v3_function?(path)).to eq false
270
276
  end
271
277
 
272
278
  it 'return v4 template path' do
@@ -299,7 +305,67 @@ describe 'function_generator' do
299
305
  expect(generator.generate_function_file).to eq(function_path)
300
306
  end
301
307
  end
302
- end
303
308
 
309
+ describe 'native' do
310
+ let(:type_name) do
311
+ 'native'
312
+ end
313
+
314
+ let(:function_path) do
315
+ File.join(module_path, 'functions', "#{function_name}.pp")
316
+ end
317
+
318
+ it 'return false when native function' do
319
+ path = File.join(module_path, 'functions', 'reduce.pp')
320
+ expect(generator.v3_function?(path)).to eq false
321
+ expect(generator.v4_function?(path)).to eq false
322
+ end
323
+
324
+ it 'return native template path' do
325
+ allow(generator).to receive(:function_type).and_return('native')
326
+ expect(generator.template_dir).to match(/functions\/native/)
327
+ end
328
+
329
+ it 'return v4 template path when context is changed' do
330
+ generator.template_dir
331
+ generator.context.function_type = 'native'
332
+ expect(generator.template_dir).to match(/functions\/native/)
333
+ end
334
+
335
+ it 'returns function path' do
336
+ path = File.join(module_path, 'functions', "#{function_name}.pp")
337
+ expect(generator.function_path).to eq(path)
338
+ end
339
+
340
+ it 'returns spec file directory' do
341
+ path = File.join(module_path, 'spec', 'functions')
342
+ expect(generator.spec_file_dir).to eq(path)
343
+ end
344
+
345
+ it 'returns function directory' do
346
+ path = File.join(module_path, 'functions')
347
+ expect(generator.function_dir).to eq(path)
348
+ end
349
+
350
+ it 'generate spec file' do
351
+ allow(generator).to receive(:discovered_functions).and_return([native_function_file])
352
+ path = File.join(generator.module_path, 'spec', 'functions', 'abs_spec.rb')
353
+ expect(generator.generate_spec_files).to eq([path])
354
+ end
355
+
356
+ it 'generate spec file content' do
357
+ allow(generator).to receive(:discovered_functions).and_return([native_function_file])
358
+ path = File.join(generator.module_path, 'spec', 'functions', 'abs_spec.rb')
359
+ expect(generator.generate_spec_files).to eq([path])
360
+ test_file_content = File.read(path)
361
+ result = "require 'spec_helper'\n\nlet(:x) do\n 'some_value_goes_here'\nend\n\ndescribe 'abs' do\n it { is_expected.to run.with_params(x).and_return('some_value') }\nend\n"
362
+ expect(test_file_content).to eq(result)
363
+ end
304
364
 
365
+ it 'generate function file path' do
366
+ file = File.join(generator.template_dir, 'function_template.pp.retrospec.erb')
367
+ expect(generator.function_file_path).to eq file
368
+ end
369
+ end
305
370
  end
371
+ end
@@ -1,7 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'function' do
4
-
5
4
  describe 'v3 function' do
6
5
  let(:function_file) do
7
6
  File.join(sample_module_path, 'lib', 'puppet', 'parser', 'functions', 'sha1.rb')
@@ -23,7 +22,7 @@ describe 'function' do
23
22
  File.join(sample_module_path, 'lib', 'puppet', 'parser', 'functions', 'bad_sha1.rb')
24
23
  end
25
24
  it 'should raise error' do
26
- expect{models.name}.to raise_error SyntaxError
25
+ expect { models.name }.to raise_error SyntaxError
27
26
  end
28
27
  end
29
28
  end
@@ -62,6 +61,5 @@ describe 'function' do
62
61
  it 'returns required methods' do
63
62
  expect(Retrospec::Puppet::Functions.find_required_methods(:reduce)).to eq([:reduce])
64
63
  end
65
-
66
64
  end
67
65
  end
@@ -1,9 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe 'HostClassGenerator' do
4
-
5
4
  before(:each) do
6
- FileUtils.rm(spec_file) if File.exists?(spec_file)
5
+ FileUtils.rm(spec_file) if File.exist?(spec_file)
7
6
  end
8
7
 
9
8
  let(:spec_files_path) do
@@ -11,7 +10,7 @@ describe 'HostClassGenerator' do
11
10
  end
12
11
 
13
12
  let(:generator_opts) do
14
- {:manifest_file => sample_file, :template_dir => retrospec_templates_path}
13
+ { :manifest_file => sample_file, :template_dir => retrospec_templates_path }
15
14
  end
16
15
 
17
16
  let(:sample_file) do
@@ -40,7 +39,7 @@ describe 'HostClassGenerator' do
40
39
 
41
40
  it 'should create spec file' do
42
41
  expect(generator.run).to eq(spec_file)
43
- expect(File.exists?(spec_file)).to eq(true)
42
+ expect(File.exist?(spec_file)).to eq(true)
44
43
  end
45
44
 
46
45
  it 'should produce correct file name' do
@@ -72,16 +71,15 @@ describe 'HostClassGenerator' do
72
71
  resources = ''
73
72
 
74
73
  expect(context.resources[1]).to eq(resources)
75
- expect(context.resources[0]).to eq( " it do\n is_expected.to contain_notify(\"$value\")\n end\n ")
74
+ expect(context.resources[0]).to eq(" it do\n is_expected.to contain_notify(\"$value\")\n end\n ")
76
75
  end
77
76
 
78
77
  describe 'spec files' do
79
-
80
78
  let(:generated_files) do
81
79
  [File.join(spec_files_path, 'another_resource_spec.rb'),
82
- File.join(spec_files_path, 'inherits_params_spec.rb'),
83
- File.join(spec_files_path, 'one_resource_spec.rb'),
84
- File.join(spec_files_path, 'params_spec.rb')]
80
+ File.join(spec_files_path, 'inherits_params_spec.rb'),
81
+ File.join(spec_files_path, 'one_resource_spec.rb'),
82
+ File.join(spec_files_path, 'params_spec.rb')]
85
83
  end
86
84
 
87
85
  it 'should generate a bunch of files' do
@@ -100,5 +98,4 @@ describe 'HostClassGenerator' do
100
98
  expect(generator.generate_content).to eq(data)
101
99
  end
102
100
  end
103
-
104
101
  end