5
require_relative 'capture_stdout'
7
class Rake::TestFileList < Test::Unit::TestCase
8
FileList = Rake::FileList
19
# FileList.select_default_ignore_patterns
20
FileUtils.rm_rf("testdata")
22
FileUtils.rm_rf(@tmpwd)
25
def test_delgating_methods_do_not_include_to_a_or_to_ary
26
assert ! FileList::DELEGATING_METHODS.include?("to_a"), "should not include to_a"
27
assert ! FileList::DELEGATING_METHODS.include?(:to_a), "should not include to_a"
28
assert ! FileList::DELEGATING_METHODS.include?("to_ary"), "should not include to_ary"
29
assert ! FileList::DELEGATING_METHODS.include?(:to_ary), "should not include to_ary"
34
assert_equal 0, fl.size
37
def test_create_with_args
38
fl = FileList.new("testdata/*.c", "x")
39
assert_equal ["testdata/abc.c", "testdata/x.c", "testdata/xyz.c", "x"].sort,
43
def test_create_with_block
44
fl = FileList.new { |f| f.include("x") }
45
assert_equal ["x"], fl.resolve
48
def test_create_with_brackets
49
fl = FileList["testdata/*.c", "x"]
50
assert_equal ["testdata/abc.c", "testdata/x.c", "testdata/xyz.c", "x"].sort,
54
def test_create_with_brackets_and_filelist
55
fl = FileList[FileList["testdata/*.c", "x"]]
56
assert_equal ["testdata/abc.c", "testdata/x.c", "testdata/xyz.c", "x"].sort,
60
def test_include_with_another_array
61
fl = FileList.new.include(["x", "y", "z"])
62
assert_equal ["x", "y", "z"].sort, fl.sort
65
def test_include_with_another_filelist
66
fl = FileList.new.include(FileList["testdata/*.c", "x"])
67
assert_equal ["testdata/abc.c", "testdata/x.c", "testdata/xyz.c", "x"].sort,
73
fl << "a.rb" << "b.rb"
74
assert_equal ['a.rb', 'b.rb'], fl
81
assert_equal ['a', 'd', 'c', 'x', 'y'], fl
82
assert_equal ['a', 'd', 'c', 'x', 'y'], fl.resolve
88
assert_equal f.object_id, g.object_id
90
assert_equal f.object_id, h.object_id
95
fl.include(File.expand_path('../test*.rb', __FILE__))
96
assert fl.include?(__FILE__)
98
fl.each { |fn| assert_match(/\.rb$/, fn) }
101
def test_add_matching
104
fl.include(File.dirname(__FILE__)+"/*.rb")
105
assert_equal "a.java", fl[0]
107
assert fl.include?(__FILE__)
110
def test_multiple_patterns
113
fl.include('*.c', '*xist*')
115
fl.include('testdata/*.c', 'testdata/*xist*')
117
'testdata/x.c', 'testdata/xyz.c', 'testdata/abc.c', 'testdata/existing'
121
def test_square_bracket_pattern
123
fl.include("testdata/abc.[ch]")
125
assert fl.include?("testdata/abc.c")
126
assert fl.include?("testdata/abc.h")
129
def test_curly_bracket_pattern
131
fl.include("testdata/abc.{c,h}")
133
assert fl.include?("testdata/abc.c")
134
assert fl.include?("testdata/abc.h")
139
fl.include %w(testdata/x.c testdata/abc.c testdata/xyz.c testdata/existing)
140
fl.reject! { |fn| fn =~ %r{/x} }
142
'testdata/abc.c', 'testdata/existing'
147
fl = FileList['testdata/x.c', 'testdata/abc.c', 'testdata/xyz.c', 'testdata/existing']
148
fl.each { |fn| touch fn, :verbose => false }
149
x = fl.exclude(%r{/x.+\.})
150
assert_equal FileList, x.class
151
assert_equal %w(testdata/x.c testdata/abc.c testdata/existing), fl
152
assert_equal fl.object_id, x.object_id
153
fl.exclude('testdata/*.c')
154
assert_equal ['testdata/existing'], fl
155
fl.exclude('testdata/existing')
159
def test_excluding_via_block
160
fl = FileList['testdata/a.c', 'testdata/b.c', 'testdata/xyz.c']
161
fl.exclude { |fn| fn.pathmap('%n') == 'xyz' }
162
assert fl.exclude?("xyz.c"), "Should exclude xyz.c"
163
assert_equal ['testdata/a.c', 'testdata/b.c'], fl
166
def test_exclude_return_on_create
167
fl = FileList['testdata/*'].exclude(/.*\.[hcx]$/)
168
assert_equal ['testdata/existing', 'testdata/cfiles'].sort, fl.sort
169
assert_equal FileList, fl.class
172
def test_exclude_with_string_return_on_create
173
fl = FileList['testdata/*'].exclude('testdata/abc.c')
174
assert_equal %w(testdata/existing testdata/cfiles testdata/x.c testdata/abc.h testdata/abc.x testdata/xyz.c).sort, fl.sort
175
assert_equal FileList, fl.class
178
def test_default_exclude
181
fl.include("**/*~", "**/*.bak", "**/core")
182
assert fl.member?("testdata/core"), "Should include core"
183
assert fl.member?("testdata/x.bak"), "Should include .bak files"
188
fl << "x.c" << "a.c" << "b.rb" << "a.c"
189
assert_equal ['x.c', 'a.c', 'b.rb', 'a.c'], fl
191
assert_equal ['x.c', 'a.c', 'b.rb'], fl
196
fl << "a.java" << "b.java"
197
assert_equal "a.java b.java", fl.to_s
198
assert_equal "a.java b.java", "#{fl}"
202
fl = FileList['a.java', 'b.java']
203
assert_equal ['a.java', 'b.java'], fl.to_a
204
assert_equal Array, fl.to_a.class
205
assert_equal ['a.java', 'b.java'], fl.to_ary
206
assert_equal Array, fl.to_ary.class
209
def test_to_s_pending
210
fl = FileList['testdata/abc.*']
212
assert_match(%r{testdata/abc\.c}, result)
213
assert_match(%r{testdata/abc\.h}, result)
214
assert_match(%r{testdata/abc\.x}, result)
215
assert_match(%r{(testdata/abc\..\b ?){2}}, result)
218
def test_inspect_pending
219
fl = FileList['testdata/abc.*']
221
assert_match(%r{"testdata/abc\.c"}, result)
222
assert_match(%r{"testdata/abc\.h"}, result)
223
assert_match(%r{"testdata/abc\.x"}, result)
224
assert_match(%r|^\[("testdata/abc\..", ){2}"testdata/abc\.."\]$|, result)
228
fl = FileList["testdata/*.c"]
229
f2 = fl.sub(/\.c$/, ".o")
230
assert_equal FileList, f2.class
231
assert_equal ["testdata/abc.o", "testdata/x.o", "testdata/xyz.o"].sort,
233
f3 = fl.gsub(/\.c$/, ".o")
234
assert_equal FileList, f3.class
235
assert_equal ["testdata/abc.o", "testdata/x.o", "testdata/xyz.o"].sort,
239
def test_claim_to_be_a_kind_of_array
240
fl = FileList['testdata/*.c']
241
assert fl.is_a?(Array)
242
assert fl.kind_of?(Array)
245
def test_claim_to_be_a_kind_of_filelist
246
fl = FileList['testdata/*.c']
247
assert fl.is_a?(FileList)
248
assert fl.kind_of?(FileList)
251
def test_claim_to_be_a_filelist_instance
252
fl = FileList['testdata/*.c']
253
assert fl.instance_of?(FileList)
256
def test_dont_claim_to_be_an_array_instance
257
fl = FileList['testdata/*.c']
258
assert ! fl.instance_of?(Array)
263
fl = FileList[f, "x/b.c"]
264
res = fl.sub!(/\.c$/, ".o")
265
assert_equal ["x/a.o", "x/b.o"].sort, fl.sort
266
assert_equal "x/a.c", f
267
assert_equal fl.object_id, res.object_id
270
def test_sub_with_block
271
fl = FileList["src/org/onestepback/a.java", "src/org/onestepback/b.java"]
272
# The block version doesn't work the way I want it to ...
273
# f2 = fl.sub(%r{^src/(.*)\.java$}) { |x| "classes/" + $1 + ".class" }
274
f2 = fl.sub(%r{^src/(.*)\.java$}, "classes/\\1.class")
276
"classes/org/onestepback/a.class",
277
"classes/org/onestepback/b.class"
283
assert_equal "one.net", "one.two".ext("net")
284
assert_equal "one.net", "one.two".ext(".net")
285
assert_equal "one.net", "one".ext("net")
286
assert_equal "one.net", "one".ext(".net")
287
assert_equal "one.two.net", "one.two.c".ext(".net")
288
assert_equal "one/two.net", "one/two.c".ext(".net")
289
assert_equal "one.x/two.net", "one.x/two.c".ext(".net")
290
assert_equal "one.x/two.net", "one.x/two".ext(".net")
291
assert_equal ".onerc.net", ".onerc.dot".ext("net")
292
assert_equal ".onerc.net", ".onerc".ext("net")
293
assert_equal ".a/.onerc.net", ".a/.onerc".ext("net")
294
assert_equal "one", "one.two".ext('')
295
assert_equal "one", "one.two".ext
296
assert_equal ".one", ".one.two".ext
297
assert_equal ".one", ".one".ext
298
assert_equal ".", ".".ext("c")
299
assert_equal "..", "..".ext("c")
300
# These only need to work in windows
301
if Rake::Win32.windows?
302
assert_equal "one.x\\two.net", "one.x\\two.c".ext(".net")
303
assert_equal "one.x\\two.net", "one.x\\two".ext(".net")
307
def test_filelist_ext
308
assert_equal FileList['one.c', '.one.c'],
309
FileList['one.net', '.one'].ext('c')
314
fl = FileList["testdata/*.c"]
315
f2 = fl.gsub(/a/, "A")
316
assert_equal ["testdAtA/Abc.c", "testdAtA/x.c", "testdAtA/xyz.c"].sort,
322
f = FileList["testdata/*.c"]
324
assert_equal ["testdAtA/Abc.c", "testdAtA/x.c", "testdAtA/xyz.c"].sort,
328
def test_egrep_with_output
329
files = FileList[File.expand_path('../test*.rb', __FILE__)]
330
the_line_number = __LINE__ + 1
331
out = capture_stdout do files.egrep(/PUGH/) end
332
assert_match(/:#{the_line_number}:/, out)
335
def test_egrep_with_block
336
files = FileList[File.expand_path('../test*.rb', __FILE__)]
338
the_line_number = __LINE__ + 1
339
files.egrep(/XYZZY/) do |fn, ln, line |
340
assert_equal __FILE__, fn
341
assert_equal the_line_number, ln
342
assert_match(/files\.egrep/, line)
345
assert found, "should have found a matching line"
349
fl = FileList['testdata/abc.c', 'testdata/notthere.c']
350
assert_equal ["testdata/abc.c"], fl.existing
351
assert fl.existing.is_a?(FileList)
355
fl = FileList['testdata/abc.c', 'testdata/notthere.c']
356
result = fl.existing!
357
assert_equal ["testdata/abc.c"], fl
358
assert_equal fl.object_id, result.object_id
361
def test_ignore_special
362
f = FileList['testdata/*']
363
assert ! f.include?("testdata/CVS"), "Should not contain CVS"
364
assert ! f.include?("testdata/.svn"), "Should not contain .svn"
365
assert ! f.include?("testdata/.dummy"), "Should not contain dot files"
366
assert ! f.include?("testdata/x.bak"), "Should not contain .bak files"
367
assert ! f.include?("testdata/x~"), "Should not contain ~ files"
368
assert ! f.include?("testdata/core"), "Should not contain core files"
371
def test_clear_ignore_patterns
372
f = FileList['testdata/*', 'testdata/.svn']
374
assert f.include?("testdata/abc.c")
375
assert f.include?("testdata/xyz.c")
376
assert f.include?("testdata/CVS")
377
assert f.include?("testdata/.svn")
378
assert f.include?("testdata/x.bak")
379
assert f.include?("testdata/x~")
382
def test_exclude_with_alternate_file_seps
384
assert fl.exclude?("x/CVS/y")
385
assert fl.exclude?("x\\CVS\\y")
386
assert fl.exclude?("x/.svn/y")
387
assert fl.exclude?("x\\.svn\\y")
388
assert fl.exclude?("x/core")
389
assert fl.exclude?("x\\core")
392
def test_add_default_exclude_list
395
assert fl.exclude?("x/CVS/y")
396
assert fl.exclude?("x\\CVS\\y")
397
assert fl.exclude?("x/.svn/y")
398
assert fl.exclude?("x\\.svn\\y")
399
assert fl.exclude?("x/core")
400
assert fl.exclude?("x\\core")
401
assert fl.exclude?("x/abc~1")
404
def test_basic_array_functions
405
f = FileList['b', 'c', 'a']
406
assert_equal 'b', f.first
407
assert_equal 'b', f[0]
408
assert_equal 'a', f.last
409
assert_equal 'a', f[2]
410
assert_equal 'a', f[-1]
411
assert_equal ['a', 'b', 'c'], f.sort
413
assert_equal ['a', 'b', 'c'], f
417
assert_equal ['a', 'testdata/x.c', 'testdata/xyz.c', 'testdata/abc.c'].sort,
418
['a', FileList['testdata/*.c']].flatten.sort
421
def test_clone_and_dup
422
a = FileList['a', 'b', 'c']
426
assert_equal ['a', 'b', 'c', 'd'], a
427
assert_equal ['a', 'b', 'c'], c
428
assert_equal ['a', 'b', 'c'], d
431
def test_dup_and_clone_replicate_taint
432
a = FileList['a', 'b', 'c']
436
assert c.tainted?, "Clone should be tainted"
437
assert d.tainted?, "Dup should be tainted"
440
def test_duped_items_will_thaw
441
a = FileList['a', 'b', 'c']
445
assert_equal ['a', 'b', 'c', 'more'], d
448
def test_cloned_items_stay_frozen
449
a = FileList['a', 'b', 'c']
452
assert_raise(TypeError, RuntimeError) do
457
def test_array_comparisons
458
fl = FileList['b', 'b']
462
assert_equal( 1, fl <=> a )
463
assert_equal( 0, fl <=> b )
464
assert_equal( -1, fl <=> c )
465
assert_equal( -1, a <=> fl )
466
assert_equal( 0, b <=> fl )
467
assert_equal( 1, c <=> fl )
470
def test_array_equality
471
a = FileList['a', 'b']
481
def test_enumeration_methods
482
a = FileList['a', 'b']
483
b = a.collect { |it| it.upcase }
484
assert_equal ['A', 'B'], b
485
assert_equal FileList, b.class
487
b = a.map { |it| it.upcase }
488
assert_equal ['A', 'B'], b
489
assert_equal FileList, b.class
492
assert_equal ['a', 'b'], b
493
assert_equal FileList, b.class
495
b = a.sort_by { |it| it }
496
assert_equal ['a', 'b'], b
497
assert_equal FileList, b.class
499
b = a.find_all { |it| it == 'b'}
500
assert_equal ['b'], b
501
assert_equal FileList, b.class
503
b = a.select { |it| it.size == 1 }
504
assert_equal ['a', 'b'], b
505
assert_equal FileList, b.class
507
b = a.reject { |it| it == 'b' }
508
assert_equal ['a'], b
509
assert_equal FileList, b.class
512
assert_equal ['a', 'b'], b
513
assert_equal FileList, b.class
515
b = a.partition { |it| it == 'b' }
516
assert_equal [['b'], ['a']], b
517
assert_equal Array, b.class
518
assert_equal FileList, b[0].class
519
assert_equal FileList, b[1].class
521
b = a.zip(['x', 'y']).to_a
522
assert_equal [['a', 'x'], ['b', 'y']], b
523
assert_equal Array, b.class
524
assert_equal Array, b[0].class
525
assert_equal Array, b[1].class
528
def test_array_operators
531
f = FileList['x', 'y']
532
g = FileList['w', 'z']
535
assert_equal ['x', 'y', 'w', 'z'], r
536
assert_equal FileList, r.class
539
assert_equal ['a', 'b', 'w', 'z'], r
540
assert_equal Array, r.class
543
assert_equal ['x', 'y', 'c', 'd'], r
544
assert_equal FileList, r.class
546
r = FileList['w', 'x', 'y', 'z'] - f
547
assert_equal ['w', 'z'], r
548
assert_equal FileList, r.class
550
r = FileList['w', 'x', 'y', 'z'] & f
551
assert_equal ['x', 'y'], r
552
assert_equal FileList, r.class
555
assert_equal ['x', 'y', 'x', 'y'], r
556
assert_equal FileList, r.class
559
assert_equal 'x,y', r
560
assert_equal String, r.class
563
assert_equal ['a', 'x', 'y'].sort, r.sort
564
assert_equal FileList, r.class
567
def test_other_array_returning_methods
568
f = FileList['a', nil, 'b']
570
assert_equal ['a', 'b'], r
571
assert_equal FileList, r.class
573
f = FileList['a', 'b']
574
r = f.concat(['x', 'y'])
575
assert_equal ['a', 'b', 'x', 'y'], r
576
assert_equal FileList, r.class
578
f = FileList['a', ['b', 'c'], FileList['d', 'e']]
580
assert_equal ['a', 'b', 'c', 'd', 'e'], r
581
assert_equal FileList, r.class
583
f = FileList['a', 'b', 'a']
585
assert_equal ['a', 'b'], r
586
assert_equal FileList, r.class
588
f = FileList['a', 'b', 'c', 'd']
590
assert_equal ['b', 'd'], r
591
assert_equal FileList, r.class
594
def test_file_utils_can_use_filelists
595
cfiles = FileList['testdata/*.c']
597
cp cfiles, @cdir, :verbose => false
599
assert File.exist?(File.join(@cdir, 'abc.c'))
600
assert File.exist?(File.join(@cdir, 'xyz.c'))
601
assert File.exist?(File.join(@cdir, 'x.c'))
607
mkdir "testdata" unless File.exist? "testdata"
608
mkdir "testdata/CVS" rescue nil
609
mkdir "testdata/.svn" rescue nil
610
@cdir = "testdata/cfiles"
611
mkdir @cdir rescue nil
612
touch "testdata/.dummy"
613
touch "testdata/x.bak"
615
touch "testdata/core"
617
touch "testdata/xyz.c"
618
touch "testdata/abc.c"
619
touch "testdata/abc.h"
620
touch "testdata/abc.x"
621
touch "testdata/existing"