PageRenderTime 42ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/projects/jruby-1.7.3/test/externals/ruby1.8/fileutils/test_fileutils.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 1085 lines | 869 code | 163 blank | 53 comment | 31 complexity | 6ae373c447d3d3921fed96e847518a52 MD5 | raw file
  1. # $Id: test_fileutils.rb 11708 2007-02-12 23:01:19Z shyouhei $
  2. require 'fileutils'
  3. require 'fileasserts'
  4. require 'pathname'
  5. require 'tmpdir'
  6. require 'test/unit'
  7. class TestFileUtils < Test::Unit::TestCase
  8. TMPROOT = "#{Dir.tmpdir}/fileutils.rb.#{$$}"
  9. end
  10. prevdir = Dir.pwd
  11. tmproot = TestFileUtils::TMPROOT
  12. Dir.mkdir tmproot unless File.directory?(tmproot)
  13. Dir.chdir tmproot
  14. def have_drive_letter?
  15. /djgpp|mswin(?!ce)|mingw|bcc|emx/ =~ RUBY_PLATFORM
  16. end
  17. def have_file_perm?
  18. /djgpp|mswin|mingw|bcc|wince|emx/ !~ RUBY_PLATFORM
  19. end
  20. $fileutils_rb_have_symlink = nil
  21. def have_symlink?
  22. if $fileutils_rb_have_symlink == nil
  23. $fileutils_rb_have_symlink = check_have_symlink?
  24. end
  25. $fileutils_rb_have_symlink
  26. end
  27. def check_have_symlink?
  28. File.symlink nil, nil
  29. rescue NotImplementedError
  30. return false
  31. rescue
  32. return true
  33. end
  34. $fileutils_rb_have_hardlink = nil
  35. def have_hardlink?
  36. if $fileutils_rb_have_hardlink == nil
  37. $fileutils_rb_have_hardlink = check_have_hardlink?
  38. end
  39. $fileutils_rb_have_hardlink
  40. end
  41. def check_have_hardlink?
  42. File.link nil, nil
  43. rescue NotImplementedError
  44. return false
  45. rescue
  46. return true
  47. end
  48. begin
  49. Dir.mkdir("\n")
  50. Dir.rmdir("\n")
  51. def lf_in_path_allowed?
  52. true
  53. end
  54. rescue
  55. def lf_in_path_allowed?
  56. false
  57. end
  58. end
  59. Dir.chdir prevdir
  60. Dir.rmdir tmproot
  61. class TestFileUtils
  62. include FileUtils
  63. def check_singleton(name)
  64. assert_equal true, ::FileUtils.public_methods.include?(name.to_s)
  65. end
  66. def my_rm_rf(path)
  67. if File.exist?('/bin/rm')
  68. system %Q[/bin/rm -rf "#{path}"]
  69. else
  70. FileUtils.rm_rf path
  71. end
  72. end
  73. def mymkdir(path)
  74. Dir.mkdir path
  75. File.chown nil, Process.gid, path if have_file_perm?
  76. end
  77. def setup
  78. @prevdir = Dir.pwd
  79. tmproot = TMPROOT
  80. mymkdir tmproot unless File.directory?(tmproot)
  81. Dir.chdir tmproot
  82. my_rm_rf 'data'; mymkdir 'data'
  83. my_rm_rf 'tmp'; mymkdir 'tmp'
  84. prepare_data_file
  85. end
  86. def teardown
  87. tmproot = Dir.pwd
  88. Dir.chdir @prevdir
  89. my_rm_rf tmproot
  90. end
  91. TARGETS = %w( data/a data/all data/random data/zero )
  92. def prepare_data_file
  93. File.open('data/a', 'w') {|f|
  94. 32.times do
  95. f.puts 'a' * 50
  96. end
  97. }
  98. all_chars = (0..255).map {|n| n.chr }.join('')
  99. File.open('data/all', 'w') {|f|
  100. 32.times do
  101. f.puts all_chars
  102. end
  103. }
  104. random_chars = (0...50).map { rand(256).chr }.join('')
  105. File.open('data/random', 'w') {|f|
  106. 32.times do
  107. f.puts random_chars
  108. end
  109. }
  110. File.open('data/zero', 'w') {|f|
  111. ;
  112. }
  113. end
  114. BIGFILE = 'data/big'
  115. def prepare_big_file
  116. File.open('data/big', 'w') {|f|
  117. (4 * 1024 * 1024 / 256).times do # 4MB
  118. f.print "aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa\n"
  119. end
  120. }
  121. end
  122. def prepare_time_data
  123. File.open('data/old', 'w') {|f| f.puts 'dummy' }
  124. File.open('data/newer', 'w') {|f| f.puts 'dummy' }
  125. File.open('data/newest', 'w') {|f| f.puts 'dummy' }
  126. t = Time.now
  127. File.utime t-8, t-8, 'data/old'
  128. File.utime t-4, t-4, 'data/newer'
  129. end
  130. def each_srcdest
  131. TARGETS.each do |path|
  132. yield path, "tmp/#{File.basename(path)}"
  133. end
  134. end
  135. #
  136. # Test Cases
  137. #
  138. def test_pwd
  139. check_singleton :pwd
  140. assert_equal Dir.pwd, pwd()
  141. cwd = Dir.pwd
  142. if have_drive_letter?
  143. cd('C:/') {
  144. assert_equal 'C:/', pwd()
  145. }
  146. assert_equal cwd, pwd()
  147. else
  148. cd('/') {
  149. assert_equal '/', pwd()
  150. }
  151. assert_equal cwd, pwd()
  152. end
  153. end
  154. def test_cmp
  155. check_singleton :cmp
  156. TARGETS.each do |fname|
  157. assert cmp(fname, fname), 'not same?'
  158. end
  159. assert_raises(ArgumentError) {
  160. cmp TARGETS[0], TARGETS[0], :undefinedoption => true
  161. }
  162. # pathname
  163. touch 'tmp/cmptmp'
  164. assert_nothing_raised {
  165. cmp Pathname.new('tmp/cmptmp'), 'tmp/cmptmp'
  166. cmp 'tmp/cmptmp', Pathname.new('tmp/cmptmp')
  167. cmp Pathname.new('tmp/cmptmp'), Pathname.new('tmp/cmptmp')
  168. }
  169. end
  170. def test_cp
  171. check_singleton :cp
  172. each_srcdest do |srcpath, destpath|
  173. cp srcpath, destpath
  174. assert_same_file srcpath, destpath
  175. cp srcpath, File.dirname(destpath)
  176. assert_same_file srcpath, destpath
  177. cp srcpath, File.dirname(destpath) + '/'
  178. assert_same_file srcpath, destpath
  179. cp srcpath, destpath, :preserve => true
  180. assert_same_file srcpath, destpath
  181. assert_same_entry srcpath, destpath
  182. end
  183. # src==dest (1) same path
  184. touch 'tmp/cptmp'
  185. assert_raises(ArgumentError) {
  186. cp 'tmp/cptmp', 'tmp/cptmp'
  187. }
  188. if have_symlink?
  189. # src==dest (2) symlink and its target
  190. File.symlink 'cptmp', 'tmp/cptmp_symlink'
  191. assert_raises(ArgumentError) {
  192. cp 'tmp/cptmp', 'tmp/cptmp_symlink'
  193. }
  194. assert_raises(ArgumentError) {
  195. cp 'tmp/cptmp_symlink', 'tmp/cptmp'
  196. }
  197. # src==dest (3) looped symlink
  198. File.symlink 'symlink', 'tmp/symlink'
  199. assert_raises(Errno::ELOOP) {
  200. cp 'tmp/symlink', 'tmp/symlink'
  201. }
  202. end
  203. # pathname
  204. assert_nothing_raised {
  205. cp 'tmp/cptmp', Pathname.new('tmp/tmpdest')
  206. cp Pathname.new('tmp/cptmp'), 'tmp/tmpdest'
  207. cp Pathname.new('tmp/cptmp'), Pathname.new('tmp/tmpdest')
  208. mkdir 'tmp/tmpdir'
  209. cp ['tmp/cptmp', 'tmp/tmpdest'], Pathname.new('tmp/tmpdir')
  210. }
  211. end
  212. def test_cp_r
  213. check_singleton :cp_r
  214. cp_r 'data', 'tmp'
  215. TARGETS.each do |fname|
  216. assert_same_file fname, "tmp/#{fname}"
  217. end
  218. cp_r 'data', 'tmp2', :preserve => true
  219. TARGETS.each do |fname|
  220. assert_same_entry fname, "tmp2/#{File.basename(fname)}"
  221. assert_same_file fname, "tmp2/#{File.basename(fname)}"
  222. end
  223. # a/* -> b/*
  224. mkdir 'tmp/cpr_src'
  225. mkdir 'tmp/cpr_dest'
  226. File.open('tmp/cpr_src/a', 'w') {|f| f.puts 'a' }
  227. File.open('tmp/cpr_src/b', 'w') {|f| f.puts 'b' }
  228. File.open('tmp/cpr_src/c', 'w') {|f| f.puts 'c' }
  229. mkdir 'tmp/cpr_src/d'
  230. cp_r 'tmp/cpr_src/.', 'tmp/cpr_dest'
  231. assert_same_file 'tmp/cpr_src/a', 'tmp/cpr_dest/a'
  232. assert_same_file 'tmp/cpr_src/b', 'tmp/cpr_dest/b'
  233. assert_same_file 'tmp/cpr_src/c', 'tmp/cpr_dest/c'
  234. assert_directory 'tmp/cpr_dest/d'
  235. my_rm_rf 'tmp/cpr_src'
  236. my_rm_rf 'tmp/cpr_dest'
  237. if have_symlink?
  238. # symlink in a directory
  239. mkdir 'tmp/cpr_src'
  240. ln_s 'SLdest', 'tmp/cpr_src/symlink'
  241. cp_r 'tmp/cpr_src', 'tmp/cpr_dest'
  242. assert_symlink 'tmp/cpr_dest/symlink'
  243. assert_equal 'SLdest', File.readlink('tmp/cpr_dest/symlink')
  244. # root is a symlink
  245. ln_s 'cpr_src', 'tmp/cpr_src2'
  246. cp_r 'tmp/cpr_src2', 'tmp/cpr_dest2'
  247. assert_directory 'tmp/cpr_dest2'
  248. #assert_not_symlink 'tmp/cpr_dest2'
  249. # * 2005-05-26: feature change on trunk
  250. #assert_symlink 'tmp/cpr_dest2'
  251. # * 2005-09-19: revert for 1.8 (:dereference_root => true by default)
  252. assert_not_symlink 'tmp/cpr_dest2'
  253. assert_symlink 'tmp/cpr_dest2/symlink'
  254. assert_equal 'SLdest', File.readlink('tmp/cpr_dest2/symlink')
  255. end
  256. # pathname
  257. touch 'tmp/cprtmp'
  258. assert_nothing_raised {
  259. cp_r Pathname.new('tmp/cprtmp'), 'tmp/tmpdest'
  260. cp_r 'tmp/cprtmp', Pathname.new('tmp/tmpdest')
  261. cp_r Pathname.new('tmp/cprtmp'), Pathname.new('tmp/tmpdest')
  262. }
  263. end
  264. def test_mv
  265. check_singleton :mv
  266. mkdir 'tmp/dest'
  267. TARGETS.each do |fname|
  268. cp fname, 'tmp/mvsrc'
  269. mv 'tmp/mvsrc', 'tmp/mvdest'
  270. assert_same_file fname, 'tmp/mvdest'
  271. mv 'tmp/mvdest', 'tmp/dest/'
  272. assert_same_file fname, 'tmp/dest/mvdest'
  273. mv 'tmp/dest/mvdest', 'tmp'
  274. assert_same_file fname, 'tmp/mvdest'
  275. end
  276. # [ruby-talk:124368]
  277. mkdir 'tmp/tmpdir'
  278. mkdir_p 'tmp/dest2/tmpdir'
  279. assert_raises(Errno::EEXIST) {
  280. mv 'tmp/tmpdir', 'tmp/dest2'
  281. }
  282. mkdir 'tmp/dest2/tmpdir/junk'
  283. assert_raises(Errno::EEXIST) {
  284. mv 'tmp/tmpdir', 'tmp/dest2'
  285. }
  286. # src==dest (1) same path
  287. touch 'tmp/cptmp'
  288. assert_raises(ArgumentError) {
  289. mv 'tmp/cptmp', 'tmp/cptmp'
  290. }
  291. if have_symlink?
  292. # src==dest (2) symlink and its target
  293. File.symlink 'cptmp', 'tmp/cptmp_symlink'
  294. assert_raises(ArgumentError) {
  295. mv 'tmp/cptmp', 'tmp/cptmp_symlink'
  296. }
  297. assert_raises(ArgumentError) {
  298. mv 'tmp/cptmp_symlink', 'tmp/cptmp'
  299. }
  300. # src==dest (3) looped symlink
  301. File.symlink 'symlink', 'tmp/symlink'
  302. assert_raises(Errno::ELOOP) {
  303. mv 'tmp/symlink', 'tmp/symlink'
  304. }
  305. end
  306. # pathname
  307. assert_nothing_raised {
  308. touch 'tmp/mvtmpsrc'
  309. mv Pathname.new('tmp/mvtmpsrc'), 'tmp/mvtmpdest'
  310. touch 'tmp/mvtmpsrc'
  311. mv 'tmp/mvtmpsrc', Pathname.new('tmp/mvtmpdest')
  312. touch 'tmp/mvtmpsrc'
  313. mv Pathname.new('tmp/mvtmpsrc'), Pathname.new('tmp/mvtmpdest')
  314. }
  315. end
  316. def test_rm
  317. check_singleton :rm
  318. TARGETS.each do |fname|
  319. cp fname, 'tmp/rmsrc'
  320. rm 'tmp/rmsrc'
  321. assert_file_not_exist 'tmp/rmsrc'
  322. end
  323. # pathname
  324. touch 'tmp/rmtmp1'
  325. touch 'tmp/rmtmp2'
  326. touch 'tmp/rmtmp3'
  327. assert_nothing_raised {
  328. rm Pathname.new('tmp/rmtmp1')
  329. rm [Pathname.new('tmp/rmtmp2'), Pathname.new('tmp/rmtmp3')]
  330. }
  331. assert_file_not_exist 'tmp/rmtmp1'
  332. assert_file_not_exist 'tmp/rmtmp2'
  333. assert_file_not_exist 'tmp/rmtmp3'
  334. end
  335. def test_rm_f
  336. check_singleton :rm_f
  337. TARGETS.each do |fname|
  338. cp fname, 'tmp/rmsrc'
  339. rm_f 'tmp/rmsrc'
  340. assert_file_not_exist 'tmp/rmsrc'
  341. end
  342. if have_symlink?
  343. File.open('tmp/lnf_symlink_src', 'w') {|f| f.puts 'dummy' }
  344. File.symlink 'tmp/lnf_symlink_src', 'tmp/lnf_symlink_dest'
  345. rm_f 'tmp/lnf_symlink_dest'
  346. assert_file_not_exist 'tmp/lnf_symlink_dest'
  347. assert_file_exist 'tmp/lnf_symlink_src'
  348. end
  349. rm_f 'notexistdatafile'
  350. rm_f 'tmp/notexistdatafile'
  351. my_rm_rf 'tmpdatadir'
  352. Dir.mkdir 'tmpdatadir'
  353. # rm_f 'tmpdatadir'
  354. Dir.rmdir 'tmpdatadir'
  355. Dir.mkdir 'tmp/tmpdir'
  356. File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
  357. File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
  358. rm_f ['tmp/tmpdir/a', 'tmp/tmpdir/b', 'tmp/tmpdir/c']
  359. assert_file_not_exist 'tmp/tmpdir/a'
  360. assert_file_not_exist 'tmp/tmpdir/c'
  361. Dir.rmdir 'tmp/tmpdir'
  362. # pathname
  363. touch 'tmp/rmtmp1'
  364. touch 'tmp/rmtmp2'
  365. touch 'tmp/rmtmp3'
  366. touch 'tmp/rmtmp4'
  367. assert_nothing_raised {
  368. rm_f Pathname.new('tmp/rmtmp1')
  369. rm_f [Pathname.new('tmp/rmtmp2'), Pathname.new('tmp/rmtmp3')]
  370. }
  371. assert_file_not_exist 'tmp/rmtmp1'
  372. assert_file_not_exist 'tmp/rmtmp2'
  373. assert_file_not_exist 'tmp/rmtmp3'
  374. assert_file_exist 'tmp/rmtmp4'
  375. end
  376. def test_rm_r
  377. check_singleton :rm_r
  378. my_rm_rf 'tmpdatadir'
  379. Dir.mkdir 'tmpdatadir'
  380. rm_r 'tmpdatadir'
  381. assert_file_not_exist 'tmpdatadir'
  382. Dir.mkdir 'tmpdatadir'
  383. rm_r 'tmpdatadir/'
  384. assert_file_not_exist 'tmpdatadir'
  385. Dir.mkdir 'tmp/tmpdir'
  386. rm_r 'tmp/tmpdir/'
  387. assert_file_not_exist 'tmp/tmpdir'
  388. assert_file_exist 'tmp'
  389. Dir.mkdir 'tmp/tmpdir'
  390. rm_r 'tmp/tmpdir'
  391. assert_file_not_exist 'tmp/tmpdir'
  392. assert_file_exist 'tmp'
  393. Dir.mkdir 'tmp/tmpdir'
  394. File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
  395. File.open('tmp/tmpdir/b', 'w') {|f| f.puts 'dummy' }
  396. File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
  397. rm_r 'tmp/tmpdir'
  398. assert_file_not_exist 'tmp/tmpdir'
  399. assert_file_exist 'tmp'
  400. Dir.mkdir 'tmp/tmpdir'
  401. File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
  402. File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
  403. rm_r ['tmp/tmpdir/a', 'tmp/tmpdir/b', 'tmp/tmpdir/c'], :force => true
  404. assert_file_not_exist 'tmp/tmpdir/a'
  405. assert_file_not_exist 'tmp/tmpdir/c'
  406. Dir.rmdir 'tmp/tmpdir'
  407. if have_symlink?
  408. # [ruby-talk:94635] a symlink to the directory
  409. Dir.mkdir 'tmp/tmpdir'
  410. File.symlink '..', 'tmp/tmpdir/symlink_to_dir'
  411. rm_r 'tmp/tmpdir'
  412. assert_file_not_exist 'tmp/tmpdir'
  413. assert_file_exist 'tmp'
  414. end
  415. # pathname
  416. Dir.mkdir 'tmp/tmpdir1'; touch 'tmp/tmpdir1/tmp'
  417. Dir.mkdir 'tmp/tmpdir2'; touch 'tmp/tmpdir2/tmp'
  418. Dir.mkdir 'tmp/tmpdir3'; touch 'tmp/tmpdir3/tmp'
  419. assert_nothing_raised {
  420. rm_r Pathname.new('tmp/tmpdir1')
  421. rm_r [Pathname.new('tmp/tmpdir2'), Pathname.new('tmp/tmpdir3')]
  422. }
  423. assert_file_not_exist 'tmp/tmpdir1'
  424. assert_file_not_exist 'tmp/tmpdir2'
  425. assert_file_not_exist 'tmp/tmpdir3'
  426. end
  427. def test_remove_entry_secure
  428. check_singleton :remove_entry_secure
  429. my_rm_rf 'tmpdatadir'
  430. Dir.mkdir 'tmpdatadir'
  431. remove_entry_secure 'tmpdatadir'
  432. assert_file_not_exist 'tmpdatadir'
  433. Dir.mkdir 'tmpdatadir'
  434. remove_entry_secure 'tmpdatadir/'
  435. assert_file_not_exist 'tmpdatadir'
  436. Dir.mkdir 'tmp/tmpdir'
  437. remove_entry_secure 'tmp/tmpdir/'
  438. assert_file_not_exist 'tmp/tmpdir'
  439. assert_file_exist 'tmp'
  440. Dir.mkdir 'tmp/tmpdir'
  441. remove_entry_secure 'tmp/tmpdir'
  442. assert_file_not_exist 'tmp/tmpdir'
  443. assert_file_exist 'tmp'
  444. Dir.mkdir 'tmp/tmpdir'
  445. File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
  446. File.open('tmp/tmpdir/b', 'w') {|f| f.puts 'dummy' }
  447. File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
  448. remove_entry_secure 'tmp/tmpdir'
  449. assert_file_not_exist 'tmp/tmpdir'
  450. assert_file_exist 'tmp'
  451. Dir.mkdir 'tmp/tmpdir'
  452. File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
  453. File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
  454. remove_entry_secure 'tmp/tmpdir/a', true
  455. remove_entry_secure 'tmp/tmpdir/b', true
  456. remove_entry_secure 'tmp/tmpdir/c', true
  457. assert_file_not_exist 'tmp/tmpdir/a'
  458. assert_file_not_exist 'tmp/tmpdir/c'
  459. Dir.rmdir 'tmp/tmpdir'
  460. if have_symlink?
  461. # [ruby-talk:94635] a symlink to the directory
  462. Dir.mkdir 'tmp/tmpdir'
  463. File.symlink '..', 'tmp/tmpdir/symlink_to_dir'
  464. remove_entry_secure 'tmp/tmpdir'
  465. assert_file_not_exist 'tmp/tmpdir'
  466. assert_file_exist 'tmp'
  467. end
  468. # pathname
  469. Dir.mkdir 'tmp/tmpdir1'; touch 'tmp/tmpdir1/tmp'
  470. assert_nothing_raised {
  471. remove_entry_secure Pathname.new('tmp/tmpdir1')
  472. }
  473. assert_file_not_exist 'tmp/tmpdir1'
  474. end
  475. def test_with_big_file
  476. prepare_big_file
  477. cp BIGFILE, 'tmp/cpdest'
  478. assert_same_file BIGFILE, 'tmp/cpdest'
  479. assert cmp(BIGFILE, 'tmp/cpdest'), 'orig != copied'
  480. mv 'tmp/cpdest', 'tmp/mvdest'
  481. assert_same_file BIGFILE, 'tmp/mvdest'
  482. assert_file_not_exist 'tmp/cpdest'
  483. rm 'tmp/mvdest'
  484. assert_file_not_exist 'tmp/mvdest'
  485. end
  486. if have_hardlink?
  487. def test_ln
  488. TARGETS.each do |fname|
  489. ln fname, 'tmp/lndest'
  490. assert_same_file fname, 'tmp/lndest'
  491. File.unlink 'tmp/lndest'
  492. end
  493. ln TARGETS, 'tmp'
  494. TARGETS.each do |fname|
  495. assert_same_file fname, 'tmp/' + File.basename(fname)
  496. end
  497. TARGETS.each do |fname|
  498. File.unlink 'tmp/' + File.basename(fname)
  499. end
  500. # src==dest (1) same path
  501. touch 'tmp/cptmp'
  502. assert_raises(Errno::EEXIST) {
  503. ln 'tmp/cptmp', 'tmp/cptmp'
  504. }
  505. if have_symlink?
  506. # src==dest (2) symlink and its target
  507. File.symlink 'cptmp', 'tmp/symlink'
  508. assert_raises(Errno::EEXIST) {
  509. ln 'tmp/cptmp', 'tmp/symlink' # normal file -> symlink
  510. }
  511. assert_raises(Errno::EEXIST) {
  512. ln 'tmp/symlink', 'tmp/cptmp' # symlink -> normal file
  513. }
  514. # src==dest (3) looped symlink
  515. File.symlink 'cptmp_symlink', 'tmp/cptmp_symlink'
  516. begin
  517. ln 'tmp/cptmp_symlink', 'tmp/cptmp_symlink'
  518. rescue => err
  519. assert_kind_of SystemCallError, err
  520. end
  521. end
  522. # pathname
  523. touch 'tmp/lntmp'
  524. assert_nothing_raised {
  525. ln Pathname.new('tmp/lntmp'), 'tmp/lndesttmp1'
  526. ln 'tmp/lntmp', Pathname.new('tmp/lndesttmp2')
  527. ln Pathname.new('tmp/lntmp'), Pathname.new('tmp/lndesttmp3')
  528. }
  529. end
  530. end
  531. if have_symlink?
  532. def test_ln_s
  533. check_singleton :ln_s
  534. TARGETS.each do |fname|
  535. ln_s fname, 'tmp/lnsdest'
  536. assert FileTest.symlink?('tmp/lnsdest'), 'not symlink'
  537. assert_equal fname, File.readlink('tmp/lnsdest')
  538. rm_f 'tmp/lnsdest'
  539. end
  540. assert_nothing_raised {
  541. ln_s 'symlink', 'tmp/symlink'
  542. }
  543. assert_symlink 'tmp/symlink'
  544. # pathname
  545. touch 'tmp/lnsdest'
  546. assert_nothing_raised {
  547. ln_s Pathname.new('lnsdest'), 'tmp/symlink_tmp1'
  548. ln_s 'lnsdest', Pathname.new('tmp/symlink_tmp2')
  549. ln_s Pathname.new('lnsdest'), Pathname.new('tmp/symlink_tmp3')
  550. }
  551. end
  552. end
  553. if have_symlink?
  554. def test_ln_sf
  555. check_singleton :ln_sf
  556. TARGETS.each do |fname|
  557. ln_sf fname, 'tmp/lnsdest'
  558. assert FileTest.symlink?('tmp/lnsdest'), 'not symlink'
  559. assert_equal fname, File.readlink('tmp/lnsdest')
  560. ln_sf fname, 'tmp/lnsdest'
  561. ln_sf fname, 'tmp/lnsdest'
  562. end
  563. assert_nothing_raised {
  564. ln_sf 'symlink', 'tmp/symlink'
  565. }
  566. # pathname
  567. touch 'tmp/lns_dest'
  568. assert_nothing_raised {
  569. ln_sf Pathname.new('lns_dest'), 'tmp/symlink_tmp1'
  570. ln_sf 'lns_dest', Pathname.new('tmp/symlink_tmp2')
  571. ln_sf Pathname.new('lns_dest'), Pathname.new('tmp/symlink_tmp3')
  572. }
  573. end
  574. end
  575. def test_mkdir
  576. check_singleton :mkdir
  577. my_rm_rf 'tmpdatadir'
  578. mkdir 'tmpdatadir'
  579. assert_directory 'tmpdatadir'
  580. Dir.rmdir 'tmpdatadir'
  581. mkdir 'tmpdatadir/'
  582. assert_directory 'tmpdatadir'
  583. Dir.rmdir 'tmpdatadir'
  584. mkdir 'tmp/mkdirdest'
  585. assert_directory 'tmp/mkdirdest'
  586. Dir.rmdir 'tmp/mkdirdest'
  587. mkdir 'tmp/tmp', :mode => 0700
  588. assert_directory 'tmp/tmp'
  589. assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
  590. Dir.rmdir 'tmp/tmp'
  591. if have_file_perm?
  592. mkdir 'tmp/tmp', :mode => 07777
  593. assert_directory 'tmp/tmp'
  594. assert_equal 07777, (File.stat('tmp/tmp').mode & 07777)
  595. Dir.rmdir 'tmp/tmp'
  596. end
  597. if lf_in_path_allowed?
  598. mkdir "tmp-first-line\ntmp-second-line"
  599. assert_directory "tmp-first-line\ntmp-second-line"
  600. Dir.rmdir "tmp-first-line\ntmp-second-line"
  601. end
  602. # pathname
  603. assert_nothing_raised {
  604. mkdir Pathname.new('tmp/tmpdirtmp')
  605. mkdir [Pathname.new('tmp/tmpdirtmp2'), Pathname.new('tmp/tmpdirtmp3')]
  606. }
  607. end
  608. def test_mkdir_p
  609. check_singleton :mkdir_p
  610. dirs = %w(
  611. tmpdir/dir/
  612. tmpdir/dir/./
  613. tmpdir/dir/./.././dir/
  614. tmpdir/a
  615. tmpdir/a/
  616. tmpdir/a/b
  617. tmpdir/a/b/
  618. tmpdir/a/b/c/
  619. tmpdir/a/b/c
  620. tmpdir/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a
  621. tmpdir/a/a
  622. )
  623. my_rm_rf 'tmpdir'
  624. dirs.each do |d|
  625. mkdir_p d
  626. assert_directory d
  627. assert_file_not_exist "#{d}/a"
  628. assert_file_not_exist "#{d}/b"
  629. assert_file_not_exist "#{d}/c"
  630. my_rm_rf 'tmpdir'
  631. end
  632. dirs.each do |d|
  633. mkdir_p d
  634. assert_directory d
  635. end
  636. rm_rf 'tmpdir'
  637. dirs.each do |d|
  638. mkdir_p "#{Dir.pwd}/#{d}"
  639. assert_directory d
  640. end
  641. rm_rf 'tmpdir'
  642. mkdir_p 'tmp/tmp/tmp', :mode => 0700
  643. assert_directory 'tmp/tmp'
  644. assert_directory 'tmp/tmp/tmp'
  645. assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
  646. assert_equal 0700, (File.stat('tmp/tmp/tmp').mode & 0777) if have_file_perm?
  647. rm_rf 'tmp/tmp'
  648. mkdir_p 'tmp/tmp', :mode => 0
  649. assert_directory 'tmp/tmp'
  650. assert_equal 0, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
  651. # DO NOT USE rm_rf here.
  652. # (rm(1) try to chdir to parent directory, it fails to remove directory.)
  653. Dir.rmdir 'tmp/tmp'
  654. Dir.rmdir 'tmp'
  655. if have_file_perm?
  656. mkdir_p 'tmp/tmp/tmp', :mode => 07777
  657. assert_directory 'tmp/tmp/tmp'
  658. assert_equal 07777, (File.stat('tmp/tmp/tmp').mode & 07777)
  659. Dir.rmdir 'tmp/tmp/tmp'
  660. Dir.rmdir 'tmp/tmp'
  661. end
  662. # pathname
  663. assert_nothing_raised {
  664. mkdir_p Pathname.new('tmp/tmp/tmp')
  665. }
  666. end
  667. def test_install
  668. check_singleton :install
  669. File.open('tmp/aaa', 'w') {|f| f.puts 'aaa' }
  670. File.open('tmp/bbb', 'w') {|f| f.puts 'bbb' }
  671. install 'tmp/aaa', 'tmp/bbb', :mode => 0600
  672. assert_equal "aaa\n", File.read('tmp/bbb')
  673. assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
  674. t = File.mtime('tmp/bbb')
  675. install 'tmp/aaa', 'tmp/bbb'
  676. assert_equal "aaa\n", File.read('tmp/bbb')
  677. assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
  678. assert_equal t, File.mtime('tmp/bbb')
  679. File.unlink 'tmp/aaa'
  680. File.unlink 'tmp/bbb'
  681. # src==dest (1) same path
  682. touch 'tmp/cptmp'
  683. assert_raises(ArgumentError) {
  684. install 'tmp/cptmp', 'tmp/cptmp'
  685. }
  686. if have_symlink?
  687. # src==dest (2) symlink and its target
  688. File.symlink 'cptmp', 'tmp/cptmp_symlink'
  689. assert_raises(ArgumentError) {
  690. install 'tmp/cptmp', 'tmp/cptmp_symlink'
  691. }
  692. assert_raises(ArgumentError) {
  693. install 'tmp/cptmp_symlink', 'tmp/cptmp'
  694. }
  695. # src==dest (3) looped symlink
  696. File.symlink 'symlink', 'tmp/symlink'
  697. assert_raises(Errno::ELOOP) {
  698. # File#install invokes open(2), always ELOOP must be raised
  699. install 'tmp/symlink', 'tmp/symlink'
  700. }
  701. end
  702. # pathname
  703. assert_nothing_raised {
  704. rm_f 'tmp/a'; touch 'tmp/a'
  705. install 'tmp/a', Pathname.new('tmp/b')
  706. rm_f 'tmp/a'; touch 'tmp/a'
  707. install Pathname.new('tmp/a'), 'tmp/b'
  708. rm_f 'tmp/a'; touch 'tmp/a'
  709. install Pathname.new('tmp/a'), Pathname.new('tmp/b')
  710. rm_f 'tmp/a'
  711. touch 'tmp/a'
  712. touch 'tmp/b'
  713. mkdir 'tmp/dest'
  714. install [Pathname.new('tmp/a'), Pathname.new('tmp/b')], 'tmp/dest'
  715. my_rm_rf 'tmp/dest'
  716. mkdir 'tmp/dest'
  717. install [Pathname.new('tmp/a'), Pathname.new('tmp/b')], Pathname.new('tmp/dest')
  718. }
  719. end
  720. if have_file_perm?
  721. def test_chmod
  722. check_singleton :chmod
  723. touch 'tmp/a'
  724. chmod 0700, 'tmp/a'
  725. assert_equal 0700, File.stat('tmp/a').mode & 0777
  726. chmod 0500, 'tmp/a'
  727. assert_equal 0500, File.stat('tmp/a').mode & 0777
  728. end
  729. def test_chmod_R
  730. check_singleton :chmod_R
  731. mkdir_p 'tmp/dir/dir'
  732. touch %w( tmp/dir/file tmp/dir/dir/file )
  733. chmod_R 0700, 'tmp/dir'
  734. assert_equal 0700, File.stat('tmp/dir').mode & 0777
  735. assert_equal 0700, File.stat('tmp/dir/file').mode & 0777
  736. assert_equal 0700, File.stat('tmp/dir/dir').mode & 0777
  737. assert_equal 0700, File.stat('tmp/dir/dir/file').mode & 0777
  738. chmod_R 0500, 'tmp/dir'
  739. assert_equal 0500, File.stat('tmp/dir').mode & 0777
  740. assert_equal 0500, File.stat('tmp/dir/file').mode & 0777
  741. assert_equal 0500, File.stat('tmp/dir/dir').mode & 0777
  742. assert_equal 0500, File.stat('tmp/dir/dir/file').mode & 0777
  743. chmod_R 0700, 'tmp/dir' # to remove
  744. end
  745. # FIXME: How can I test this method?
  746. def test_chown
  747. check_singleton :chown
  748. end
  749. # FIXME: How can I test this method?
  750. def test_chown_R
  751. check_singleton :chown_R
  752. end
  753. end
  754. def test_copy_entry
  755. check_singleton :copy_entry
  756. each_srcdest do |srcpath, destpath|
  757. copy_entry srcpath, destpath
  758. assert_same_file srcpath, destpath
  759. assert_equal File.stat(srcpath).ftype, File.stat(destpath).ftype
  760. end
  761. if have_symlink?
  762. # root is a symlink
  763. File.symlink 'somewhere', 'tmp/symsrc'
  764. copy_entry 'tmp/symsrc', 'tmp/symdest'
  765. assert_symlink 'tmp/symdest'
  766. assert_equal 'somewhere', File.readlink('tmp/symdest')
  767. # content is a symlink
  768. mkdir 'tmp/dir'
  769. File.symlink 'somewhere', 'tmp/dir/sym'
  770. copy_entry 'tmp/dir', 'tmp/dirdest'
  771. assert_directory 'tmp/dirdest'
  772. assert_not_symlink 'tmp/dirdest'
  773. assert_symlink 'tmp/dirdest/sym'
  774. assert_equal 'somewhere', File.readlink('tmp/dirdest/sym')
  775. end
  776. end
  777. def test_copy_file
  778. check_singleton :copy_file
  779. each_srcdest do |srcpath, destpath|
  780. copy_file srcpath, destpath
  781. assert_same_file srcpath, destpath
  782. end
  783. end
  784. def test_copy_stream
  785. check_singleton :copy_stream
  786. # IO
  787. each_srcdest do |srcpath, destpath|
  788. File.open(srcpath) {|src|
  789. File.open(destpath, 'w') {|dest|
  790. copy_stream src, dest
  791. }
  792. }
  793. assert_same_file srcpath, destpath
  794. end
  795. # duck typing test [ruby-dev:25369]
  796. my_rm_rf 'tmp'
  797. Dir.mkdir 'tmp'
  798. each_srcdest do |srcpath, destpath|
  799. File.open(srcpath) {|src|
  800. File.open(destpath, 'w') {|dest|
  801. copy_stream Stream.new(src), Stream.new(dest)
  802. }
  803. }
  804. assert_same_file srcpath, destpath
  805. end
  806. end
  807. def test_remove_file
  808. check_singleton :remove_file
  809. File.open('data/tmp', 'w') {|f| f.puts 'dummy' }
  810. remove_file 'data/tmp'
  811. assert_file_not_exist 'data/tmp'
  812. if have_file_perm?
  813. File.open('data/tmp', 'w') {|f| f.puts 'dummy' }
  814. File.chmod 0, 'data/tmp'
  815. remove_file 'data/tmp'
  816. assert_file_not_exist 'data/tmp'
  817. end
  818. end
  819. def test_remove_dir
  820. check_singleton :remove_dir
  821. Dir.mkdir 'data/tmpdir'
  822. File.open('data/tmpdir/a', 'w') {|f| f.puts 'dummy' }
  823. remove_dir 'data/tmpdir'
  824. assert_file_not_exist 'data/tmpdir'
  825. if have_file_perm?
  826. Dir.mkdir 'data/tmpdir'
  827. File.chmod 0555, 'data/tmpdir'
  828. remove_dir 'data/tmpdir'
  829. assert_file_not_exist 'data/tmpdir'
  830. end
  831. end
  832. def test_compare_file
  833. check_singleton :compare_file
  834. # FIXME
  835. end
  836. def test_compare_stream
  837. check_singleton :compare_stream
  838. # FIXME
  839. end
  840. class Stream
  841. def initialize(f)
  842. @f = f
  843. end
  844. def read(n)
  845. @f.read(n)
  846. end
  847. def write(str)
  848. @f.write str
  849. end
  850. end
  851. def test_uptodate?
  852. check_singleton :uptodate?
  853. prepare_time_data
  854. Dir.chdir('data') {
  855. assert( uptodate?('newest', %w(old newer notexist)) )
  856. assert( ! uptodate?('newer', %w(old newest notexist)) )
  857. assert( ! uptodate?('notexist', %w(old newest newer)) )
  858. }
  859. # pathname
  860. touch 'tmp/a'
  861. touch 'tmp/b'
  862. touch 'tmp/c'
  863. assert_nothing_raised {
  864. uptodate? Pathname.new('tmp/a'), ['tmp/b', 'tmp/c']
  865. uptodate? 'tmp/a', [Pathname.new('tmp/b'), 'tmp/c']
  866. uptodate? 'tmp/a', ['tmp/b', Pathname.new('tmp/c')]
  867. uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')]
  868. }
  869. end
  870. def test_cd
  871. check_singleton :cd
  872. end
  873. def test_chdir
  874. check_singleton :chdir
  875. end
  876. def test_getwd
  877. check_singleton :getwd
  878. end
  879. def test_identical?
  880. check_singleton :identical?
  881. end
  882. def test_link
  883. check_singleton :link
  884. end
  885. def test_makedirs
  886. check_singleton :makedirs
  887. end
  888. def test_mkpath
  889. check_singleton :mkpath
  890. end
  891. def test_move
  892. check_singleton :move
  893. end
  894. def test_rm_rf
  895. check_singleton :rm_rf
  896. end
  897. def test_rmdir
  898. check_singleton :rmdir
  899. end
  900. def test_rmtree
  901. check_singleton :rmtree
  902. end
  903. def test_safe_unlink
  904. check_singleton :safe_unlink
  905. end
  906. def test_symlink
  907. check_singleton :symlink
  908. end
  909. def test_touch
  910. check_singleton :touch
  911. end
  912. def test_collect_methods
  913. end
  914. def test_commands
  915. end
  916. def test_have_option?
  917. end
  918. def test_options
  919. end
  920. def test_options_of
  921. end
  922. end