Skip to content

Commit

Permalink
Added existing and existing! methods to FileList.
Browse files Browse the repository at this point in the history
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
jimweirich committed Apr 1, 2007
1 parent 98758e1 commit 0c5500a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 20 deletions.
8 changes: 7 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
26 changes: 15 additions & 11 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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'
Expand Down
32 changes: 26 additions & 6 deletions lib/rake.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}]"
}
Expand Down Expand Up @@ -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|
Expand Down Expand Up @@ -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
#
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
51 changes: 49 additions & 2 deletions test/test_filelist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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/*']
Expand Down Expand Up @@ -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~"
Expand Down

0 comments on commit 0c5500a

Please sign in to comment.