3
require File.dirname(__FILE__) + '/../lib/puppettest'
6
require 'puppet/util/autoload'
9
class TestAutoload < Test::Unit::TestCase
12
def self.newthing(name)
24
def mkfile(name, path)
25
# Now create a file to load
26
File.open(path, "w") do |f|
28
TestAutoload.newthing(:#{name.to_s})
42
rbdir = File.join(dir, name.to_s)
47
assert_nothing_raised {
48
loader = Puppet::Util::Autoload.new(self.class, name)
54
dir, loader = mk_loader(:yayness)
56
assert_equal(loader.object_id, Puppet::Util::Autoload[self.class].object_id,
57
"Did not retrieve loader object by class")
59
# Make sure we don't fail on missing files
60
assert_nothing_raised {
61
assert_equal(false, loader.load(:mything),
62
"got incorrect return on failed load")
65
# Now create a couple of files for testing
66
path = File.join(dir, "mything.rb")
67
mkfile(:mything, path)
68
opath = File.join(dir, "othing.rb")
69
mkfile(:othing, opath)
71
# Now try to actually load it.
72
assert_nothing_raised {
73
assert_equal(true, loader.load(:mything),
74
"got incorrect return on load")
77
assert(loader.loaded?(:mything), "Not considered loaded")
79
assert(self.class.thing?(:mything),
80
"Did not get loaded thing")
84
[:mything, :othing].each do |thing|
86
assert(loader.loaded?(thing), "#{thing.to_s} not considered loaded")
87
assert(loader.loaded?("%s.rb" % thing), "#{thing.to_s} not considered loaded with .rb")
88
assert(Puppet::Util::Autoload.loaded?("yayness/%s" % thing), "%s not considered loaded by the main class" % thing)
89
assert(Puppet::Util::Autoload.loaded?("yayness/%s.rb" % thing), "%s not considered loaded by the main class with .rb" % thing)
91
assert(self.class.thing?(thing),
92
"Did not get loaded #{thing.to_s}")
96
# Make sure that autoload dynamically modifies $: with the libdir as
101
loader = Puppet::Util::Autoload.new(self, "testing")
103
assert(loader.send(:searchpath).include?(dir), "searchpath does not include the libdir")
106
# This tests #1027, which was caused by using the unqualified
107
# path for requires, which was initially done so that the kernel
108
# would keep track of which files got loaded.
109
def test_require_uses_full_path
111
loader = Puppet::Util::Autoload.new(self.class, loadname)
113
basedir = "/some/dir"
114
dir = File.join(basedir, loadname)
115
loader.expects(:eachdir).yields(dir)
119
file = File.join(dir, subname) + ".rb"
121
Dir.expects(:glob).with("#{dir}/*.rb").returns(file)
123
Kernel.expects(:require).with(file)
127
def test_searchpath_includes_plugin_dirs
128
moddir = "/what/ever"
129
libdir = "/other/dir"
130
Puppet.settings.stubs(:value).with(:modulepath).returns(moddir)
131
Puppet.settings.stubs(:value).with(:libdir).returns(libdir)
134
loader = Puppet::Util::Autoload.new(self.class, loadname)
136
# Currently, include both plugins and libs.
137
paths = %w{plugins lib}.inject({}) { |hash, d| hash[d] = File.join(moddir, "testing", d); FileTest.stubs(:directory?).with(hash[d]).returns(true); hash }
138
Dir.expects(:glob).with("#{moddir}/*/{plugins,lib}").returns(paths.values)
140
searchpath = loader.searchpath
141
paths.each do |dir, path|
142
assert(searchpath.include?(path), "search path did not include path for %s" % dir)