1require 'fileutils'
2require 'test/unit'
3require 'tmpdir'
4require_relative 'fileasserts'
5
6class TestFileUtils < Test::Unit::TestCase
7end
8
9module TestFileUtils::Clobber
10  include Test::Unit::FileAssertions
11
12  def my_rm_rf(path)
13    if File.exist?('/bin/rm')
14      system %Q[/bin/rm -rf "#{path}"]
15    else
16      FileUtils.rm_rf path
17    end
18  end
19
20  SRC  = 'data/src'
21  COPY = 'data/copy'
22
23  def setup
24    @prevdir = Dir.pwd
25    class << (@fileutils_output = "")
26      alias puts <<
27    end
28    tmproot = "#{Dir.tmpdir}/fileutils.rb.#{$$}"
29    Dir.mkdir tmproot unless File.directory?(tmproot)
30    Dir.chdir tmproot
31    my_rm_rf 'data'; Dir.mkdir 'data'
32    my_rm_rf 'tmp'; Dir.mkdir 'tmp'
33    File.open(SRC,  'w') {|f| f.puts 'dummy' }
34    File.open(COPY, 'w') {|f| f.puts 'dummy' }
35  end
36
37  def teardown
38    tmproot = Dir.pwd
39    Dir.chdir @prevdir
40    my_rm_rf tmproot
41  end
42
43  def test_cp
44    cp SRC, 'tmp/cp'
45    check 'tmp/cp'
46  end
47
48  def test_mv
49    mv SRC, 'tmp/mv'
50    check 'tmp/mv'
51  end
52
53  def check(dest, message=nil)
54    assert_file_not_exist dest, message
55    assert_file_exist SRC, message
56    assert_same_file SRC, COPY, message
57  end
58
59  def test_rm
60    rm SRC
61    assert_file_exist SRC
62    assert_same_file SRC, COPY
63  end
64
65  def test_rm_f
66    rm_f SRC
67    assert_file_exist SRC
68    assert_same_file SRC, COPY
69  end
70
71  def test_rm_rf
72    rm_rf SRC
73    assert_file_exist SRC
74    assert_same_file SRC, COPY
75  end
76
77  def test_mkdir
78    mkdir 'dir'
79    assert_file_not_exist 'dir'
80  end
81
82  def test_mkdir_p
83    mkdir 'dir/dir/dir'
84    assert_file_not_exist 'dir'
85  end
86
87  def test_copy_entry
88    copy_entry SRC, 'tmp/copy_entry'
89    check 'tmp/copy_entry', bug4331 = '[ruby-dev:43129]'
90  end
91end
92