rspec-puppet 2.11.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +154 -469
  3. data/README.md +71 -168
  4. data/bin/rspec-puppet-init +4 -3
  5. data/lib/rspec-puppet/adapters.rb +84 -230
  6. data/lib/rspec-puppet/cache.rb +3 -2
  7. data/lib/rspec-puppet/consts.rb +16 -14
  8. data/lib/rspec-puppet/coverage.rb +37 -42
  9. data/lib/rspec-puppet/errors.rb +6 -6
  10. data/lib/rspec-puppet/example/class_example_group.rb +3 -1
  11. data/lib/rspec-puppet/example/define_example_group.rb +3 -1
  12. data/lib/rspec-puppet/example/function_example_group.rb +42 -57
  13. data/lib/rspec-puppet/example/host_example_group.rb +3 -1
  14. data/lib/rspec-puppet/example/provider_example_group.rb +2 -0
  15. data/lib/rspec-puppet/example/type_alias_example_group.rb +4 -2
  16. data/lib/rspec-puppet/example/type_example_group.rb +3 -1
  17. data/lib/rspec-puppet/example.rb +6 -9
  18. data/lib/rspec-puppet/facter_impl.rb +11 -10
  19. data/lib/rspec-puppet/matchers/allow_value.rb +10 -10
  20. data/lib/rspec-puppet/matchers/compile.rb +54 -61
  21. data/lib/rspec-puppet/matchers/count_generic.rb +18 -18
  22. data/lib/rspec-puppet/matchers/create_generic.rb +69 -79
  23. data/lib/rspec-puppet/matchers/dynamic_matchers.rb +15 -4
  24. data/lib/rspec-puppet/matchers/include_class.rb +5 -4
  25. data/lib/rspec-puppet/matchers/parameter_matcher.rb +11 -12
  26. data/lib/rspec-puppet/matchers/raise_error.rb +5 -1
  27. data/lib/rspec-puppet/matchers/run.rb +41 -42
  28. data/lib/rspec-puppet/matchers/type_matchers.rb +37 -48
  29. data/lib/rspec-puppet/matchers.rb +2 -0
  30. data/lib/rspec-puppet/monkey_patches/win32/registry.rb +7 -5
  31. data/lib/rspec-puppet/monkey_patches/win32/taskscheduler.rb +3 -1
  32. data/lib/rspec-puppet/monkey_patches/windows/taskschedulerconstants.rb +208 -205
  33. data/lib/rspec-puppet/monkey_patches.rb +80 -93
  34. data/lib/rspec-puppet/rake_task.rb +6 -4
  35. data/lib/rspec-puppet/raw_string.rb +2 -0
  36. data/lib/rspec-puppet/sensitive.rb +31 -39
  37. data/lib/rspec-puppet/setup.rb +45 -56
  38. data/lib/rspec-puppet/spec_helper.rb +2 -3
  39. data/lib/rspec-puppet/support.rb +129 -177
  40. data/lib/rspec-puppet/tasks/release_test.rb +8 -5
  41. data/lib/rspec-puppet/version.rb +5 -0
  42. data/lib/rspec-puppet.rb +37 -61
  43. metadata +18 -15
  44. data/lib/rspec-puppet/example/application_example_group.rb +0 -19
@@ -1,35 +1,32 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'pathname'
2
4
 
3
5
  # Load this library before enabling the monkey-patches to avoid HI-581
4
6
  begin
5
- require 'hiera/util/win32'
7
+ require 'hiera/util/win32'
6
8
  rescue LoadError
7
9
  # ignore this on installs without hiera, e.g. puppet 3 gems
8
10
  end
9
11
 
10
12
  class RSpec::Puppet::EventListener
11
13
  def self.example_started(example)
