summaryrefslogtreecommitdiffstats
path: root/test/util/features.rb
blob: ef69ad72b144a10afc6b9bbb09a1fa9878f7951e (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
#!/usr/bin/env ruby
#
#  Created by Luke Kanies on 2006-11-07.
#  Copyright (c) 2006. All rights reserved.

require File.dirname(__FILE__) + '/../lib/puppettest'

require 'puppettest'
require 'puppet/util/feature'

class TestFeatures < Test::Unit::TestCase
    include PuppetTest

    def setup
        super
        @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"
            end
        end
        
        assert(@features.respond_to?(:failer?), "Feature method did not get added")
        assert_nothing_raised("failure propagated outside of feature") do
            assert(! @features.failer?, "failure was considered true")
        end
        
        # Now make one that succeeds
        $succeeds = nil
        assert_nothing_raised("Failed to add normal feature") do
            @features.add(:succeeds) do
                $succeeds = true
            end
        end
        assert($succeeds, "Block was not called on initialization")
        
        assert(@features.respond_to?(:succeeds?), "Did not add succeeding feature")
        assert_nothing_raised("Failed to call succeeds") { assert(@features.succeeds?, "Feature was not true") }
    end
    
    def test_libs
        assert_nothing_raised do
            @features.add(:puppet, :libs => %w{puppet})
        end
        
        assert(@features.puppet?)
        
        assert_nothing_raised do
            @features.add(:missing, :libs => %w{puppet no/such/library/okay})
        end
        
        assert(! @features.missing?, "Missing lib was considered true")
    end

    def test_dynamic_loading
        # Make sure it defaults to false
        assert_nothing_raised("Undefined features throw an exception") do
            assert(! @features.nosuchfeature?, "missing feature returned true")
        end

        $features = @features
        cleanup { $features = nil }
        # Now create a feature and make sure it loads.
        FileUtils.mkdir_p(@path)
        nope = File.join(@path, "nope.rb")
        File.open(nope, "w") { |f|
            f.puts "$features.add(:nope, :libs => %w{nosuchlib})"
        }
        assert_nothing_raised("Failed to autoload features") do
            assert(! @features.nope?, "'nope' returned true")
        end

        # First make sure "yep?" returns false
        assert_nothing_raised("Missing feature threw an exception") do
            assert(! @features.yep?, "'yep' returned true before definition")
        end

        yep = File.join(@path, "yep.rb")
        File.open(yep, "w") { |f|
            f.puts "$features.add(:yep, :libs => %w{puppet})"
        }

        # Now make sure the value is not cached or anything.
        assert_nothing_raised("Failed to autoload features") do
            assert(@features.yep?, "'yep' returned false")
        end
    end
end