Skip to content

Commit 920b340

Browse files
Merge pull request #360 from puppetlabs/CONT-237-rubocop_updates
(CONT-237) Rubocop updates
2 parents 84b0453 + 82a8192 commit 920b340

File tree

13 files changed

+216
-217
lines changed

13 files changed

+216
-217
lines changed

lib/puppetlabs_spec_helper/module_spec_helper.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def verify_contents(subject, title, expected_lines)
5353
end
5454

5555
# Add all spec lib dirs to LOAD_PATH
56-
components = module_path.split(File::PATH_SEPARATOR).collect do |dir|
56+
components = module_path.split(File::PATH_SEPARATOR).map do |dir|
5757
next unless Dir.exist? dir
58-
Dir.entries(dir).reject { |f| f =~ %r{^\.} }.collect { |f| File.join(dir, f, 'spec', 'lib') }
58+
Dir.entries(dir).reject { |f| f =~ %r{^\.} }.map { |f| File.join(dir, f, 'spec', 'lib') }
5959
end
6060
components.flatten.each do |d|
6161
$LOAD_PATH << d if FileTest.directory?(d) && !$LOAD_PATH.include?(d)

lib/puppetlabs_spec_helper/puppet_spec_helper.rb

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,62 +48,63 @@
4848
begin
4949
require 'puppet/test/test_helper'
5050
rescue LoadError
51+
# Continue gracefully
5152
end
5253

5354
# This is just a utility class to allow us to isolate the various version-specific
5455
# branches of initialization logic into methods without polluting the global namespace.#
55-
module Puppet
56-
class PuppetSpecInitializer
57-
# This method is for initializing puppet state for testing for older versions
58-
# of puppet that do not support the new TestHelper API. As you can see,
59-
# this involves explicitly modifying global variables, directly manipulating
60-
# Puppet's Settings singleton object, and other fun implementation details
61-
# that code external to puppet should really never know about.
62-
def self.initialize_via_fallback_compatibility(config)
63-
warn('Warning: you appear to be using an older version of puppet; spec_helper will use fallback compatibility mode.')
64-
config.before :all do
65-
# nothing to do for now
66-
end
56+
class Puppet::PuppetSpecInitializer
57+
# This method is for initializing puppet state for testing for older versions
58+
# of puppet that do not support the new TestHelper API. As you can see,
59+
# this involves explicitly modifying global variables, directly manipulating
60+
# Puppet's Settings singleton object, and other fun implementation details
61+
# that code external to puppet should really never know about.
62+
def self.initialize_via_fallback_compatibility(config)
63+
warn('Warning: you appear to be using an older version of puppet; spec_helper will use fallback compatibility mode.')
64+
config.before :all do
65+
# nothing to do for now
66+
end
6767

68-
config.after :all do
69-
# nothing to do for now
70-
end
68+
config.after :all do
69+
# nothing to do for now
70+
end
7171

72-
config.before :each do
73-
# these globals are set by Application
74-
$puppet_application_mode = nil
75-
$puppet_application_name = nil
72+
config.before :each do
73+
# these globals are set by Application
74+
$puppet_application_mode = nil # rubocop:disable Style/GlobalVars
75+
$puppet_application_name = nil # rubocop:disable Style/GlobalVars
7676

77-
# REVISIT: I think this conceals other bad tests, but I don't have time to
78-
# fully diagnose those right now. When you read this, please come tell me
79-
# I suck for letting this float. --daniel 2011-04-21
80-
Signal.stubs(:trap)
77+
# REVISIT: I think this conceals other bad tests, but I don't have time to
78+
# fully diagnose those right now. When you read this, please come tell me
79+
# I suck for letting this float. --daniel 2011-04-21
80+
Signal.stubs(:trap)
8181

82-
# Set the confdir and vardir to gibberish so that tests
83-
# have to be correctly mocked.
84-
Puppet[:confdir] = '/dev/null'
85-
Puppet[:vardir] = '/dev/null'
82+
# Set the confdir and vardir to gibberish so that tests
83+
# have to be correctly mocked.
84+
Puppet[:confdir] = '/dev/null'
85+
Puppet[:vardir] = '/dev/null'
8686

