File: uniquefile_spec.rb

package info (click to toggle)
puppet 5.5.22-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 21,316 kB
  • sloc: ruby: 254,925; sh: 1,608; xml: 219; makefile: 153; sql: 103
file content (215 lines) | stat: -rw-r--r-- 5,763 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
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
213
214
215
require 'spec_helper'

describe Puppet::FileSystem::Uniquefile do
  include PuppetSpec::Files

  it "makes the name of the file available" do
    Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
      expect(file.path).to match(/foo/)
    end
  end

  it "provides a writeable file" do
    Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
      file.write("stuff")
      file.flush

      expect(Puppet::FileSystem.read(file.path)).to eq("stuff")
    end
  end

  it "returns the value of the block" do
    the_value = Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
      "my value"
    end

    expect(the_value).to eq("my value")
  end

  it "unlinks the temporary file" do
    filename = Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
      file.path
    end

    expect(Puppet::FileSystem.exist?(filename)).to be_falsey
  end

  it "unlinks the temporary file even if the block raises an error" do
    filename = nil

    begin
      Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
        filename = file.path
        raise "error!"
      end
    rescue
    end

    expect(Puppet::FileSystem.exist?(filename)).to be_falsey
  end

  it "propagates lock creation failures" do
    # use an arbitrary exception so as not accidentally collide
    # with the ENOENT that occurs when trying to call rmdir
    allow(Puppet::FileSystem::Uniquefile).to receive(:mkdir).and_raise('arbitrary failure')
    expect(Puppet::FileSystem::Uniquefile).not_to receive(:rmdir)

    expect {
      Puppet::FileSystem::Uniquefile.open_tmp('foo') { |tmp| }
    }.to raise_error('arbitrary failure')
  end

  it "only removes lock files that exist" do
    # prevent the .lock directory from being created
    allow(Puppet::FileSystem::Uniquefile).to receive(:mkdir)

    # and expect cleanup to be skipped
    expect(Puppet::FileSystem::Uniquefile).not_to receive(:rmdir)

    Puppet::FileSystem::Uniquefile.open_tmp('foo') { |tmp| }
  end

  it "reports when a parent directory does not exist" do
    dir = tmpdir('uniquefile')
    lock = File.join(dir, 'path', 'to', 'lock')

    expect {
      Puppet::FileSystem::Uniquefile.open_tmp(lock) { |tmp| }
    }.to raise_error(Errno::ENOENT, %r{No such file or directory - A directory component in .* does not exist or is a dangling symbolic link})
  end

  context "Ruby 1.9.3 Tempfile tests" do
    # the remaining tests in this file are ported directly from the ruby 1.9.3 source,
    # since most of this file was ported from there
    # see: https://github.com/ruby/ruby/blob/v1_9_3_547/test/test_tempfile.rb

    def tempfile(*args, &block)
      t = Puppet::FileSystem::Uniquefile.new(*args, &block)
      @tempfile = (t unless block)
    end

    after(:each) do
      if @tempfile
        @tempfile.close!
      end
    end

    it "creates tempfiles" do
      t = tempfile("foo")
      path = t.path
      t.write("hello world")
      t.close
      expect(File.read(path)).to eq("hello world")
    end

    it "saves in tmpdir by default" do
      t = tempfile("foo")
      expect(Dir.tmpdir).to eq(File.dirname(t.path))
    end

    it "saves in given directory" do
      subdir = File.join(Dir.tmpdir, "tempfile-test-#{rand}")
      Dir.mkdir(subdir)
      begin
        tempfile = Tempfile.new("foo", subdir)
        tempfile.close
        begin
          expect(subdir).to eq(File.dirname(tempfile.path))
        ensure
          tempfile.unlink
        end
      ensure
        Dir.rmdir(subdir)
      end
    end

    it "supports basename" do
      t = tempfile("foo")
      expect(File.basename(t.path)).to match(/^foo/)
    end

    it "supports basename with suffix" do
      t = tempfile(["foo", ".txt"])
      expect(File.basename(t.path)).to match(/^foo/)
      expect(File.basename(t.path)).to match(/\.txt$/)
    end

    it "supports unlink" do
      t = tempfile("foo")
      path = t.path
      t.close
      expect(File.exist?(path)).to eq(true)
      t.unlink
      expect(File.exist?(path)).to eq(false)
      expect(t.path).to eq(nil)
    end

    it "supports closing" do
      t = tempfile("foo")
      expect(t.closed?).to eq(false)
      t.close
      expect(t.closed?).to eq(true)
    end

    it "supports closing and unlinking via boolean argument" do
      t = tempfile("foo")
      path = t.path
      t.close(true)
      expect(t.closed?).to eq(true)
      expect(t.path).to eq(nil)
      expect(File.exist?(path)).to eq(false)
    end

    context "on unix platforms", :unless => Puppet.features.microsoft_windows? do
      it "close doesn't unlink if already unlinked" do
        t = tempfile("foo")
        path = t.path
        t.unlink
        File.open(path, "w").close
        begin
          t.close(true)
          expect(File.exist?(path)).to eq(true)
        ensure
          File.unlink(path) rescue nil
        end
      end
    end

    it "supports close!" do
      t = tempfile("foo")
      path = t.path
      t.close!
      expect(t.closed?).to eq(true)
      expect(t.path).to eq(nil)
      expect(File.exist?(path)).to eq(false)
    end

    context "on unix platforms", :unless => Puppet.features.microsoft_windows? do
      it "close! doesn't unlink if already unlinked" do
        t = tempfile("foo")
        path = t.path
        t.unlink
        File.open(path, "w").close
        begin
          t.close!
          expect(File.exist?(path)).to eq(true)
        ensure
          File.unlink(path) rescue nil
        end
      end
    end

    it "close does not make path nil" do
      t = tempfile("foo")
      t.close
      expect(t.path.nil?).to eq(false)
    end

    it "close flushes buffer" do
      t = tempfile("foo")
      t.write("hello")
      t.close
      expect(File.size(t.path)).to eq(5)
    end
  end
end