PageRenderTime 50ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_file_exhaustive.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 808 lines | 713 code | 88 blank | 7 comment | 53 complexity | 83dd1e53db88312af8b35679bb71a1e3 MD5 | raw file
  1. require "test/unit"
  2. require "fileutils"
  3. require "tmpdir"
  4. class TestFileExhaustive < Test::Unit::TestCase
  5. DRIVE = Dir.pwd[%r'\A(?:[a-z]:|//[^/]+/[^/]+)'i]
  6. def assert_incompatible_encoding
  7. d = "\u{3042}\u{3044}".encode("utf-16le")
  8. assert_raise(Encoding::CompatibilityError) {yield d}
  9. m = Class.new {define_method(:to_path) {d}}
  10. assert_raise(Encoding::CompatibilityError) {yield m.new}
  11. end
  12. def setup
  13. @dir = Dir.mktmpdir("rubytest-file")
  14. File.chown(-1, Process.gid, @dir)
  15. @file = make_tmp_filename("file")
  16. @zerofile = make_tmp_filename("zerofile")
  17. @nofile = make_tmp_filename("nofile")
  18. @symlinkfile = make_tmp_filename("symlinkfile")
  19. @hardlinkfile = make_tmp_filename("hardlinkfile")
  20. make_file("foo", @file)
  21. make_file("", @zerofile)
  22. @time = Time.now
  23. begin
  24. File.symlink(@file, @symlinkfile)
  25. rescue NotImplementedError
  26. @symlinkfile = nil
  27. end
  28. begin
  29. File.link(@file, @hardlinkfile)
  30. rescue NotImplementedError, Errno::EINVAL # EINVAL for Windows Vista
  31. @hardlinkfile = nil
  32. end
  33. end
  34. def teardown
  35. GC.start
  36. FileUtils.remove_entry_secure @dir
  37. end
  38. def make_file(content, file = @file)
  39. open(file, "w") {|fh| fh << content }
  40. end
  41. def make_tmp_filename(prefix)
  42. @hardlinkfile = @dir + "/" + prefix + File.basename(__FILE__) + ".#{$$}.test"
  43. end
  44. def test_path
  45. file = @file
  46. assert_equal(file, File.open(file) {|f| f.path})
  47. assert_equal(file, File.path(file))
  48. o = Object.new
  49. class << o; self; end.class_eval do
  50. define_method(:to_path) { file }
  51. end
  52. assert_equal(file, File.path(o))
  53. end
  54. def assert_integer(n)
  55. assert(n.is_a?(Integer), n.inspect + " is not Fixnum.")
  56. end
  57. def assert_integer_or_nil(n)
  58. assert(n.is_a?(Integer) || n.equal?(nil), n.inspect + " is neither Fixnum nor nil.")
  59. end
  60. def test_stat
  61. sleep(@time - Time.now + 1.1)
  62. make_file("foo", @file + "2")
  63. fs1, fs2 = File.stat(@file), File.stat(@file + "2")
  64. assert_nothing_raised do
  65. assert_equal(0, fs1 <=> fs1)
  66. assert_equal(-1, fs1 <=> fs2)
  67. assert_equal(1, fs2 <=> fs1)
  68. assert_nil(fs1 <=> nil)
  69. assert_integer(fs1.dev)
  70. assert_integer_or_nil(fs1.rdev)
  71. assert_integer_or_nil(fs1.dev_major)
  72. assert_integer_or_nil(fs1.dev_minor)
  73. assert_integer_or_nil(fs1.rdev_major)
  74. assert_integer_or_nil(fs1.rdev_minor)
  75. assert_integer(fs1.ino)
  76. assert_integer(fs1.mode)
  77. unless /emx|mswin|mingw/ =~ RUBY_PLATFORM
  78. # on Windows, nlink is always 1. but this behavior will be changed
  79. # in the future.
  80. assert_equal(@hardlinkfile ? 2 : 1, fs1.nlink)
  81. end
  82. assert_integer(fs1.uid)
  83. assert_integer(fs1.gid)
  84. assert_equal(3, fs1.size)
  85. assert_integer_or_nil(fs1.blksize)
  86. assert_integer_or_nil(fs1.blocks)
  87. assert_kind_of(Time, fs1.atime)
  88. assert_kind_of(Time, fs1.mtime)
  89. assert_kind_of(Time, fs1.ctime)
  90. assert_kind_of(String, fs1.inspect)
  91. end
  92. assert_raise(Errno::ENOENT) { File.stat(@nofile) }
  93. assert_kind_of(File::Stat, File.open(@file) {|f| f.stat})
  94. assert_raise(Errno::ENOENT) { File.lstat(@nofile) }
  95. assert_kind_of(File::Stat, File.open(@file) {|f| f.lstat})
  96. end
  97. def test_directory_p
  98. assert(File.directory?(@dir))
  99. assert(!(File.directory?(@dir+"/...")))
  100. assert(!(File.directory?(@file)))
  101. assert(!(File.directory?(@nofile)))
  102. end
  103. def test_pipe_p ## xxx
  104. assert(!(File.pipe?(@dir)))
  105. assert(!(File.pipe?(@file)))
  106. assert(!(File.pipe?(@nofile)))
  107. end
  108. def test_symlink_p
  109. assert(!(File.symlink?(@dir)))
  110. assert(!(File.symlink?(@file)))
  111. assert(File.symlink?(@symlinkfile)) if @symlinkfile
  112. assert(!(File.symlink?(@hardlinkfile))) if @hardlinkfile
  113. assert(!(File.symlink?(@nofile)))
  114. end
  115. def test_socket_p ## xxx
  116. assert(!(File.socket?(@dir)))
  117. assert(!(File.socket?(@file)))
  118. assert(!(File.socket?(@nofile)))
  119. end
  120. def test_blockdev_p ## xxx
  121. assert(!(File.blockdev?(@dir)))
  122. assert(!(File.blockdev?(@file)))
  123. assert(!(File.blockdev?(@nofile)))
  124. end
  125. def test_chardev_p ## xxx
  126. assert(!(File.chardev?(@dir)))
  127. assert(!(File.chardev?(@file)))
  128. assert(!(File.chardev?(@nofile)))
  129. end
  130. def test_exist_p
  131. assert(File.exist?(@dir))
  132. assert(File.exist?(@file))
  133. assert(!(File.exist?(@nofile)))
  134. end
  135. def test_readable_p
  136. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  137. return if Process.euid == 0
  138. File.chmod(0200, @file)
  139. assert(!(File.readable?(@file)))
  140. File.chmod(0600, @file)
  141. assert(File.readable?(@file))
  142. assert(!(File.readable?(@nofile)))
  143. end
  144. def test_readable_real_p
  145. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  146. return if Process.euid == 0
  147. File.chmod(0200, @file)
  148. assert(!(File.readable_real?(@file)))
  149. File.chmod(0600, @file)
  150. assert(File.readable_real?(@file))
  151. assert(!(File.readable_real?(@nofile)))
  152. end
  153. def test_world_readable_p
  154. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  155. File.chmod(0006, @file)
  156. assert(File.world_readable?(@file))
  157. File.chmod(0060, @file)
  158. assert(!(File.world_readable?(@file)))
  159. File.chmod(0600, @file)
  160. assert(!(File.world_readable?(@file)))
  161. assert(!(File.world_readable?(@nofile)))
  162. end
  163. def test_writable_p
  164. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  165. return if Process.euid == 0
  166. File.chmod(0400, @file)
  167. assert(!(File.writable?(@file)))
  168. File.chmod(0600, @file)
  169. assert(File.writable?(@file))
  170. assert(!(File.writable?(@nofile)))
  171. end
  172. def test_writable_real_p
  173. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  174. return if Process.euid == 0
  175. File.chmod(0400, @file)
  176. assert(!(File.writable_real?(@file)))
  177. File.chmod(0600, @file)
  178. assert(File.writable_real?(@file))
  179. assert(!(File.writable_real?(@nofile)))
  180. end
  181. def test_world_writable_p
  182. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  183. File.chmod(0006, @file)
  184. assert(File.world_writable?(@file))
  185. File.chmod(0060, @file)
  186. assert(!(File.world_writable?(@file)))
  187. File.chmod(0600, @file)
  188. assert(!(File.world_writable?(@file)))
  189. assert(!(File.world_writable?(@nofile)))
  190. end
  191. def test_executable_p
  192. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  193. File.chmod(0100, @file)
  194. assert(File.executable?(@file))
  195. File.chmod(0600, @file)
  196. assert(!(File.executable?(@file)))
  197. assert(!(File.executable?(@nofile)))
  198. end
  199. def test_executable_real_p
  200. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  201. File.chmod(0100, @file)
  202. assert(File.executable_real?(@file))
  203. File.chmod(0600, @file)
  204. assert(!(File.executable_real?(@file)))
  205. assert(!(File.executable_real?(@nofile)))
  206. end
  207. def test_file_p
  208. assert(!(File.file?(@dir)))
  209. assert(File.file?(@file))
  210. assert(!(File.file?(@nofile)))
  211. end
  212. def test_zero_p
  213. assert_nothing_raised { File.zero?(@dir) }
  214. assert(!(File.zero?(@file)))
  215. assert(File.zero?(@zerofile))
  216. assert(!(File.zero?(@nofile)))
  217. end
  218. def test_size_p
  219. assert_nothing_raised { File.size?(@dir) }
  220. assert_equal(3, File.size?(@file))
  221. assert(!(File.size?(@zerofile)))
  222. assert(!(File.size?(@nofile)))
  223. end
  224. def test_owned_p ## xxx
  225. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  226. assert(File.owned?(@file))
  227. assert(File.grpowned?(@file))
  228. end
  229. def test_suid_sgid_sticky ## xxx
  230. assert(!(File.setuid?(@file)))
  231. assert(!(File.setgid?(@file)))
  232. assert(!(File.sticky?(@file)))
  233. end
  234. def test_identical_p
  235. assert(File.identical?(@file, @file))
  236. assert(!(File.identical?(@file, @zerofile)))
  237. assert(!(File.identical?(@file, @nofile)))
  238. assert(!(File.identical?(@nofile, @file)))
  239. end
  240. def test_s_size
  241. assert_integer(File.size(@dir))
  242. assert_equal(3, File.size(@file))
  243. assert_equal(0, File.size(@zerofile))
  244. assert_raise(Errno::ENOENT) { File.size(@nofile) }
  245. end
  246. def test_ftype
  247. assert_equal("directory", File.ftype(@dir))
  248. assert_equal("file", File.ftype(@file))
  249. assert_equal("link", File.ftype(@symlinkfile)) if @symlinkfile
  250. assert_equal("file", File.ftype(@hardlinkfile)) if @hardlinkfile
  251. assert_raise(Errno::ENOENT) { File.ftype(@nofile) }
  252. end
  253. def test_atime
  254. t1 = File.atime(@file)
  255. t2 = File.open(@file) {|f| f.atime}
  256. assert_kind_of(Time, t1)
  257. assert_kind_of(Time, t2)
  258. assert_equal(t1, t2)
  259. assert_raise(Errno::ENOENT) { File.atime(@nofile) }
  260. end
  261. def test_mtime
  262. t1 = File.mtime(@file)
  263. t2 = File.open(@file) {|f| f.mtime}
  264. assert_kind_of(Time, t1)
  265. assert_kind_of(Time, t2)
  266. assert_equal(t1, t2)
  267. assert_raise(Errno::ENOENT) { File.mtime(@nofile) }
  268. end
  269. def test_ctime
  270. t1 = File.ctime(@file)
  271. t2 = File.open(@file) {|f| f.ctime}
  272. assert_kind_of(Time, t1)
  273. assert_kind_of(Time, t2)
  274. assert_equal(t1, t2)
  275. assert_raise(Errno::ENOENT) { File.ctime(@nofile) }
  276. end
  277. def test_chmod
  278. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  279. assert_equal(1, File.chmod(0444, @file))
  280. assert_equal(0444, File.stat(@file).mode % 01000)
  281. assert_equal(0, File.open(@file) {|f| f.chmod(0222)})
  282. assert_equal(0222, File.stat(@file).mode % 01000)
  283. File.chmod(0600, @file)
  284. assert_raise(Errno::ENOENT) { File.chmod(0600, @nofile) }
  285. end
  286. def test_lchmod
  287. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  288. assert_equal(1, File.lchmod(0444, @file))
  289. assert_equal(0444, File.stat(@file).mode % 01000)
  290. File.lchmod(0600, @file)
  291. assert_raise(Errno::ENOENT) { File.lchmod(0600, @nofile) }
  292. rescue NotImplementedError
  293. end
  294. def test_chown ## xxx
  295. end
  296. def test_lchown ## xxx
  297. end
  298. def test_symlink
  299. return unless @symlinkfile
  300. assert_equal("link", File.ftype(@symlinkfile))
  301. assert_raise(Errno::EEXIST) { File.symlink(@file, @file) }
  302. end
  303. def test_utime
  304. t = Time.local(2000)
  305. File.utime(t + 1, t + 2, @zerofile)
  306. assert_equal(t + 1, File.atime(@zerofile))
  307. assert_equal(t + 2, File.mtime(@zerofile))
  308. end
  309. def test_hardlink
  310. return unless @hardlinkfile
  311. assert_equal("file", File.ftype(@hardlinkfile))
  312. assert_raise(Errno::EEXIST) { File.link(@file, @file) }
  313. end
  314. def test_readlink
  315. return unless @symlinkfile
  316. assert_equal(@file, File.readlink(@symlinkfile))
  317. assert_raise(Errno::EINVAL) { File.readlink(@file) }
  318. assert_raise(Errno::ENOENT) { File.readlink(@nofile) }
  319. if fs = Encoding.find("filesystem")
  320. assert_equal(fs, File.readlink(@symlinkfile).encoding)
  321. end
  322. rescue NotImplementedError
  323. end
  324. def test_unlink
  325. assert_equal(1, File.unlink(@file))
  326. make_file("foo", @file)
  327. assert_raise(Errno::ENOENT) { File.unlink(@nofile) }
  328. end
  329. def test_rename
  330. assert_equal(0, File.rename(@file, @nofile))
  331. assert(!(File.exist?(@file)))
  332. assert(File.exist?(@nofile))
  333. assert_equal(0, File.rename(@nofile, @file))
  334. assert_raise(Errno::ENOENT) { File.rename(@nofile, @file) }
  335. end
  336. def test_umask
  337. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  338. prev = File.umask(0777)
  339. assert_equal(0777, File.umask)
  340. open(@nofile, "w") { }
  341. assert_equal(0, File.stat(@nofile).mode % 01000)
  342. File.unlink(@nofile)
  343. assert_equal(0777, File.umask(prev))
  344. assert_raise(ArgumentError) { File.umask(0, 1, 2) }
  345. end
  346. def test_expand_path
  347. assert_equal(@file, File.expand_path(File.basename(@file), File.dirname(@file)))
  348. if /cygwin|mingw|mswin|bccwin/ =~ RUBY_PLATFORM
  349. assert_equal(@file, File.expand_path(@file + " "))
  350. assert_equal(@file, File.expand_path(@file + "."))
  351. assert_equal(@file, File.expand_path(@file + "::$DATA"))
  352. assert_match(/\Ac:\//i, File.expand_path('c:'), '[ruby-core:31591]')
  353. assert_match(/\Ac:\//i, File.expand_path('c:foo', 'd:/bar'))
  354. assert_match(%r'\Ac:/bar/foo\z'i, File.expand_path('c:foo', 'c:/bar'))
  355. end
  356. if drive = Dir.pwd[%r'\A(?:[a-z]:|//[^/]+/[^/]+)'i]
  357. assert_match(%r"\Az:/foo\z"i, File.expand_path('/foo', "z:/bar"))
  358. assert_match(%r"\A//host/share/foo\z"i, File.expand_path('/foo', "//host/share/bar"))
  359. assert_match(%r"\A#{drive}/foo\z"i, File.expand_path('/foo'))
  360. else
  361. assert_equal("/foo", File.expand_path('/foo'))
  362. end
  363. assert_kind_of(String, File.expand_path("~")) if ENV["HOME"]
  364. assert_raise(ArgumentError) { File.expand_path("~foo_bar_baz_unknown_user_wahaha") }
  365. assert_raise(ArgumentError) { File.expand_path("~foo_bar_baz_unknown_user_wahaha", "/") }
  366. begin
  367. bug3630 = '[ruby-core:31537]'
  368. home = ENV["HOME"]
  369. ENV["HOME"] = nil
  370. assert_raise(ArgumentError) { File.expand_path("~") }
  371. ENV["HOME"] = "~"
  372. assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
  373. ENV["HOME"] = "."
  374. assert_raise(ArgumentError, bug3630) { File.expand_path("~") }
  375. ensure
  376. ENV["HOME"] = home
  377. end
  378. assert_incompatible_encoding {|d| File.expand_path(d)}
  379. end
  380. def test_basename
  381. assert_equal(File.basename(@file).sub(/\.test$/, ""), File.basename(@file, ".test"))
  382. assert_equal("", s = File.basename(""))
  383. assert(!s.frozen?, '[ruby-core:24199]')
  384. assert_equal("foo", s = File.basename("foo"))
  385. assert(!s.frozen?, '[ruby-core:24199]')
  386. assert_equal("foo", File.basename("foo", ".ext"))
  387. assert_equal("foo", File.basename("foo.ext", ".ext"))
  388. assert_equal("foo", File.basename("foo.ext", ".*"))
  389. if /cygwin|mingw|mswin|bccwin/ =~ RUBY_PLATFORM
  390. basename = File.basename(@file)
  391. assert_equal(basename, File.basename(@file + " "))
  392. assert_equal(basename, File.basename(@file + "."))
  393. assert_equal(basename, File.basename(@file + "::$DATA"))
  394. basename.chomp!(".test")
  395. assert_equal(basename, File.basename(@file + " ", ".test"))
  396. assert_equal(basename, File.basename(@file + ".", ".test"))
  397. assert_equal(basename, File.basename(@file + "::$DATA", ".test"))
  398. assert_equal(basename, File.basename(@file + " ", ".*"))
  399. assert_equal(basename, File.basename(@file + ".", ".*"))
  400. assert_equal(basename, File.basename(@file + "::$DATA", ".*"))
  401. end
  402. assert_incompatible_encoding {|d| File.basename(d)}
  403. assert_incompatible_encoding {|d| File.basename(d, ".*")}
  404. assert_raise(Encoding::CompatibilityError) {File.basename("foo.ext", ".*".encode("utf-16le"))}
  405. end
  406. def test_dirname
  407. assert(@file.start_with?(File.dirname(@file)))
  408. assert_equal(".", File.dirname(""))
  409. assert_incompatible_encoding {|d| File.dirname(d)}
  410. end
  411. def test_extname
  412. assert_equal(".test", File.extname(@file))
  413. prefixes = ["", "/", ".", "/.", "bar/.", "/bar/."]
  414. infixes = ["", " ", "."]
  415. infixes2 = infixes + [".ext "]
  416. appendixes = [""]
  417. if /cygwin|mingw|mswin|bccwin/ =~ RUBY_PLATFORM
  418. appendixes << " " << "." << "::$DATA" << "::$DATA.bar"
  419. end
  420. prefixes.each do |prefix|
  421. appendixes.each do |appendix|
  422. infixes.each do |infix|
  423. path = "#{prefix}foo#{infix}#{appendix}"
  424. assert_equal("", File.extname(path), "File.extname(#{path.inspect})")
  425. end
  426. infixes2.each do |infix|
  427. path = "#{prefix}foo#{infix}.ext#{appendix}"
  428. assert_equal(".ext", File.extname(path), "File.extname(#{path.inspect})")
  429. end
  430. end
  431. end
  432. bug3175 = '[ruby-core:29627]'
  433. assert_equal(".rb", File.extname("/tmp//bla.rb"), bug3175)
  434. assert_incompatible_encoding {|d| File.extname(d)}
  435. end
  436. def test_split
  437. d, b = File.split(@file)
  438. assert_equal(File.dirname(@file), d)
  439. assert_equal(File.basename(@file), b)
  440. end
  441. def test_join
  442. s = "foo" + File::SEPARATOR + "bar" + File::SEPARATOR + "baz"
  443. assert_equal(s, File.join("foo", "bar", "baz"))
  444. assert_equal(s, File.join(["foo", "bar", "baz"]))
  445. o = Object.new
  446. def o.to_path; "foo"; end
  447. assert_equal(s, File.join(o, "bar", "baz"))
  448. assert_equal(s, File.join("foo" + File::SEPARATOR, "bar", File::SEPARATOR + "baz"))
  449. end
  450. def test_truncate
  451. assert_equal(0, File.truncate(@file, 1))
  452. assert(File.exist?(@file))
  453. assert_equal(1, File.size(@file))
  454. assert_equal(0, File.truncate(@file, 0))
  455. assert(File.exist?(@file))
  456. assert(File.zero?(@file))
  457. make_file("foo", @file)
  458. assert_raise(Errno::ENOENT) { File.truncate(@nofile, 0) }
  459. f = File.new(@file, "w")
  460. assert_equal(0, f.truncate(2))
  461. assert(File.exist?(@file))
  462. assert_equal(2, File.size(@file))
  463. assert_equal(0, f.truncate(0))
  464. assert(File.exist?(@file))
  465. assert(File.zero?(@file))
  466. f.close
  467. make_file("foo", @file)
  468. assert_raise(IOError) { File.open(@file) {|ff| ff.truncate(0)} }
  469. rescue NotImplementedError
  470. end
  471. def test_flock ## xxx
  472. f = File.new(@file, "r+")
  473. f.flock(File::LOCK_EX)
  474. f.flock(File::LOCK_SH)
  475. f.flock(File::LOCK_UN)
  476. f.close
  477. rescue NotImplementedError
  478. end
  479. def test_test
  480. sleep(@time - Time.now + 1.1)
  481. make_file("foo", @file + "2")
  482. [@dir, @file, @zerofile, @symlinkfile, @hardlinkfile].compact.each do |f|
  483. assert_equal(File.atime(f), test(?A, f))
  484. assert_equal(File.ctime(f), test(?C, f))
  485. assert_equal(File.mtime(f), test(?M, f))
  486. assert_equal(File.blockdev?(f), test(?b, f))
  487. assert_equal(File.chardev?(f), test(?c, f))
  488. assert_equal(File.directory?(f), test(?d, f))
  489. assert_equal(File.exist?(f), test(?e, f))
  490. assert_equal(File.file?(f), test(?f, f))
  491. assert_equal(File.setgid?(f), test(?g, f))
  492. assert_equal(File.grpowned?(f), test(?G, f))
  493. assert_equal(File.sticky?(f), test(?k, f))
  494. assert_equal(File.symlink?(f), test(?l, f))
  495. assert_equal(File.owned?(f), test(?o, f))
  496. assert_nothing_raised { test(?O, f) }
  497. assert_equal(File.pipe?(f), test(?p, f))
  498. assert_equal(File.readable?(f), test(?r, f))
  499. assert_equal(File.readable_real?(f), test(?R, f))
  500. assert_equal(File.size?(f), test(?s, f))
  501. assert_equal(File.socket?(f), test(?S, f))
  502. assert_equal(File.setuid?(f), test(?u, f))
  503. assert_equal(File.writable?(f), test(?w, f))
  504. assert_equal(File.writable_real?(f), test(?W, f))
  505. assert_equal(File.executable?(f), test(?x, f))
  506. assert_equal(File.executable_real?(f), test(?X, f))
  507. assert_equal(File.zero?(f), test(?z, f))
  508. end
  509. assert_equal(false, test(?-, @dir, @file))
  510. assert_equal(true, test(?-, @file, @file))
  511. assert_equal(true, test(?=, @file, @file))
  512. assert_equal(false, test(?>, @file, @file))
  513. assert_equal(false, test(?<, @file, @file))
  514. unless /cygwin/ =~ RUBY_PLATFORM
  515. assert_equal(false, test(?=, @file, @file + "2"))
  516. assert_equal(false, test(?>, @file, @file + "2"))
  517. assert_equal(true, test(?>, @file + "2", @file))
  518. assert_equal(true, test(?<, @file, @file + "2"))
  519. assert_equal(false, test(?<, @file + "2", @file))
  520. end
  521. assert_raise(ArgumentError) { test }
  522. assert_raise(Errno::ENOENT) { test(?A, @nofile) }
  523. assert_raise(ArgumentError) { test(?a) }
  524. assert_raise(ArgumentError) { test("\0".ord) }
  525. end
  526. def test_stat_init
  527. sleep(@time - Time.now + 1.1)
  528. make_file("foo", @file + "2")
  529. fs1, fs2 = File::Stat.new(@file), File::Stat.new(@file + "2")
  530. assert_nothing_raised do
  531. assert_equal(0, fs1 <=> fs1)
  532. assert_equal(-1, fs1 <=> fs2)
  533. assert_equal(1, fs2 <=> fs1)
  534. assert_nil(fs1 <=> nil)
  535. assert_integer(fs1.dev)
  536. assert_integer_or_nil(fs1.rdev)
  537. assert_integer_or_nil(fs1.dev_major)
  538. assert_integer_or_nil(fs1.dev_minor)
  539. assert_integer_or_nil(fs1.rdev_major)
  540. assert_integer_or_nil(fs1.rdev_minor)
  541. assert_integer(fs1.ino)
  542. assert_integer(fs1.mode)
  543. unless /emx|mswin|mingw/ =~ RUBY_PLATFORM
  544. # on Windows, nlink is always 1. but this behavior will be changed
  545. # in the future.
  546. assert_equal(@hardlinkfile ? 2 : 1, fs1.nlink)
  547. end
  548. assert_integer(fs1.uid)
  549. assert_integer(fs1.gid)
  550. assert_equal(3, fs1.size)
  551. assert_integer_or_nil(fs1.blksize)
  552. assert_integer_or_nil(fs1.blocks)
  553. assert_kind_of(Time, fs1.atime)
  554. assert_kind_of(Time, fs1.mtime)
  555. assert_kind_of(Time, fs1.ctime)
  556. assert_kind_of(String, fs1.inspect)
  557. end
  558. assert_raise(Errno::ENOENT) { File::Stat.new(@nofile) }
  559. assert_kind_of(File::Stat, File::Stat.new(@file).dup)
  560. assert_raise(TypeError) do
  561. File::Stat.new(@file).instance_eval { initialize_copy(0) }
  562. end
  563. end
  564. def test_stat_ftype
  565. assert_equal("directory", File::Stat.new(@dir).ftype)
  566. assert_equal("file", File::Stat.new(@file).ftype)
  567. # File::Stat uses stat
  568. assert_equal("file", File::Stat.new(@symlinkfile).ftype) if @symlinkfile
  569. assert_equal("file", File::Stat.new(@hardlinkfile).ftype) if @hardlinkfile
  570. end
  571. def test_stat_directory_p
  572. assert(File::Stat.new(@dir).directory?)
  573. assert(!(File::Stat.new(@file).directory?))
  574. end
  575. def test_stat_pipe_p ## xxx
  576. assert(!(File::Stat.new(@dir).pipe?))
  577. assert(!(File::Stat.new(@file).pipe?))
  578. end
  579. def test_stat_symlink_p
  580. assert(!(File::Stat.new(@dir).symlink?))
  581. assert(!(File::Stat.new(@file).symlink?))
  582. # File::Stat uses stat
  583. assert(!(File::Stat.new(@symlinkfile).symlink?)) if @symlinkfile
  584. assert(!(File::Stat.new(@hardlinkfile).symlink?)) if @hardlinkfile
  585. end
  586. def test_stat_socket_p ## xxx
  587. assert(!(File::Stat.new(@dir).socket?))
  588. assert(!(File::Stat.new(@file).socket?))
  589. end
  590. def test_stat_blockdev_p ## xxx
  591. assert(!(File::Stat.new(@dir).blockdev?))
  592. assert(!(File::Stat.new(@file).blockdev?))
  593. end
  594. def test_stat_chardev_p ## xxx
  595. assert(!(File::Stat.new(@dir).chardev?))
  596. assert(!(File::Stat.new(@file).chardev?))
  597. end
  598. def test_stat_readable_p
  599. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  600. return if Process.euid == 0
  601. File.chmod(0200, @file)
  602. assert(!(File::Stat.new(@file).readable?))
  603. File.chmod(0600, @file)
  604. assert(File::Stat.new(@file).readable?)
  605. end
  606. def test_stat_readable_real_p
  607. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  608. return if Process.euid == 0
  609. File.chmod(0200, @file)
  610. assert(!(File::Stat.new(@file).readable_real?))
  611. File.chmod(0600, @file)
  612. assert(File::Stat.new(@file).readable_real?)
  613. end
  614. def test_stat_world_readable_p
  615. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  616. File.chmod(0006, @file)
  617. assert(File::Stat.new(@file).world_readable?)
  618. File.chmod(0060, @file)
  619. assert(!(File::Stat.new(@file).world_readable?))
  620. File.chmod(0600, @file)
  621. assert(!(File::Stat.new(@file).world_readable?))
  622. end
  623. def test_stat_writable_p
  624. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  625. return if Process.euid == 0
  626. File.chmod(0400, @file)
  627. assert(!(File::Stat.new(@file).writable?))
  628. File.chmod(0600, @file)
  629. assert(File::Stat.new(@file).writable?)
  630. end
  631. def test_stat_writable_real_p
  632. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  633. return if Process.euid == 0
  634. File.chmod(0400, @file)
  635. assert(!(File::Stat.new(@file).writable_real?))
  636. File.chmod(0600, @file)
  637. assert(File::Stat.new(@file).writable_real?)
  638. end
  639. def test_stat_world_writable_p
  640. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  641. File.chmod(0006, @file)
  642. assert(File::Stat.new(@file).world_writable?)
  643. File.chmod(0060, @file)
  644. assert(!(File::Stat.new(@file).world_writable?))
  645. File.chmod(0600, @file)
  646. assert(!(File::Stat.new(@file).world_writable?))
  647. end
  648. def test_stat_executable_p
  649. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  650. File.chmod(0100, @file)
  651. assert(File::Stat.new(@file).executable?)
  652. File.chmod(0600, @file)
  653. assert(!(File::Stat.new(@file).executable?))
  654. end
  655. def test_stat_executable_real_p
  656. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  657. File.chmod(0100, @file)
  658. assert(File::Stat.new(@file).executable_real?)
  659. File.chmod(0600, @file)
  660. assert(!(File::Stat.new(@file).executable_real?))
  661. end
  662. def test_stat_file_p
  663. assert(!(File::Stat.new(@dir).file?))
  664. assert(File::Stat.new(@file).file?)
  665. end
  666. def test_stat_zero_p
  667. assert_nothing_raised { File::Stat.new(@dir).zero? }
  668. assert(!(File::Stat.new(@file).zero?))
  669. assert(File::Stat.new(@zerofile).zero?)
  670. end
  671. def test_stat_size_p
  672. assert_nothing_raised { File::Stat.new(@dir).size? }
  673. assert_equal(3, File::Stat.new(@file).size?)
  674. assert(!(File::Stat.new(@zerofile).size?))
  675. end
  676. def test_stat_owned_p ## xxx
  677. return if /cygwin|mswin|bccwin|mingw|emx/ =~ RUBY_PLATFORM
  678. assert(File::Stat.new(@file).owned?)
  679. assert(File::Stat.new(@file).grpowned?)
  680. end
  681. def test_stat_suid_sgid_sticky ## xxx
  682. assert(!(File::Stat.new(@file).setuid?))
  683. assert(!(File::Stat.new(@file).setgid?))
  684. assert(!(File::Stat.new(@file).sticky?))
  685. end
  686. def test_stat_size
  687. assert_integer(File::Stat.new(@dir).size)
  688. assert_equal(3, File::Stat.new(@file).size)
  689. assert_equal(0, File::Stat.new(@zerofile).size)
  690. end
  691. def test_stat_special_file
  692. # test for special files such as pagefile.sys on Windows
  693. assert_nothing_raised do
  694. Dir::glob("C:/*.sys") {|f| File::Stat.new(f) }
  695. end
  696. end if DRIVE
  697. def test_path_check
  698. assert_nothing_raised { ENV["PATH"] }
  699. end
  700. def test_find_file
  701. assert_raise(SecurityError) do
  702. Thread.new do
  703. $SAFE = 4
  704. load(@file)
  705. end.join
  706. end
  707. end
  708. def test_size
  709. assert_equal(3, File.open(@file) {|f| f.size })
  710. File.open(@file, "a") do |f|
  711. f.write("bar")
  712. assert_equal(6, f.size)
  713. end
  714. end
  715. def test_absolute_path
  716. assert_equal(File.join(Dir.pwd, "~foo"), File.absolute_path("~foo"))
  717. dir = File.expand_path("/bar")
  718. assert_equal(File.join(dir, "~foo"), File.absolute_path("~foo", dir))
  719. end
  720. end