87-
# Avoid opening ports to the outside world
88-
Puppet.settings[:bindaddress] = '127.0.0.1'
89-
end
87+
# Avoid opening ports to the outside world
88+
Puppet.settings[:bindaddress] = '127.0.0.1'
89+
end
9090

91-
config.after :each do
92-
Puppet.settings.clear
91+
config.after :each do
92+
Puppet.settings.clear
9393

94-
Puppet::Node::Environment.clear
95-
Puppet::Util::Storage.clear
96-
Puppet::Util::ExecutionStub.reset if Puppet::Util.constants.include? 'ExecutionStub'
94+
Puppet::Node::Environment.clear
95+
Puppet::Util::Storage.clear
96+
Puppet::Util::ExecutionStub.reset if Puppet::Util.constants.include? 'ExecutionStub'
9797

98-
PuppetlabsSpec::Files.cleanup
99-
end
98+
PuppetlabsSpec::Files.cleanup
10099
end
101100
end
102101
end
103102

104103
# JJM Hack to make the stdlib tests run in Puppet 2.6 (See puppet commit cf183534)
105104
unless Puppet.constants.include? 'Test'
106105
module Puppet::Test
106+
# This is a stub class to allow the stdlib tests to run in Puppet 2.6
107+
# This class will be removed in another commit.
107108
class LogCollector
108109
def initialize(logs)
109110
@logs = logs

lib/puppetlabs_spec_helper/puppetlabs_spec/files.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
# rubocop:disable Style/GlobalVars # TODO: investigate the use of instance variables
4+
35
require 'fileutils'
46
require 'tempfile'
57
require 'pathname'
@@ -20,7 +22,7 @@ def self.in_tmp(path)
2022

2123
def self.cleanup
2224
$global_tempfiles ||= []
23-
while path = $global_tempfiles.pop
25+
while (path = $global_tempfiles.pop)
2426
raise "Not deleting tmpfile #{path} outside regular tmpdir" unless in_tmp(path)
2527

2628
begin

lib/puppetlabs_spec_helper/puppetlabs_spec/fixtures.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def fixtures(*rest)
1414
# <project>/spec/fixture/unit/facter/foo
1515
def my_fixture_dir
1616
callers = caller
17-
while line = callers.shift
18-
next unless found = line.match(%r{/spec/(.*)_spec\.rb:})
17+
while (line = callers.shift)
18+
next unless (found = line.match(%r{/spec/(.*)_spec\.rb:}))
1919

2020
return fixtures(found[1])
2121
end
@@ -47,7 +47,7 @@ def my_fixtures(glob = '*', flags = 0, &block)
4747
raise "fixture '#{glob}' for #{my_fixture_dir} had no files!"
4848
end
4949

50-
block_given? && files.each(&block)
50+
block && files.each(&block)
5151
files
5252
end
5353
end

lib/puppetlabs_spec_helper/puppetlabs_spec/matchers.rb

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55

66
########################################################################
77
# Backward compatibility for Jenkins outdated environment.
8-
module RSpec
9-
module Matchers
10-
module BlockAliases
11-
if method_defined?(:should) && !method_defined?(:to)
12-
alias to should
13-
end
14-
if method_defined? :should_not
15-
alias to_not should_not unless method_defined? :to_not
16-
alias not_to should_not unless method_defined? :not_to
17-
end
18-
end
8+
module RSpec::Matchers::BlockAliases
9+
if method_defined?(:should) && !method_defined?(:to)
10+
alias to should
11+
end
12+
if method_defined? :should_not
13+
alias to_not should_not unless method_defined? :to_not
14+
alias not_to should_not unless method_defined? :not_to
1915
end
2016
end
21-
2217
########################################################################
2318
# Custom matchers...
2419
RSpec::Matchers.define :have_matching_element do |expected|

lib/puppetlabs_spec_helper/puppetlabs_spec/puppet_internals.rb

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -4,74 +4,73 @@
44
# 'puppetlabs_spec_helper/puppet_spec_helper' library
55
require 'puppetlabs_spec_helper/puppet_spec_helper'
66

