puppetlabs_spec_helper 2.6.2 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'stringio'
2
4
  require 'rspec/expectations'
3
5
 
@@ -6,18 +8,17 @@ require 'rspec/expectations'
6
8
  module RSpec
7
9
  module Matchers
8
10
  module BlockAliases
9
- if method_defined? :should
10
- alias_method :to, :should unless method_defined? :to
11
+ if method_defined?(:should) && !method_defined?(:to)
12
+ alias to should
11
13
  end
12
14
  if method_defined? :should_not
13
- alias_method :to_not, :should_not unless method_defined? :to_not
14
- alias_method :not_to, :should_not unless method_defined? :not_to
15
+ alias to_not should_not unless method_defined? :to_not
16
+ alias not_to should_not unless method_defined? :not_to
15
17
  end
16
18
  end
17
19
  end
18
20
  end
19
21
 
20
-
21
22
  ########################################################################
22
23
  # Custom matchers...
23
24
  RSpec::Matchers.define :have_matching_element do |expected|
@@ -26,7 +27,6 @@ RSpec::Matchers.define :have_matching_element do |expected|
26
27
  end
27
28
  end
28
29
 
29
-
30
30
  RSpec::Matchers.define :exit_with do |expected|
31
31
  actual = nil
32
32
  match do |block|
@@ -35,13 +35,13 @@ RSpec::Matchers.define :exit_with do |expected|
35
35
  rescue SystemExit => e
36
36
  actual = e.status
37
37
  end
38
- actual and actual == expected
38
+ actual && actual == expected
39
39
  end
40
- failure_message_for_should do |block|
40
+ failure_message_for_should do |_block|
41
41
  "expected exit with code #{expected} but " +
42
- (actual.nil? ? " exit was not called" : "we exited with #{actual} instead")
42
+ (actual.nil? ? ' exit was not called' : "we exited with #{actual} instead")
43
43
  end
44
- failure_message_for_should_not do |block|
44
+ failure_message_for_should_not do |_block|
45
45
  "expected that exit would not be called with #{expected}"
46
46
  end
47
47
  description do
@@ -49,7 +49,6 @@ RSpec::Matchers.define :exit_with do |expected|
49
49
  end
50
50
  end
51
51
 
52
-
53
52
  RSpec::Matchers.define :have_printed do |expected|
54
53
  match do |block|
55
54
  $stderr = $stdout = StringIO.new
@@ -64,7 +63,7 @@ RSpec::Matchers.define :have_printed do |expected|
64
63
  $stderr = STDERR
65
64
  end
66
65
 
67
- if @actual then
66
+ if @actual
68
67
  case expected
69
68
  when String
70
69
  @actual.include? expected
@@ -77,7 +76,7 @@ RSpec::Matchers.define :have_printed do |expected|
77
76
  end
78
77
 
79
78
  failure_message_for_should do |actual|
80
- if actual.nil? then
79
+ if actual.nil?
81
80
  "expected #{expected.inspect}, but nothing was printed"
82
81
  else
83
82
  "expected #{expected.inspect} to be printed; got:\n#{actual}"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Initialize puppet for testing by loading the
2
4
  # 'puppetlabs_spec_helper/puppet_spec_helper' library
3
5
  require 'puppetlabs_spec_helper/puppet_spec_helper'
@@ -8,9 +10,9 @@ module PuppetlabsSpec
8
10
  # instance suitable for placing in a test harness with the intent of
9
11
  # testing parser functions from modules.
10
12
  def scope(parts = {})
11
- RSpec.deprecate('scope', :replacement => 'rspec-puppet 2.2.0 provides a scope property')
13
+ RSpec.deprecate('scope', replacement: 'rspec-puppet 2.2.0 provides a scope property')
12
14
 
13
- if Puppet.version =~ /^2\.[67]/
15
+ if %r{^2\.[67]}.match?(Puppet.version)
14
16
  # loadall should only be necessary prior to 3.x
