summaryrefslogtreecommitdiffstats
path: root/test/parser/parser.rb
blob: a9b2631f157e31a2ba367cdd5057e978be89c878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
if __FILE__ == $0
    $:.unshift '../../lib'
    $:.unshift '..'
    $puppetbase = "../.."
end

require 'puppet'
require 'puppet/parser/parser'
require 'test/unit'
require 'puppettest'

class TestParser < Test::Unit::TestCase
	include ParserTesting
    def setup
        super
        Puppet[:parseonly] = true
        #@lexer = Puppet::Parser::Lexer.new()
        @parser = Puppet::Parser::Parser.new()
    end

    def test_each_file
        textfiles { |file|
            Puppet.debug("parsing %s" % file) if __FILE__ == $0
            assert_nothing_raised() {
                @parser.file = file
                @parser.parse
            }

            Puppet::Type.eachtype { |type|
                type.each { |obj|
                    assert(obj.file)
                    assert(obj.name)
                    assert(obj.line)
                }
            }
            Puppet::Type.allclear
        }
    end

    def test_failers
        failers { |file|
            Puppet.debug("parsing failer %s" % file) if __FILE__ == $0
            assert_raise(Puppet::ParseError) {
                @parser.file = file
                ast = @parser.parse
                Puppet::Parser::Scope.new.evaluate(:ast => ast)
            }
            Puppet::Type.allclear
        }
    end

    def test_arrayrvalues
        parser = Puppet::Parser::Parser.new()
        ret = nil
        file = tempfile()
        assert_nothing_raised {
            parser.string = "file { \"#{file}\": mode => [755, 640] }"
        }

        assert_nothing_raised {
            ret = parser.parse
        }
    end

    def mkmanifest(file)
        name = File.join(tmpdir, "file%s" % rand(100))
        @@tmpfiles << name

        File.open(file, "w") { |f|
            f.puts "file { \"%s\": ensure => file, mode => 755 }\n" %
               name
        }
    end

    def test_importglobbing
        basedir = File.join(tmpdir(), "importesting")
        @@tmpfiles << basedir
        Dir.mkdir(basedir)

        subdir = "subdir"
        Dir.mkdir(File.join(basedir, subdir))
        manifest = File.join(basedir, "manifest")
        File.open(manifest, "w") { |f|
            f.puts "import \"%s/*\"" % subdir
        }

        4.times { |i|
            path = File.join(basedir, subdir, "subfile%s" % i)
            mkmanifest(path)
        }

        assert_nothing_raised("Could not parse multiple files") {
            parser = Puppet::Parser::Parser.new()
            parser.file = manifest
            parser.parse
        }
    end

    def test_nonexistent_import
        basedir = File.join(tmpdir(), "importesting")
        @@tmpfiles << basedir
        Dir.mkdir(basedir)
        manifest = File.join(basedir, "manifest")
        File.open(manifest, "w") do |f|
            f.puts "import \" no such file \""
        end
        assert_raise(Puppet::ParseError) {
            parser = Puppet::Parser::Parser.new()
            parser.file = manifest
            parser.parse
        }
    end

    def test_defaults
        basedir = File.join(tmpdir(), "defaulttesting")
        @@tmpfiles << basedir
        Dir.mkdir(basedir)

        defs1 = {
            "testing" => "value"
        }

        defs2 = {
            "one" => "two",
            "three" => "four",
            "five" => false,
            "seven" => "eight",
            "nine" => true,
            "eleven" => "twelve"
        }

        mkdef = proc { |hash|
            hash.collect { |arg, value|
                "%s = %s" % [arg, value]
            }.join(", ")
        }
        manifest = File.join(basedir, "manifest")
        File.open(manifest, "w") { |f|
            f.puts "
    define method(#{mkdef.call(defs1)}, other) {
        $variable = $testing
    }

    define othermethod(#{mkdef.call(defs2)}, goodness) {
        $more = less
    }

    method {
        other => yayness
    }

    othermethod {
        goodness => rahness
    }
"
        }

        ast = nil
        assert_nothing_raised("Could not parse multiple files") {
            parser = Puppet::Parser::Parser.new()
            parser.file = manifest
            ast = parser.parse
        }

        assert(ast, "Did not receive AST while parsing defaults")

        scope = nil
        assert_nothing_raised("Could not evaluate defaults parse tree") {
            scope = Puppet::Parser::Scope.new()
            scope.name = "parsetest"
            scope.type = "parsetest"
            objects = scope.evaluate(:ast => ast)
        }

        method = nil
        othermethod = nil
        assert_nothing_raised {
            method = scope.find { |child|
                child.is_a?(Puppet::Parser::Scope) and child.type == "method"
            }
            defs1.each { |var, value|
                curval = method.lookupvar(var)
                assert_equal(value, curval, "Did not get default")
            }
        }

        assert_nothing_raised {
            method = scope.find { |child|
                child.is_a?(Puppet::Parser::Scope) and child.type == "othermethod"
            }
            defs2.each { |var, value|
                curval = method.lookupvar(var)
                assert_equal(value, curval, "Did not get default")
            }
        }
    end

    def test_trailingcomma
        path = tempfile()
        str = %{file { "#{path}": ensure => file, }
        }

        parser = Puppet::Parser::Parser.new
        parser.string = str

        assert_nothing_raised("Could not parse trailing comma") {
            parser.parse
        }
    end
end

# $Id$