Skip to content

Commit 0c5500a

Browse files
author
jimweirich
committed
Added existing and existing! methods to FileList.
FileLists now claim (via is_a?) to be Arrays. git-svn-id: svn+ssh://rubyforge.org/var/svn/rake/trunk@582 5af023f1-ac1a-0410-98d6-829a145c37ef
1 parent 98758e1 commit 0c5500a

File tree

4 files changed

+97
-20
lines changed

4 files changed

+97
-20
lines changed

CHANGES

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
= Rake Changelog
22

3-
== Pre-Version 0.7.2
3+
== Pre-version 0.7.3
4+
5+
* Added existing and existing! methods to FileList
6+
* FileLists now claim to be Arrays (via is_a?) to get better support
7+
from the FileUtil module.
8+
9+
== Version 0.7.2
410

511
* Error messages are now send to stderr rather than stdout (from
612
Payton Quackenbush).

Rakefile

+15-11
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Rake::TestTask.new(:test_all) do |t|
6767
'test/fun*.rb'
6868
]
6969
t.warning = true
70-
t.verbose = true
70+
t.verbose = false
7171
end
7272

7373
Rake::TestTask.new(:test_units) do |t|
@@ -88,16 +88,20 @@ Rake::TestTask.new(:test_contribs) do |t|
8888
t.warning = true
8989
end
9090

91-
require 'rcov/rcovtask'
92-
93-
Rcov::RcovTask.new do |t|
94-
t.libs << "test"
95-
t.rcov_opts = ['-xRakefile', '-xrakefile', '-xpublish.rf', '--text-report']
96-
t.test_files = FileList[
97-
'test/test*.rb',
98-
'test/contrib/test*.rb'
99-
]
100-
t.verbose = true
91+
begin
92+
require 'rcov/rcovtask'
93+
94+
Rcov::RcovTask.new do |t|
95+
t.libs << "test"
96+
t.rcov_opts = ['-xRakefile', '-xrakefile', '-xpublish.rf', '--text-report']
97+
t.test_files = FileList[
98+
'test/test*.rb',
99+
'test/contrib/test*.rb'
100+
]
101+
t.verbose = true
102+
end
103+
rescue LoadError
104+
# No rcov available
101105
end
102106

103107
directory 'testdata'

lib/rake.rb

+26-6
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ def sh(*cmd, &block)
725725
options = (Hash === cmd.last) ? cmd.pop : {}
726726
unless block_given?
727727
show_command = cmd.join(" ")
728-
show_command = show_command[0,42] + "..." if show_command.length > 45
728+
show_command = show_command[0,42] + "..."
729+
# TODO code application logic heref show_command.length > 45
729730
block = lambda { |ok, status|
730731
ok or fail "Command failed with status (#{status.exitstatus}): [#{show_command}]"
731732
}
@@ -1000,7 +1001,7 @@ class FileList
10001001
+ - & |
10011002
]
10021003

1003-
DELEGATING_METHODS = (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).sort.uniq
1004+
DELEGATING_METHODS = (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).collect{ |s| s.to_s }.sort.uniq
10041005

10051006
# Now do the delegation.
10061007
DELEGATING_METHODS.each_with_index do |sym, i|
@@ -1030,9 +1031,9 @@ def #{sym}(*args, &block)
10301031
# time, use the "yield self" pattern.
10311032
#
10321033
# Example:
1033-
# file_list = FileList.new['lib/**/*.rb', 'test/test*.rb']
1034+
# file_list = FileList.new('lib/**/*.rb', 'test/test*.rb')
10341035
#
1035-
# pkg_files = FileList.new['lib/**/*'] do |fl|
1036+
# pkg_files = FileList.new('lib/**/*') do |fl|
10361037
# fl.exclude(/\bCVS\b/)
10371038
# end
10381039
#
@@ -1122,9 +1123,14 @@ def to_a
11221123

11231124
# Return the internal array object.
11241125
def to_ary
1125-
resolve
1126-
@items
1126+
to_a
11271127
end
1128+
1129+
# Lie about our class.
1130+
def is_a?(klass)
1131+
klass == Array || super(klass)
1132+
end
1133+
alias kind_of? is_a?
11281134

11291135
# Redefine * to return either a string or a new file list.
11301136
def *(other)
@@ -1261,6 +1267,20 @@ def egrep(pattern)
12611267
end
12621268
end
12631269

