summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rake/test_rake_rules.rb
blob: ece75e5d9ea30152e7f3aaa054b26f5d71ef7658 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
require File.expand_path('../helper', __FILE__)
require 'fileutils'

class TestRakeRules < Rake::TestCase
  include Rake

  SRCFILE    = "abc.c"
  SRCFILE2   = "xyz.c"
  FTNFILE    = "abc.f"
  OBJFILE    = "abc.o"
  FOOFILE    = "foo"
  DOTFOOFILE = ".foo"

  def setup
    super

    Task.clear
    @runs = []
  end

  def test_multiple_rules1
    create_file(FTNFILE)
    delete_file(SRCFILE)
    delete_file(OBJFILE)
    rule(/\.o$/ => ['.c']) do @runs << :C end
    rule(/\.o$/ => ['.f']) do @runs << :F end
    t = Task[OBJFILE]
    t.invoke
    Task[OBJFILE].invoke
    assert_equal [:F], @runs
  end

  def test_multiple_rules2
    create_file(FTNFILE)
    delete_file(SRCFILE)
    delete_file(OBJFILE)
    rule(/\.o$/ => ['.f']) do @runs << :F end
    rule(/\.o$/ => ['.c']) do @runs << :C end
    Task[OBJFILE].invoke
    assert_equal [:F], @runs
  end

  def test_create_with_source
    create_file(SRCFILE)
    rule(/\.o$/ => ['.c']) do |t|
      @runs << t.name
      assert_equal OBJFILE, t.name
      assert_equal SRCFILE, t.source
    end
    Task[OBJFILE].invoke
    assert_equal [OBJFILE], @runs
  end

  def test_single_dependent
    create_file(SRCFILE)
    rule(/\.o$/ => '.c') do |t|
      @runs << t.name
    end
    Task[OBJFILE].invoke
    assert_equal [OBJFILE], @runs
  end

  def test_rule_can_be_created_by_string
    create_file(SRCFILE)
    rule '.o' => ['.c'] do |t|
      @runs << t.name
    end
    Task[OBJFILE].invoke
    assert_equal [OBJFILE], @runs
  end

  def test_rule_prereqs_can_be_created_by_string
    create_file(SRCFILE)
    rule '.o' => '.c' do |t|
      @runs << t.name
    end
    Task[OBJFILE].invoke
    assert_equal [OBJFILE], @runs
  end

  def test_plain_strings_as_dependents_refer_to_files
    create_file(SRCFILE)
    rule '.o' => SRCFILE do |t|
      @runs << t.name
    end
    Task[OBJFILE].invoke
    assert_equal [OBJFILE], @runs
  end

  def test_file_names_beginning_with_dot_can_be_tricked_into_referring_to_file
    verbose(false) do
      create_file('.foo')
      rule '.o' => "./.foo" do |t|
        @runs << t.name
      end
      Task[OBJFILE].invoke
      assert_equal [OBJFILE], @runs
    end
  end

  def test_file_names_beginning_with_dot_can_be_wrapped_in_lambda
    verbose(false) do

      create_file(".foo")
      rule '.o' => lambda { ".foo" } do |t|
        @runs << "#{t.name} - #{t.source}"
      end
      Task[OBJFILE].invoke
      assert_equal ["#{OBJFILE} - .foo"], @runs
    end
  end

  def test_file_names_containing_percent_can_be_wrapped_in_lambda
    verbose(false) do
      create_file("foo%x")
      rule '.o' => lambda { "foo%x" } do |t|
        @runs << "#{t.name} - #{t.source}"
      end
      Task[OBJFILE].invoke
      assert_equal ["#{OBJFILE} - foo%x"], @runs
    end
  end

  def test_non_extension_rule_name_refers_to_file
    verbose(false) do
      create_file("abc.c")
      rule "abc" => '.c' do |t|
        @runs << t.name
      end
      Task["abc"].invoke
      assert_equal ["abc"], @runs
    end
  end

  def test_pathmap_automatically_applies_to_name
    verbose(false) do
      create_file("zzabc.c")
      rule ".o" => 'zz%{x,a}n.c' do |t|
        @runs << "#{t.name} - #{t.source}"
      end
      Task["xbc.o"].invoke
      assert_equal ["xbc.o - zzabc.c"], @runs
    end
  end

  def test_plain_strings_are_just_filenames
    verbose(false) do
      create_file("plainname")
      rule ".o" => 'plainname' do |t|
        @runs << "#{t.name} - #{t.source}"
      end
      Task["xbc.o"].invoke
      assert_equal ["xbc.o - plainname"], @runs
    end
  end

  def test_rule_runs_when_explicit_task_has_no_actions
    create_file(SRCFILE)
    create_file(SRCFILE2)
    delete_file(OBJFILE)
    rule '.o' => '.c' do |t|
      @runs << t.source
    end
    file OBJFILE => [SRCFILE2]
    Task[OBJFILE].invoke
    assert_equal [SRCFILE], @runs
  end

  def test_close_matches_on_name_do_not_trigger_rule
    create_file("x.c")
    rule '.o' => ['.c'] do |t|
      @runs << t.name
    end
    assert_raises(RuntimeError) { Task['x.obj'].invoke }
    assert_raises(RuntimeError) { Task['x.xyo'].invoke }
  end

  def test_rule_rebuilds_obj_when_source_is_newer
    create_timed_files(OBJFILE, SRCFILE)
    rule(/\.o$/ => ['.c']) do
      @runs << :RULE
    end
    Task[OBJFILE].invoke
    assert_equal [:RULE], @runs
  end

  def test_rule_with_two_sources_runs_if_both_sources_are_present
    create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
    rule OBJFILE => [lambda { SRCFILE }, lambda { SRCFILE2 }] do
      @runs << :RULE
    end
    Task[OBJFILE].invoke
    assert_equal [:RULE], @runs
  end

  def test_rule_with_two_sources_but_one_missing_does_not_run
    create_timed_files(OBJFILE, SRCFILE)
    delete_file(SRCFILE2)
    rule OBJFILE => [lambda { SRCFILE }, lambda { SRCFILE2 }] do
      @runs << :RULE
    end
    Task[OBJFILE].invoke
    assert_equal [], @runs
  end

  def test_rule_with_two_sources_builds_both_sources
    task 'x.aa'
    task 'x.bb'
    rule '.a' => '.aa' do
      @runs << "A"
    end
    rule '.b' => '.bb' do
      @runs << "B"
    end
    rule ".c" => ['.a', '.b'] do
      @runs << "C"
    end
    Task["x.c"].invoke
    assert_equal ["A", "B", "C"], @runs.sort
  end

  def test_second_rule_runs_when_first_rule_doesnt
    create_timed_files(OBJFILE, SRCFILE)
    delete_file(SRCFILE2)
    rule OBJFILE => [lambda { SRCFILE }, lambda { SRCFILE2 }] do
      @runs << :RULE1
    end
    rule OBJFILE => [lambda { SRCFILE }] do
      @runs << :RULE2
    end
    Task[OBJFILE].invoke
    assert_equal [:RULE2], @runs
  end

  def test_second_rule_doest_run_if_first_triggers
    create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
    rule OBJFILE => [lambda { SRCFILE }, lambda { SRCFILE2 }] do
      @runs << :RULE1
    end
    rule OBJFILE => [lambda { SRCFILE }] do
      @runs << :RULE2
    end
    Task[OBJFILE].invoke
    assert_equal [:RULE1], @runs
  end

  def test_second_rule_doest_run_if_first_triggers_with_reversed_rules
    create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
    rule OBJFILE => [lambda { SRCFILE }] do
      @runs << :RULE1
    end
    rule OBJFILE => [lambda { SRCFILE }, lambda { SRCFILE2 }] do
      @runs << :RULE2
    end
    Task[OBJFILE].invoke
    assert_equal [:RULE1], @runs
  end

  def test_rule_with_proc_dependent_will_trigger
    mkdir_p("src/jw")
    create_file("src/jw/X.java")
    rule %r(classes/.*\.class) => [
      proc { |fn| fn.pathmap("%{classes,src}d/%n.java") }
    ] do |task|
      assert_equal task.name, 'classes/jw/X.class'
      assert_equal task.source, 'src/jw/X.java'
      @runs << :RULE
    end
    Task['classes/jw/X.class'].invoke
    assert_equal [:RULE], @runs
  ensure
    rm_r("src", :verbose=>false) rescue nil
  end

  def test_proc_returning_lists_are_flattened_into_prereqs
    ran = false
    mkdir_p("flatten")
    create_file("flatten/a.txt")
    task 'flatten/b.data' do |t|
      ran = true
      touch t.name, :verbose => false
    end
    rule '.html' =>
      proc { |fn|
      [
        fn.ext("txt"),
        "flatten/b.data"
      ]
    } do |task|
    end
    Task['flatten/a.html'].invoke
    assert ran, "Should have triggered flattened dependency"
  ensure
    rm_r("flatten", :verbose=>false) rescue nil
  end

  def test_recursive_rules_will_work_as_long_as_they_terminate
    actions = []
    create_file("abc.xml")
    rule '.y' => '.xml' do actions << 'y' end
    rule '.c' => '.y' do actions << 'c'end
    rule '.o' => '.c' do actions << 'o'end
    rule '.exe' => '.o' do actions << 'exe'end
    Task["abc.exe"].invoke
    assert_equal ['y', 'c', 'o', 'exe'], actions
  end

  def test_recursive_rules_that_dont_terminate_will_overflow
    create_file("a.a")
    prev = 'a'
    ('b'..'z').each do |letter|
      rule ".#{letter}" => ".#{prev}" do |t| puts "#{t.name}" end
      prev = letter
    end
    ex = assert_raises(Rake::RuleRecursionOverflowError) {
      Task["a.z"].invoke
    }
    assert_match(/a\.z => a.y/, ex.message)
  end

  def test_rules_with_bad_dependents_will_fail
    rule "a" => [1] do |t| puts t.name end
    assert_raises(RuntimeError) do Task['a'].invoke end
  end

  def test_string_rule_with_args
    delete_file(OBJFILE)
    create_file(SRCFILE)
    rule '.o', [:a] => SRCFILE do |t, args|
      assert_equal 'arg', args.a
    end
    Task[OBJFILE].invoke('arg')
  end

  def test_regex_rule_with_args
    delete_file(OBJFILE)
    create_file(SRCFILE)
    rule(/.o$/, [:a] => SRCFILE) do |t, args|
      assert_equal 'arg', args.a
    end
    Task[OBJFILE].invoke('arg')
  end

  def test_string_rule_with_args_and_lambda_prereq
    delete_file(OBJFILE)
    create_file(SRCFILE)
    rule '.o', [:a] => [lambda{SRCFILE}]do |t, args|
      assert_equal 'arg', args.a
    end
    Task[OBJFILE].invoke('arg')
  end

  def test_regex_rule_with_args_and_lambda_prereq
    delete_file(OBJFILE)
    create_file(SRCFILE)
    rule(/.o$/, [:a] => [lambda{SRCFILE}]) do |t, args|
      assert_equal 'arg', args.a
    end
    Task[OBJFILE].invoke('arg')
  end

  def test_rule_with_method_prereq
    create_file(".foo")
    obj = Object.new
    def obj.find_prereq
      ".foo"
    end
    rule '.o' => obj.method(:find_prereq) do |t|
      @runs << "#{t.name} - #{t.source}"
    end
    Task[OBJFILE].invoke
    assert_equal ["#{OBJFILE} - .foo"], @runs
  end

  def test_rule_with_one_arg_method_prereq
    create_file(SRCFILE)
    obj = Object.new
    def obj.find_prereq(task_name)
      task_name.ext(".c")
    end
    rule '.o' => obj.method(:find_prereq) do |t|
      @runs << "#{t.name} - #{t.source}"
    end
    Task[OBJFILE].invoke
    assert_equal ["#{OBJFILE} - abc.c"], @runs
  end

end