12
- if rspec3?
13
- @rspec_puppet_example = example.example.example_group.ancestors.include?(RSpec::Puppet::Support)
14
- @current_example = example.example
15
- if !@current_example.respond_to?(:environment) && @current_example.respond_to?(:example_group_instance)
16
- @current_example = @current_example.example_group_instance
17
- end
18
- else
19
- @rspec_puppet_example = example.example_group.ancestors.include?(RSpec::Puppet::Support)
20
- @current_example = example
21
- end
14
+ @rspec_puppet_example = example.example.example_group.ancestors.include?(RSpec::Puppet::Support)
15
+ @current_example = example.example
16
+ return unless !@current_example.respond_to?(:environment) && @current_example.respond_to?(:example_group_instance)
17
+
18
+ @current_example = @current_example.example_group_instance
22
19
  end
23
20
 
24
- def self.example_passed(example)
21
+ def self.example_passed(_example)
25
22
  @rspec_puppet_example = false
26
23
  end
27
24
 
28
- def self.example_pending(example)
25
+ def self.example_pending(_example)
29
26
  @rspec_puppet_example = false
30
27
  end
31
28
 
32
- def self.example_failed(example)
29
+ def self.example_failed(_example)
33
30
  @rspec_puppet_example = false
34
31
  end
35
32
 
@@ -37,20 +34,13 @@ class RSpec::Puppet::EventListener
37
34
  @rspec_puppet_example || false
38
35
  end
39
36
 
40
- def self.rspec3?
41
- if @rspec3.nil?
42
- @rspec3 = defined?(RSpec::Core::Notifications)
43
- end
44
-
45
- @rspec3
46
- end
47
-
48
- def self.current_example
49
- @current_example
37
+ class << self
38
+ attr_reader :current_example
50
39
  end
51
40
  end
52
41
 
53
- RSpec.configuration.reporter.register_listener(RSpec::Puppet::EventListener, :example_started, :example_pending, :example_passed, :example_failed)
42
+ RSpec.configuration.reporter.register_listener(RSpec::Puppet::EventListener, :example_started, :example_pending,
43
+ :example_passed, :example_failed)
54
44
 
55
45
  require 'rspec-puppet/monkey_patches/win32/taskscheduler'
56
46
  require 'rspec-puppet/monkey_patches/win32/registry'
@@ -84,24 +74,20 @@ module Puppet
84
74
  end
85
75
  end
86
76
 
87
- retval = old_set_default.bind(self).call(attr)
77
+ retval = old_set_default.bind_call(self, attr)
88
78
 
89
- unless old_posix.nil?
90
- Puppet.features.add(:posix) { old_posix }
91
- end
92
- unless old_microsoft_windows.nil?
93
- Puppet.features.add(:microsoft_windows) { old_microsoft_windows }
94
- end
79
+ Puppet.features.add(:posix) { old_posix } unless old_posix.nil?
80
+ Puppet.features.add(:microsoft_windows) { old_microsoft_windows } unless old_microsoft_windows.nil?
95
81
 
96
82
  retval
97
83
  else
98
- old_set_default.bind(self).call(attr)
84
+ old_set_default.bind_call(self, attr)
99
85
  end
100
86
  end
101
87
  end
102
88
 
103
89
  module Parser::Files
104
- alias :old_find_manifests_in_modules :find_manifests_in_modules
90
+ alias old_find_manifests_in_modules find_manifests_in_modules
105
91
  module_function :old_find_manifests_in_modules
106
92
 
107
93
  def find_manifests_in_modules(pattern, environment)
@@ -113,8 +99,9 @@ module Puppet
113
99
  RSpec::Puppet::Consts.stub_consts_for(RSpec.configuration.platform)
114
100
  end
115
101
 
116
- if pretending && pretending != Puppet::Util::Platform.actual_platform
117
- environment.send(:value_cache).clear if environment.respond_to?(:value_cache, true)
102
+ if pretending && pretending != Puppet::Util::Platform.actual_platform && environment.respond_to?(:value_cache,
103
+ true)
104
+ environment.send(:value_cache).clear
118
105
  end
119
106
  output = old_find_manifests_in_modules(pattern, environment)
