Redoing autoload a bit in preparation for adding a plugindir

git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2518 980ebf18-57e1-0310-9a29-db15c13687c0
This commit is contained in:
luke 2007-05-16 00:00:47 +00:00
Родитель dbedcd7b98
Коммит e95734b8bb
6 изменённых файлов: 89 добавлений и 72 удалений

Просмотреть файл

@ -1,3 +1,7 @@
Added a 'dynamicfacts' configuration option; any facts in that
comma-separated list will be ignored when comparing facts to
see if they have changed and thus whether a recompile is necessary.
Renamed some poorly named internal variables:
@models in providers are now either @resource or
@resource_type (#605).

Просмотреть файл

@ -18,14 +18,11 @@ class Puppet::Util::Autoload
Puppet::Util.proxy self, :loaded, :clear
def handle_libdir
dir = Puppet[:libdir]
$: << dir unless $:.include?(dir)
end
def initialize(obj, path, options = {})
@path = path.to_s
if @path !~ /^\w/
raise ArgumentError, "Autoload paths cannot be fully qualified"
end
@object = obj
self.class[obj] = self
@ -46,62 +43,77 @@ class Puppet::Util::Autoload
@loaded = {}
end
# Load a single plugin by name.
def load(name)
name = symbolize(name)
handle_libdir()
path = name.to_s + ".rb"
path = File.join(@path, name.to_s + ".rb")
begin
Kernel.load path, @wrap
@loaded[name] = true
return true
rescue LoadError => detail
# I have no idea what's going on here, but different versions
# of ruby are raising different errors on missing files.
unless detail.to_s =~ /^no such file/i
warn "Could not autoload %s: %s" % [name, detail]
if Puppet[:trace]
puts detail.backtrace
eachdir do |dir|
file = File.join(dir, path)
next unless FileTest.exists?(file)
begin
Kernel.load file, @wrap
name = symbolize(name)
@loaded[name] = true
return true
rescue LoadError => detail
# I have no idea what's going on here, but different versions
# of ruby are raising different errors on missing files.
unless detail.to_s =~ /^no such file/i
warn "Could not autoload %s: %s" % [name, detail]
if Puppet[:trace]
puts detail.backtrace
end
end
return false
end
return false
end
return false
end
# Indicate whether the specfied plugin has been loaded.
def loaded?(name)
name = symbolize(name)
@loaded[name]
@loaded[symbolize(name)]
end
def loadall
handle_libdir()
# Load every instance of everything we can find.
$:.each do |dir|
fdir = File.join(dir, @path)
if FileTest.exists?(fdir) and FileTest.directory?(fdir)
Dir.glob("#{fdir}/*.rb").each do |file|
# Load here, rather than require, so that facts
# can be reloaded. This has some short-comings, I
# believe, but it works as long as real classes
# aren't used.
name = File.basename(file).sub(".rb", '').intern
next if @loaded.include? name
next if $".include?(File.join(@path, name.to_s + ".rb"))
filepath = File.join(@path, name.to_s + ".rb")
begin
Kernel.require filepath
@loaded[name] = true
rescue => detail
if Puppet[:trace]
puts detail.backtrace
end
warn "Could not autoload %s: %s" % [file.inspect, detail]
eachdir do |dir|
Dir.glob("#{dir}/*.rb").each do |file|
# Load here, rather than require, so that facts
# can be reloaded. This has some short-comings, I
# believe, but it works as long as real classes
# aren't used.
name = File.basename(file).sub(".rb", '').intern
next if @loaded.include? name
next if $".include?(File.join(@path, name.to_s + ".rb"))
filepath = File.join(@path, name.to_s + ".rb")
begin
Kernel.require filepath
@loaded[name] = true
rescue => detail
if Puppet[:trace]
puts detail.backtrace
end
warn "Could not autoload %s: %s" % [file.inspect, detail]
end
end
end
end
private
# Yield each subdir in turn.
def eachdir
searchpath.each do |dir|
subdir = File.join(dir, @path)
yield subdir if FileTest.directory?(subdir)
end
end
# The list of directories to search through for loadable plugins.
def searchpath
[Puppet[:libdir], $:].flatten
end
end
# $Id$

Просмотреть файл

@ -118,6 +118,19 @@ module PuppetTest
$0 =~ /test_loader/
end
# Redirect stdout and stderr
def redirect
@stderr = tempfile
@stdout = tempfile
$stderr = File.open(@stderr, "w")
$stdout = File.open(@stdout, "w")
cleanup do
$stderr = STDERR
$stdout = STDOUT
end
end
def setup
@memoryatstart = Puppet::Util.memory
if defined? @@testcount

Просмотреть файл

@ -74,7 +74,7 @@ TestAutoload.newthing(:#{name.to_s})
# Now try to actually load it.
assert_nothing_raised {
assert_equal(true, loader.load(:mything),
"got incorrect return on failed load")
"got incorrect return on load")
}
assert(loader.loaded?(:mything), "Not considered loaded")
@ -103,28 +103,13 @@ TestAutoload.newthing(:#{name.to_s})
# Make sure that autoload dynamically modifies $: with the libdir as
# appropriate.
def test_autoload_uses_libdir
def test_searchpath
dir = Puppet[:libdir]
unless FileTest.directory?(dir)
Dir.mkdir(dir)
end
loader = File.join(dir, "test")
Dir.mkdir(loader)
name = "funtest"
file = File.join(loader, "funtest.rb")
File.open(file, "w") do |f|
f.puts "$loaded = true"
end
loader = Puppet::Util::Autoload.new(self, "testing")
auto = Puppet::Util::Autoload.new(self, "test")
# Now make sure autoloading modifies $: as necessary
assert(! $:.include?(dir), "search path already includes libdir")
assert_nothing_raised do
assert(auto.load("funtest"), "did not successfully load funtest")
end
assert($:.include?(dir), "libdir did not get added to search path")
assert(loader.send(:searchpath).include?(dir), "searchpath does not include the libdir")
end
end
# $Id$

Просмотреть файл

@ -13,11 +13,14 @@ class TestFeatures < Test::Unit::TestCase
def setup
super
libdir = tempfile()
@features = Puppet::Util::Feature.new(libdir)
@libdir = tempfile()
Puppet[:libdir] = @libdir
@path = File.join(@libdir, "features")
@features = Puppet::Util::Feature.new("features")
end
def test_new
redirect
assert_nothing_raised do
@features.add(:failer) do
raise ArgumentError, "nopes"
@ -65,8 +68,8 @@ class TestFeatures < Test::Unit::TestCase
$features = @features
cleanup { $features = nil }
# Now create a feature and make sure it loads.
Dir.mkdir(@features.path)
nope = File.join(@features.path, "nope.rb")
FileUtils.mkdir_p(@path)
nope = File.join(@path, "nope.rb")
File.open(nope, "w") { |f|
f.puts "$features.add(:nope, :libs => %w{nosuchlib})"
}
@ -79,7 +82,7 @@ class TestFeatures < Test::Unit::TestCase
assert(! @features.yep?, "'yep' returned true before definition")
end
yep = File.join(@features.path, "yep.rb")
yep = File.join(@path, "yep.rb")
File.open(yep, "w") { |f|
f.puts "$features.add(:yep, :libs => %w{puppet})"
}

Просмотреть файл

@ -2,8 +2,8 @@
$:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/
require 'puppet'
require 'puppettest'
require 'mocha'
class TestPuppetUtil < Test::Unit::TestCase
include PuppetTest