summaryrefslogtreecommitdiff
path: root/test/test_tempfile.rb
blob: 2fe62d37c5b4bb2aad4d0cd662732b86e5b79f39 (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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
require 'test/unit'
require 'tempfile'
require 'thread'

class TestTempfile < Test::Unit::TestCase
  def initialize(*)
    super
    @tempfile = nil
  end

  def tempfile(*args, &block)
    t = Tempfile.new(*args, &block)
    @tempfile = (t unless block)
  end

  def teardown
    if @tempfile
      @tempfile.close!
    end
  end

  def test_basic
    t = tempfile("foo")
    path = t.path
    t.write("hello world")
    t.close
    assert_equal "hello world", File.read(path)
  end

  def test_saves_in_dir_tmpdir_by_default
    t = tempfile("foo")
    assert_equal Dir.tmpdir, File.dirname(t.path)
    bug3733 = '[ruby-dev:42089]'
    assert_nothing_raised(SecurityError, bug3733) {
      proc {$SAFE = 1; File.expand_path(Dir.tmpdir)}.call
    }
  end

  def test_saves_in_given_directory
    subdir = File.join(Dir.tmpdir, "tempfile-test-#{rand}")
    Dir.mkdir(subdir)
    begin
      tempfile = Tempfile.new("foo", subdir)
      tempfile.close
      begin
        assert_equal subdir, File.dirname(tempfile.path)
      ensure
        tempfile.unlink
      end
    ensure
      Dir.rmdir(subdir)
    end
  end

  def test_basename
    t = tempfile("foo")
    assert_match(/^foo/, File.basename(t.path))
  end

  def test_basename_with_suffix
    t = tempfile(["foo", ".txt"])
    assert_match(/^foo/, File.basename(t.path))
    assert_match(/\.txt$/, File.basename(t.path))
  end

  def test_unlink
    t = tempfile("foo")
    path = t.path

    t.close
    assert File.exist?(path)

    t.unlink
    assert !File.exist?(path)

    assert_nil t.path
  end

  def test_unlink_silently_fails_on_windows
    tempfile = tempfile("foo")
    path = tempfile.path
    begin
      assert_nothing_raised do
        tempfile.unlink
      end
    ensure
      tempfile.close
      File.unlink(path) if File.exist?(path)
    end
  end

  def test_unlink_before_close_works_on_posix_systems
    tempfile = tempfile("foo")
    begin
      path = tempfile.path
      tempfile.unlink
      assert !File.exist?(path)
      tempfile.write("hello ")
      tempfile.write("world\n")
      tempfile.rewind
      assert_equal "hello world\n", tempfile.read
    ensure
      tempfile.close
      tempfile.unlink
    end
  end unless /mswin|mingw/ =~ RUBY_PLATFORM

  def test_close_and_close_p
    t = tempfile("foo")
    assert !t.closed?
    t.close
    assert t.closed?
  end

  def test_close_with_unlink_now_true_works
    t = tempfile("foo")
    path = t.path
    t.close(true)
    assert t.closed?
    assert_nil t.path
    assert !File.exist?(path)
  end

  def test_close_with_unlink_now_true_does_not_unlink_if_already_unlinked
    t = tempfile("foo")
    path = t.path
    t.unlink
    File.open(path, "w").close
    begin
      t.close(true)
      assert File.exist?(path)
    ensure
      File.unlink(path) rescue nil
    end
  end unless /mswin|mingw/ =~ RUBY_PLATFORM

  def test_close_bang_works
    t = tempfile("foo")
    path = t.path
    t.close!
    assert t.closed?
    assert_nil t.path
    assert !File.exist?(path)
  end

  def test_close_bang_does_not_unlink_if_already_unlinked
    t = tempfile("foo")
    path = t.path
    t.unlink
    File.open(path, "w").close
    begin
      t.close!
      assert File.exist?(path)
    ensure
      File.unlink(path) rescue nil
    end
  end unless /mswin|mingw/ =~ RUBY_PLATFORM

  def test_finalizer_does_not_unlink_if_already_unlinked
    assert_in_out_err('-rtempfile', <<-'EOS') do |(filename,*), (error,*)|
file = Tempfile.new('foo')
path = file.path
puts path
file.close!
File.open(path, "w").close
    EOS
      assert File.exist?(filename)
      File.unlink(filename)
      assert_nil error
    end

    assert_in_out_err('-rtempfile', <<-'EOS') do |(filename,*), (error,*)|
file = Tempfile.new('foo')
path = file.path
file.unlink
puts path
File.open(path, "w").close
    EOS
      if !filename.empty?
        # POSIX unlink semantics supported, continue with test
        assert File.exist?(filename)
        File.unlink(filename)
      end
      assert_nil error
    end
  end unless /mswin|mingw/ =~ RUBY_PLATFORM

  def test_close_does_not_make_path_nil
    t = tempfile("foo")
    t.close
    assert_not_nil t.path
  end

  def test_close_flushes_buffer
    t = tempfile("foo")
    t.write("hello")
    t.close
    assert_equal 5, File.size(t.path)
  end

  def test_tempfile_is_unlinked_when_ruby_exits
    assert_in_out_err('-rtempfile', <<-'EOS') do |(filename), (error)|
puts Tempfile.new('foo').path
    EOS
      assert !File.exist?(filename)
      assert_nil(error)
    end
  end

  def test_tempfile_finalizer_does_not_run_if_unlinked
    bug8768 = '[ruby-core:56521] [Bug #8768]'
    args = %w(--disable-gems -rtempfile)
    assert_in_out_err(args, <<-'EOS') do |(filename), (error)|
      tmp = Tempfile.new('foo')
      puts tmp.path
      tmp.close
      tmp.unlink
      $DEBUG = true
      EOS
      assert_file.not_exist?(filename)
      assert_nil(error, "#{bug8768} we used to get a confusing 'removing ...done' here")
    end
  end

  def test_size_flushes_buffer_before_determining_file_size
    t = tempfile("foo")
    t.write("hello")
    assert_equal 0, File.size(t.path)
    assert_equal 5, t.size
    assert_equal 5, File.size(t.path)
  end

  def test_size_works_if_file_is_closed
    t = tempfile("foo")
    t.write("hello")
    t.close
    assert_equal 5, t.size
  end

  def test_concurrency
    threads = []
    tempfiles = []
    lock = Mutex.new
    cond = ConditionVariable.new
    start = false

    4.times do
      threads << Thread.new do
        lock.synchronize do
          while !start
            cond.wait(lock)
          end
        end
        result = []
        30.times do
          result << Tempfile.new('foo')
        end
        Thread.current[:result] = result
      end
    end

    lock.synchronize do
      start = true
      cond.broadcast
    end
    threads.each do |thread|
      thread.join
      tempfiles |= thread[:result]
    end
    filenames = tempfiles.map { |f| f.path }
    begin
      assert_equal filenames.size, filenames.uniq.size
    ensure
      tempfiles.each do |tempfile|
        tempfile.close!
      end
    end
  end

  module M
  end

  def test_extend
    o = tempfile("foo")
    o.extend M
    assert(M === o, "[ruby-dev:32932]")
  end

  def test_tempfile_encoding_nooption
    default_external=Encoding.default_external
    t = tempfile("TEST")
    t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
    t.rewind
    assert_equal(default_external,t.read.encoding)
  end

  def test_tempfile_encoding_ascii8bit
    t = tempfile("TEST",:encoding=>"ascii-8bit")
    t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
    t.rewind
    assert_equal(Encoding::ASCII_8BIT,t.read.encoding)
  end

  def test_tempfile_encoding_ascii8bit2
    t = tempfile("TEST",Dir::tmpdir,:encoding=>"ascii-8bit")
    t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
    t.rewind
    assert_equal(Encoding::ASCII_8BIT,t.read.encoding)
  end

  def test_binmode
    t = tempfile("TEST", mode: IO::BINARY)
    if IO::BINARY.nonzero?
      assert(t.binmode?)
      t.open
      assert(t.binmode?, 'binmode after reopen')
    else
      assert_equal(0600, t.stat.mode & 0777)
    end
  end

  def test_create_with_block
    path = nil
    Tempfile.create("tempfile-create") {|f|
      path = f.path
      assert(File.exist?(path))
    }
    assert(!File.exist?(path))
  end

  def test_create_without_block
    path = nil
    f = Tempfile.create("tempfile-create")
    path = f.path
    assert(File.exist?(path))
    f.close
    assert(File.exist?(path))
  ensure
    f.close if f && !f.closed?
    File.unlink path if path
  end
end