PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_tempfile.rb

https://github.com/fizx/ruby
Ruby | 302 lines | 265 code | 36 blank | 1 comment | 5 complexity | 84e5541ff09577277d7143d61227c365 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0, GPL-2.0, BSD-3-Clause
  1. require 'test/unit'
  2. require 'tempfile'
  3. require_relative 'ruby/envutil'
  4. class TestTempfile < Test::Unit::TestCase
  5. def initialize(*)
  6. super
  7. @tempfile = nil
  8. end
  9. def tempfile(*args, &block)
  10. t = Tempfile.new(*args, &block)
  11. @tempfile = (t unless block)
  12. end
  13. def teardown
  14. if @tempfile
  15. @tempfile.close!
  16. end
  17. end
  18. def test_basic
  19. t = tempfile("foo")
  20. path = t.path
  21. t.write("hello world")
  22. t.close
  23. assert_equal "hello world", File.read(path)
  24. end
  25. def test_saves_in_dir_tmpdir_by_default
  26. t = tempfile("foo")
  27. assert_equal Dir.tmpdir, File.dirname(t.path)
  28. end
  29. def test_saves_in_given_directory
  30. subdir = File.join(Dir.tmpdir, "tempfile-test-#{rand}")
  31. Dir.mkdir(subdir)
  32. begin
  33. tempfile = Tempfile.new("foo", subdir)
  34. tempfile.close
  35. begin
  36. assert_equal subdir, File.dirname(tempfile.path)
  37. ensure
  38. tempfile.unlink
  39. end
  40. ensure
  41. Dir.rmdir(subdir)
  42. end
  43. end
  44. def test_basename
  45. t = tempfile("foo")
  46. assert_match(/^foo/, File.basename(t.path))
  47. end
  48. def test_basename_with_suffix
  49. t = tempfile(["foo", ".txt"])
  50. assert_match(/^foo/, File.basename(t.path))
  51. assert_match(/\.txt$/, File.basename(t.path))
  52. end
  53. def test_unlink
  54. t = tempfile("foo")
  55. path = t.path
  56. t.close
  57. assert File.exist?(path)
  58. t.unlink
  59. assert !File.exist?(path)
  60. assert_nil t.path
  61. end
  62. def test_unlink_silently_fails_on_windows
  63. tempfile = tempfile("foo")
  64. path = tempfile.path
  65. begin
  66. assert_nothing_raised do
  67. tempfile.unlink
  68. end
  69. ensure
  70. tempfile.close
  71. File.unlink(path) if File.exist?(path)
  72. end
  73. end
  74. def test_unlink_before_close_works_on_posix_systems
  75. tempfile = tempfile("foo")
  76. begin
  77. path = tempfile.path
  78. tempfile.unlink
  79. assert !File.exist?(path)
  80. tempfile.write("hello ")
  81. tempfile.write("world\n")
  82. tempfile.rewind
  83. assert_equal "hello world\n", tempfile.read
  84. ensure
  85. tempfile.close
  86. tempfile.unlink
  87. end
  88. end
  89. def test_close_and_close_p
  90. t = tempfile("foo")
  91. assert !t.closed?
  92. t.close
  93. assert t.closed?
  94. end
  95. def test_close_with_unlink_now_true_works
  96. t = tempfile("foo")
  97. path = t.path
  98. t.close(true)
  99. assert t.closed?
  100. assert_nil t.path
  101. assert !File.exist?(path)
  102. end
  103. def test_close_with_unlink_now_true_does_not_unlink_if_already_unlinked
  104. t = tempfile("foo")
  105. path = t.path
  106. t.unlink
  107. File.open(path, "w").close
  108. begin
  109. t.close(true)
  110. assert File.exist?(path)
  111. ensure
  112. File.unlink(path) rescue nil
  113. end
  114. end
  115. def test_close_bang_works
  116. t = tempfile("foo")
  117. path = t.path
  118. t.close!
  119. assert t.closed?
  120. assert_nil t.path
  121. assert !File.exist?(path)
  122. end
  123. def test_close_bang_does_not_unlink_if_already_unlinked
  124. t = tempfile("foo")
  125. path = t.path
  126. t.unlink
  127. File.open(path, "w").close
  128. begin
  129. t.close!
  130. assert File.exist?(path)
  131. ensure
  132. File.unlink(path) rescue nil
  133. end
  134. end
  135. def test_finalizer_does_not_unlink_if_already_unlinked
  136. assert_in_out_err('-rtempfile', <<-'EOS') do |(filename), (error)|
  137. file = Tempfile.new('foo')
  138. path = file.path
  139. puts path
  140. file.close!
  141. File.open(path, "w").close
  142. EOS
  143. assert File.exist?(filename)
  144. File.unlink(filename)
  145. assert_nil error
  146. end
  147. assert_in_out_err('-rtempfile', <<-'EOS') do |(filename), (error)|
  148. file = Tempfile.new('foo')
  149. path = file.path
  150. file.unlink
  151. puts path
  152. File.open(path, "w").close
  153. EOS
  154. if !filename.empty?
  155. # POSIX unlink semantics supported, continue with test
  156. assert File.exist?(filename)
  157. File.unlink(filename)
  158. end
  159. assert_nil error
  160. end
  161. end
  162. def test_close_does_not_make_path_nil
  163. t = tempfile("foo")
  164. t.close
  165. assert_not_nil t.path
  166. end
  167. def test_close_flushes_buffer
  168. t = tempfile("foo")
  169. t.write("hello")
  170. t.close
  171. assert 5, File.size(t.path)
  172. end
  173. def test_tempfile_is_unlinked_when_ruby_exits
  174. assert_in_out_err('-rtempfile', <<-'EOS') do |(filename), (error)|
  175. puts Tempfile.new('foo').path
  176. EOS
  177. assert !File.exist?(filename)
  178. end
  179. end
  180. def test_size_flushes_buffer_before_determining_file_size
  181. t = tempfile("foo")
  182. t.write("hello")
  183. assert 0, File.size(t.path)
  184. assert 5, t.size
  185. assert 5, File.size(t.path)
  186. end
  187. def test_size_works_if_file_is_closed
  188. t = tempfile("foo")
  189. t.write("hello")
  190. t.close
  191. assert 5, t.size
  192. end
  193. def test_concurrency
  194. threads = []
  195. tempfiles = []
  196. lock = Mutex.new
  197. cond = ConditionVariable.new
  198. start = false
  199. 4.times do
  200. threads << Thread.new do
  201. lock.synchronize do
  202. while !start
  203. cond.wait(lock)
  204. end
  205. end
  206. result = []
  207. 30.times do
  208. result << Tempfile.new('foo')
  209. end
  210. Thread.current[:result] = result
  211. end
  212. end
  213. lock.synchronize do
  214. start = true
  215. cond.broadcast
  216. end
  217. threads.each do |thread|
  218. thread.join
  219. tempfiles |= thread[:result]
  220. end
  221. filenames = tempfiles.map { |f| f.path }
  222. begin
  223. assert_equal filenames.size, filenames.uniq.size
  224. ensure
  225. tempfiles.each do |tempfile|
  226. tempfile.close!
  227. end
  228. end
  229. end
  230. module M
  231. end
  232. def test_extend
  233. o = tempfile("foo")
  234. o.extend M
  235. assert(M === o, "[ruby-dev:32932]")
  236. end
  237. def test_tempfile_encoding_nooption
  238. default_external=Encoding.default_external
  239. t = tempfile("TEST")
  240. t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
  241. t.rewind
  242. assert_equal(default_external,t.read.encoding)
  243. end
  244. def test_tempfile_encoding_ascii8bit
  245. default_external=Encoding.default_external
  246. t = tempfile("TEST",:encoding=>"ascii-8bit")
  247. t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
  248. t.rewind
  249. assert_equal(Encoding::ASCII_8BIT,t.read.encoding)
  250. end
  251. def test_tempfile_encoding_ascii8bit2
  252. default_external=Encoding.default_external
  253. t = tempfile("TEST",Dir::tmpdir,:encoding=>"ascii-8bit")
  254. t.write("\xE6\x9D\xBE\xE6\xB1\x9F")
  255. t.rewind
  256. assert_equal(Encoding::ASCII_8BIT,t.read.encoding)
  257. end
  258. def test_binmode
  259. t = tempfile("TEST", mode: IO::BINARY)
  260. if IO::BINARY.nonzero?
  261. assert(t.binmode?)
  262. else
  263. assert_equal(0600, t.stat.mode & 0777)
  264. end
  265. end
  266. end