Skip to content

Commit c129775

Browse files
author
Jeff McCune
committed
(Maint) Fix up the get_module_path parser function
This patch switches the spec tests for the get_module_path function to use mock objects. The underlying Puppet::Module.find method has reasonable test coverage inside of Puppet core so we might as well break the tight dependency while we're fixing up the specs to use the new parser scope. The behavior of the parser function itself should still have complete coverage even though the tests have switched to mock the implementation inside of Puppet.
1 parent 98ff3ab commit c129775

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

lib/puppet/parser/functions/get_module_path.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module Puppet::Parser::Functions
77
$module_path = get_module_path('stdlib')
88
EOT
99
) do |args|
10-
raise(Puppet::ParseError, "get_module_name(): Wrong number of arguments, expects one") unless args.size == 1
10+
raise(Puppet::ParseError, "get_module_path(): Wrong number of arguments, expects one") unless args.size == 1
1111
if module_path = Puppet::Module.find(args[0], compiler.environment.to_s)
1212
module_path.path
1313
else
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
1-
#!/usr/bin/env rspec
2-
require 'puppet'
3-
require 'fileutils'
1+
#! /usr/bin/env ruby -S rspec
42
require 'spec_helper'
3+
54
describe Puppet::Parser::Functions.function(:get_module_path) do
6-
include PuppetSpec::Files
5+
Internals = PuppetlabsSpec::PuppetInternals
76

8-
def get_scope(environment = 'production')
9-
scope = Puppet::Parser::Scope.new
10-
scope.compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("floppy", :environment => environment))
11-
scope
7+
def scope(environment = "production")
8+
Internals.scope(:compiler => Internals.compiler(:node => Internals.node(:environment => environment)))
129
end
10+
1311
it 'should only allow one argument' do
14-
expect { get_scope.function_get_module_path([]) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
15-
expect { get_scope.function_get_module_path(['1','2','3']) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
12+
expect { scope.function_get_module_path([]) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
13+
expect { scope.function_get_module_path(['1','2','3']) }.should raise_error(Puppet::ParseError, /Wrong number of arguments, expects one/)
1614
end
1715
it 'should raise an exception when the module cannot be found' do
18-
expect { get_scope.function_get_module_path(['foo']) }.should raise_error(Puppet::ParseError, /Could not find module/)
16+
expect { scope.function_get_module_path(['foo']) }.should raise_error(Puppet::ParseError, /Could not find module/)
1917
end
2018
describe 'when locating a module' do
21-
let(:modulepath) { tmpdir('modulepath') }
22-
let(:foo_path) { File.join(modulepath, 'foo') }
23-
before(:each) { FileUtils.mkdir(foo_path) }
19+
let(:modulepath) { "/tmp/does_not_exist" }
20+
let(:path_of_module_foo) do
21+
mod = mock("Puppet::Module")
22+
mod.stubs(:path).returns("/tmp/does_not_exist/foo")
23+
mod
24+
end
25+
26+
before(:each) { Puppet[:modulepath] = modulepath }
27+
2428
it 'should be able to find module paths from the modulepath setting' do
25-
Puppet[:modulepath] = modulepath
26-
get_scope.function_get_module_path(['foo']).should == foo_path
29+
Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo)
30+
scope.function_get_module_path(['foo']).should == path_of_module_foo.path
2731
end
2832
it 'should be able to find module paths when the modulepath is a list' do
2933
Puppet[:modulepath] = modulepath + ":/tmp"
30-
get_scope.function_get_module_path(['foo']).should == foo_path
34+
Puppet::Module.expects(:find).with('foo', 'production').returns(path_of_module_foo)
35+
scope.function_get_module_path(['foo']).should == path_of_module_foo.path
3136
end
32-
it 'should be able to find module paths from the environment' do
33-
conf_file = tmpfile('conffile')
34-
File.open(conf_file, 'w') do |fh|
35-
fh.write("[dansenvironment]\nmodulepath = #{modulepath}")
36-
end
37-
Puppet[:config] = conf_file
38-
Puppet.parse_config
39-
get_scope('dansenvironment').function_get_module_path(['foo']).should ==foo_path
37+
it 'should respect the environment' do
38+
pending("Disabled on Puppet 2.6.x") if Puppet.version =~ /^2\.6\b/
39+
Puppet.settings[:environment] = 'danstestenv'
40+
Puppet::Module.expects(:find).with('foo', 'danstestenv').returns(path_of_module_foo)
41+
scope('danstestenv').function_get_module_path(['foo']).should == path_of_module_foo.path
4042
end
4143
end
4244
end

0 commit comments

Comments
 (0)