7-
module PuppetlabsSpec
8-
module PuppetInternals
9-
# parser_scope is intended to return a Puppet::Parser::Scope
10-
# instance suitable for placing in a test harness with the intent of
11-
# testing parser functions from modules.
12-
def scope(parts = {})
13-
RSpec.deprecate('scope', replacement: 'rspec-puppet 2.2.0 provides a scope property')
7+
# PuppetInternals provides a set of methods that interface
8+
# with internal puppet implementations.
9+
module PuppetlabsSpec::PuppetInternals
10+
# parser_scope is intended to return a Puppet::Parser::Scope
11+
# instance suitable for placing in a test harness with the intent of
12+
# testing parser functions from modules.
13+
def scope(parts = {})
14+
RSpec.deprecate('scope', replacement: 'rspec-puppet 2.2.0 provides a scope property')
1415

15-
if %r{^2\.[67]}.match?(Puppet.version)
16-
# loadall should only be necessary prior to 3.x
17-
# Please note, loadall needs to happen first when creating a scope, otherwise
18-
# you might receive undefined method `function_*' errors
19-
Puppet::Parser::Functions.autoloader.loadall
20-
end
16+
if %r{^2\.[67]}.match?(Puppet.version)
17+
# loadall should only be necessary prior to 3.x
18+
# Please note, loadall needs to happen first when creating a scope, otherwise
19+
# you might receive undefined method `function_*' errors
20+
Puppet::Parser::Functions.autoloader.loadall
21+
end
2122

22-
scope_compiler = parts[:compiler] || compiler
23-
scope_parent = parts[:parent] || scope_compiler.topscope
24-
scope_resource = parts[:resource] || resource(type: :node, title: scope_compiler.node.name)
23+
scope_compiler = parts[:compiler] || compiler
24+
scope_parent = parts[:parent] || scope_compiler.topscope
2525

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
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
3131

32-
scope.source = Puppet::Resource::Type.new(:node, 'foo')
33-
scope.parent = scope_parent
34-
scope
35-
end
36-
module_function :scope
32+
scope.source = Puppet::Resource::Type.new(:node, 'foo')
33+
scope.parent = scope_parent
34+
scope
35+
end
36+
module_function :scope
3737

38-
def resource(parts = {})
39-
resource_type = parts[:type] || :hostclass
40-
resource_name = parts[:name] || 'testing'
41-
Puppet::Resource::Type.new(resource_type, resource_name)
42-
end
43-
module_function :resource
38+
def resource(parts = {})
39+
resource_type = parts[:type] || :hostclass
40+
resource_name = parts[:name] || 'testing'
41+
Puppet::Resource::Type.new(resource_type, resource_name)
42+
end
43+
module_function :resource
4444

45-
def compiler(parts = {})
46-
compiler_node = parts[:node] || node
47-
Puppet::Parser::Compiler.new(compiler_node)
48-
end
49-
module_function :compiler
45+
def compiler(parts = {})
46+
compiler_node = parts[:node] || node
47+
Puppet::Parser::Compiler.new(compiler_node)
48+
end
49+
module_function :compiler
5050

51-
def node(parts = {})
52-
node_name = parts[:name] || 'testinghost'
53-
options = parts[:options] || {}
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
60-
Puppet::Node.new(node_name, options)
61-
end
62-
module_function :node
51+
def node(parts = {})
52+
node_name = parts[:name] || 'testinghost'
53+
options = parts[:options] || {}
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
60+
Puppet::Node.new(node_name, options)
61+
end
62+
module_function :node
6363

64-
# Return a method instance for a given function. This is primarily useful
65-
# for rspec-puppet
66-
def function_method(name, parts = {})
67-
scope = parts[:scope] || scope()
68-
# Ensure the method instance is defined by side-effect of checking if it
69-
# exists. This is a hack, but at least it's a hidden hack and not an
70-
# exposed hack.
71-
return nil unless Puppet::Parser::Functions.function(name)
64+
# Return a method instance for a given function. This is primarily useful
65+
# for rspec-puppet
66+
def function_method(name, parts = {})
67+
scope = parts[:scope] || scope()
68+
# Ensure the method instance is defined by side-effect of checking if it
69+
# exists. This is a hack, but at least it's a hidden hack and not an
70+
# exposed hack.
71+
return nil unless Puppet::Parser::Functions.function(name)
7272

73-
scope.method("function_#{name}".intern)
74-
end
75-
module_function :function_method
73+
scope.method("function_#{name}".to_sym)
7674
end
75+
module_function :function_method
7776
end

0 commit comments

Comments
 (0)