File: file_tests.rb

package info (click to toggle)
ruby-fog-local 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 212 kB
  • sloc: ruby: 709; makefile: 2
file content (155 lines) | stat: -rw-r--r-- 4,727 bytes parent folder | download
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
Shindo.tests('Storage[:local] | file', ["local"]) do

  pending if Fog.mocking?

  before do
    @options = { :local_root => Dir.mktmpdir('fog-tests') }
  end

  after do
    FileUtils.remove_entry_secure @options[:local_root]
  end

  tests('#public_url') do
    tests('when connection has an endpoint').
      returns('http://example.com/files/directory/file.txt') do
        @options[:endpoint] = 'http://example.com/files'

        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'directory')
        file = directory.files.new(:key => 'file.txt')

        file.public_url
      end

    tests('when connection has no endpoint').
      returns(nil) do
        @options[:endpoint] = nil

        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'directory')
        file = directory.files.new(:key => 'file.txt')

        file.public_url
      end

    tests('when file path has escapable characters').
      returns('http://example.com/files/my%20directory/my%20file.txt') do
        @options[:endpoint] = 'http://example.com/files'

        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'my directory')
        file = directory.files.new(:key => 'my file.txt')

        file.public_url
      end

    tests('when key has safe characters').
      returns('http://example.com/files/my/directory/my/file.txt') do
        @options[:endpoint] = 'http://example.com/files'

        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'my/directory')
        file = directory.files.new(:key => 'my/file.txt')

        file.public_url
      end
  end

  tests('#save') do
    tests('creates non-existent subdirs') do
      returns(true) do
        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'path1')
        file = directory.files.new(:key => 'path2/file.rb', :body => "my contents")
        file.save
        File.exists?(@options[:local_root] + "/path1/path2/file.rb")
      end
    end

    tests('with tempfile').returns('tempfile') do
      connection = Fog::Local::Storage.new(@options)
      directory = connection.directories.create(:key => 'directory')

      tempfile = Tempfile.new(['file', '.txt'])
      tempfile.write('tempfile')
      tempfile.rewind

      tempfile.instance_eval do
        def read
          raise 'must not be read'
        end
      end
      file = directory.files.new(:key => 'tempfile.txt', :body => tempfile)
      file.save
      tempfile.close
      tempfile.unlink
      directory.files.get('tempfile.txt').body
    end
  end

  tests('#destroy') do
    # - removes dir if it contains no files
    # - keeps dir if it contains non-hidden files
    # - keeps dir if it contains hidden files
    # - stays in the same directory

    tests('removes enclosing dir if it is empty') do
      returns(false) do
        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'path1')

        file = directory.files.new(:key => 'path2/file.rb', :body => "my contents")
        file.save
        file.destroy

        File.exists?(@options[:local_root] + "/path1/path2")
      end
    end

    tests('keeps enclosing dir if it is not empty') do
      returns(true) do
        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'path1')

        file = directory.files.new(:key => 'path2/file.rb', :body => "my contents")
        file.save

        file = directory.files.new(:key => 'path2/file2.rb', :body => "my contents")
        file.save
        file.destroy

        File.exists?(@options[:local_root] + "/path1/path2")
      end
    end

    tests('keeps enclosing dir if contains only hidden files') do
      returns(true) do
        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'path1')

        file = directory.files.new(:key => 'path2/.file.rb', :body => "my contents")
        file.save

        file = directory.files.new(:key => 'path2/.file2.rb', :body => "my contents")
        file.save
        file.destroy

        File.exists?(@options[:local_root] + "/path1/path2")
      end
    end

    tests('it stays in the same directory') do
      returns(Dir.pwd) do
        connection = Fog::Local::Storage.new(@options)
        directory = connection.directories.new(:key => 'path1')

        file = directory.files.new(:key => 'path2/file2.rb', :body => "my contents")
        file.save
        file.destroy

        Dir.pwd
      end
    end
  end
end