1require File.expand_path('../helper', __FILE__)
2require 'fileutils'
3
4class TestRakeDefinitions < Rake::TestCase
5  include Rake
6
7  EXISTINGFILE = "existing"
8
9  def setup
10    super
11
12    Task.clear
13  end
14
15  def test_task
16    done = false
17    task :one => [:two] do done = true end
18    task :two
19    task :three => [:one, :two]
20    check_tasks(:one, :two, :three)
21    assert done, "Should be done"
22  end
23
24  def test_file_task
25    done = false
26    file "one" => "two" do done = true end
27    file "two"
28    file "three" => ["one", "two"]
29    check_tasks("one", "two", "three")
30    assert done, "Should be done"
31  end
32
33  def check_tasks(n1, n2, n3)
34    t = Task[n1]
35    assert Task === t, "Should be a Task"
36    assert_equal n1.to_s, t.name
37    assert_equal [n2.to_s], t.prerequisites.collect{|n| n.to_s}
38    t.invoke
39    t2 = Task[n2]
40    assert_equal FileList[], t2.prerequisites
41    t3 = Task[n3]
42    assert_equal [n1.to_s, n2.to_s], t3.prerequisites.collect{|n|n.to_s}
43  end
44
45  def test_incremental_definitions
46    runs = []
47    task :t1 => [:t2] do runs << "A"; 4321 end
48    task :t1 => [:t3] do runs << "B"; 1234 end
49    task :t1 => [:t3]
50    task :t2
51    task :t3
52    Task[:t1].invoke
53    assert_equal ["A", "B"], runs
54    assert_equal ["t2", "t3"], Task[:t1].prerequisites
55  end
56
57  def test_missing_dependencies
58    task :x => ["missing"]
59    assert_raises(RuntimeError) { Task[:x].invoke }
60  end
61
62  def test_implicit_file_dependencies
63    runs = []
64    create_existing_file
65    task :y => [EXISTINGFILE] do |t| runs << t.name end
66    Task[:y].invoke
67    assert_equal runs, ['y']
68  end
69
70  private # ----------------------------------------------------------
71
72  def create_existing_file
73    Dir.mkdir File.dirname(EXISTINGFILE) unless
74      File.exist?(File.dirname(EXISTINGFILE))
75    open(EXISTINGFILE, "w") do |f| f.puts "HI" end unless
76      File.exist?(EXISTINGFILE)
77  end
78
79end
80
81