120
107
 
@@ -135,30 +122,30 @@ module Puppet
135
122
  # Fix for removal of default_env function
136
123
  # Bug: https://github.com/rodjek/rspec-puppet/issues/796
137
124
  # Upstream: https://github.com/puppetlabs/puppet/commit/94df3c1a3992d89b2d7d5db8a70373c135bdd86b
138
- if !respond_to?(:default_env)
139
- def default_env()
125
+ unless respond_to?(:default_env)
126
+ def default_env
140
127
  DEFAULT_ENV
141
128
  end
142
129
  module_function :default_env
143
130
  end
144
131
 
145
132
  if respond_to?(:get_env)
146
- alias :old_get_env :get_env
133
+ alias old_get_env get_env
147
134
  module_function :old_get_env
148
135
 
149
136
  def get_env(name, mode = default_env)
150
137
  if RSpec::Puppet.rspec_puppet_example?
151
138
  # use the actual platform, not the pretended
152
- old_get_env(name, Platform.actual_platform)
139
+ old_get_env(name, Platform.actual_platform)
153
140
  else
154
- old_get_env(name, mode)
141
+ old_get_env(name, mode)
155
142
  end
156
143
  end
157
144
  module_function :get_env
158
145
  end
159
146
 
160
147
  if respond_to?(:path_to_uri)
161
- alias :old_path_to_uri :path_to_uri
148
+ alias old_path_to_uri path_to_uri
162
149
  module_function :old_path_to_uri
163
150
 
164
151
  def path_to_uri(*args)
@@ -175,12 +162,12 @@ module Puppet
175
162
 
176
163
  # Allow rspec-puppet to pretend to be different platforms.
177
164
  module Platform
178
- alias :old_windows? :windows?
165
+ alias old_windows? windows?
179
166
  module_function :old_windows?
180
167
 
181
168
  def windows?
182
169
  if RSpec::Puppet.rspec_puppet_example?
183
- !pretending? ? (actual_platform == :windows) : pretend_windows?
170
+ pretending? ? pretend_windows? : (actual_platform == :windows)
184
171
  else
185
172
  old_windows?
186
173
  end
@@ -249,7 +236,7 @@ module Puppet
249
236
  if RSpec::Puppet.rspec_puppet_example?
250
237
  true
251
238
  else
252
- old_pass.bind(self).call(value)
239
+ old_pass.bind_call(self, value)
253
240
  end
254
241
  end
255
242
  end
@@ -263,43 +250,46 @@ module Puppet
263
250
  if RSpec::Puppet.rspec_puppet_example?
264
251
  true
265
252
  else
266
- old_pass.bind(self).call(value)
253
+ old_pass.bind_call(self, value)
267
254
  end
268
255
  end
269
256
  end
270
257
  end
271
258
 
272
- if Puppet::Util::Package.versioncmp(Puppet.version, '4.9.0') >= 0
273
- class Module
274
- old_hiera_conf_file = instance_method(:hiera_conf_file)
275
- define_method(:hiera_conf_file) do
276
- if RSpec::Puppet.rspec_puppet_example?
277
- if RSpec.configuration.disable_module_hiera
278
- return nil
279
- elsif RSpec.configuration.fixture_hiera_configs.key?(name)
280
- config = RSpec.configuration.fixture_hiera_configs[name]
281
- config = File.absolute_path(config, path) unless config.nil?
282
- return config
283
- elsif RSpec.configuration.use_fixture_spec_hiera
284
- config = RSpec::Puppet.current_example.fixture_spec_hiera_conf(self)
285
- return config unless config.nil? && RSpec.configuration.fallback_to_default_hiera
286
- end
259
+ class Module
260
+ old_hiera_conf_file = instance_method(:hiera_conf_file)
261
+ define_method(:hiera_conf_file) do
262
+ if RSpec::Puppet.rspec_puppet_example?
263
+ if RSpec.configuration.disable_module_hiera
264
+ return nil
265
+ elsif RSpec.configuration.fixture_hiera_configs.key?(name)
266
+ config = RSpec.configuration.fixture_hiera_configs[name]
267
+ config = File.absolute_path(config, path) unless config.nil?
268
+ return config
269
+ elsif RSpec.configuration.use_fixture_spec_hiera
270
+ config = RSpec::Puppet.current_example.fixture_spec_hiera_conf(self)
271
+ return config unless config.nil? && RSpec.configuration.fallback_to_default_hiera
287
272
  end
