|
4 | 4 | # 'puppetlabs_spec_helper/puppet_spec_helper' library |
5 | 5 | require 'puppetlabs_spec_helper/puppet_spec_helper' |
6 | 6 |
|
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') |
14 | 15 |
|
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 |
21 | 22 |
|
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 |
25 | 25 |
|
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 |
31 | 31 |
|
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 |
37 | 37 |
|
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 |
44 | 44 |
|
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 |
50 | 50 |
|
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 |
63 | 63 |
|
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) |
72 | 72 |
|
73 | | - scope.method("function_#{name}".intern) |
74 | | - end |
75 | | - module_function :function_method |
| 73 | + scope.method("function_#{name}".to_sym) |
76 | 74 | end |
| 75 | + module_function :function_method |
77 | 76 | end |
0 commit comments