2003-10-08 09:21:28 -04:00
|
|
|
require 'test/unit/testsuite'
|
|
|
|
require 'test/unit/collector'
|
|
|
|
|
|
|
|
module Test
|
|
|
|
module Unit
|
|
|
|
module Collector
|
|
|
|
class Dir
|
|
|
|
include Collector
|
|
|
|
|
2004-12-18 21:01:59 -05:00
|
|
|
attr_reader :pattern, :exclude
|
2006-10-01 12:25:37 -04:00
|
|
|
attr_accessor :base
|
2003-10-08 09:21:28 -04:00
|
|
|
|
|
|
|
def initialize(dir=::Dir, file=::File, object_space=::ObjectSpace, req=nil)
|
|
|
|
super()
|
|
|
|
@dir = dir
|
|
|
|
@file = file
|
|
|
|
@object_space = object_space
|
|
|
|
@req = req
|
2004-12-18 21:01:59 -05:00
|
|
|
@pattern = [/\btest_.*\.rb\Z/m]
|
|
|
|
@exclude = []
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def collect(*from)
|
2006-10-12 13:31:15 -04:00
|
|
|
basedir = @base
|
2006-10-14 12:04:16 -04:00
|
|
|
$:.push(basedir) if basedir
|
2003-10-08 09:21:28 -04:00
|
|
|
if(from.empty?)
|
|
|
|
recursive_collect('.', find_test_cases)
|
|
|
|
elsif(from.size == 1)
|
|
|
|
recursive_collect(from.first, find_test_cases)
|
|
|
|
else
|
|
|
|
suites = []
|
|
|
|
from.each do |f|
|
|
|
|
suite = recursive_collect(f, find_test_cases)
|
|
|
|
suites << suite unless(suite.tests.empty?)
|
|
|
|
end
|
|
|
|
suite = TestSuite.new("[#{from.join(', ')}]")
|
|
|
|
sort(suites).each{|s| suite << s}
|
|
|
|
suite
|
|
|
|
end
|
2006-10-12 13:31:15 -04:00
|
|
|
ensure
|
|
|
|
$:.delete_at($:.rindex(basedir)) if basedir
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_test_cases(ignore=[])
|
|
|
|
cases = []
|
|
|
|
@object_space.each_object(Class) do |c|
|
|
|
|
cases << c if(c < TestCase && !ignore.include?(c))
|
|
|
|
end
|
|
|
|
ignore.concat(cases)
|
|
|
|
cases
|
|
|
|
end
|
|
|
|
|
|
|
|
def recursive_collect(name, already_gathered)
|
|
|
|
sub_suites = []
|
2006-10-01 12:25:37 -04:00
|
|
|
path = realdir(name)
|
|
|
|
if @file.directory?(path)
|
2006-10-14 12:04:16 -04:00
|
|
|
dir_name = name unless name == '.'
|
2006-10-01 12:25:37 -04:00
|
|
|
@dir.entries(path).each do |e|
|
2003-10-08 09:21:28 -04:00
|
|
|
next if(e == '.' || e == '..')
|
2006-10-14 12:04:16 -04:00
|
|
|
e_name = dir_name ? @file.join(dir_name, e) : e
|
2006-10-01 12:25:37 -04:00
|
|
|
if @file.directory?(realdir(e_name))
|
2004-12-18 21:01:59 -05:00
|
|
|
next if /\ACVS\z/ =~ e
|
2003-10-08 09:21:28 -04:00
|
|
|
sub_suite = recursive_collect(e_name, already_gathered)
|
|
|
|
sub_suites << sub_suite unless(sub_suite.empty?)
|
|
|
|
else
|
2004-12-18 21:01:59 -05:00
|
|
|
next if /~\z/ =~ e_name or /\A\.\#/ =~ e
|
|
|
|
if @pattern and !@pattern.empty?
|
|
|
|
next unless @pattern.any? {|pat| pat =~ e_name}
|
|
|
|
end
|
|
|
|
if @exclude and !@exclude.empty?
|
|
|
|
next if @exclude.any? {|pat| pat =~ e_name}
|
|
|
|
end
|
2003-10-08 09:21:28 -04:00
|
|
|
collect_file(e_name, sub_suites, already_gathered)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
collect_file(name, sub_suites, already_gathered)
|
|
|
|
end
|
|
|
|
suite = TestSuite.new(@file.basename(name))
|
|
|
|
sort(sub_suites).each{|s| suite << s}
|
|
|
|
suite
|
|
|
|
end
|
|
|
|
|
|
|
|
def collect_file(name, suites, already_gathered)
|
2006-10-12 13:31:15 -04:00
|
|
|
dir = @file.dirname(@file.expand_path(name, @base))
|
2005-02-16 23:50:49 -05:00
|
|
|
$:.unshift(dir)
|
2003-10-08 09:21:28 -04:00
|
|
|
if(@req)
|
|
|
|
@req.require(name)
|
|
|
|
else
|
|
|
|
require(name)
|
|
|
|
end
|
|
|
|
find_test_cases(already_gathered).each{|t| add_suite(suites, t.suite)}
|
2003-10-21 02:57:14 -04:00
|
|
|
ensure
|
2006-10-16 10:35:51 -04:00
|
|
|
$:.delete_at($:.index(dir)) if dir
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
2006-10-01 12:25:37 -04:00
|
|
|
|
|
|
|
def realdir(path)
|
|
|
|
if @base
|
|
|
|
@file.join(@base, path)
|
|
|
|
else
|
|
|
|
path
|
|
|
|
end
|
|
|
|
end
|
2003-10-08 09:21:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|