15
17
  # Please note, loadall needs to happen first when creating a scope, otherwise
16
18
  # you might receive undefined method `function_*' errors
@@ -19,15 +21,15 @@ module PuppetlabsSpec
19
21
 
20
22
  scope_compiler = parts[:compiler] || compiler
21
23
  scope_parent = parts[:parent] || scope_compiler.topscope
22
- scope_resource = parts[:resource] || resource(:type => :node, :title => scope_compiler.node.name)
24
+ scope_resource = parts[:resource] || resource(type: :node, title: scope_compiler.node.name)
23
25
 
24
- if Puppet.version =~ /^2\.[67]/
25
- scope = Puppet::Parser::Scope.new(:compiler => scope_compiler)
26
- else
27
- scope = Puppet::Parser::Scope.new(scope_compiler)
28
- end
26
+ scope = if %r{^2\.[67]}.match?(Puppet.version)
27
+ Puppet::Parser::Scope.new(compiler: scope_compiler)
28
+ else
29
+ Puppet::Parser::Scope.new(scope_compiler)
30
+ end
29
31
 
30
- scope.source = Puppet::Resource::Type.new(:node, "foo")
32
+ scope.source = Puppet::Resource::Type.new(:node, 'foo')
31
33
  scope.parent = scope_parent
32
34
  scope
33
35
  end
@@ -35,13 +37,13 @@ module PuppetlabsSpec
35
37
 
36
38
  def resource(parts = {})
37
39
  resource_type = parts[:type] || :hostclass
38
- resource_name = parts[:name] || "testing"
40
+ resource_name = parts[:name] || 'testing'
39
41
  Puppet::Resource::Type.new(resource_type, resource_name)
40
42
  end
41
43
  module_function :resource
42
44
 
43
45
  def compiler(parts = {})
44
- compiler_node = parts[:node] || node()
46
+ compiler_node = parts[:node] || node
45
47
  Puppet::Parser::Compiler.new(compiler_node)
46
48
  end
47
49
  module_function :compiler
@@ -49,12 +51,12 @@ module PuppetlabsSpec
49
51
  def node(parts = {})
50
52
  node_name = parts[:name] || 'testinghost'
51
53
  options = parts[:options] || {}
52
- if Puppet.version.to_f >= 4.0
53
- node_environment = Puppet::Node::Environment.create(parts[:environment] || 'test', [])
54
- else
55
- node_environment = Puppet::Node::Environment.new(parts[:environment] || 'test')
56
- end
57
- options.merge!({:environment => node_environment})
54
+ node_environment = if Puppet.version.to_f >= 4.0
55
+ Puppet::Node::Environment.create(parts[:environment] || 'test', [])
56
+ else
57
+ Puppet::Node::Environment.new(parts[:environment] || 'test')
58
+ end
59
+ options[:environment] = node_environment
58
60
  Puppet::Node.new(node_name, options)
59
61
  end
60
62
  module_function :node
@@ -67,6 +69,7 @@ module PuppetlabsSpec
67
69
  # exists. This is a hack, but at least it's a hidden hack and not an
68
70
  # exposed hack.
69
71
  return nil unless Puppet::Parser::Functions.function(name)
72
+
70
73
  scope.method("function_#{name}".intern)
71
74
  end
72
75
  module_function :function_method
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Define the main module namespace for use by the helper modules
2
4
  module PuppetlabsSpec
3
5
  # FIXTURE_DIR represents the standard locations of all fixture data. Normally
4
6
  # this represents <project>/spec/fixtures. This will be used by the fixtures
5
7
  # library to find relative fixture data.
6
- FIXTURE_DIR = File.join("spec", "fixtures") unless defined?(FIXTURE_DIR)
8
+ FIXTURE_DIR = File.join('spec', 'fixtures') unless defined?(FIXTURE_DIR)
7
9
  end
8
10
 
9
11
  # Require all necessary helper libraries so they can be used later