~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to test/rake/test_file_task.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'test/unit'
2
 
require 'fileutils'
3
 
require 'rake'
4
 
require_relative 'filecreation'
5
 
 
6
 
######################################################################
7
 
class Rake::TestFileTask < Test::Unit::TestCase
8
 
  include Rake
9
 
  include FileCreation
10
 
 
11
 
  def setup
12
 
    Task.clear
13
 
    @runs = Array.new
14
 
    FileUtils.rm_f NEWFILE
15
 
    FileUtils.rm_f OLDFILE
16
 
  end
17
 
 
18
 
  def test_file_need
19
 
    name = "testdata/dummy"
20
 
    file name
21
 
    ftask = Task[name]
22
 
    assert_equal name.to_s, ftask.name
23
 
    File.delete(ftask.name) rescue nil
24
 
    assert ftask.needed?, "file should be needed"
25
 
    open(ftask.name, "w") { |f| f.puts "HI" }
26
 
    assert_equal nil, ftask.prerequisites.collect{|n| Task[n].timestamp}.max
27
 
    assert ! ftask.needed?, "file should not be needed"
28
 
    File.delete(ftask.name) rescue nil
29
 
  end
30
 
 
31
 
  def test_file_times_new_depends_on_old
32
 
    create_timed_files(OLDFILE, NEWFILE)
33
 
 
34
 
    t1 = Rake.application.intern(FileTask, NEWFILE).enhance([OLDFILE])
35
 
    t2 = Rake.application.intern(FileTask, OLDFILE)
36
 
    assert ! t2.needed?, "Should not need to build old file"
37
 
    assert ! t1.needed?, "Should not need to rebuild new file because of old"
38
 
  end
39
 
 
40
 
  def test_file_times_old_depends_on_new
41
 
    create_timed_files(OLDFILE, NEWFILE)
42
 
 
43
 
    t1 = Rake.application.intern(FileTask,OLDFILE).enhance([NEWFILE])
44
 
    t2 = Rake.application.intern(FileTask, NEWFILE)
45
 
    assert ! t2.needed?, "Should not need to build new file"
46
 
    preq_stamp = t1.prerequisites.collect{|t| Task[t].timestamp}.max
47
 
    assert_equal t2.timestamp, preq_stamp
48
 
    assert t1.timestamp < preq_stamp, "T1 should be older"
49
 
    assert t1.needed?, "Should need to rebuild old file because of new"
50
 
  end
51
 
 
52
 
  def test_file_depends_on_task_depend_on_file
53
 
    create_timed_files(OLDFILE, NEWFILE)
54
 
 
55
 
    file NEWFILE => [:obj] do |t| @runs << t.name end
56
 
    task :obj => [OLDFILE] do |t| @runs << t.name end
57
 
    file OLDFILE           do |t| @runs << t.name end
58
 
 
59
 
    Task[:obj].invoke
60
 
    Task[NEWFILE].invoke
61
 
    assert ! @runs.include?(NEWFILE)
62
 
  end
63
 
 
64
 
  def test_existing_file_depends_on_non_existing_file
65
 
    create_file(OLDFILE)
66
 
    delete_file(NEWFILE)
67
 
    file NEWFILE
68
 
    file OLDFILE => NEWFILE
69
 
    assert_nothing_raised do Task[OLDFILE].invoke end
70
 
  end
71
 
 
72
 
  # I have currently disabled this test.  I'm not convinced that
73
 
  # deleting the file target on failure is always the proper thing to
74
 
  # do.  I'm willing to hear input on this topic.
75
 
  def ztest_file_deletes_on_failure
76
 
    task :obj 
77
 
    file NEWFILE => [:obj] do |t|
78
 
      FileUtils.touch NEWFILE
79
 
      fail "Ooops"
80
 
    end
81
 
    assert Task[NEWFILE]
82
 
    begin
83
 
      Task[NEWFILE].invoke
84
 
    rescue Exception
85
 
    end
86
 
    assert( ! File.exist?(NEWFILE), "NEWFILE should be deleted")
87
 
  end
88
 
 
89
 
end
90
 
 
91
 
######################################################################
92
 
class Rake::TestDirectoryTask < Test::Unit::TestCase
93
 
  include Rake
94
 
 
95
 
  def setup
96
 
    rm_rf "testdata", :verbose=>false
97
 
  end
98
 
 
99
 
  def teardown
100
 
    rm_rf "testdata", :verbose=>false
101
 
  end
102
 
 
103
 
  def test_directory
104
 
    desc "DESC"
105
 
    directory "testdata/a/b/c"
106
 
    assert_equal FileCreationTask, Task["testdata"].class
107
 
    assert_equal FileCreationTask, Task["testdata/a"].class
108
 
    assert_equal FileCreationTask, Task["testdata/a/b/c"].class
109
 
    assert_nil             Task["testdata"].comment
110
 
    assert_equal "DESC",   Task["testdata/a/b/c"].comment
111
 
    assert_nil             Task["testdata/a/b"].comment
112
 
    verbose(false) {
113
 
      Task['testdata/a/b'].invoke
114
 
    }
115
 
    assert File.exist?("testdata/a/b")
116
 
    assert ! File.exist?("testdata/a/b/c")
117
 
  end
118
 
 
119
 
  if Rake::Win32.windows?
120
 
    def test_directory_win32
121
 
      desc "WIN32 DESC"
122
 
      FileUtils.mkdir_p("testdata")
123
 
      Dir.chdir("testdata") do
124
 
        directory 'c:/testdata/a/b/c'
125
 
        assert_equal FileCreationTask, Task['c:/testdata'].class
126
 
        assert_equal FileCreationTask, Task['c:/testdata/a'].class
127
 
        assert_equal FileCreationTask, Task['c:/testdata/a/b/c'].class
128
 
        assert_nil             Task['c:/testdata'].comment
129
 
        assert_equal "WIN32 DESC",   Task['c:/testdata/a/b/c'].comment
130
 
        assert_nil             Task['c:/testdata/a/b'].comment
131
 
        verbose(false) {
132
 
          Task['c:/testdata/a/b'].invoke
133
 
        }
134
 
        assert File.exist?('c:/testdata/a/b')
135
 
        assert ! File.exist?('c:/testdata/a/b/c')
136
 
      end
137
 
    end
138
 
  end
139
 
end