diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-09-15 06:06:50 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2006-09-15 06:06:50 +0000 |
| commit | 5669d1b30f8a6cef1f239eef4bb185e82ceb9c6f (patch) | |
| tree | fd381074fc306490405f8a12341031831b0a039d /test | |
| parent | fbdd6c471161f0438399227140cb7195c0e6993d (diff) | |
| download | puppet-5669d1b30f8a6cef1f239eef4bb185e82ceb9c6f.tar.gz puppet-5669d1b30f8a6cef1f239eef4bb185e82ceb9c6f.tar.xz puppet-5669d1b30f8a6cef1f239eef4bb185e82ceb9c6f.zip | |
This commit adds two important features (but which probably were not
worth the priority I suddenly placed on them).
First, it adds search paths as I originally requested in #114. There is
now a 'lib' setting, which can be used to tell Puppet where to find
manifests. Any file you tell Puppet to parse will have its directory
automatically added to the lib path. Also, Puppet will check the
PUPPETLIB environment variable for further directories to search.
Second, it converts the 'import' mechanism into a normal function, which
means that you can now use variables and what-have-you in it. Of
course, this function uses the lib mechanism. This is something that's
always bothered me about the language, and having it fixed means you can
do simple things like have custom code in the top scope for each
operating system and then do "import os/$operatingsystem" to evaluate
that code. Without this, you would either need a huge case statement or
the code would need to be in a class, which often isn't sufficient.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1605 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
| -rwxr-xr-x | test/language/functions.rb | 52 | ||||
| -rwxr-xr-x | test/language/interpreter.rb | 31 | ||||
| -rw-r--r-- | test/language/parser.rb | 134 |
3 files changed, 188 insertions, 29 deletions
diff --git a/test/language/functions.rb b/test/language/functions.rb index 97ddbca00..b35a1ac4a 100755 --- a/test/language/functions.rb +++ b/test/language/functions.rb @@ -267,6 +267,58 @@ class TestLangFunctions < Test::Unit::TestCase assert(Puppet::Parser::Scope.method_defined?(:function_autofunc), "Did not set function correctly") end + + def test_import + dir = tempfile() + Dir.mkdir(dir) + Puppet[:lib] = [dir] + + # Now make our file to import + file = File.join(dir, "file.pp") + File.open(file, "w") { |f| f.puts "$variable = value" } + + # Now try our import multiple ways + ["file", "file*", "file.pp", file].each do |path| + func = nil + assert_nothing_raised do + func = Puppet::Parser::AST::Function.new( + :name => "import", + :ftype => :statement, + :arguments => AST::ASTArray.new( + :children => [stringobj(path)] + ) + ) + end + + scope = Puppet::Parser::Scope.new() + assert_nothing_raised do + func.evaluate(:scope => scope) + end + + assert_equal("value", scope.lookupvar("variable"), + "Import did not work") + end + + # Now make sure we raise an ImportError if we try to import a non-existent + # file, again using multiple styles + ["nosuchfile", "nosuch*"].each do |path| + func = nil + assert_nothing_raised do + func = Puppet::Parser::AST::Function.new( + :name => "import", + :ftype => :statement, + :arguments => AST::ASTArray.new( + :children => [stringobj(path)] + ) + ) + end + + scope = Puppet::Parser::Scope.new() + assert_raise(Puppet::ImportError) do + func.evaluate(:scope => scope) + end + end + end end # $Id$ diff --git a/test/language/interpreter.rb b/test/language/interpreter.rb index 51ee1e5f4..0179514be 100755 --- a/test/language/interpreter.rb +++ b/test/language/interpreter.rb @@ -349,4 +349,35 @@ class nothernode {} end + + def test_filedirs_added_to_libdir + manifest = tempfile() + File.open(manifest, "w") do |f| f.puts "$yay = rah" end + interp = nil + assert_nothing_raised { + interp = Puppet::Parser::Interpreter.new( + :Manifest => manifest + ) + } + + assert(Puppet[:lib].include?(File.dirname(manifest)), + "File dir did not get added") + + # Now try it with a local file + dir = tempfile() + Dir.mkdir(dir) + Dir.chdir(dir) do + manifest = "file" + File.open(manifest, "w") do |f| f.puts "$yay = rah" end + interp = nil + assert_nothing_raised { + interp = Puppet::Parser::Interpreter.new( + :Manifest => manifest + ) + } + end + + assert(Puppet[:lib].include?("."), + "File dir did not get added") + end end diff --git a/test/language/parser.rb b/test/language/parser.rb index 6dd9b4bca..e3d7bc5af 100644 --- a/test/language/parser.rb +++ b/test/language/parser.rb @@ -77,6 +77,8 @@ class TestParser < Test::Unit::TestCase @@tmpfiles << basedir Dir.mkdir(basedir) + Puppet[:lib] = [basedir] + subdir = "subdir" Dir.mkdir(File.join(basedir, subdir)) manifest = File.join(basedir, "manifest") @@ -91,6 +93,7 @@ class TestParser < Test::Unit::TestCase assert_nothing_raised("Could not parse multiple files") { parser = Puppet::Parser::Parser.new() + #parser.import("#{subdir}/*") parser.file = manifest parser.parse } @@ -239,6 +242,7 @@ class TestParser < Test::Unit::TestCase def test_fqfilesandlocalfiles dir = tempfile() Dir.mkdir(dir) + Puppet[:lib] = [dir] importer = File.join(dir, "site.pp") fullfile = File.join(dir, "full.pp") localfile = File.join(dir, "local.pp") @@ -275,38 +279,11 @@ class TestParser < Test::Unit::TestCase assert_creates(importer, *files) end - # Make sure the parser adds '.pp' when necessary - def test_addingpp - dir = tempfile() - Dir.mkdir(dir) - importer = File.join(dir, "site.pp") - localfile = File.join(dir, "local.pp") - - files = [] - - File.open(importer, "w") do |f| - f.puts %{import "local"\ninclude local} - end - - file = tempfile() - files << file - - File.open(localfile, "w") do |f| - f.puts %{class local { file { "#{file}": ensure => file }}} - end - - parser = Puppet::Parser::Parser.new - parser.file = importer - - assert_nothing_raised { - parser.parse - } - end - # Make sure that file importing changes file relative names. def test_changingrelativenames dir = tempfile() Dir.mkdir(dir) + Puppet[:lib] = [dir] Dir.mkdir(File.join(dir, "subdir")) top = File.join(dir, "site.pp") subone = File.join(dir, "subdir/subone") @@ -323,7 +300,7 @@ class TestParser < Test::Unit::TestCase otherfile = tempfile() files << otherfile File.open(subtwo + ".pp", "w") do |f| - f.puts %{import "subone"\n class two inherits one { + f.puts %{import "subdir/subone"\n class two inherits one { file { "#{otherfile}": ensure => file } }} end @@ -501,6 +478,105 @@ file { "/tmp/yayness": assert_instance_of(Puppet::Parser::AST::IfStatement, ret) assert_instance_of(Puppet::Parser::AST::Else, ret.else) end + + def test_find + parser = Puppet::Parser::Parser.new() + + dir = tempfile() + Dir.mkdir(dir) + name = "file" + file = File.join(dir, "#{name}.pp") + File.open(file, "w") { |f| f.puts "" } + + Puppet[:lib] = dir + + [name, name + ".pp", file].each do |f| + full = nil + assert_nothing_raised do + full = parser.class.find(f) + end + + assert_equal(file, full) + end + + assert_nil(parser.class.find("nosuchfile")) + end + + def test_libsetup + lib = [tempfile, tempfile] + assert_nothing_raised do + Puppet[:lib] = lib.join(":") + end + env = [tempfile, tempfile] + assert_nothing_raised do + ENV["PUPPETLIB"] = env.join(":") + end + + parser = Puppet::Parser::Parser.new() + assert_nothing_raised do + parser.class.libsetup + end + + old = Puppet[:lib] + + [lib, env].flatten.each do |dir| + assert(Puppet[:lib].include?(dir), "Did not include %s" % dir) + end + + assert_nothing_raised do + parser.class.libsetup + end + + assert_equal(old, Puppet[:lib], "Libdirs changed on second run") + end + + def test_glob + dirs = [] + subdirs = [] + files = [] + subfiles = [] + 2.times { |i| + dir = tempfile() + dirs << dir + + Dir.mkdir(dir) + + file = File.join(dir, "file.pp") + File.open(file, "w") { |f| f.puts "" } + files << file + + subdir = File.join(dir, "subdir") + Dir.mkdir(subdir) + subdirs << subdir + subfile = File.join(subdir, "file.pp") + File.open(subfile, "w") { |f| f.puts "" } + subfiles << subfile + } + Puppet[:lib] = dirs.join(":") + + klass = Puppet::Parser::Parser + + # Okay, first glob a full path + dir = dirs[0] + assert_nothing_raised do + result = klass.glob("#{dir}/*") + + assert_equal([File.join(dir, "file.pp")], result) + end + + # Now check it across our search path + assert_nothing_raised do + result = klass.glob("file*") + + assert_equal(files, result, "Did not find globbed files") + + result = klass.glob("subdir/*") + + assert_equal(subfiles, result, "Did not find globbed subfiles") + end + + # Now try it with a normal file. + end end # $Id$ |