288
- old_hiera_conf_file.bind(self).call
289
273
  end
274
+ old_hiera_conf_file.bind_call(self)
290
275
  end
276
+ end
291
277
 
292
- class Pops::Lookup::ModuleDataProvider
293
- old_configuration_path = instance_method(:configuration_path)
294
- define_method(:configuration_path) do |lookup_invocation|
295
- if RSpec::Puppet.rspec_puppet_example?
296
- env = lookup_invocation.scope.environment
297
- mod = env.module(module_name)
298
- raise Puppet::DataBinding::LookupError, _("Environment '%{env}', cannot find module '%{module_name}'") % { :env => env.name, :module_name => module_name } unless mod
299
- return Pathname.new(mod.hiera_conf_file)
278
+ class Pops::Lookup::ModuleDataProvider
279
+ old_configuration_path = instance_method(:configuration_path)
280
+ define_method(:configuration_path) do |lookup_invocation|
281
+ if RSpec::Puppet.rspec_puppet_example?
282
+ env = lookup_invocation.scope.environment
283
+ mod = env.module(module_name)
284
+ unless mod
285
+ raise Puppet::DataBinding::LookupError,
286
+ format(_("Environment '%<env>s', cannot find module '%<module_name>s'"), env: env.name,
287
+ module_name: module_name)
300
288
  end
301
- old_configuration_path.bind(self).call(lookup_invocation)
289
+
290
+ return Pathname.new(mod.hiera_conf_file)
302
291
  end
292
+ old_configuration_path.bind_call(self, lookup_invocation)
303
293
  end
304
294
  end
305
295
  end
@@ -308,29 +298,26 @@ class Pathname
308
298
  def rspec_puppet_basename(path)
309
299
  raise ArgumentError, 'pathname stubbing not enabled' unless RSpec.configuration.enable_pathname_stubbing
310
300
 
