~ubuntu-branches/ubuntu/wily/puppet/wily-proposed

« back to all changes in this revision

Viewing changes to spec/unit/file_system/uniquefile_spec.rb

  • Committer: Package Import Robot
  • Author(s): Stig Sandbeck Mathisen
  • Date: 2014-10-24 13:47:15 UTC
  • mfrom: (3.1.64 sid)
  • Revision ID: package-import@ubuntu.com-20141024134715-6ig54u0c4gar36ss
Tags: 3.7.2-1
* Imported upstream release 3.7.2
* Declare compliance with Debian Policy 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require 'spec_helper'
 
2
 
 
3
describe Puppet::FileSystem::Uniquefile do
 
4
  it "makes the name of the file available" do
 
5
    Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
 
6
      expect(file.path).to match(/foo/)
 
7
    end
 
8
  end
 
9
 
 
10
  it "provides a writeable file" do
 
11
    Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
 
12
      file.write("stuff")
 
13
      file.flush
 
14
 
 
15
      expect(Puppet::FileSystem.read(file.path)).to eq("stuff")
 
16
    end
 
17
  end
 
18
 
 
19
  it "returns the value of the block" do
 
20
    the_value = Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
 
21
      "my value"
 
22
    end
 
23
 
 
24
    expect(the_value).to eq("my value")
 
25
  end
 
26
 
 
27
  it "unlinks the temporary file" do
 
28
    filename = Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
 
29
      file.path
 
30
    end
 
31
 
 
32
    expect(Puppet::FileSystem.exist?(filename)).to be_false
 
33
  end
 
34
 
 
35
  it "unlinks the temporary file even if the block raises an error" do
 
36
    filename = nil
 
37
 
 
38
    begin
 
39
      Puppet::FileSystem::Uniquefile.open_tmp('foo') do |file|
 
40
        filename = file.path
 
41
        raise "error!"
 
42
      end
 
43
    rescue
 
44
    end
 
45
 
 
46
    expect(Puppet::FileSystem.exist?(filename)).to be_false
 
47
  end
 
48
 
 
49
 
 
50
  context "Ruby 1.9.3 Tempfile tests" do
 
51
    # the remaining tests in this file are ported directly from the ruby 1.9.3 source,
 
52
    # since most of this file was ported from there
 
53
    # see: https://github.com/ruby/ruby/blob/v1_9_3_547/test/test_tempfile.rb
 
54
 
 
55
    def tempfile(*args, &block)
 
56
      t = Puppet::FileSystem::Uniquefile.new(*args, &block)
 
57
      @tempfile = (t unless block)
 
58
    end
 
59
 
 
60
    after(:each) do
 
61
      if @tempfile
 
62
        @tempfile.close!
 
63
      end
 
64
    end
 
65
 
 
66
    it "creates tempfiles" do
 
67
      t = tempfile("foo")
 
68
      path = t.path
 
69
      t.write("hello world")
 
70
      t.close
 
71
      expect(File.read(path)).to eq("hello world")
 
72
    end
 
73
 
 
74
    it "saves in tmpdir by default" do
 
75
      t = tempfile("foo")
 
76
      expect(Dir.tmpdir).to eq(File.dirname(t.path))
 
77
    end
 
78
 
 
79
    it "saves in given directory" do
 
80
      subdir = File.join(Dir.tmpdir, "tempfile-test-#{rand}")
 
81
      Dir.mkdir(subdir)
 
82
      begin
 
83
        tempfile = Tempfile.new("foo", subdir)
 
84
        tempfile.close
 
85
        begin
 
86
          expect(subdir).to eq(File.dirname(tempfile.path))
 
87
        ensure
 
88
          tempfile.unlink
 
89
        end
 
90
      ensure
 
91
        Dir.rmdir(subdir)
 
92
      end
 
93
    end
 
94
 
 
95
    it "supports basename" do
 
96
      t = tempfile("foo")
 
97
      expect(File.basename(t.path)).to match(/^foo/)
 
98
    end
 
99
 
 
100
    it "supports basename with suffix" do
 
101
      t = tempfile(["foo", ".txt"])
 
102
      expect(File.basename(t.path)).to match(/^foo/)
 
103
      expect(File.basename(t.path)).to match(/\.txt$/)
 
104
    end
 
105
 
 
106
    it "supports unlink" do
 
107
      t = tempfile("foo")
 
108
      path = t.path
 
109
      t.close
 
110
      expect(File.exist?(path)).to eq(true)
 
111
      t.unlink
 
112
      expect(File.exist?(path)).to eq(false)
 
113
      expect(t.path).to eq(nil)
 
114
    end
 
115
 
 
116
    it "supports closing" do
 
117
      t = tempfile("foo")
 
118
      expect(t.closed?).to eq(false)
 
119
      t.close
 
120
      expect(t.closed?).to eq(true)
 
121
    end
 
122
 
 
123
    it "supports closing and unlinking via boolean argument" do
 
124
      t = tempfile("foo")
 
125
      path = t.path
 
126
      t.close(true)
 
127
      expect(t.closed?).to eq(true)
 
128
      expect(t.path).to eq(nil)
 
129
      expect(File.exist?(path)).to eq(false)
 
130
    end
 
131
 
 
132
    context "on unix platforms", :unless => Puppet.features.microsoft_windows? do
 
133
      it "close doesn't unlink if already unlinked" do
 
134
        t = tempfile("foo")
 
135
        path = t.path
 
136
        t.unlink
 
137
        File.open(path, "w").close
 
138
        begin
 
139
          t.close(true)
 
140
          expect(File.exist?(path)).to eq(true)
 
141
        ensure
 
142
          File.unlink(path) rescue nil
 
143
        end
 
144
      end
 
145
    end
 
146
 
 
147
    it "supports close!" do
 
148
      t = tempfile("foo")
 
149
      path = t.path
 
150
      t.close!
 
151
      expect(t.closed?).to eq(true)
 
152
      expect(t.path).to eq(nil)
 
153
      expect(File.exist?(path)).to eq(false)
 
154
    end
 
155
 
 
156
    context "on unix platforms", :unless => Puppet.features.microsoft_windows? do
 
157
      it "close! doesn't unlink if already unlinked" do
 
158
        t = tempfile("foo")
 
159
        path = t.path
 
160
        t.unlink
 
161
        File.open(path, "w").close
 
162
        begin
 
163
          t.close!
 
164
          expect(File.exist?(path)).to eq(true)
 
165
        ensure
 
166
          File.unlink(path) rescue nil
 
167
        end
 
168
      end
 
169
    end
 
170
 
 
171
    it "close does not make path nil" do
 
172
      t = tempfile("foo")
 
173
      t.close
 
174
      expect(t.path.nil?).to eq(false)
 
175
    end
 
176
 
 
177
    it "close flushes buffer" do
 
178
      t = tempfile("foo")
 
179
      t.write("hello")
 
180
      t.close
 
181
      expect(File.size(t.path)).to eq(5)
 
182
    end
 
183
  end
 
184
end