Class: Test::Unit::Collector::Dir

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Collector
Defined in:
lib/test/unit/collector/dir.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Test::Unit::Collector

#add_suite, #add_test_cases, #filter=, #include?, #sort

Constructor Details

#initialize(dir = ::Dir, file = ::File, object_space = ::ObjectSpace, req = nil) ⇒ Dir

Returns a new instance of Dir



13
14
15
16
17
18
19
20
21
22
# File 'lib/test/unit/collector/dir.rb', line 13

def initialize(dir=::Dir, file=::File, object_space=::ObjectSpace, req=nil)
  super()
  @dir = dir
  @file = file
  @object_space = object_space
  @req = req
  @pattern = [/\btest_.*\.rb\Z/m]
  @exclude = []
  @base = nil
end

Instance Attribute Details

#baseObject

Returns the value of attribute base



11
12
13
# File 'lib/test/unit/collector/dir.rb', line 11

def base
  @base
end

#excludeObject (readonly)

Returns the value of attribute exclude



10
11
12
# File 'lib/test/unit/collector/dir.rb', line 10

def exclude
  @exclude
end

#patternObject (readonly)

Returns the value of attribute pattern



10
11
12
# File 'lib/test/unit/collector/dir.rb', line 10

def pattern
  @pattern
end

Instance Method Details

#collect(*from) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/test/unit/collector/dir.rb', line 24

def collect(*from)
  basedir = @base
  $:.push(basedir) if basedir
  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
ensure
  $:.delete_at($:.rindex(basedir)) if basedir
end

#collect_file(name, suites, already_gathered) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/test/unit/collector/dir.rb', line 85

def collect_file(name, suites, already_gathered)
  dir = @file.dirname(@file.expand_path(name, @base))
  $:.unshift(dir)
  if(@req)
    @req.require(name)
  else
    require(name)
  end
  find_test_cases(already_gathered).each{|t| add_suite(suites, t.suite)}
ensure
  $:.delete_at($:.rindex(dir)) if(dir)
end

#find_test_cases(ignore = []) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/test/unit/collector/dir.rb', line 45

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

#realdir(path) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/test/unit/collector/dir.rb', line 98

def realdir(path)
  if @base
    @file.join(@base, path)
  else
    path
  end
end

#recursive_collect(name, already_gathered) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/test/unit/collector/dir.rb', line 54

def recursive_collect(name, already_gathered)
  sub_suites = []
  path = realdir(name)
  if @file.directory?(path)
	    dir_name = name unless name == '.'
    @dir.entries(path).each do |e|
      next if(e == '.' || e == '..')
      e_name = dir_name ? @file.join(dir_name, e) : e
      if @file.directory?(realdir(e_name))
        next if /\A(?:CVS|\.svn|\.git)\z/ =~ e
        sub_suite = recursive_collect(e_name, already_gathered)
        sub_suites << sub_suite unless(sub_suite.empty?)
      else
        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
        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