PageRenderTime 62ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/test_file.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1160 lines | 919 code | 149 blank | 92 comment | 8 complexity | 8446bafc0c5be1edc81fc453c1e9d777 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. require 'test/unit'
  3. require 'rbconfig'
  4. require 'fileutils'
  5. require 'tempfile'
  6. require 'pathname'
  7. require 'jruby'
  8. class TestFile < Test::Unit::TestCase
  9. WINDOWS = RbConfig::CONFIG['host_os'] =~ /Windows|mswin/
  10. def setup
  11. @teardown_blocks = []
  12. end
  13. def teardown
  14. @teardown_blocks.each do |b|
  15. b.call
  16. end
  17. end
  18. def jruby_specific_test
  19. flunk("JRuby specific test") unless defined?(JRUBY_VERSION)
  20. end
  21. def test_file_separator_constants_defined
  22. assert(File::SEPARATOR)
  23. assert(File::PATH_SEPARATOR)
  24. end
  25. def test_basename
  26. assert_equal("", File.basename(""))
  27. assert_equal("a", File.basename("a"))
  28. assert_equal("b", File.basename("a/b"))
  29. assert_equal("c", File.basename("a/b/c"))
  30. assert_equal("b", File.basename("a/b", ""))
  31. assert_equal("b", File.basename("a/bc", "c"))
  32. assert_equal("b", File.basename("a/b.c", ".c"))
  33. assert_equal("b", File.basename("a/b.c", ".*"))
  34. assert_equal(".c", File.basename("a/.c", ".*"))
  35. assert_equal("a", File.basename("a/"))
  36. assert_equal("b", File.basename("a/b/"))
  37. assert_equal("/", File.basename("/"))
  38. end
  39. # Added to add more flexibility on windows. Depending on subsystem (msys,
  40. # cygwin, cmd) environment sometimes we have mixed case drive letters. Since
  41. # windows is still case insensitive, downcasing seems a simple solution.
  42. def paths_equal(expected, actual)
  43. if WINDOWS
  44. assert_equal(expected.downcase, actual.downcase)
  45. else
  46. assert_equal(expected, actual)
  47. end
  48. end
  49. def test_expand_path_cross_platform
  50. paths_equal(Dir.pwd, File.expand_path(""))
  51. paths_equal(Dir.pwd, File.expand_path("."))
  52. paths_equal(Dir.pwd, File.expand_path(".", "."))
  53. paths_equal(Dir.pwd, File.expand_path("", "."))
  54. paths_equal(Dir.pwd, File.expand_path(".", ""))
  55. paths_equal(Dir.pwd, File.expand_path("", ""))
  56. paths_equal(File.join(Dir.pwd, "x/y/z/a/b"), File.expand_path("a/b", "x/y/z"))
  57. paths_equal(File.join(Dir.pwd, "bin"), File.expand_path("../../bin", "tmp/x"))
  58. # JRUBY-2143
  59. assert_nothing_raised {
  60. File.expand_path("../../bin", "/tmp/x")
  61. File.expand_path("/bin", "/tmp/x")
  62. File.expand_path("../../bin", "C:/tmp/x")
  63. File.expand_path("../bin", "C:/tmp/x")
  64. }
  65. end
  66. def test_expand_path_nil
  67. assert_raise(TypeError) { File.expand_path(nil) }
  68. assert_raise(TypeError) { File.expand_path(nil, "/") }
  69. assert_raise(TypeError) { File.expand_path(nil, nil) }
  70. end
  71. # JRUBY-1116: these are currently broken on windows
  72. # what are these testing anyway?!?!
  73. if WINDOWS
  74. def test_windows_basename
  75. assert_equal "", File.basename("c:")
  76. assert_equal "\\", File.basename("c:\\")
  77. assert_equal "abc", File.basename("c:abc")
  78. end
  79. # JRUBY-2052: this is important for fileutils
  80. def test_windows_dirname
  81. assert_equal("C:/", File.dirname("C:/"))
  82. assert_equal("C:\\", File.dirname("C:\\"))
  83. assert_equal("C:/", File.dirname("C:///////"))
  84. assert_equal("C:\\", File.dirname("C:\\\\\\\\"))
  85. assert_equal("C:/", File.dirname("C:///////blah"))
  86. assert_equal("C:\\", File.dirname("C:\\\\\\\\blah"))
  87. assert_equal("C:.", File.dirname("C:blah"))
  88. assert_equal "C:/", File.dirname("C:/temp/")
  89. assert_equal "c:\\", File.dirname('c:\\temp')
  90. assert_equal "C:.", File.dirname("C:")
  91. assert_equal "C:/temp", File.dirname("C:/temp/foobar.txt")
  92. end
  93. def test_windows_network_path
  94. assert_equal("\\\\network\\share", File.dirname("\\\\network\\share\\file.bat"))
  95. assert_equal("\\\\network\\share", File.dirname("\\\\network\\share"))
  96. assert_equal("\\\\localhost\\c$", File.dirname("\\\\localhost\\c$\\boot.bat"))
  97. assert_equal("\\\\localhost\\c$", File.dirname("\\\\localhost\\c$"))
  98. end
  99. def test_expand_path_windows
  100. assert_equal("C:/", File.expand_path("C:/"))
  101. assert_equal("C:/dir", File.expand_path("C:/dir"))
  102. assert_equal("C:/dir", File.expand_path("C:/dir/two/../"))
  103. assert_equal("C:/dir/two", File.expand_path("C:/dir/two/", "D:/"))
  104. assert_equal("C:/", File.expand_path("C:/", nil))
  105. # JRUBY-2161
  106. assert_equal("C:/", File.expand_path("C:/dir/../"))
  107. assert_equal("C:/", File.expand_path("C:/.."))
  108. assert_equal("C:/", File.expand_path("C:/../../"))
  109. assert_equal("C:/", File.expand_path("..", "C:/"))
  110. assert_equal("C:/", File.expand_path("..", "C:"))
  111. assert_equal("C:/", File.expand_path("C:/dir/two/../../"))
  112. assert_equal("C:/", File.expand_path("C:/dir/two/../../../../../"))
  113. # JRUBY-546
  114. current_drive_letter = Dir.pwd[0..2]
  115. paths_equal(current_drive_letter, File.expand_path(".", "/"))
  116. paths_equal(current_drive_letter, File.expand_path("..", "/"))
  117. paths_equal(current_drive_letter, File.expand_path("/", "/"))
  118. paths_equal(current_drive_letter, File.expand_path("../..", "/"))
  119. paths_equal(current_drive_letter, File.expand_path("../..", "/dir/two"))
  120. paths_equal(current_drive_letter + "dir",
  121. File.expand_path("../..", "/dir/two/three"))
  122. paths_equal(current_drive_letter, File.expand_path("/../..", "/"))
  123. paths_equal(current_drive_letter + "hello", File.expand_path("hello", "/"))
  124. paths_equal(current_drive_letter, File.expand_path("hello/..", "/"))
  125. paths_equal(current_drive_letter, File.expand_path("hello/../../..", "/"))
  126. paths_equal(current_drive_letter + "three/four",
  127. File.expand_path("/three/four", "/dir/two"))
  128. paths_equal(current_drive_letter + "two", File.expand_path("/two", "/one"))
  129. paths_equal(current_drive_letter + "three/four",
  130. File.expand_path("/three/four", "/dir/two"))
  131. assert_equal("C:/two", File.expand_path("/two", "C:/one"))
  132. assert_equal("C:/two", File.expand_path("/two", "C:/one/.."))
  133. assert_equal("C:/", File.expand_path("/two/..", "C:/one/.."))
  134. assert_equal("C:/", File.expand_path("/two/..", "C:/one"))
  135. assert_equal("//two", File.expand_path("//two", "/one"))
  136. assert_equal("//two", File.expand_path("//two", "//one"))
  137. assert_equal("//two", File.expand_path("//two", "///one"))
  138. assert_equal("//two", File.expand_path("//two", "////one"))
  139. assert_equal("//", File.expand_path("//", "//one"))
  140. # Corner cases that fail with JRuby on Windows (but pass with MRI 1.8.6)
  141. #
  142. ### assert_equal("///two", File.expand_path("///two", "/one"))
  143. ### assert_equal("///two", File.expand_path("///two/..", "/one"))
  144. ### assert_equal("////two", File.expand_path("////two", "/one"))
  145. ### assert_equal("////two", File.expand_path("////two", "//one"))
  146. ### assert_equal("//two", File.expand_path("//two/..", "/one"))
  147. ### assert_equal("////two", File.expand_path("////two/..", "/one"))
  148. #
  149. ### assert_equal("//bar/foo", File.expand_path("../foo", "//bar"))
  150. ### assert_equal("///bar/foo", File.expand_path("../foo", "///bar"))
  151. ### assert_equal("//one/two", File.expand_path("/two", "//one"))
  152. end
  153. def test_pathname_windows
  154. assert_equal(Pathname('foo.bar.rb').expand_path.relative_path_from(Pathname(Dir.pwd)), Pathname('foo.bar.rb'))
  155. end
  156. else
  157. def test_expand_path
  158. assert_equal("/bin", File.expand_path("../../bin", "/foo/bar"))
  159. assert_equal("/foo/bin", File.expand_path("../bin", "/foo/bar"))
  160. assert_equal("//abc/def/jkl/mno", File.expand_path("//abc//def/jkl//mno"))
  161. assert_equal("//abc/def/jkl/mno/foo", File.expand_path("foo", "//abc//def/jkl//mno"))
  162. begin
  163. File.expand_path("~nonexistent")
  164. assert(false)
  165. rescue ArgumentError => e
  166. assert(true)
  167. rescue Exception => e
  168. assert(false)
  169. end
  170. assert_equal("/bin", File.expand_path("../../bin", "/tmp/x"))
  171. assert_equal("/bin", File.expand_path("../../bin", "/tmp"))
  172. assert_equal("/bin", File.expand_path("../../bin", "/"))
  173. assert_equal("/bin", File.expand_path("../../../../../../../bin", "/"))
  174. assert_equal(File.join(Dir.pwd, "x/y/z/a/b"), File.expand_path("a/b", "x/y/z"))
  175. assert_equal(File.join(Dir.pwd, "bin"), File.expand_path("../../bin", "tmp/x"))
  176. assert_equal("/bin", File.expand_path("./../foo/./.././../bin", "/a/b"))
  177. # JRUBY-2160
  178. assert_equal("/dir1/subdir", File.expand_path("/dir1/subdir/stuff/../"))
  179. assert_equal("///fedora/stuff/blah/MORE", File.expand_path("///fedora///stuff/blah/again//..////MORE/"))
  180. assert_equal("/", File.expand_path("/dir1/../"))
  181. assert_equal("/", File.expand_path("/"))
  182. assert_equal("/", File.expand_path("/.."))
  183. assert_equal("/hello", File.expand_path("/hello/world/three/../../"))
  184. assert_equal("/dir/two", File.expand_path("", "/dir/two"))
  185. assert_equal("/dir", File.expand_path("..", "/dir/two"))
  186. assert_equal("/file/abs", File.expand_path("/file/abs", '/abs/dir/here'))
  187. assert_equal("/", File.expand_path("/", nil))
  188. assert_equal("//two", File.expand_path("//two", "//one"))
  189. assert_equal("/", File.expand_path("/two/..", "//one"))
  190. # Corner cases that fail with JRuby on Linux (but pass with MRI 1.8.6)
  191. #
  192. # assert_equal("", File.expand_path("//two/..", "//one"))
  193. # assert_equal("", File.expand_path("///two/..", "//one"))
  194. # assert_equal("/blah", File.expand_path("///two/../blah", "//one"))
  195. end
  196. def test_expand_path_with_file_prefix
  197. jruby_specific_test
  198. assert_equal "file:/foo/bar", File.expand_path("file:/foo/bar")
  199. assert_equal "file:/bar", File.expand_path("file:/foo/../bar")
  200. assert_equal "file:/foo/bar/baz", File.expand_path("baz", "file:/foo/bar")
  201. assert_equal "file:/foo/bar", File.expand_path("file:/foo/bar", "file:/baz/quux")
  202. end
  203. def test_expand_path_with_file_url_relative_path
  204. jruby_specific_test
  205. assert_equal "file:#{Dir.pwd}/foo/bar", File.expand_path("file:foo/bar")
  206. end
  207. # JRUBY-5219
  208. def test_expand_path_looks_like_url
  209. jruby_specific_test
  210. assert_equal "classpath:/META-INF/jruby.home", File.expand_path("classpath:/META-INF/jruby.home")
  211. assert_equal "http://example.com/a.jar", File.expand_path("http://example.com/a.jar")
  212. assert_equal "http://example.com/", File.expand_path("..", "http://example.com/a.jar")
  213. assert_equal "classpath:/foo/bar/baz", File.expand_path("baz", "classpath:/foo/bar")
  214. assert_equal "classpath:/foo/bar", File.expand_path("classpath:/foo/bar", "classpath:/baz/quux")
  215. assert_equal "classpath:/foo", File.expand_path("..", "classpath:/foo/bar")
  216. end
  217. def test_mkdir_with_non_file_uri_raises_error
  218. assert_raises(Errno::ENOTDIR) { FileUtils.mkdir_p("classpath:/META-INF/jruby.home") }
  219. assert !File.directory?("classpath:/META-INF/jruby.home")
  220. end
  221. def test_mkdir_with_file_uri_works_as_expected
  222. FileUtils.mkdir("file:test_mkdir_with_file_uri_works_as_expected")
  223. assert File.directory?("test_mkdir_with_file_uri_works_as_expected")
  224. assert File.directory?("file:test_mkdir_with_file_uri_works_as_expected")
  225. ensure
  226. FileUtils.rm_rf("test_mkdir_with_file_uri_works_as_expected")
  227. end
  228. def test_expand_path_corner_case
  229. # this would fail on MRI 1.8.6 (MRI returns "/foo").
  230. assert_equal("//foo", File.expand_path("../foo", "//bar"))
  231. end
  232. end # if windows
  233. def test_dirname
  234. assert_equal(".", File.dirname(""))
  235. assert_equal(".", File.dirname("."))
  236. assert_equal(".", File.dirname(".."))
  237. assert_equal(".", File.dirname("a"))
  238. assert_equal(".", File.dirname("./a"))
  239. assert_equal("./a", File.dirname("./a/b"))
  240. assert_equal("/", File.dirname("/"))
  241. assert_equal("/", File.dirname("/a"))
  242. assert_equal("/a", File.dirname("/a/b"))
  243. assert_equal("/a", File.dirname("/a/b/"))
  244. assert_equal("/", File.dirname("/"))
  245. end
  246. def test_extname
  247. assert_equal("", File.extname(""))
  248. assert_equal("", File.extname("abc"))
  249. assert_equal(".foo", File.extname("abc.foo"))
  250. assert_equal(".foo", File.extname("abc.bar.foo"))
  251. assert_equal("", File.extname("abc.bar/foo"))
  252. assert_equal("", File.extname(".bashrc"))
  253. assert_equal("", File.extname("."))
  254. assert_equal("", File.extname("/."))
  255. assert_equal("", File.extname(".."))
  256. assert_equal("", File.extname(".foo."))
  257. assert_equal("", File.extname("foo."))
  258. end
  259. def test_fnmatch
  260. assert_equal(true, File.fnmatch('cat', 'cat'))
  261. assert_equal(false, File.fnmatch('cat', 'category'))
  262. assert_equal(false, File.fnmatch('c{at,ub}s', 'cats'))
  263. assert_equal(false, File.fnmatch('c{at,ub}s', 'cubs'))
  264. assert_equal(false, File.fnmatch('c{at,ub}s', 'cat'))
  265. assert_equal(true, File.fnmatch('c?t', 'cat'))
  266. assert_equal(false, File.fnmatch('c\?t', 'cat'))
  267. assert_equal(true, File.fnmatch('c\?t', 'c?t'))
  268. assert_equal(false, File.fnmatch('c??t', 'cat'))
  269. assert_equal(true, File.fnmatch('c*', 'cats'));
  270. assert_equal(true, File.fnmatch('c*t', 'cat'))
  271. #assert_equal(true, File.fnmatch('c\at', 'cat')) # Doesn't work correctly on both Unix and Win32
  272. assert_equal(false, File.fnmatch('c\at', 'cat', File::FNM_NOESCAPE))
  273. assert_equal(true, File.fnmatch('a?b', 'a/b'))
  274. assert_equal(false, File.fnmatch('a?b', 'a/b', File::FNM_PATHNAME))
  275. assert_equal(false, File.fnmatch('a?b', 'a/b', File::FNM_PATHNAME))
  276. assert_equal(false, File.fnmatch('*', '.profile'))
  277. assert_equal(true, File.fnmatch('*', '.profile', File::FNM_DOTMATCH))
  278. assert_equal(true, File.fnmatch('*', 'dave/.profile'))
  279. assert_equal(true, File.fnmatch('*', 'dave/.profile', File::FNM_DOTMATCH))
  280. assert_equal(false, File.fnmatch('*', 'dave/.profile', File::FNM_PATHNAME))
  281. assert_equal(false, File.fnmatch("/.ht*",""))
  282. assert_equal(false, File.fnmatch("/*~",""))
  283. assert_equal(false, File.fnmatch("/.ht*","/"))
  284. assert_equal(false, File.fnmatch("/*~","/"))
  285. assert_equal(false, File.fnmatch("/.ht*",""))
  286. assert_equal(false, File.fnmatch("/*~",""))
  287. assert_equal(false, File.fnmatch("/.ht*","/stylesheets"))
  288. assert_equal(false, File.fnmatch("/*~","/stylesheets"))
  289. assert_equal(false, File.fnmatch("/.ht*",""))
  290. assert_equal(false, File.fnmatch("/*~",""))
  291. assert_equal(false, File.fnmatch("/.ht*","/favicon.ico"))
  292. assert_equal(false, File.fnmatch("/*~","/favicon.ico"))
  293. # JRUBY-1986, make sure that fnmatch is sharing aware
  294. assert_equal(true, File.fnmatch("foobar"[/foo(.*)/, 1], "bar"))
  295. end
  296. # JRUBY-2196
  297. def test_fnmatch_double_star
  298. assert(File.fnmatch('**/foo', 'a/b/c/foo', File::FNM_PATHNAME))
  299. assert(File.fnmatch('**/foo', '/foo', File::FNM_PATHNAME))
  300. assert(!File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME))
  301. assert(File.fnmatch('**/foo', 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH))
  302. assert(File.fnmatch('**/foo', '/root/foo', File::FNM_PATHNAME))
  303. assert(File.fnmatch('**/foo', 'c:/root/foo', File::FNM_PATHNAME))
  304. assert(File.fnmatch("lib/**/*.rb", "lib/a.rb", File::FNM_PATHNAME | File::FNM_DOTMATCH))
  305. assert(File.fnmatch("lib/**/*.rb", "lib/a/b.rb", File::FNM_PATHNAME | File::FNM_DOTMATCH))
  306. assert(File.fnmatch('**/b/**/*', 'c/a/b/c/t', File::FNM_PATHNAME))
  307. assert(File.fnmatch('c/**/b/**/*', 'c/a/b/c/t', File::FNM_PATHNAME))
  308. assert(File.fnmatch('c**/**/b/**/*', 'c/a/b/c/t', File::FNM_PATHNAME))
  309. assert(File.fnmatch('h**o/**/b/**/*', 'hello/a/b/c/t', File::FNM_PATHNAME))
  310. assert(!File.fnmatch('h**o/**/b/**', 'hello/a/b/c/t', File::FNM_PATHNAME))
  311. assert(File.fnmatch('**', 'hello', File::FNM_PATHNAME))
  312. assert(!File.fnmatch('**/', 'hello', File::FNM_PATHNAME))
  313. assert(File.fnmatch('**/*', 'hello', File::FNM_PATHNAME))
  314. assert(File.fnmatch("**/*", "one/two/three/four", File::FNM_PATHNAME))
  315. assert(!File.fnmatch("**", "one/two/three", File::FNM_PATHNAME))
  316. assert(!File.fnmatch("**/three", ".one/two/three", File::FNM_PATHNAME))
  317. assert(File.fnmatch("**/three", ".one/two/three", File::FNM_PATHNAME | File::FNM_DOTMATCH))
  318. assert(!File.fnmatch("*/**", "one/two/three", File::FNM_PATHNAME))
  319. assert(!File.fnmatch("*/**/", "one/two/three", File::FNM_PATHNAME))
  320. assert(File.fnmatch("*/**/*", "one/two/three", File::FNM_PATHNAME))
  321. assert(File.fnmatch("**/*", ".one/two/three/four", File::FNM_PATHNAME | File::FNM_DOTMATCH))
  322. assert(!File.fnmatch("**/.one/*", ".one/.two/.three/.four", File::FNM_PATHNAME | File::FNM_DOTMATCH))
  323. end
  324. # JRUBY-2199
  325. def test_fnmatch_bracket_pattern
  326. assert(!File.fnmatch('[a-z]', 'D'))
  327. assert(File.fnmatch('[a-z]', 'D', File::FNM_CASEFOLD))
  328. assert(!File.fnmatch('[a-zA-Y]', 'Z'))
  329. assert(File.fnmatch('[^a-zA-Y]', 'Z'))
  330. assert(File.fnmatch('[a-zA-Y]', 'Z', File::FNM_CASEFOLD))
  331. assert(File.fnmatch('[a-zA-Z]', 'Z'))
  332. assert(File.fnmatch('[a-zA-Z]', 'A'))
  333. assert(File.fnmatch('[a-zA-Z]', 'a'))
  334. assert(!File.fnmatch('[b-zA-Z]', 'a'))
  335. assert(File.fnmatch('[^b-zA-Z]', 'a'))
  336. assert(File.fnmatch('[b-zA-Z]', 'a', File::FNM_CASEFOLD))
  337. end
  338. def test_join
  339. [
  340. ["a", "b", "c", "d"],
  341. ["a"],
  342. [],
  343. ["a", "b", "..", "c"]
  344. ].each do |a|
  345. assert_equal(a.join(File::SEPARATOR), File.join(*a))
  346. end
  347. assert_equal("////heh////bar/heh", File.join("////heh////bar", "heh"))
  348. assert_equal("/heh/bar/heh", File.join("/heh/bar/", "heh"))
  349. assert_equal("/heh/bar/heh", File.join("/heh/bar/", "/heh"))
  350. assert_equal("/heh//bar/heh", File.join("/heh//bar/", "/heh"))
  351. assert_equal("/heh/bar/heh", File.join("/heh/", "/bar/", "/heh"))
  352. assert_equal("/HEH/BAR/FOO/HOH", File.join("/HEH", ["/BAR", "FOO"], "HOH"))
  353. assert_equal("/heh/bar", File.join("/heh//", "/bar"))
  354. assert_equal("/heh///bar", File.join("/heh", "///bar"))
  355. end
  356. def test_split
  357. assert_equal([".", ""], File.split(""))
  358. assert_equal([".", "."], File.split("."))
  359. assert_equal([".", ".."], File.split(".."))
  360. assert_equal(["/", "/"], File.split("/"))
  361. assert_equal([".", "a"], File.split("a"))
  362. assert_equal([".", "a"], File.split("a/"))
  363. assert_equal(["a", "b"], File.split("a/b"))
  364. assert_equal(["a/b", "c"], File.split("a/b/c"))
  365. assert_equal(["/", "a"], File.split("/a"))
  366. assert_equal(["/", "a"], File.split("/a/"))
  367. assert_equal(["/a", "b"], File.split("/a/b"))
  368. assert_equal(["/a/b", "c"], File.split("/a/b/c"))
  369. #assert_equal(["//", "a"], File.split("//a"))
  370. assert_equal(["../a/..", "b"], File.split("../a/../b/"))
  371. end
  372. def test_io_readlines
  373. # IO#readlines, IO::readlines, open, close, delete, ...
  374. assert_raises(Errno::ENOENT) { File.open("NO_SUCH_FILE_EVER") }
  375. f = open("testFile_tmp", "w")
  376. f.write("one\ntwo\nthree\n")
  377. f.close
  378. f = open("testFile_tmp")
  379. assert_equal(["one", "two", "three"],
  380. f.readlines.collect {|l| l.strip })
  381. f.close
  382. assert_equal(["one", "two", "three"],
  383. IO.readlines("testFile_tmp").collect {|l| l.strip })
  384. assert(File.delete("testFile_tmp"))
  385. end
  386. def test_mkdir
  387. begin
  388. Dir.mkdir("dir_tmp")
  389. assert(File.lstat("dir_tmp").directory?)
  390. ensure
  391. Dir.rmdir("dir_tmp")
  392. end
  393. end
  394. def test_file_query # - file?
  395. assert(File.file?('test/test_file.rb'))
  396. assert(! File.file?('test'))
  397. end
  398. def test_file_exist_query
  399. assert(File.exist?('test'))
  400. end
  401. def test_file_exist_in_jar_file
  402. jruby_specific_test
  403. assert(File.exist?("file:" + File.expand_path("test/dir with spaces/test_jar.jar") + "!/abc/foo.rb"))
  404. assert(File.exist?("file:" + File.expand_path("test/dir with spaces/test_jar.jar") + "!/inside_jar.rb"))
  405. assert(!File.exist?("file:" + File.expand_path("test/dir with spaces/test_jar.jar") + "!/inside_jar2.rb"))
  406. assert(!File.exist?("file:" + File.expand_path("test/dir with spaces/test_jar.jar") + "!/"))
  407. end
  408. def with_load_path(entry)
  409. begin
  410. $LOAD_PATH.unshift entry
  411. yield
  412. ensure
  413. $LOAD_PATH.shift
  414. end
  415. end
  416. def test_require_from_jar_url_with_spaces_in_load_path
  417. assert_nothing_raised do
  418. with_load_path("file:" + File.expand_path("test/dir with spaces/test_jar.jar") + "!/abc") do
  419. assert require('foo')
  420. assert $LOADED_FEATURES.pop =~ /foo\.rb$/
  421. end
  422. with_load_path("file:" + File.expand_path("test/dir with spaces/test_jar.jar") + "!") do
  423. assert require('abc/foo')
  424. assert $LOADED_FEATURES.pop =~ /foo\.rb$/
  425. end
  426. with_load_path(File.expand_path("test/dir with spaces/test_jar.jar")) do
  427. assert require('abc/foo')
  428. assert $LOADED_FEATURES.pop =~ /foo\.rb$/
  429. end
  430. end
  431. end
  432. def test_file_read_in_jar_file
  433. jruby_specific_test
  434. assert_equal("foobarx\n", File.read("file:" + File.expand_path("test/test_jar2.jar") + "!/test_value.rb"))
  435. assert_raises(Errno::ENOENT) do
  436. File.read("file:" + File.expand_path("test/test_jar2.jar") + "!/inside_jar2.rb")
  437. end
  438. assert_raises(Errno::ENOENT) do
  439. File.read("file:" + File.expand_path("test/test_jar3.jar") + "!/inside_jar2.rb")
  440. end
  441. val = ""
  442. open("file:" + File.expand_path("test/test_jar2.jar") + "!/test_value.rb") do |f|
  443. val = f.read
  444. end
  445. assert_equal "foobarx\n", val
  446. values = ""
  447. File.open("file:" + File.expand_path("test/test_jar2.jar") + "!/test_value.rb") do |f|
  448. f.each do |s|
  449. values << s
  450. end
  451. end
  452. assert_equal "foobarx\n", values
  453. end
  454. # JRUBY-2357
  455. def test_truncate_file_in_jar_file
  456. jruby_specific_test
  457. File.open("file:" + File.expand_path("test/test_jar2.jar") + "!/test_value.rb", "r+") do |f|
  458. assert_raise(Errno::EINVAL) { f.truncate(2) }
  459. end
  460. end
  461. # JRUBY-1886
  462. def test_file_truncated_after_changing_directory
  463. subdir = "./testDir_1"
  464. Dir.mkdir(subdir)
  465. Dir.chdir(subdir) { |dir|
  466. begin
  467. file = File.open("__dummy_file.txt", "wb") { |file|
  468. file.write("dummy text")
  469. file
  470. }
  471. assert_nothing_raised { File.truncate(file.path, 0) }
  472. ensure
  473. File.unlink(file.path)
  474. end
  475. }
  476. ensure
  477. Dir.rmdir(subdir)
  478. end
  479. def test_file_size_query
  480. assert(File.size?('build.xml'))
  481. end
  482. # JRUBY-2275
  483. def test_file_size_empty_file
  484. filename = "__empty_test__file"
  485. File.open(filename, "w+") { }
  486. assert_equal(nil, File.size?(filename))
  487. assert_equal(nil, FileTest.size?(filename))
  488. assert_equal(0, File.size(filename))
  489. assert_equal(0, FileTest.size(filename))
  490. ensure
  491. File.delete(filename)
  492. end
  493. # JRUBY-2524
  494. def test_filetest_exists_uri_prefixes
  495. assert(!FileTest.exists?("file:/!"))
  496. end
  497. def test_file_exists_uri_prefixes
  498. assert(File.exists?("file:test/dir with spaces/test_jar.jar!/abc/foo.rb"))
  499. assert(File.exists?("jar:file:test/dir with spaces/test_jar.jar!/abc/foo.rb"))
  500. end
  501. # JRUBY-2524
  502. def test_file_stat_uri_prefixes
  503. assert_raise(Errno::ENOENT) do
  504. File.lstat("file:")
  505. end
  506. assert_raise(Errno::ENOENT) do
  507. File.lstat("file:!")
  508. end
  509. assert_raise(Errno::ENOENT) do
  510. File.stat("file:")
  511. end
  512. assert_raise(Errno::ENOENT) do
  513. File.stat("file:!")
  514. end
  515. end
  516. # JRUBY-2524
  517. def test_file_time_uri_prefixes
  518. assert_raise(Errno::ENOENT) do
  519. File.atime("file:")
  520. end
  521. assert_raise(Errno::ENOENT) do
  522. File.atime("file:!")
  523. end
  524. assert_raise(Errno::ENOENT) do
  525. File.ctime("file:")
  526. end
  527. assert_raise(Errno::ENOENT) do
  528. File.ctime("file:!")
  529. end
  530. end
  531. def test_file_open_utime
  532. filename = "__test__file"
  533. File.open(filename, "w") {|f| }
  534. time = Time.now - 3600
  535. begin
  536. File.utime(time, time, filename)
  537. rescue Object => o
  538. o.printStackTrace
  539. o.getCause.printStackTrace
  540. end
  541. # File mtime resolution may not be sub-second on all platforms (e.g., windows)
  542. # allow for some slop
  543. assert((time.to_i - File.atime(filename).to_i).abs < 5)
  544. assert((time.to_i - File.mtime(filename).to_i).abs < 5)
  545. File.unlink(filename)
  546. end
  547. def test_file_utime_nil
  548. filename = '__test__file'
  549. File.open(filename, 'w') {|f| }
  550. time = File.mtime(filename)
  551. sleep 2
  552. File.utime(nil, nil, filename)
  553. assert((File.atime(filename).to_i - time.to_i) >= 2)
  554. assert((File.mtime(filename).to_i - time.to_i) >= 2)
  555. File.unlink(filename)
  556. end
  557. def test_file_utime_bad_time_raises_typeerror
  558. args = [ [], {}, '4000' ]
  559. filename = '__test__file'
  560. File.open(filename, 'w') {|f| }
  561. args.each do |arg|
  562. assert_raises(TypeError) { File.utime(arg, nil, filename) }
  563. assert_raises(TypeError) { File.utime(nil, arg, filename) }
  564. assert_raises(TypeError) { File.utime(arg, arg, filename) }
  565. end
  566. time = Time.now
  567. assert_raises(TypeError) { File.utime(time, nil, filename) }
  568. assert_raises(TypeError) { File.utime(nil, time, filename) }
  569. File.unlink(filename)
  570. end
  571. # JRUBY-1982 and JRUBY-1983
  572. def test_file_mtime_after_fileutils_touch
  573. filename = '__test__file'
  574. File.open(filename, 'w') {|f| }
  575. time = File.mtime(filename)
  576. FileUtils.touch(filename)
  577. # File mtime resolution may not be sub-second on all platforms (e.g., windows)
  578. # allow for some slop
  579. assert((time.to_i - File.mtime(filename).to_i).abs < 2)
  580. end
  581. def test_file_stat # File::Stat tests
  582. stat = File.stat('test');
  583. stat2 = File.stat('build.xml');
  584. assert(stat.directory?)
  585. assert(stat2.file?)
  586. assert_equal("directory", stat.ftype)
  587. assert_equal("file", stat2.ftype)
  588. assert(stat2.readable?)
  589. end
  590. if WINDOWS && JRuby::runtime.posix.native?
  591. # JRUBY-2351
  592. def test_not_implemented_methods_on_windows
  593. # the goal here is to make sure that those "weird"
  594. # POSIX methods don't break JRuby, since there were
  595. # numerous regressions in this area
  596. begin
  597. # TODO: See JRUBY-2818.
  598. File.readlink('build.xml')
  599. rescue NotImplementedError
  600. rescue Errno::EINVAL # TODO: this exception is wrong (see bug above)
  601. end
  602. begin
  603. # TODO: See JRUBY-2817.
  604. File.chown(100, 100, 'build.xml')
  605. rescue NotImplementedError
  606. end
  607. assert_raise(NotImplementedError) { File.lchown(100, 100, 'build.xml') }
  608. assert_raise(NotImplementedError) { File.lchmod(0644, 'build.xml') }
  609. end
  610. end
  611. unless(WINDOWS) # no symlinks on Windows
  612. def test_file_symlink
  613. # Test File.symlink? if possible
  614. system("ln -s build.xml build.xml.link")
  615. if File.exist? "build.xml.link"
  616. assert(File.symlink?("build.xml.link"))
  617. # JRUBY-683 - Test that symlinks don't effect Dir and File.expand_path
  618. assert_equal(['build.xml.link'], Dir['build.xml.link'])
  619. assert_equal(File.expand_path('.') + '/build.xml.link', File.expand_path('build.xml.link'))
  620. File.delete("build.xml.link")
  621. end
  622. end
  623. def test_require_symlink
  624. # Create a ruby file that sets a global variable to its view of __FILE__
  625. f = File.open("real_file.rb", "w")
  626. f.write("$test_require_symlink_filename=__FILE__")
  627. f.close()
  628. system("ln -s real_file.rb linked_file.rb")
  629. assert(File.symlink?("linked_file.rb"))
  630. # JRUBY-5167 - Test that symlinks don't effect __FILE__ during load or require
  631. # Note: This bug only manifests for absolute paths that point to symlinks.
  632. abs_path = File.join(Dir.pwd, "real_file.rb")
  633. require abs_path
  634. assert_equal($test_require_symlink_filename, abs_path)
  635. abs_path_linked = File.join(Dir.pwd, "linked_file.rb")
  636. unless RUBY_VERSION =~ /1\.9/
  637. require abs_path_linked
  638. assert_equal($test_require_symlink_filename, abs_path_linked)
  639. load abs_path_linked
  640. assert_equal($test_require_symlink_filename, abs_path_linked)
  641. end
  642. ensure
  643. File.delete("real_file.rb")
  644. File.delete("linked_file.rb")
  645. end
  646. end
  647. def test_file_times
  648. # Note: atime, mtime, ctime are all implemented using modification time
  649. assert_nothing_raised do
  650. [ "build.xml", "file:test/dir with spaces/test_jar.jar!/abc/foo.rb" ].each do |file|
  651. File.mtime(file)
  652. File.atime(file)
  653. File.ctime(file)
  654. File.new(file).mtime
  655. File.new(file).atime
  656. File.new(file).ctime
  657. end
  658. end
  659. assert_raises(Errno::ENOENT) { File.mtime("NO_SUCH_FILE_EVER") }
  660. end
  661. def test_file_times_types
  662. # Note: atime, mtime, ctime are all implemented using modification time
  663. assert_instance_of Time, File.mtime("build.xml")
  664. assert_instance_of Time, File.atime("build.xml")
  665. assert_instance_of Time, File.ctime("build.xml")
  666. assert_instance_of Time, File.new("build.xml").mtime
  667. assert_instance_of Time, File.new("build.xml").atime
  668. assert_instance_of Time, File.new("build.xml").ctime
  669. end
  670. def test_more_constants
  671. jruby_specific_test
  672. # FIXME: Not sure how I feel about pulling in Java here
  673. if Java::java.lang.System.get_property("file.separator") == '/'
  674. assert_equal(nil, File::ALT_SEPARATOR)
  675. else
  676. assert_equal("\\", File::ALT_SEPARATOR)
  677. end
  678. end
  679. # JRUBY-2572
  680. def test_fnm_syscase_constant
  681. if (WINDOWS)
  682. assert_equal(File::FNM_CASEFOLD, File::FNM_SYSCASE)
  683. assert File.fnmatch?('cat', 'CAT', File::FNM_SYSCASE)
  684. else
  685. assert_not_equal(File::FNM_CASEFOLD, File::FNM_SYSCASE)
  686. assert !File.fnmatch?('cat', 'CAT', File::FNM_SYSCASE)
  687. end
  688. end
  689. def test_truncate
  690. # JRUBY-1025: negative int passed to truncate should raise EINVAL
  691. filename = "__truncate_test_file"
  692. assert_raises(Errno::EINVAL) {
  693. begin
  694. f = File.open(filename, 'w')
  695. f.truncate(-1)
  696. ensure
  697. f.close
  698. end
  699. }
  700. assert_raises(Errno::EINVAL) {
  701. File.truncate(filename, -1)
  702. }
  703. ensure
  704. File.delete(filename)
  705. end
  706. def test_file_utf8
  707. # name contains a German "umlaut", maybe this should be encoded as Unicode integer (\u00FC) somehow
  708. filename = 'jrüby'
  709. f = File.new(filename, File::CREAT)
  710. begin
  711. assert_equal(nil, f.read(1))
  712. assert File.file?(filename)
  713. assert File.exist?(filename)
  714. ensure
  715. f.close
  716. File.delete(filename)
  717. assert !File.file?(filename)
  718. assert !File.exist?(filename)
  719. end
  720. end
  721. def test_file_create
  722. filename = '2nnever'
  723. f = File.new(filename, File::CREAT)
  724. begin
  725. assert_equal(nil, f.read(1))
  726. ensure
  727. f.close
  728. File.delete(filename)
  729. end
  730. f = File.new(filename, File::CREAT)
  731. begin
  732. assert_raises(IOError) { f << 'b' }
  733. ensure
  734. f.close
  735. File.delete(filename)
  736. end
  737. end
  738. # http://jira.codehaus.org/browse/JRUBY-1023
  739. def test_file_reuse_fileno
  740. fh = File.new(STDIN.fileno, 'r')
  741. assert_equal(STDIN.fileno, fh.fileno)
  742. end
  743. # http://jira.codehaus.org/browse/JRUBY-1231
  744. def test_file_directory_empty_name
  745. assert !File.directory?("")
  746. assert !FileTest.directory?("")
  747. assert_raises(Errno::ENOENT) { File::Stat.new("") }
  748. end
  749. def test_file_test
  750. assert(FileTest.file?('test/test_file.rb'))
  751. assert(! FileTest.file?('test'))
  752. end
  753. def test_flock
  754. filename = '__lock_test__'
  755. file = File.open(filename,'w')
  756. file.flock(File::LOCK_EX | File::LOCK_NB)
  757. assert_equal(0, file.flock(File::LOCK_UN | File::LOCK_NB))
  758. file.close
  759. File.delete(filename)
  760. end
  761. def test_truncate_doesnt_create_file
  762. name = "___foo_bar___"
  763. assert(!File.exists?(name))
  764. assert_raises(Errno::ENOENT) { File.truncate(name, 100) }
  765. assert(!File.exists?(name))
  766. end
  767. # JRUBY-2340
  768. def test_opening_readonly_file_for_write_raises_eacces
  769. filename = "__read_only__"
  770. begin
  771. File.open(filename, "w+", 0444) { }
  772. assert_raise(Errno::EACCES) { File.open(filename, "w") { } }
  773. ensure
  774. File.delete(filename)
  775. end
  776. end
  777. # JRUBY-2397
  778. unless(WINDOWS)
  779. def test_chown_accepts_nil_and_minus_one
  780. # chown
  781. assert_equal(1, File.chown(-1, -1, 'build.xml'))
  782. assert_equal(1, File.chown(nil, nil, 'build.xml'))
  783. # lchown
  784. assert_equal(1, File.lchown(-1, -1, 'build.xml'))
  785. assert_equal(1, File.lchown(nil, nil, 'build.xml'))
  786. File.open('build.xml') { |file|
  787. # chown
  788. assert_equal(0, file.chown(-1, -1))
  789. assert_equal(0, file.chown(nil, nil))
  790. # lchown
  791. # NOTE: hmm, it seems that MRI
  792. # doesn't have File#lchown method at all!
  793. assert_equal(0, file.lchown(-1, -1))
  794. assert_equal(0, file.lchown(nil, nil))
  795. }
  796. end
  797. end
  798. unless WINDOWS
  799. # JRUBY-2491
  800. def test_umask_noarg_does_not_zero
  801. mask = 0200
  802. orig_mask = File.umask(mask)
  803. assert_equal(mask, File.umask)
  804. # Subsequent calls should still return the same umask, not zero
  805. assert_equal(mask, File.umask)
  806. ensure
  807. File.umask(orig_mask)
  808. end
  809. # JRUBY-4937
  810. def test_umask_respects_existing_umask_value
  811. orig_mask = File.umask
  812. # Cleanup old test files just in case
  813. FileUtils.rm_rf %w[ file_test_out.1.0644 file_test_out.2 ]
  814. File.umask( 0172 ) # Set umask to fixed weird test value
  815. open( "file_test_out.1.0644", 'w', 0707 ) { |f| assert_equal(0172, File.umask) }
  816. open( "file_test_out.2", 'w' ) { |f| assert_equal(0172, File.umask) }
  817. ensure
  818. FileUtils.rm_rf %w[ file_test_out.1.0644 file_test_out.2 ]
  819. File.umask(orig_mask)
  820. end
  821. end
  822. def test_allow_override_of_make_tmpname
  823. # mimics behavior of attachment_fu, which overrides private #make_tmpname
  824. Tempfile.class_eval do
  825. alias_method :save_make_tmpname, :make_tmpname
  826. def make_tmpname(basename, n)
  827. ext = nil
  828. sprintf("%s%d-%d%s", basename.to_s.gsub(/\.\w+$/) { |s| ext = s; '' }, $$, n, ext)
  829. end
  830. end
  831. @teardown_blocks << proc do
  832. Tempfile.class_eval { alias_method :make_tmpname, :save_make_tmpname }
  833. end
  834. begin
  835. t = Tempfile.new "tcttac.jpg", File.dirname(__FILE__)
  836. assert t.path =~ /\.jpg$/
  837. ensure
  838. t.close
  839. end
  840. end
  841. unless WINDOWS
  842. def test_mode_of_tempfile_is_600
  843. t = Tempfile.new "tcttac.jpg"
  844. assert_equal 0100600, File.stat(t.path).mode
  845. end
  846. end
  847. # See JRUBY-2694; we don't have 1.8.7 support yet
  848. def test_tempfile_with_suffix
  849. Tempfile.open(['prefix', 'suffix']) { |f|
  850. assert_match(/^prefix/, File.basename(f.path))
  851. assert_match(/suffix$/, f.path)
  852. }
  853. end
  854. def test_file_size
  855. size = File.size('build.xml')
  856. assert(size > 0)
  857. assert_equal(size, File.size(File.new('build.xml')))
  858. end
  859. # JRUBY-4073
  860. def test_file_sizes
  861. filename = '100_bytes.bin'
  862. begin
  863. File.open(filename, 'wb+') { |f|
  864. f.write('0' * 100)
  865. }
  866. assert_equal(100, File.size(filename))
  867. assert_equal(100, File.stat(filename).size)
  868. assert_equal(100, File.stat(filename).size?)
  869. assert_match(/\ssize=100,/, File.stat(filename).inspect)
  870. assert_equal(100, File::Stat.new(filename).size)
  871. assert_equal(100, File::Stat.new(filename).size?)
  872. assert_match(/\ssize=100,/, File::Stat.new(filename).inspect)
  873. ensure
  874. File.unlink(filename)
  875. end
  876. end
  877. # JRUBY-4149
  878. def test_open_file_sizes
  879. filename = '100_bytes.bin'
  880. begin
  881. File.open(filename, 'wb+') { |f|
  882. f.write('0' * 100)
  883. f.flush; f.flush
  884. assert_equal(100, f.stat.size)
  885. assert_equal(100, f.stat.size?)
  886. assert_match(/\ssize=100,/, f.stat.inspect)
  887. assert_equal(100, File.size(filename))
  888. assert_equal(100, File.stat(filename).size)
  889. assert_equal(100, File.stat(filename).size?)
  890. assert_match(/\ssize=100,/, File.stat(filename).inspect)
  891. assert_equal(100, File::Stat.new(filename).size)
  892. assert_equal(100, File::Stat.new(filename).size?)
  893. assert_match(/\ssize=100,/, File::Stat.new(filename).inspect)
  894. }
  895. ensure
  896. File.unlink(filename)
  897. end
  898. end
  899. # JRUBY-4537: File.open raises Errno::ENOENT instead of Errno::EACCES
  900. def test_write_open_permission_denied
  901. t = Tempfile.new('tmp' + File.basename(__FILE__))
  902. t.close
  903. File.open(t.path, 'w') {}
  904. File.chmod(0555, t.path)
  905. # jruby 1.4 raises ENOENT here
  906. assert_raises(Errno::EACCES) do
  907. File.open(t.path, 'w') {}
  908. end
  909. # jruby 1.4 raises ENOENT here
  910. assert_raises(Errno::EACCES) do
  911. File.open(t.path, File::WRONLY) {}
  912. end
  913. end
  914. #JRUBY-4380: File.open raises IOError instead of Errno::ENOENT
  915. def test_open_with_nonexisting_directory
  916. file_path = "/foo/bar"
  917. assert(!File.exist?(file_path))
  918. assert_raises(Errno::ENOENT) { File.open(file_path, "wb") }
  919. end
  920. def test_open_file_in_jar
  921. File.open('file:test/dir with spaces/test_jar.jar!/abc/foo.rb'){}
  922. File.open('jar:file:test/dir with spaces/test_jar.jar!/abc/foo.rb'){}
  923. end
  924. # JRUBY-3634: File.read or File.open with a url to a file resource fails with StringIndexOutOfBounds exception
  925. def test_file_url
  926. path = File.expand_path(__FILE__)
  927. expect = File.open(__FILE__, mode_string = "rb").read(100)
  928. got = File.open("file:///#{path}", mode_string = "rb").read(100)
  929. assert_equal(expect, got)
  930. end
  931. def test_basename_unicode
  932. utf8_filename = "dir/glk\u00a9.pdf"
  933. assert_equal("glk\u00a9.pdf", File.basename(utf8_filename))
  934. end
  935. #JRUBY-4387, JRUBY-4416
  936. unless RUBY_VERSION =~ /1\.9/
  937. def test_file_gets_separator
  938. filename = 'gets.out'
  939. begin
  940. File.open(filename, "wb") do |file|
  941. file.print "this is a test\xFFit is only a test\ndoes it work?"
  942. end
  943. file = File.open("gets.out", "rb") do |file|
  944. assert_equal("this is a test\377", file.gets("\xFF"))
  945. end
  946. ensure
  947. File.unlink(filename)
  948. end
  949. end
  950. end
  951. def test_file_stat_with_missing_path
  952. assert_raise(Errno::ENOENT) {
  953. File::Stat.new("file:" + File.expand_path("test/test_jar2.jar") + "!/foo_bar.rb").file?
  954. }
  955. end
  956. # JRUBY-4859
  957. def test_file_delete_directory
  958. Dir.mkdir("dir_tmp")
  959. assert_raise(Errno::EPERM) {
  960. File.delete "dir_tmp"
  961. }
  962. ensure
  963. Dir.rmdir("dir_tmp")
  964. end
  965. unless WINDOWS
  966. # JRUBY-4927
  967. def test_chmod_when_chdir
  968. pwd = Dir.getwd
  969. path = Tempfile.new("somewhere").path
  970. FileUtils.rm_rf path
  971. FileUtils.mkpath path
  972. FileUtils.mkpath File.join(path, "src")
  973. Dir.chdir path
  974. 1.upto(4) do |i|
  975. File.open("src/file#{i}", "w+") {|f| f.write "file#{i} raw"}
  976. end
  977. Dir['src/*'].each do |file|
  978. File.chmod(0o755, file)
  979. assert_equal 0o755, (File.stat(file).mode & 0o755)
  980. end
  981. ensure
  982. FileUtils.rm_rf(path)
  983. Dir.chdir(pwd)
  984. end
  985. end
  986. # JRUBY-5282
  987. def test_file_methods_with_closed_stream
  988. filename = 'test.txt'
  989. begin
  990. file = File.open(filename, 'w+')
  991. file.close
  992. %w{atime ctime lstat mtime stat}.each do |method|
  993. assert_raise(IOError) { file.send(method.to_sym) }
  994. end
  995. assert_raise(IOError) { file.truncate(0) }
  996. assert_raise(IOError) { file.chmod(777) }
  997. assert_raise(IOError) { file.chown(0, 0) }
  998. ensure
  999. File.unlink(filename)
  1000. end
  1001. end
  1002. # JRUBY-5286
  1003. def test_file_path_is_tainted
  1004. filename = 'test.txt'
  1005. io = File.new(filename, 'w')
  1006. assert io.path.tainted?
  1007. ensure
  1008. io.close
  1009. File.unlink(filename)
  1010. end
  1011. end