PageRenderTime 69ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

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

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