1
require File.expand_path('../helper', __FILE__)
4
class TestRakeRules < Rake::TestCase
21
def test_multiple_rules1
25
rule(/\.o$/ => ['.c']) do @runs << :C end
26
rule(/\.o$/ => ['.f']) do @runs << :F end
30
assert_equal [:F], @runs
33
def test_multiple_rules2
37
rule(/\.o$/ => ['.f']) do @runs << :F end
38
rule(/\.o$/ => ['.c']) do @runs << :C end
40
assert_equal [:F], @runs
43
def test_create_with_source
45
rule(/\.o$/ => ['.c']) do |t|
47
assert_equal OBJFILE, t.name
48
assert_equal SRCFILE, t.source
51
assert_equal [OBJFILE], @runs
54
def test_single_dependent
56
rule(/\.o$/ => '.c') do |t|
60
assert_equal [OBJFILE], @runs
63
def test_rule_can_be_created_by_string
65
rule '.o' => ['.c'] do |t|
69
assert_equal [OBJFILE], @runs
72
def test_rule_prereqs_can_be_created_by_string
74
rule '.o' => '.c' do |t|
78
assert_equal [OBJFILE], @runs
81
def test_plain_strings_as_dependents_refer_to_files
83
rule '.o' => SRCFILE do |t|
87
assert_equal [OBJFILE], @runs
90
def test_file_names_beginning_with_dot_can_be_tricked_into_referring_to_file
93
rule '.o' => "./.foo" do |t|
97
assert_equal [OBJFILE], @runs
101
def test_file_names_beginning_with_dot_can_be_wrapped_in_lambda
105
rule '.o' => lambda{".foo"} do |t|
106
@runs << "#{t.name} - #{t.source}"
109
assert_equal ["#{OBJFILE} - .foo"], @runs
113
def test_file_names_containing_percent_can_be_wrapped_in_lambda
116
rule '.o' => lambda{"foo%x"} do |t|
117
@runs << "#{t.name} - #{t.source}"
120
assert_equal ["#{OBJFILE} - foo%x"], @runs
124
def test_non_extension_rule_name_refers_to_file
127
rule "abc" => '.c' do |t|
131
assert_equal ["abc"], @runs
135
def test_pathmap_automatically_applies_to_name
137
create_file("zzabc.c")
138
rule ".o" => 'zz%{x,a}n.c' do |t|
139
@runs << "#{t.name} - #{t.source}"
142
assert_equal ["xbc.o - zzabc.c"], @runs
146
def test_plain_strings_are_just_filenames
148
create_file("plainname")
149
rule ".o" => 'plainname' do |t|
150
@runs << "#{t.name} - #{t.source}"
153
assert_equal ["xbc.o - plainname"], @runs
157
def test_rule_runs_when_explicit_task_has_no_actions
159
create_file(SRCFILE2)
161
rule '.o' => '.c' do |t|
164
file OBJFILE => [SRCFILE2]
166
assert_equal [SRCFILE], @runs
169
def test_close_matches_on_name_do_not_trigger_rule
171
rule '.o' => ['.c'] do |t|
174
assert_raises(RuntimeError) { Task['x.obj'].invoke }
175
assert_raises(RuntimeError) { Task['x.xyo'].invoke }
178
def test_rule_rebuilds_obj_when_source_is_newer
179
create_timed_files(OBJFILE, SRCFILE)
180
rule(/\.o$/ => ['.c']) do
184
assert_equal [:RULE], @runs
187
def test_rule_with_two_sources_runs_if_both_sources_are_present
188
create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
189
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
193
assert_equal [:RULE], @runs
196
def test_rule_with_two_sources_but_one_missing_does_not_run
197
create_timed_files(OBJFILE, SRCFILE)
198
delete_file(SRCFILE2)
199
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
203
assert_equal [], @runs
206
def test_rule_with_two_sources_builds_both_sources
209
rule '.a' => '.aa' do
212
rule '.b' => '.bb' do
215
rule ".c" => ['.a', '.b'] do
219
assert_equal ["A", "B", "C"], @runs.sort
222
def test_second_rule_runs_when_first_rule_doesnt
223
create_timed_files(OBJFILE, SRCFILE)
224
delete_file(SRCFILE2)
225
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
228
rule OBJFILE => [lambda{SRCFILE}] do
232
assert_equal [:RULE2], @runs
235
def test_second_rule_doest_run_if_first_triggers
236
create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
237
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
240
rule OBJFILE => [lambda{SRCFILE}] do
244
assert_equal [:RULE1], @runs
247
def test_second_rule_doest_run_if_first_triggers_with_reversed_rules
248
create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
249
rule OBJFILE => [lambda{SRCFILE}] do
252
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
256
assert_equal [:RULE1], @runs
259
def test_rule_with_proc_dependent_will_trigger
261
create_file("src/jw/X.java")
262
rule %r(classes/.*\.class) => [
263
proc { |fn| fn.pathmap("%{classes,src}d/%n.java") }
265
assert_equal task.name, 'classes/jw/X.class'
266
assert_equal task.source, 'src/jw/X.java'
269
Task['classes/jw/X.class'].invoke
270
assert_equal [:RULE], @runs
272
rm_r("src", :verbose=>false) rescue nil
275
def test_proc_returning_lists_are_flattened_into_prereqs
278
create_file("flatten/a.txt")
279
task 'flatten/b.data' do |t|
281
touch t.name, :verbose => false
291
Task['flatten/a.html'].invoke
292
assert ran, "Should have triggered flattened dependency"
294
rm_r("flatten", :verbose=>false) rescue nil
297
def test_recursive_rules_will_work_as_long_as_they_terminate
299
create_file("abc.xml")
300
rule '.y' => '.xml' do actions << 'y' end
301
rule '.c' => '.y' do actions << 'c'end
302
rule '.o' => '.c' do actions << 'o'end
303
rule '.exe' => '.o' do actions << 'exe'end
304
Task["abc.exe"].invoke
305
assert_equal ['y', 'c', 'o', 'exe'], actions
308
def test_recursive_rules_that_dont_terminate_will_overflow
311
('b'..'z').each do |letter|
312
rule ".#{letter}" => ".#{prev}" do |t| puts "#{t.name}" end
315
ex = assert_raises(Rake::RuleRecursionOverflowError) {
318
assert_match(/a\.z => a.y/, ex.message)
321
def test_rules_with_bad_dependents_will_fail
322
rule "a" => [ 1 ] do |t| puts t.name end
323
assert_raises(RuntimeError) do Task['a'].invoke end