Permalink
Browse files

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
  • Loading branch information...
1 parent 98758e1 commit 0c5500a0f7a0fa73061a9f96fc33be4064b28c45 jimweirich committed Apr 1, 2007
Showing with 97 additions and 20 deletions.
  1. +7 −1 CHANGES
  2. +15 −11 Rakefile
  3. +26 −6 lib/rake.rb
  4. +49 −2 test/test_filelist.rb
View
@@ -1,6 +1,12 @@
= Rake Changelog
-== Pre-Version 0.7.2
+== Pre-version 0.7.3
+
+* Added existing and existing! methods to FileList
+* FileLists now claim to be Arrays (via is_a?) to get better support
+ from the FileUtil module.
+
+== Version 0.7.2
* Error messages are now send to stderr rather than stdout (from
Payton Quackenbush).
View
@@ -67,7 +67,7 @@ Rake::TestTask.new(:test_all) do |t|
'test/fun*.rb'
]
t.warning = true
- t.verbose = true
+ t.verbose = false
end
Rake::TestTask.new(:test_units) do |t|
@@ -88,16 +88,20 @@ Rake::TestTask.new(:test_contribs) do |t|
t.warning = true
end
-require 'rcov/rcovtask'
-
-Rcov::RcovTask.new do |t|
- t.libs << "test"
- t.rcov_opts = ['-xRakefile', '-xrakefile', '-xpublish.rf', '--text-report']
- t.test_files = FileList[
- 'test/test*.rb',
- 'test/contrib/test*.rb'
- ]
- t.verbose = true
+begin
+ require 'rcov/rcovtask'
+
+ Rcov::RcovTask.new do |t|
+ t.libs << "test"
+ t.rcov_opts = ['-xRakefile', '-xrakefile', '-xpublish.rf', '--text-report']
+ t.test_files = FileList[
+ 'test/test*.rb',
+ 'test/contrib/test*.rb'
+ ]
+ t.verbose = true
+ end
+rescue LoadError
+ # No rcov available
end
directory 'testdata'
View
@@ -725,7 +725,8 @@ def sh(*cmd, &block)
options = (Hash === cmd.last) ? cmd.pop : {}
unless block_given?
show_command = cmd.join(" ")
- show_command = show_command[0,42] + "..." if show_command.length > 45
+ show_command = show_command[0,42] + "..."
+ # TODO code application logic heref show_command.length > 45
block = lambda { |ok, status|
ok or fail "Command failed with status (#{status.exitstatus}): [#{show_command}]"
}
@@ -1000,7 +1001,7 @@ class FileList
+ - & |
]
- DELEGATING_METHODS = (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).sort.uniq
+ DELEGATING_METHODS = (ARRAY_METHODS + MUST_DEFINE - MUST_NOT_DEFINE).collect{ |s| s.to_s }.sort.uniq
# Now do the delegation.
DELEGATING_METHODS.each_with_index do |sym, i|
@@ -1030,9 +1031,9 @@ def #{sym}(*args, &block)
# time, use the "yield self" pattern.
#
# Example:
- # file_list = FileList.new['lib/**/*.rb', 'test/test*.rb']
+ # file_list = FileList.new('lib/**/*.rb', 'test/test*.rb')
#
- # pkg_files = FileList.new['lib/**/*'] do |fl|
+ # pkg_files = FileList.new('lib/**/*') do |fl|
# fl.exclude(/\bCVS\b/)
# end
#
@@ -1122,9 +1123,14 @@ def to_a
# Return the internal array object.
def to_ary
- resolve
- @items
+ to_a
end
+
+ # Lie about our class.
+ def is_a?(klass)
+ klass == Array || super(klass)
+ end
+ alias kind_of? is_a?
# Redefine * to return either a string or a new file list.
def *(other)
@@ -1261,6 +1267,20 @@ def egrep(pattern)
end
end
+ # Return a new file list that only contains file names from the current
+ # file list that exist on the file system.
+ def existing
+ select { |fn| File.exists?(fn) }
+ end
+
+ # Modify the current file list so that it contains only file name that
+ # exist on the file system.
+ def existing!
+ resolve
+ @items = @items.select { |fn| File.exists?(fn) }
+ self
+ end
+
# FileList version of partition. Needed because the nested arrays
# should be FileLists in this version.
def partition(&block) # :nodoc:
View
@@ -153,13 +153,13 @@ def test_excluding_via_block
def test_exclude_return_on_create
fl = FileList['testdata/*'].exclude(/.*\.[hcx]$/)
- assert_equal ['testdata/existing'], fl
+ assert_equal ['testdata/existing', 'testdata/cfiles'].sort, fl.sort
assert_equal FileList, fl.class
end
def test_exclude_with_string_return_on_create
fl = FileList['testdata/*'].exclude('testdata/abc.c')
- assert_equal %w(testdata/existing testdata/x.c testdata/abc.h testdata/abc.x testdata/xyz.c).sort, fl.sort
+ assert_equal %w(testdata/existing testdata/cfiles testdata/x.c testdata/abc.h testdata/abc.x testdata/xyz.c).sort, fl.sort
assert_equal FileList, fl.class
end
@@ -215,6 +215,28 @@ def test_sub
assert_equal ["testdata/abc.o", "testdata/x.o", "testdata/xyz.o"].sort,
f3.sort
end
+
+ def test_claim_to_be_a_kind_of_array
+ fl = FileList['testdata/*.c']
+ assert fl.is_a?(Array)
+ assert fl.kind_of?(Array)
+ end
+
+ def test_claim_to_be_a_kind_of_filelist
+ fl = FileList['testdata/*.c']
+ assert fl.is_a?(FileList)
+ assert fl.kind_of?(FileList)
+ end
+
+ def test_claim_to_be_a_filelist_instance
+ fl = FileList['testdata/*.c']
+ assert fl.instance_of?(FileList)
+ end
+
+ def test_dont_claim_to_be_an_array_instance
+ fl = FileList['testdata/*.c']
+ assert ! fl.instance_of?(Array)
+ end
def test_sub!
f = "x/a.c"
@@ -300,6 +322,18 @@ def test_egrep_with_block
assert found, "should have found a matching line"
end
+ def test_existing
+ fl = FileList['testdata/abc.c', 'testdata/notthere.c']
+ assert_equal ["testdata/abc.c"], fl.existing
+ assert fl.existing.is_a?(FileList)
+ end
+
+ def test_existing!
+ fl = FileList['testdata/abc.c', 'testdata/notthere.c']
+ result = fl.existing!
+ assert_equal ["testdata/abc.c"], fl
+ assert_equal fl.object_id, result.object_id
+ end
def test_ignore_special
f = FileList['testdata/*']
@@ -505,12 +539,25 @@ def test_other_array_returning_methods
assert_equal ['b', 'd'], r
assert_equal FileList, r.class
end
+
+ def test_file_utils_can_use_filelists
+ cfiles = FileList['testdata/*.c']
+
+ cp cfiles, @cdir
+
+ assert File.exist?(File.join(@cdir, 'abc.c'))
+ assert File.exist?(File.join(@cdir, 'xyz.c'))
+ assert File.exist?(File.join(@cdir, 'x.c'))
+ end
def create_test_data
verbose(false) do
+
mkdir "testdata" unless File.exist? "testdata"
mkdir "testdata/CVS" rescue nil
mkdir "testdata/.svn" rescue nil
+ @cdir = "testdata/cfiles"
+ mkdir @cdir rescue nil
touch "testdata/.dummy"
touch "testdata/x.bak"
touch "testdata/x~"

0 comments on commit 0c5500a

Please sign in to comment.