PageRenderTime 91ms CodeModel.GetById 20ms app.highlight 66ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/gems/1.8/gems/rake-0.9.2/test/test_rake_rules.rb

http://github.com/agross/netopenspace
Ruby | 346 lines | 310 code | 35 blank | 1 comment | 0 complexity | b74f8a3ce1b56cb848257ea20d767272 MD5 | raw file
  1require File.expand_path('../helper', __FILE__)
  2require 'fileutils'
  3
  4######################################################################
  5class TestRakeRules < Rake::TestCase
  6  include Rake
  7
  8  SRCFILE  = "testdata/abc.c"
  9  SRCFILE2 =  "testdata/xyz.c"
 10  FTNFILE  = "testdata/abc.f"
 11  OBJFILE  = "testdata/abc.o"
 12  FOOFILE  = "testdata/foo"
 13  DOTFOOFILE = "testdata/.foo"
 14
 15  def setup
 16    super
 17
 18    Task.clear
 19    @runs = []
 20    FileUtils.mkdir_p 'testdata' # HACK use tmpdir
 21  end
 22
 23  def teardown
 24    FileList['testdata/*'].uniq.each do |f| rm_r(f, :verbose=>false) end
 25
 26    super
 27  end
 28
 29  def test_multiple_rules1
 30    create_file(FTNFILE)
 31    delete_file(SRCFILE)
 32    delete_file(OBJFILE)
 33    rule(/\.o$/ => ['.c']) do @runs << :C end
 34    rule(/\.o$/ => ['.f']) do @runs << :F end
 35    t = Task[OBJFILE]
 36    t.invoke
 37    Task[OBJFILE].invoke
 38    assert_equal [:F], @runs
 39  end
 40
 41  def test_multiple_rules2
 42    create_file(FTNFILE)
 43    delete_file(SRCFILE)
 44    delete_file(OBJFILE)
 45    rule(/\.o$/ => ['.f']) do @runs << :F end
 46    rule(/\.o$/ => ['.c']) do @runs << :C end
 47    Task[OBJFILE].invoke
 48    assert_equal [:F], @runs
 49  end
 50
 51  def test_create_with_source
 52    create_file(SRCFILE)
 53    rule(/\.o$/ => ['.c']) do |t|
 54      @runs << t.name
 55      assert_equal OBJFILE, t.name
 56      assert_equal SRCFILE, t.source
 57    end
 58    Task[OBJFILE].invoke
 59    assert_equal [OBJFILE], @runs
 60  end
 61
 62  def test_single_dependent
 63    create_file(SRCFILE)
 64    rule(/\.o$/ => '.c') do |t|
 65      @runs << t.name
 66    end
 67    Task[OBJFILE].invoke
 68    assert_equal [OBJFILE], @runs
 69  end
 70
 71  def test_rule_can_be_created_by_string
 72    create_file(SRCFILE)
 73    rule '.o' => ['.c'] do |t|
 74      @runs << t.name
 75    end
 76    Task[OBJFILE].invoke
 77    assert_equal [OBJFILE], @runs
 78  end
 79
 80  def test_rule_prereqs_can_be_created_by_string
 81    create_file(SRCFILE)
 82    rule '.o' => '.c' do |t|
 83      @runs << t.name
 84    end
 85    Task[OBJFILE].invoke
 86    assert_equal [OBJFILE], @runs
 87  end
 88
 89  def test_plain_strings_as_dependents_refer_to_files
 90    create_file(SRCFILE)
 91    rule '.o' => SRCFILE do |t|
 92      @runs << t.name
 93    end
 94    Task[OBJFILE].invoke
 95    assert_equal [OBJFILE], @runs
 96  end
 97
 98  def test_file_names_beginning_with_dot_can_be_tricked_into_referring_to_file
 99    verbose(false) do
