summaryrefslogtreecommitdiffstats
path: root/spec/unit/file_serving/base.rb
blob: de7ea475674fc8121ba44e794df49894c1aa521f (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
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../../spec_helper'

require 'puppet/file_serving/base'

describe Puppet::FileServing::Base do
    it "should accept a path" do
        Puppet::FileServing::Base.new("/module/dir/file").path.should == "/module/dir/file"
    end

    it "should require that paths be fully qualified" do
        lambda { Puppet::FileServing::Base.new("module/dir/file") }.should raise_error(ArgumentError)
    end

    it "should allow specification of whether links should be managed" do
        Puppet::FileServing::Base.new("/module/dir/file", :links => :manage).links.should == :manage
    end

    it "should have a :source attribute" do
        file = Puppet::FileServing::Base.new("/module/dir/file")
        file.should respond_to(:source)
        file.should respond_to(:source=)
    end

    it "should consider :ignore links equivalent to :manage links" do
        Puppet::FileServing::Base.new("/module/dir/file", :links => :ignore).links.should == :manage
    end

    it "should fail if :links is set to anything other than :manage, :follow, or :ignore" do
        proc { Puppet::FileServing::Base.new("/module/dir/file", :links => :else) }.should raise_error(ArgumentError)
    end

    it "should allow links values to be set as strings" do
        Puppet::FileServing::Base.new("/module/dir/file", :links => "follow").links.should == :follow
    end

    it "should default to :manage for :links" do
        Puppet::FileServing::Base.new("/module/dir/file").links.should == :manage
    end

    it "should allow specification of a path" do
        FileTest.stubs(:exists?).returns(true)
        Puppet::FileServing::Base.new("/module/dir/file", :path => "/my/file").path.should == "/my/file"
    end

    it "should allow specification of a relative path" do
        FileTest.stubs(:exists?).returns(true)
        Puppet::FileServing::Base.new("/module/dir/file", :relative_path => "my/file").relative_path.should == "my/file"
    end

    it "should have a means of determining if the file exists" do
        Puppet::FileServing::Base.new("/blah").should respond_to(:exist?)
    end

    it "should correctly indicate if the file is present" do
        File.expects(:lstat).with("/my/file").returns(mock("stat"))
        Puppet::FileServing::Base.new("/my/file").exist?.should be_true
    end

    it "should correctly indicate if the file is absent" do
        File.expects(:lstat).with("/my/file").raises RuntimeError
        Puppet::FileServing::Base.new("/my/file").exist?.should be_false
    end

    describe "when setting the relative path" do
        it "should require that the relative path be unqualified" do
            @file = Puppet::FileServing::Base.new("/module/dir/file")
            FileTest.stubs(:exists?).returns(true)
            proc { @file.relative_path = "/qualified/file" }.should raise_error(ArgumentError)
        end
    end

    describe "when determining the full file path" do
        before do
            @file = Puppet::FileServing::Base.new("/this/file")
        end

        it "should return the path if there is no relative path" do
            @file.full_path.should == "/this/file"
        end

        it "should return the path if the relative_path is set to ''" do
            @file.relative_path = ""
            @file.full_path.should == "/this/file"
        end

        it "should return the path if the relative_path is set to '.'" do
            @file.relative_path = "."
            @file.full_path.should == "/this/file"
        end

        it "should return the path joined with the relative path if there is a relative path and it is not set to '/' or ''" do
            @file.relative_path = "not/qualified"
            @file.full_path.should == "/this/file/not/qualified"
        end

        it "should strip extra slashes" do
            file = Puppet::FileServing::Base.new("//this//file")
            file.full_path.should == "/this/file"
        end
    end

    describe "when stat'ing files" do
        before do
            @file = Puppet::FileServing::Base.new("/this/file")
        end

        it "should stat the file's full path" do
            @file.stubs(:full_path).returns("/this/file")
            File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file")
            @file.stat
        end

        it "should fail if the file does not exist" do
            @file.stubs(:full_path).returns("/this/file")
            File.expects(:lstat).with("/this/file").raises(Errno::ENOENT)
            proc { @file.stat }.should raise_error(Errno::ENOENT)
        end

        it "should use :lstat if :links is set to :manage" do
            File.expects(:lstat).with("/this/file").returns stub("stat", :ftype => "file")
            @file.stat
        end

        it "should use :stat if :links is set to :follow" do
            File.expects(:stat).with("/this/file").returns stub("stat", :ftype => "file")
            @file.links = :follow
            @file.stat
        end
    end
end