summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rake/test_rake_task_manager.rb
blob: c2730b67e2f7f5490fe01eee09579e6384aa72e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
require File.expand_path('../helper', __FILE__)

class TestRakeTaskManager < Rake::TestCase

  def setup
    super

    @tm = Rake::TestCase::TaskManager.new
  end

  def test_create_task_manager
    refute_nil @tm
    assert_equal [], @tm.tasks
  end

  def test_define_task
    t = @tm.define_task(Rake::Task, :t)
    assert_equal "t", t.name
    assert_equal @tm, t.application
  end

  def test_index
    e = assert_raises RuntimeError do
      @tm['bad']
    end

    assert_equal "Don't know how to build task 'bad'", e.message
  end

  def test_name_lookup
    t = @tm.define_task(Rake::Task, :t)
    assert_equal t, @tm[:t]
  end

  def test_namespace_task_create
    @tm.in_namespace("x") do
      t = @tm.define_task(Rake::Task, :t)
      assert_equal "x:t", t.name
    end
    assert_equal ["x:t"], @tm.tasks.map { |t| t.name }
  end

  def test_define_namespaced_task
    t = @tm.define_task(Rake::Task, 'n:a:m:e:t')
    assert_equal Rake::Scope.make("e", "m", "a", "n"), t.scope
    assert_equal "n:a:m:e:t", t.name
    assert_equal @tm, t.application
  end

  def test_define_namespace_in_namespace
    t = nil
    @tm.in_namespace("n") do
      t = @tm.define_task(Rake::Task, 'a:m:e:t')
    end
    assert_equal Rake::Scope.make("e", "m", "a", "n"), t.scope
    assert_equal "n:a:m:e:t", t.name
    assert_equal @tm, t.application
  end

  def test_anonymous_namespace
    anon_ns = @tm.in_namespace(nil) do
      t = @tm.define_task(Rake::Task, :t)
      assert_equal "_anon_1:t", t.name
    end
    task = anon_ns[:t]
    assert_equal "_anon_1:t", task.name
  end

  def test_create_filetask_in_namespace
    @tm.in_namespace("x") do
      t = @tm.define_task(Rake::FileTask, "fn")
      assert_equal "fn", t.name
    end

    assert_equal ["fn"], @tm.tasks.map { |t| t.name }
  end

  def test_namespace_yields_same_namespace_as_returned
    yielded_namespace = nil
    returned_namespace = @tm.in_namespace("x") do |ns|
      yielded_namespace = ns
    end
    assert_equal returned_namespace, yielded_namespace
  end

  def test_name_lookup_with_implicit_file_tasks
    FileUtils.touch 'README.rdoc'

    t = @tm["README.rdoc"]

    assert_equal "README.rdoc", t.name
    assert Rake::FileTask === t
  end

  def test_name_lookup_with_nonexistent_task
    assert_raises(RuntimeError) {
      @tm["DOES NOT EXIST"]
    }
  end

  def test_name_lookup_in_multiple_scopes
    aa = nil
    bb = nil
    xx = @tm.define_task(Rake::Task, :xx)
    top_z = @tm.define_task(Rake::Task, :z)
    @tm.in_namespace("a") do
      aa = @tm.define_task(Rake::Task, :aa)
      mid_z = @tm.define_task(Rake::Task, :z)
      ns_d = @tm.define_task(Rake::Task, "n:t")
      @tm.in_namespace("b") do
        bb = @tm.define_task(Rake::Task, :bb)
        bot_z = @tm.define_task(Rake::Task, :z)

        assert_equal Rake::Scope.make("b", "a"), @tm.current_scope

        assert_equal bb, @tm["a:b:bb"]
        assert_equal aa, @tm["a:aa"]
        assert_equal xx, @tm["xx"]
        assert_equal bot_z, @tm["z"]
        assert_equal mid_z, @tm["^z"]
        assert_equal top_z, @tm["^^z"]
        assert_equal top_z, @tm["^^^z"] # Over the top
        assert_equal top_z, @tm["rake:z"]
      end

      assert_equal Rake::Scope.make("a"), @tm.current_scope

      assert_equal bb, @tm["a:b:bb"]
      assert_equal aa, @tm["a:aa"]
      assert_equal xx, @tm["xx"]
      assert_equal bb, @tm["b:bb"]
      assert_equal aa, @tm["aa"]
      assert_equal mid_z, @tm["z"]
      assert_equal top_z, @tm["^z"]
      assert_equal top_z, @tm["^^z"] # Over the top
      assert_equal top_z, @tm["rake:z"]
      assert_equal ns_d, @tm["n:t"]
      assert_equal ns_d, @tm["a:n:t"]
    end

    assert_equal Rake::Scope.make, @tm.current_scope

    assert_equal Rake::Scope.make, xx.scope
    assert_equal Rake::Scope.make('a'), aa.scope
    assert_equal Rake::Scope.make('b', 'a'), bb.scope
  end

  def test_lookup_with_explicit_scopes
    t1, t2, t3, s = (0...4).map { nil }
    t1 = @tm.define_task(Rake::Task, :t)
    @tm.in_namespace("a") do
      t2 = @tm.define_task(Rake::Task, :t)
      s =  @tm.define_task(Rake::Task, :s)
      @tm.in_namespace("b") do
        t3 = @tm.define_task(Rake::Task, :t)
      end
    end
    assert_equal t1, @tm[:t, Rake::Scope.make]
    assert_equal t2, @tm[:t, Rake::Scope.make("a")]
    assert_equal t3, @tm[:t, Rake::Scope.make("b", "a")]
    assert_equal s,  @tm[:s, Rake::Scope.make("b", "a")]
    assert_equal s,  @tm[:s, Rake::Scope.make("a")]
  end

  def test_correctly_scoped_prerequisites_are_invoked
    values = []
    @tm = Rake::Application.new
    @tm.define_task(Rake::Task, :z) do values << "top z" end
    @tm.in_namespace("a") do
      @tm.define_task(Rake::Task, :z) do values << "next z" end
      @tm.define_task(Rake::Task, :x => :z)
    end

    @tm["a:x"].invoke
    assert_equal ["next z"], values
  end

end