100      chdir("testdata") do
101        create_file('.foo')
102        rule '.o' => "./.foo" do |t|
103          @runs << t.name
104        end
105        Task[OBJFILE].invoke
106        assert_equal [OBJFILE], @runs
107      end
108    end
109  end
110
111  def test_file_names_beginning_with_dot_can_be_wrapped_in_lambda
112    verbose(false) do
113      chdir("testdata") do
114        create_file(".foo")
115        rule '.o' => lambda{".foo"} do |t|
116          @runs << "#{t.name} - #{t.source}"
117        end
118        Task[OBJFILE].invoke
119        assert_equal ["#{OBJFILE} - .foo"], @runs
120      end
121    end
122  end
123
124  def test_file_names_containing_percent_can_be_wrapped_in_lambda
125    verbose(false) do
126      chdir("testdata") do
127        create_file("foo%x")
128        rule '.o' => lambda{"foo%x"} do |t|
129          @runs << "#{t.name} - #{t.source}"
130        end
131        Task[OBJFILE].invoke
132        assert_equal ["#{OBJFILE} - foo%x"], @runs
133      end
134    end
135  end
136
137  def test_non_extension_rule_name_refers_to_file
138    verbose(false) do
139      chdir("testdata") do
140        create_file("abc.c")
141        rule "abc" => '.c' do |t|
142          @runs << t.name
143        end
144        Task["abc"].invoke
145        assert_equal ["abc"], @runs
146      end
147    end
148  end
149
150  def test_pathmap_automatically_applies_to_name
151    verbose(false) do
152      chdir("testdata") do
153        create_file("zzabc.c")
154        rule ".o" => 'zz%{x,a}n.c' do |t|
155          @runs << "#{t.name} - #{t.source}"
156        end
157        Task["xbc.o"].invoke
158        assert_equal ["xbc.o - zzabc.c"], @runs
159      end
160    end
161  end
162
163  def test_plain_strings_are_just_filenames
164    verbose(false) do
165      chdir("testdata") do
166        create_file("plainname")
167        rule ".o" => 'plainname' do |t|
168          @runs << "#{t.name} - #{t.source}"
169        end
170        Task["xbc.o"].invoke
171        assert_equal ["xbc.o - plainname"], @runs
172      end
173    end
174  end
175
176  def test_rule_runs_when_explicit_task_has_no_actions
177    create_file(SRCFILE)
178    create_file(SRCFILE2)
179    delete_file(OBJFILE)
180    rule '.o' => '.c' do |t|
181      @runs << t.source
182    end
183    file OBJFILE => [SRCFILE2]
184    Task[OBJFILE].invoke
185    assert_equal [SRCFILE], @runs
186  end
187
188  def test_close_matches_on_name_do_not_trigger_rule
189    create_file("testdata/x.c")
190    rule '.o' => ['.c'] do |t|
191      @runs << t.name
192    end
193    assert_raises(RuntimeError) { Task['testdata/x.obj'].invoke }
194    assert_raises(RuntimeError) { Task['testdata/x.xyo'].invoke }
195  end
196
197  def test_rule_rebuilds_obj_when_source_is_newer
198    create_timed_files(OBJFILE, SRCFILE)
199    rule(/\.o$/ => ['.c']) do
200      @runs << :RULE
201    end
202    Task[OBJFILE].invoke
203    assert_equal [:RULE], @runs
204  end
205
206  def test_rule_with_two_sources_runs_if_both_sources_are_present
207    create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
208    rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
209      @runs << :RULE
210    end
211    Task[OBJFILE].invoke
212    assert_equal [:RULE], @runs
213  end
214
215  def test_rule_with_two_sources_but_one_missing_does_not_run
216    create_timed_files(OBJFILE, SRCFILE)
217    delete_file(SRCFILE2)
218    rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
219      @runs << :RULE
220    end
221    Task[OBJFILE].invoke
222    assert_equal [], @runs
223  end
224
225  def test_rule_with_two_sources_builds_both_sources
226    task 'x.aa'
227    task 'x.bb'
228    rule '.a' => '.aa' do
229      @runs << "A"
230    end
231    rule '.b' => '.bb' do
232      @runs << "B"
233    end
234    rule ".c" => ['.a', '.b'] do
235      @runs << "C"
236    end
237    Task["x.c"].invoke
238    assert_equal ["A", "B", "C"], @runs.sort
239  end
240
241  def test_second_rule_runs_when_first_rule_doesnt
242    create_timed_files(OBJFILE, SRCFILE)
243    delete_file(SRCFILE2)
244    rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
245      @runs << :RULE1
246    end
247    rule OBJFILE => [lambda{SRCFILE}] do
248      @runs << :RULE2
249    end
250    Task[OBJFILE].invoke
251    assert_equal [:RULE2], @runs
252  end
253
254  def test_second_rule_doest_run_if_first_triggers
255    create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
256    rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
257      @runs << :RULE1
258    end
259    rule OBJFILE => [lambda{SRCFILE}] do
260      @runs << :RULE2
261    end
262    Task[OBJFILE].invoke
263    assert_equal [:RULE1], @runs
264  end
265
266  def test_second_rule_doest_run_if_first_triggers_with_reversed_rules
267    create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
268    rule OBJFILE => [lambda{SRCFILE}] do
269      @runs << :RULE1
270    end
271    rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
272      @runs << :RULE2
273    end
274    Task[OBJFILE].invoke
275    assert_equal [:RULE1], @runs
276  end
277
278  def test_rule_with_proc_dependent_will_trigger
279    mkdir_p("testdata/src/jw")
280    create_file("testdata/src/jw/X.java")
281    rule %r(classes/.*\.class) => [
282      proc { |fn| fn.pathmap("%{classes,testdata/src}d/%n.java") }
283    ] do |task|
284      assert_equal task.name, 'classes/jw/X.class'
285      assert_equal task.source, 'testdata/src/jw/X.java'
286      @runs << :RULE
287    end
288    Task['classes/jw/X.class'].invoke
289    assert_equal [:RULE], @runs
290  ensure
291    rm_r("testdata/src", :verbose=>false) rescue nil
292  end
293
294  def test_proc_returning_lists_are_flattened_into_prereqs
295    ran = false
296    mkdir_p("testdata/flatten")
297    create_file("testdata/flatten/a.txt")
298    task 'testdata/flatten/b.data' do |t|
299      ran = true
300      touch t.name, :verbose => false
301    end
302    rule '.html' =>
303      proc { |fn|
304      [
305        fn.ext("txt"),
306        "testdata/flatten/b.data"
307      ]
308    } do |task|
309    end
310    Task['testdata/flatten/a.html'].invoke
311    assert ran, "Should have triggered flattened dependency"
312  ensure
313    rm_r("testdata/flatten", :verbose=>false) rescue nil
314  end
315
316  def test_recursive_rules_will_work_as_long_as_they_terminate
317    actions = []
318    create_file("testdata/abc.xml")
319    rule '.y' => '.xml' do actions << 'y' end
320    rule '.c' => '.y' do actions << 'c'end
321    rule '.o' => '.c' do actions << 'o'end
322    rule '.exe' => '.o' do actions << 'exe'end
323    Task["testdata/abc.exe"].invoke
324    assert_equal ['y', 'c', 'o', 'exe'], actions
325  end
326
327  def test_recursive_rules_that_dont_terminate_will_overflow
328    create_file("testdata/a.a")
329    prev = 'a'
330    ('b'..'z').each do |letter|
331      rule ".#{letter}" => ".#{prev}" do |t| puts "#{t.name}" end
332      prev = letter
333    end
334    ex = assert_raises(Rake::RuleRecursionOverflowError) {
335      Task["testdata/a.z"].invoke
336    }
337    assert_match(/a\.z => testdata\/a.y/, ex.message)
338  end
339
340  def test_rules_with_bad_dependents_will_fail
341    rule "a" => [ 1 ] do |t| puts t.name end
342    assert_raises(RuntimeError) do Task['a'].invoke end
343  end
344
345end
346