Skip to content

Commit aa9aeed

Browse files
author
jimweirich
committed
Added plain file names to rule dependency (suggested by Nobu Nakada)
git-svn-id: svn+ssh://rubyforge.org/var/svn/rake/trunk@569 5af023f1-ac1a-0410-98d6-829a145c37ef
1 parent 1a5bbe3 commit aa9aeed

File tree

3 files changed

+117
-53
lines changed

3 files changed

+117
-53
lines changed

lib/rake.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -1571,8 +1571,12 @@ def attempt_rule(task_name, extensions, block, level)
15711571
def make_sources(task_name, extensions)
15721572
extensions.collect { |ext|
15731573
case ext
1574+
when %r{/}
1575+
ext
1576+
when /^\./
1577+
task_name.ext(ext)
15741578
when String
1575-
task_name.sub(/\.[^.]*$/, ext)
1579+
ext
15761580
when Proc
15771581
ext.call(task_name)
15781582
else

test/test_pathmap.rb

+37-36
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ def test_returns_self_with_no_args
1313
def test_s_returns_file_separator
1414
sep = File::ALT_SEPARATOR || File::SEPARATOR
1515
assert_equal sep, "abc.rb".pathmap("%s")
16+
assert_equal sep, "".pathmap("%s")
1617
assert_equal "a#{sep}b", "a/b".pathmap("%d%s%f")
1718
end
1819

19-
def test_b_returns_basename
20+
def test_f_returns_basename
2021
assert_equal "abc.rb", "abc.rb".pathmap("%f")
2122
assert_equal "abc.rb", "this/is/a/dir/abc.rb".pathmap("%f")
2223
assert_equal "abc.rb", "/this/is/a/dir/abc.rb".pathmap("%f")
2324
end
2425

25-
def test_B_returns_basename_without_extension
26+
def test_n_returns_basename_without_extension
2627
assert_equal "abc", "abc.rb".pathmap("%n")
2728
assert_equal "abc", "abc".pathmap("%n")
2829
assert_equal "abc", "this/is/a/dir/abc.rb".pathmap("%n")
@@ -37,7 +38,7 @@ def test_d_returns_dirname
3738
assert_equal "/this/is/a/dir", "/this/is/a/dir/abc.rb".pathmap("%d")
3839
end
3940

40-
def test_nnnd_returns_partial_dirname
41+
def test_9d_returns_partial_dirname
4142
assert_equal "this/is", "this/is/a/dir/abc.rb".pathmap("%2d")
4243
assert_equal "this", "this/is/a/dir/abc.rb".pathmap("%1d")
4344
assert_equal ".", "this/is/a/dir/abc.rb".pathmap("%0d")
@@ -47,6 +48,39 @@ def test_nnnd_returns_partial_dirname
4748
assert_equal "this/is/a/dir", "this/is/a/dir/abc.rb".pathmap("%-100d")
4849
end
4950

51+
def test_x_returns_extension
52+
assert_equal "", "abc".pathmap("%x")
53+
assert_equal ".rb", "abc.rb".pathmap("%x")
54+
assert_equal ".rb", "abc.xyz.rb".pathmap("%x")
55+
assert_equal "", ".depends".pathmap("%x")
56+
assert_equal "", "dir/.depends".pathmap("%x")
57+
end
58+
59+
def test_X_returns_everything_but_extension
60+
assert_equal "abc", "abc".pathmap("%X")
61+
assert_equal "abc", "abc.rb".pathmap("%X")
62+
assert_equal "abc.xyz", "abc.xyz.rb".pathmap("%X")
63+
assert_equal ".depends", ".depends".pathmap("%X")
64+
assert_equal "a/dir/.depends", "a/dir/.depends".pathmap("%X")
65+
assert_equal "/.depends", "/.depends".pathmap("%X")
66+
end
67+
68+
def test_p_returns_entire_pathname
69+
assert_equal "abc.rb", "abc.rb".pathmap("%p")
70+
assert_equal "this/is/a/dir/abc.rb", "this/is/a/dir/abc.rb".pathmap("%p")
71+
assert_equal "/this/is/a/dir/abc.rb", "/this/is/a/dir/abc.rb".pathmap("%p")
72+
end
73+
74+
def test_percent_percent_returns_percent
75+
assert_equal "a%b", "".pathmap("a%%b")
76+
end
77+
78+
def test_undefined_percent_causes_error
79+
ex = assert_raise(ArgumentError) {
80+
"dir/abc.rb".pathmap("%z")
81+
}
82+
end
83+
5084
def test_pattern_returns_substitutions
5185
assert_equal "bin/org/osb",
5286
"src/org/osb/Xyz.java".pathmap("%{src,bin}d")
@@ -92,39 +126,6 @@ def test_pattern_with_invalid_operator
92126
assert_match(/unknown.*pathmap.*spec.*z/i, ex.message)
93127
end
94128

95-
def test_x_returns_extension
96-
assert_equal "", "abc".pathmap("%x")
97-
assert_equal ".rb", "abc.rb".pathmap("%x")
98-
assert_equal ".rb", "abc.xyz.rb".pathmap("%x")
99-
assert_equal "", ".depends".pathmap("%x")
100-
assert_equal "", "dir/.depends".pathmap("%x")
101-
end
102-
103-
def test_X_returns_everything_but_extension
104-
assert_equal "abc", "abc".pathmap("%X")
105-
assert_equal "abc", "abc.rb".pathmap("%X")
106-
assert_equal "abc.xyz", "abc.xyz.rb".pathmap("%X")
107-
assert_equal ".depends", ".depends".pathmap("%X")
108-
assert_equal "a/dir/.depends", "a/dir/.depends".pathmap("%X")
109-
assert_equal "/.depends", "/.depends".pathmap("%X")
110-
end
111-
112-
def test_p_returns_entire_pathname
113-
assert_equal "abc.rb", "abc.rb".pathmap("%p")
114-
assert_equal "this/is/a/dir/abc.rb", "this/is/a/dir/abc.rb".pathmap("%p")
115-
assert_equal "/this/is/a/dir/abc.rb", "/this/is/a/dir/abc.rb".pathmap("%p")
116-
end
117-
118-
def test_percent_percent_returns_percent
119-
assert_equal "a%b", "".pathmap("a%%b")
120-
end
121-
122-
def test_undefined_percent_causes_error
123-
ex = assert_raise(ArgumentError) {
124-
"dir/abc.rb".pathmap("%z")
125-
}
126-
end
127-
128129
def test_works_with_windows_separators
129130
if File::ALT_SEPARATOR
130131
assert_equal "abc", 'dir\abc.rb'.pathmap("%n")

test/test_rules.rb

+75-16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class TestRules < Test::Unit::TestCase
1414
SRCFILE2 = "testdata/xyz.c"
1515
FTNFILE = "testdata/abc.f"
1616
OBJFILE = "testdata/abc.o"
17+
FOOFILE = "testdata/foo"
18+
DOTFOOFILE = "testdata/.foo"
1719

1820
def setup
1921
Task.clear
@@ -66,7 +68,7 @@ def test_single_dependent
6668
assert_equal [OBJFILE], @runs
6769
end
6870

69-
def test_create_by_string
71+
def test_rule_can_be_created_by_string
7072
create_file(SRCFILE)
7173
rule '.o' => ['.c'] do |t|
7274
@runs << t.name
@@ -75,7 +77,64 @@ def test_create_by_string
7577
assert_equal [OBJFILE], @runs
7678
end
7779

78-
def test_rule_and_no_action_task
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_refering_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
117+
end
118+
Task[OBJFILE].invoke
119+
assert_equal [OBJFILE], @runs
120+
end
121+
end
122+
end
123+
124+
def test_non_extension_rule_name_refers_to_file
125+
verbose(false) do
126+
chdir("testdata") do
127+
create_file("abc.c")
128+
rule "abc" => '.c' do |t|
129+
@runs << t.name
130+
end
131+
Task["abc"].invoke
132+
assert_equal ["abc"], @runs
133+
end
134+
end
135+
end
136+
137+
def test_rule_runs_when_explicit_task_has_no_actions
79138
create_file(SRCFILE)
80139
create_file(SRCFILE2)
81140
delete_file(OBJFILE)
@@ -87,7 +146,7 @@ def test_rule_and_no_action_task
87146
assert_equal [SRCFILE], @runs
88147
end
89148

90-
def test_string_close_matches
149+
def test_close_matches_on_name_do_not_trigger_rule
91150
create_file("testdata/x.c")
92151
rule '.o' => ['.c'] do |t|
93152
@runs << t.name
@@ -96,7 +155,7 @@ def test_string_close_matches
96155
assert_raises(RuntimeError) { Task['testdata/x.xyo'].invoke }
97156
end
98157

99-
def test_precedence_rule_vs_implicit
158+
def test_rule_rebuilds_obj_when_source_is_newer
100159
create_timed_files(OBJFILE, SRCFILE)
101160
rule(/\.o$/ => ['.c']) do
102161
@runs << :RULE
@@ -105,7 +164,7 @@ def test_precedence_rule_vs_implicit
105164
assert_equal [:RULE], @runs
106165
end
107166

108-
def test_rule_with_two_sources
167+
def test_rule_with_two_sources_runs_if_both_sources_are_present
109168
create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
110169
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
111170
@runs << :RULE
@@ -114,7 +173,7 @@ def test_rule_with_two_sources
114173
assert_equal [:RULE], @runs
115174
end
116175

117-
def test_rule_with_two_sources_but_one_missing
176+
def test_rule_with_two_sources_but_one_missing_does_not_run
118177
create_timed_files(OBJFILE, SRCFILE)
119178
delete_file(SRCFILE2)
120179
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
@@ -124,7 +183,7 @@ def test_rule_with_two_sources_but_one_missing
124183
assert_equal [], @runs
125184
end
126185

127-
def test_rule_ordering_finding_second_rule
186+
def test_second_rule_runs_when_first_rule_doesnt
128187
create_timed_files(OBJFILE, SRCFILE)
129188
delete_file(SRCFILE2)
130189
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
@@ -137,7 +196,7 @@ def test_rule_ordering_finding_second_rule
137196
assert_equal [:RULE2], @runs
138197
end
139198

140-
def test_rule_ordering_finding_first_rule
199+
def test_second_rule_doest_run_if_first_triggers
141200
create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
142201
rule OBJFILE => [lambda{SRCFILE}, lambda{SRCFILE2}] do
143202
@runs << :RULE1
@@ -149,7 +208,7 @@ def test_rule_ordering_finding_first_rule
149208
assert_equal [:RULE1], @runs
150209
end
151210

152-
def test_rule_ordering_not_finding_second_rule
211+
def test_second_rule_doest_run_if_first_triggers_with_reversed_rules
153212
create_timed_files(OBJFILE, SRCFILE, SRCFILE2)
154213
rule OBJFILE => [lambda{SRCFILE}] do
155214
@runs << :RULE1
@@ -161,19 +220,19 @@ def test_rule_ordering_not_finding_second_rule
161220
assert_equal [:RULE1], @runs
162221
end
163222

164-
def test_proc_dependent
223+
def test_rule_with_proc_dependent_will_trigger
165224
ran = false
166225
File.makedirs("testdata/src/jw")
167226
create_file("testdata/src/jw/X.java")
168227
rule %r(classes/.*\.class) => [
169-
proc { |fn| fn.sub(/^classes/, 'testdata/src').sub(/\.class$/, '.java') }
228+
proc { |fn| fn.pathmap("%{classes,testdata/src}d/%n.java") }
170229
] do |task|
171230
assert_equal task.name, 'classes/jw/X.class'
172231
assert_equal task.source, 'testdata/src/jw/X.java'
173-
ran = true
232+
@runs << :RULE
174233
end
175234
Task['classes/jw/X.class'].invoke
176-
assert ran, "Should have triggered rule"
235+
assert_equal [:RULE], @runs
177236
ensure
178237
rm_r("testdata/src", :verbose=>false) rescue nil
179238
end
@@ -200,7 +259,7 @@ def test_proc_returning_lists_are_flattened_into_prereqs
200259
rm_r("testdata/flatten", :verbose=>false) rescue nil
201260
end
202261

203-
def test_recursive_rules
262+
def test_recursive_rules_will_work_as_long_as_they_terminate
204263
actions = []
205264
create_file("testdata/abc.xml")
206265
rule '.y' => '.xml' do actions << 'y' end
@@ -211,7 +270,7 @@ def test_recursive_rules
211270
assert_equal ['y', 'c', 'o', 'exe'], actions
212271
end
213272

214-
def test_recursive_overflow
273+
def test_recursive_rules_that_dont_terminate_will_overflow
215274
create_file("testdata/a.a")
216275
prev = 'a'
217276
('b'..'z').each do |letter|
@@ -224,7 +283,7 @@ def test_recursive_overflow
224283
assert_match(/a\.z => testdata\/a.y/, ex.message)
225284
end
226285

227-
def test_bad_dependent
286+
def test_rules_with_bad_dependents_will_fail
228287
rule "a" => [ 1 ] do |t| puts t.name end
229288
assert_raise(RuntimeError) do Task['a'].invoke end
230289
end

0 commit comments

Comments
 (0)