1270+
# Return a new file list that only contains file names from the current
1271+
# file list that exist on the file system.
1272+
def existing
1273+
select { |fn| File.exists?(fn) }
1274+
end
1275+
1276+
# Modify the current file list so that it contains only file name that
1277+
# exist on the file system.
1278+
def existing!
1279+
resolve
1280+
@items = @items.select { |fn| File.exists?(fn) }
1281+
self
1282+
end
1283+
12641284
# FileList version of partition. Needed because the nested arrays
12651285
# should be FileLists in this version.
12661286
def partition(&block) # :nodoc:

test/test_filelist.rb

+49-2
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ def test_excluding_via_block
153153

154154
def test_exclude_return_on_create
155155
fl = FileList['testdata/*'].exclude(/.*\.[hcx]$/)
156-
assert_equal ['testdata/existing'], fl
156+
assert_equal ['testdata/existing', 'testdata/cfiles'].sort, fl.sort
157157
assert_equal FileList, fl.class
158158
end
159159

160160
def test_exclude_with_string_return_on_create
161161
fl = FileList['testdata/*'].exclude('testdata/abc.c')
162-
assert_equal %w(testdata/existing testdata/x.c testdata/abc.h testdata/abc.x testdata/xyz.c).sort, fl.sort
162+
assert_equal %w(testdata/existing testdata/cfiles testdata/x.c testdata/abc.h testdata/abc.x testdata/xyz.c).sort, fl.sort
163163
assert_equal FileList, fl.class
164164
end
165165

@@ -215,6 +215,28 @@ def test_sub
215215
assert_equal ["testdata/abc.o", "testdata/x.o", "testdata/xyz.o"].sort,
216216
f3.sort
217217
end
218+
219+
def test_claim_to_be_a_kind_of_array
220+
fl = FileList['testdata/*.c']
221+
assert fl.is_a?(Array)
222+
assert fl.kind_of?(Array)
223+
end
224+
225+
def test_claim_to_be_a_kind_of_filelist
226+
fl = FileList['testdata/*.c']
227+
assert fl.is_a?(FileList)
228+
assert fl.kind_of?(FileList)
229+
end
230+
231+
def test_claim_to_be_a_filelist_instance
232+
fl = FileList['testdata/*.c']
233+
assert fl.instance_of?(FileList)
234+
end
235+
236+
def test_dont_claim_to_be_an_array_instance
237+
fl = FileList['testdata/*.c']
238+
assert ! fl.instance_of?(Array)
239+
end
218240

219241
def test_sub!
220242
f = "x/a.c"
@@ -300,6 +322,18 @@ def test_egrep_with_block
300322
assert found, "should have found a matching line"
301323
end
302324

325+
def test_existing
326+
fl = FileList['testdata/abc.c', 'testdata/notthere.c']
327+
assert_equal ["testdata/abc.c"], fl.existing
328+
assert fl.existing.is_a?(FileList)
329+
end
330+
331+
def test_existing!
332+
fl = FileList['testdata/abc.c', 'testdata/notthere.c']
333+
result = fl.existing!
334+
assert_equal ["testdata/abc.c"], fl
335+
assert_equal fl.object_id, result.object_id
336+
end
303337

304338
def test_ignore_special
305339
f = FileList['testdata/*']
@@ -505,12 +539,25 @@ def test_other_array_returning_methods
505539
assert_equal ['b', 'd'], r
506540
assert_equal FileList, r.class
507541
end
542+
543+
def test_file_utils_can_use_filelists
544+
cfiles = FileList['testdata/*.c']
545+
546+
cp cfiles, @cdir
547+
548+
assert File.exist?(File.join(@cdir, 'abc.c'))
549+
assert File.exist?(File.join(@cdir, 'xyz.c'))
550+
assert File.exist?(File.join(@cdir, 'x.c'))
551+
end
508552

509553
def create_test_data
510554
verbose(false) do
555+
511556
mkdir "testdata" unless File.exist? "testdata"
512557
mkdir "testdata/CVS" rescue nil
513558
mkdir "testdata/.svn" rescue nil
559+
@cdir = "testdata/cfiles"
560+
mkdir @cdir rescue nil
514561
touch "testdata/.dummy"
515562
touch "testdata/x.bak"
516563
touch "testdata/x~"

0 commit comments

Comments
 (0)