311
- if path =~ /\A[a-zA-Z]:(#{SEPARATOR_PAT}.*)\z/
312
- path = path[2..-1]
313
- end
301
+ path = path[2..-1] if /\A[a-zA-Z]:(#{SEPARATOR_PAT}.*)\z/.match?(path)
314
302
  path.split(SEPARATOR_PAT).last || path[/(#{SEPARATOR_PAT})/, 1] || path
315
303
  end
316
304
 
317
- if instance_methods.include?("chop_basename")
305
+ if instance_methods.include?('chop_basename')
318
306
  old_chop_basename = instance_method(:chop_basename)
319
307
 
320
308
  define_method(:chop_basename) do |path|
321
309
  if RSpec::Puppet.rspec_puppet_example?
322
310
  if RSpec.configuration.enable_pathname_stubbing
323
311
  base = rspec_puppet_basename(path)
324
- if /\A#{SEPARATOR_PAT}?\z/o =~ base
325
- return nil
326
- else
327
- return path[0, path.rindex(base)], base
328
- end
312
+ return nil if /\A#{SEPARATOR_PAT}?\z/o.match?(base)
313
+
314
+ [path[0, path.rindex(base)], base]
315
+
329
316
  else
330
- old_chop_basename.bind(self).call(path)
317
+ old_chop_basename.bind_call(self, path)
331
318
  end
332
319
  else
333
- old_chop_basename.bind(self).call(path)
320
+ old_chop_basename.bind_call(self, path)
334
321
  end
335
322
  end
336
323
  end
@@ -345,10 +332,8 @@ class Puppet::Module
345
332
  old_match_manifests = instance_method(:match_manifests)
346
333
 
347
334
  define_method(:match_manifests) do |rest|
348
- result = old_match_manifests.bind(self).call(rest)
349
- if result.length > 1 && File.basename(result[0]) == 'init.pp'
350
- result.shift
351
- end
335
+ result = old_match_manifests.bind_call(self, rest)
336
+ result.shift if result.length > 1 && File.basename(result[0]) == 'init.pp'
352
337
  result
353
338
  end
354
339
  end
@@ -361,7 +346,7 @@ Puppet::Type.type(:file).paramclass(:path).munge do |value|
361
346
  if RSpec::Puppet.rspec_puppet_example?
362
347
  value
363
348
  else
364
- file_path_munge.bind(self).call(value)
349
+ file_path_munge.bind_call(self, value)
365
350
  end
366
351
  end
367
352
 
@@ -373,7 +358,7 @@ Puppet::Type.type(:exec).paramclass(:user).validate do |value|
373
358
  if RSpec::Puppet.rspec_puppet_example?
374
359
  true
375
360
  else
376
- exec_user_validate.bind(self).call(value)
361
+ exec_user_validate.bind_call(self, value)
377
362
  end
378
363
  end
379
364
 
@@ -383,11 +368,11 @@ end
383
368
  Puppet::Type.type(:file).provide(:windows).class_eval do
384
369
  old_supports_acl = instance_method(:supports_acl?) if respond_to?(:supports_acl?)
385
370
 
386
- def supports_acl?(path)
371
+ def supports_acl?(_path)
387
372
  if RSpec::Puppet.rspec_puppet_example?
388
373
  true
389
374
  else
390
- old_supports_acl.bind(self).call(value)
375
+ old_supports_acl.bind_call(self, value)
391
376
  end
392
377
  end
393
378
 
@@ -397,7 +382,7 @@ Puppet::Type.type(:file).provide(:windows).class_eval do
397
382
  if RSpec::Puppet.rspec_puppet_example?
398
383
  true
399
384
  else
400
- old_manages_symlinks.bind(self).call(value)
385
+ old_manages_symlinks.bind_call(self, value)
401
386
  end
402
387
  end
403
388
  end
@@ -406,9 +391,11 @@ end
406
391
  # windows, otherwise it will require other libraries that probably won't be
407
392
  # available on non-windows hosts.
408
393
  module Kernel
409
- alias :old_require :require
394
+ alias old_require require
410
395
  def require(path)
411
- return if (['puppet/util/windows', 'win32/registry'].include?(path)) && RSpec::Puppet.rspec_puppet_example? && Puppet::Util::Platform.pretend_windows?
396
+ return if ['puppet/util/windows',
397
+ 'win32/registry'].include?(path) && RSpec::Puppet.rspec_puppet_example? && Puppet::Util::Platform.pretend_windows?
398
+
412
399
  old_require(path)
413
400
  end
414
401
  end
@@ -1,19 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rake'
2
4
  require 'rspec/core/rake_task'
3
5
 
4
- desc "Run all RSpec code examples"
6
+ desc 'Run all RSpec code examples'
5
7
  RSpec::Core::RakeTask.new(:rspec) do |t|
6
- File.exist?('spec/spec.opts') ? opts = File.read("spec/spec.opts").chomp : opts = ""
8
+ opts = File.exist?('spec/spec.opts') ? File.read('spec/spec.opts').chomp : ''
7
9
  t.rspec_opts = opts
8
10
  end
9
11
 
10
- SPEC_SUITES = (Dir.entries('spec') - ['.', '..','fixtures']).select {|e| File.directory? "spec/#{e}" }
12
+ SPEC_SUITES = (Dir.entries('spec') - ['.', '..', 'fixtures']).select { |e| File.directory? "spec/#{e}" }
11
13
  namespace :rspec do
12
14
  SPEC_SUITES.each do |suite|
13
15
  desc "Run #{suite} RSpec code examples"
14
16
  RSpec::Core::RakeTask.new(suite) do |t|
15
17
  t.pattern = "spec/#{suite}/**/*_spec.rb"
16
- File.exist?('spec/spec.opts') ? opts = File.read("spec/spec.opts").chomp : opts = ""
18
+ opts = File.exist?('spec/spec.opts') ? File.read('spec/spec.opts').chomp : ''
17
19
  t.rspec_opts = opts
18
20
  end
19
21
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RSpec::Puppet
2
4
  # A raw string object, that is used by helpers to allow consumers to return non-quoted strings
3
5
  # as part of their params section.
@@ -1,51 +1,43 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module RSpec::Puppet
2
- if defined?(::Puppet::Pops::Types::PSensitiveType::Sensitive)
3
- # A wrapper representing Sensitive data type, eg. in class params.
4
- class Sensitive < ::Puppet::Pops::Types::PSensitiveType::Sensitive
5
- # Create a new Sensitive object
6
- # @param [Object] value to wrap
7
- def initialize(value)
8
- @value = value
9
- end
4
+ # A wrapper representing Sensitive data type, eg. in class params.
5
+ class Sensitive < ::Puppet::Pops::Types::PSensitiveType::Sensitive
6
+ # Create a new Sensitive object
7
+ # @param [Object] value to wrap
8
+ def initialize(value)
9
+ @value = value
10
+ end
10
11
 
11
- # @return the wrapped value
12
- def unwrap
13
- @value
14
- end
12
+ # @return the wrapped value
13
+ def unwrap
14
+ @value
15
+ end
15
16
 
16
- # @return true
17
- def sensitive?
18
- true
19
- end
17
+ # @return true
18
+ def sensitive?
19
+ true
20
+ end
20
21
 
21
- # @return inspect of the wrapped value, inside Sensitive()
22
- def inspect
23
- "Sensitive(#{@value.inspect})"
24
- end
22
+ # @return inspect of the wrapped value, inside Sensitive()
23
+ def inspect
24
+ "Sensitive(#{@value.inspect})"
25
+ end
25
26
 
26
- # Check for equality with another value.
27
- # If compared to Puppet Sensitive type, it compares the wrapped values.
27
+ # Check for equality with another value.
28
+ # If compared to Puppet Sensitive type, it compares the wrapped values.
28
29
 
29
- # @param other [#unwrap, Object] value to compare to
30
- def == other
31
- if other.respond_to? :unwrap
32
- if unwrap.kind_of?(Regexp)
33
- return unwrap =~ other.unwrap
34
- else
35
- return unwrap == other.unwrap
36
- end
30
+ # @param other [#unwrap, Object] value to compare to
31
+ def ==(other)
32
+ if other.respond_to? :unwrap
33
+ if unwrap.is_a?(Regexp)
34
+ unwrap =~ other.unwrap
37
35
  else
38
- super
36
+ unwrap == other.unwrap
39
37
  end
38
+ else
39
+ super
40
40
  end
41
41
  end
42
- else
43
- #:nocov:
44
- class Sensitive
45
- def initialize(value)
46
- raise 'The use of the Sensitive data type is not supported by this Puppet version'
47
- end
48
- end
49
- #:nocov:
50
42
  end
51
43
  end
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'English'
1
4
  require 'puppet'
2
5
  require 'fileutils'
3
6
 
@@ -9,17 +12,17 @@ end
9
12
 
10
13
  module RSpec::Puppet
11
14
  class Setup
12
- def self.run(module_name=nil)
15
+ def self.run(module_name = nil)
13
16
  unless is_module_dir?
14
- $stderr.puts "Does not appear to be a Puppet module. Aborting"
17
+ warn 'Does not appear to be a Puppet module. Aborting'
15
18
  return false
16
19
  end
17
20
 
18
21
  if control_repo?
19
- $stderr.puts <<-END
20
- Unable to find a metadata.json file. If this is a module, please create a
21
- metadata.json file and try again.
22
- END
22
+ warn <<~END
23
+ Unable to find a metadata.json file. If this is a module, please create a
24
+ metadata.json file and try again.
25
+ END
23
26
  return false
24
27
  end
25
28
 
@@ -30,16 +33,16 @@ END
30
33
  safe_create_rakefile
31
34
  end
32
35
 
33
- def self.safe_setup_directories(module_name=nil, verbose=true)
36
+ def self.safe_setup_directories(module_name = nil, verbose = true)
34
37
  if control_repo?
35
- $stderr.puts "Unable to setup rspec-puppet automatically in a control repo" if verbose
38
+ warn 'Unable to setup rspec-puppet automatically in a control repo' if verbose
36
39
  return false
37
40
  end
38
41
 
39
42
  if module_name.nil?
40
43
  module_name = get_module_name
41
44
  if module_name.nil?
42
- $stderr.puts "Unable to determine module name. Aborting" if verbose
45
+ warn 'Unable to determine module name. Aborting' if verbose
43
46
  return false
44
47
  end
45
48
  end
@@ -52,35 +55,35 @@ END
52
55
  File.join('spec', 'hosts'),
53
56
  File.join('spec', 'fixtures'),
54
57
  File.join('spec', 'fixtures', 'manifests'),
55
- File.join('spec', 'fixtures', 'modules'),
58
+ File.join('spec', 'fixtures', 'modules')
56
59
  ].each { |dir| safe_mkdir(dir, verbose) }
57
60
 
58
61
  target = File.join('spec', 'fixtures', 'modules', module_name)
59
62
  safe_make_link('.', target, verbose)
60
63
  end
61
64
 
62
- def self.safe_teardown_links(module_name=nil)
65
+ def self.safe_teardown_links(module_name = nil)
63
66
  if module_name.nil?
64
67
  module_name = get_module_name
65
68
  if module_name.nil?
66
- $stderr.puts "Unable to determine module name. Aborting"
69
+ warn 'Unable to determine module name. Aborting'
67
70
  return false
68
71
  end
69
72
  end
70
73
 
71
74
  target = File.join('spec', 'fixtures', 'modules', module_name)
72
- if File.symlink?(target) && File.readlink(target) == File.expand_path('.')
73
- File.unlink(target)
74
- end
75
+ return unless File.symlink?(target) && File.readlink(target) == File.expand_path('.')
76
+
77
+ File.unlink(target)
75
78
  end
76
- protected
79
+
77
80
  def self.control_repo?
78
81
  !File.exist?('metadata.json')
79
82
  end
80
83
 
81
84
  def self.get_module_name
82
85
  module_name = nil
83
- Dir["manifests/*.pp"].entries.each do |manifest|
86
+ Dir['manifests/*.pp'].entries.each do |manifest|
84
87
  module_name = get_module_name_from_file(manifest)
85
88
  break unless module_name.nil?
86
89
  end
@@ -88,35 +91,25 @@ END
88
91
  end
89
92
 
90
93
  def self.get_module_name_from_file(file)
91
- # FIXME: see discussion at
92
- # https://github.com/rodjek/rspec-puppet/issues/290
93
- if Puppet.version.to_f >= 4.0 || RSpec.configuration.parser == 'future'
94
- require 'puppet/pops'
95
- p = Puppet::Pops::Parser::Lexer2.new
96
- else
97
- p = Puppet::Parser::Lexer.new
98
- end
94
+ require 'puppet/pops'
95
+ p = Puppet::Pops::Parser::Lexer2.new
99
96
  module_name = nil
100
97
  p.string = File.read(file)
101
98
  tokens = p.fullscan
102
99
 
103
- i = tokens.index { |token| [:CLASS, :DEFINE].include? token.first }
104
- unless i.nil?
105
- module_name = tokens[i + 1].last[:value].split('::').first
106
- end
100
+ i = tokens.index { |token| %i[CLASS DEFINE].include? token.first }
101
+ module_name = tokens[i + 1].last[:value].split('::').first unless i.nil?
107
102
 
108
103
  module_name
109
104
  end
110
105
 
111
106
  def self.is_module_dir?
112
- Dir["*"].entries.include? "manifests"
107
+ Dir['*'].entries.include? 'manifests'
113
108
  end
114
109
 
115
- def self.safe_mkdir(dir, verbose=true)
110
+ def self.safe_mkdir(dir, verbose = true)
116
111
  if File.exist? dir
117
- unless File.directory? dir
118
- $stderr.puts "!! #{dir} already exists and is not a directory"
119
- end
112
+ warn "!! #{dir} already exists and is not a directory" unless File.directory? dir
120
113
  else
121
114
  begin
122
115
  FileUtils.mkdir dir
@@ -129,9 +122,7 @@ END
129
122
 
130
123
  def self.safe_touch(file)
131
124
  if File.exist? file
132
- unless File.file? file
133
- $stderr.puts "!! #{file} already exists and is not a regular file"
134
- end
125
+ warn "!! #{file} already exists and is not a regular file" unless File.file? file
135
126
  else
136
127
  FileUtils.touch file
137
128
  puts " + #{file}"
@@ -141,9 +132,7 @@ END
141
132
  def self.safe_create_file(filename, content)
142
133
  if File.exist? filename
143
134
  old_content = File.read(filename)
144
- if old_content != content
145
- $stderr.puts "!! #{filename} already exists and differs from template"
146
- end
135
+ warn "!! #{filename} already exists and differs from template" if old_content != content
147
136
  else
148
137
  File.open(filename, 'w') do |f|
149
138
  f.puts content
@@ -169,17 +158,17 @@ END
169
158
  Dir.respond_to?(:junction?) ? Dir.junction?(target) : File.symlink?(target)
170
159
  end
171
160
 
172
- def self.safe_make_link(source, target, verbose=true)
161
+ def self.safe_make_link(source, target, verbose = true)
173
162
  if File.exist?(target) && !link?(target)
174
- $stderr.puts "!! #{target} already exists and is not a symlink"
163
+ warn "!! #{target} already exists and is not a symlink"
175
164
  return
176
165
  end
177
166
 
178
167
  return if link_to_source?(target, source)
179
168
 
180
169
  if Puppet::Util::Platform.windows?
181
- output = `call mklink /J "#{target.gsub('/', '\\')}" "#{source}"`
182
- unless $?.success?
170
+ output = `call mklink /J "#{target.tr('/', '\\')}" "#{source}"`
171
+ unless $CHILD_STATUS.success?
183
172
  puts output
184
173
  abort
185
174
  end
@@ -194,19 +183,19 @@ END
194
183
  end
195
184
 
196
185
  def self.safe_create_rakefile
197
- content = <<-'EOF'
198
- require 'rspec-puppet/rake_task'
186
+ content = <<~EOF
187
+ require 'rspec-puppet/rake_task'
199
188
 
200
- begin
201
- if Gem::Specification::find_by_name('puppet-lint')
202
- require 'puppet-lint/tasks/puppet-lint'
203
- PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "vendor/**/*.pp"]
204
- task :default => [:rspec, :lint]
205
- end
206
- rescue Gem::LoadError
207
- task :default => :rspec
208
- end
209
- EOF
189
+ begin
190
+ if Gem::Specification::find_by_name('puppet-lint')
191
+ require 'puppet-lint/tasks/puppet-lint'
192
+ PuppetLint.configuration.ignore_paths = ["spec/**/*.pp", "vendor/**/*.pp"]
193
+ task :default => [:rspec, :lint]
194
+ end
195
+ rescue Gem::LoadError
196
+ task :default => :rspec
197
+ end
198
+ EOF
210
199
  safe_create_file('Rakefile', content)
211
200
  end
212
201
  end