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

require File.dirname(__FILE__) + '/../../spec_helper'
require 'puppet/indirector/file'

module FileTerminusTesting
    def setup
        Puppet::Indirector::Terminus.stubs(:register_terminus_class)
        @model = mock 'model'
        @indirection = stub 'indirection', :name => :mystuff, :register_terminus_type => nil, :model => @model
        Puppet::Indirector::Indirection.stubs(:instance).returns(@indirection)

        @file_class = Class.new(Puppet::Indirector::File) do
            def self.to_s
                "Testing"
            end
        end

        @searcher = @file_class.new

        @path = "/my/file"
        @dir = "/my"
    end
end

describe Puppet::Indirector::File, " when finding files" do
    include FileTerminusTesting

    it "should provide a method to return file contents at a specified path" do
    end

    it "should return file contents as an instance of the model" do
        content = "my content"

        file = mock 'file'
        @model.expects(:new).with(@path).returns(file)
        file.expects(:content=).with(content)

        File.expects(:exist?).with(@path).returns(true)
        File.expects(:read).with(@path).returns(content)
        @searcher.find(@path)
    end

    it "should set the file contents as the 'content' attribute of the returned instance" do
        content = "my content"

        file = mock 'file'
        @model.expects(:new).with(@path).returns(file)
        file.expects(:content=).with(content)

        File.expects(:exist?).with(@path).returns(true)
        File.expects(:read).with(@path).returns(content)
        @searcher.find(@path).should equal(file)
    end

    it "should return nil if no file is found" do
        File.expects(:exist?).with(@path).returns(false)
        @searcher.find(@path).should be_nil
    end

    it "should fail intelligently if a found file cannot be read" do
        content = "my content"
        File.expects(:exist?).with(@path).returns(true)
        File.expects(:read).with(@path).raises(RuntimeError)
        proc { @searcher.find(@path) }.should raise_error(Puppet::Error)
    end
end

describe Puppet::Indirector::File, " when saving files" do
    include FileTerminusTesting

    it "should provide a method to save file contents at a specified path" do
        filehandle = mock 'file'
        content = "my content"
        File.expects(:directory?).with(@dir).returns(true)
        File.expects(:open).with(@path, "w").yields(filehandle)
        filehandle.expects(:print).with(content)

        file = stub 'file', :content => content, :path => @path, :name => @path

        @searcher.save(file)
    end

    it "should fail intelligently if the file's parent directory does not exist" do
        File.expects(:directory?).with(@dir).returns(false)

        file = stub 'file', :path => @path, :name => @path

        proc { @searcher.save(file) }.should raise_error(Puppet::Error)
    end

    it "should fail intelligently if a file cannot be written" do
        filehandle = mock 'file'
        content = "my content"
        File.expects(:directory?).with(@dir).returns(true)
        File.expects(:open).with(@path, "w").yields(filehandle)
        filehandle.expects(:print).with(content).raises(ArgumentError)

        file = stub 'file', :content => content, :path => @path, :name => @path

        proc { @searcher.save(file) }.should raise_error(Puppet::Error)
    end
end

describe Puppet::Indirector::File, " when removing files" do
    include FileTerminusTesting

    it "should provide a method to remove files at a specified path" do
        file = stub 'file', :path => @path, :name => @path
        File.expects(:exist?).with(@path).returns(true)
        File.expects(:unlink).with(@path)

        @searcher.destroy(file)
    end

    it "should throw an exception if the file is not found" do
        file = stub 'file', :path => @path, :name => @path
        File.expects(:exist?).with(@path).returns(false)

        proc { @searcher.destroy(file) }.should raise_error(Puppet::Error)
    end

    it "should fail intelligently if the file cannot be removed" do
        file = stub 'file', :path => @path, :name => @path
        File.expects(:exist?).with(@path).returns(true)
        File.expects(:unlink).with(@path).raises(ArgumentError)

        proc { @searcher.destroy(file) }.should raise_error(Puppet::Error)
    end
end