Class: Test::Unit::Collector::Load
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
::Test::Unit::Collector
|
|
| Inherits: | Object |
| Defined in: | lib/test/unit/collector/load.rb |
Class Method Summary
- .new ⇒ Load constructor
Instance Attribute Summary
- #base rw
- #base=(base) rw
- #default_test_paths rw
- #default_test_paths=(paths) rw
- #excludes readonly
- #patterns readonly
::Test::Unit::Collector - Included
Instance Method Summary
- #collect(*froms)
- #find_test_cases(already_gathered, descendants = TestCase::DESCENDANTS)
- #add_load_path(path) private
- #add_require_failed_test_suite(test_suites) private
- #box_file?(path) ⇒ Boolean private
- #collect_box_file(expanded_path, test_suites, already_gathered) private
- #collect_file(path, test_suites, already_gathered) private
- #collect_recursive(path, already_gathered) private
- #excluded_directory?(base) ⇒ Boolean private
- #excluded_file?(base) ⇒ Boolean private
- #resolve_path(path) private
::Test::Unit::Collector - Included
Constructor Details
.new ⇒ Load
# File 'lib/test/unit/collector/load.rb', line 15
def initialize super @system_excludes = [/~\z/, /\A\.\#/] @system_directory_excludes = [/\A(?:CVS|\.svn|\.git)\z/] @patterns = [ /\Atest[_\-].+\.rb\z/m, # The test prefix pattern (test*.box.rb) is covered by the pattern above # /\Atest[_\-].+\.box\.rb\z/m, /[_\-]test\.rb\z/, /[_\-]test\.box\.rb\z/ ] @excludes = [] @base = nil @default_test_paths = [] @require_failed_infos = [] end
Instance Attribute Details
#base (rw)
[ GitHub ]#base=(base) (rw)
[ GitHub ]#default_test_paths (rw)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 13
attr_reader :default_test_paths
#default_test_paths=(paths) (rw)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 37
def default_test_paths=(paths) @default_test_paths = paths.collect do |path| Pathname(path) end end
#excludes (readonly)
[ GitHub ]#patterns (readonly)
[ GitHub ]Instance Method Details
#add_load_path(path) (private)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 173
def add_load_path(path) return yield if path.nil? path = path.to_s begin $LOAD_PATH.unshift(path) yield ensure index = $LOAD_PATH.index(path) $LOAD_PATH.delete_at(index) if index end end
#add_require_failed_test_suite(test_suites) (private)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 206
def add_require_failed_test_suite(test_suites) return if @require_failed_infos.empty? require_failed_infos = @require_failed_infos require_failed_errors = Class.new(Test::Unit::TestCase) require_failed_errors.class_eval do class << self def name "RequireFailedErrors" end end require_failed_infos.each do |info| path = info[:path] normalized_path = path.to_s.gsub(/[^a-z0-9\_]+/i, '_') normalized_path = normalized_path.gsub(/\A_+/, '') exception = info[:exception] if exception define_method("test_require_#{normalized_path}") do raise(exception.class, "failed to load <#{path}>: #{exception.}", exception.backtrace) end else = info[:] define_method("test_require_#{normalized_path}") do raise("failed to load <#{path}>: #{}") end end end def priority 100 end end add_suite(test_suites, require_failed_errors.suite) end
#box_file?(path) ⇒ Boolean (private)
# File 'lib/test/unit/collector/load.rb', line 136
def box_file?(path) path.to_s.end_with?(".box.rb") end
#collect(*froms)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 43
def collect(*froms) add_load_path(@base) do froms = @default_test_paths if froms.empty? froms = ["."] if froms.empty? test_suites = [] already_gathered = {} find_test_cases(already_gathered) froms.each do |from| from = resolve_path(from) if from.directory? test_suite = collect_recursive(from, already_gathered) test_suites << test_suite unless test_suite.tests.empty? else collect_file(from, test_suites, already_gathered) end end add_require_failed_test_suite(test_suites) if test_suites.size > 1 test_suite = TestSuite.new("[#{froms.join(', ')}]") sort(test_suites).each do |sub_test_suite| test_suite << sub_test_suite end else test_suite = test_suites.first end adjust_ractor_tests(test_suite) test_suite end end
#collect_box_file(expanded_path, test_suites, already_gathered) (private)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 140
def collect_box_file(, test_suites, already_gathered) unless Test::Unit.box_available? errmsg = "Ruby::Box is unavailable while loading <#{}>" + " Use Ruby 4.1 or later with RUBY_BOX=1" @require_failed_infos << {:path => , : => errmsg} return end box = Ruby::Box.new # The box must resolve require("test/unit") to the same # test-unit as the current box's one. test_unit_lib_path = File.("../../../", __dir__) box.load_path.unshift(.dirname.to_s, test_unit_lib_path) begin box.require(.to_s) rescue LoadError @require_failed_infos << {:path => , :exception => $!} return end return unless defined?(box::Test::Unit::TestCase) box::Test::Unit::AutoRunner.need_auto_run = false test_cases = find_test_cases(already_gathered, box::Test::Unit::TestCase::DESCENDANTS) add_test_cases(test_suites, test_cases) end
#collect_file(path, test_suites, already_gathered) (private)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 118
def collect_file(path, test_suites, already_gathered) @program_file ||= File.($0) = path. return if @program_file == .to_s if box_file?() collect_box_file(, test_suites, already_gathered) return end add_load_path(.dirname) do begin require(.to_s) rescue LoadError @require_failed_infos << {:path => , :exception => $!} end add_test_cases(test_suites, find_test_cases(already_gathered)) end end
#collect_recursive(path, already_gathered) (private)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 87
def collect_recursive(path, already_gathered) sub_test_suites = [] if path.directory? directories, files = path.children.partition do |child| child.directory? end files.each do |child| next if excluded_file?(child.basename.to_s) collect_file(child, sub_test_suites, already_gathered) end directories.each do |child| next if excluded_directory?(child.basename.to_s) sub_test_suite = collect_recursive(child, already_gathered) sub_test_suites << sub_test_suite unless sub_test_suite.empty? end else unless excluded_file?(path.basename.to_s) collect_file(path, sub_test_suites, already_gathered) end end test_suite = TestSuite.new(path.basename.to_s) sort(sub_test_suites).each do |sub_test_suite| test_suite << sub_test_suite end test_suite end
#excluded_directory?(base) ⇒ Boolean (private)
#excluded_file?(base) ⇒ Boolean (private)
# File 'lib/test/unit/collector/load.rb', line 190
def excluded_file?(base) return true if @system_excludes.any? {|pattern| pattern =~ base} patterns = @patterns || [] unless patterns.empty? return true unless patterns.any? {|pattern| pattern =~ base} end excludes = @excludes || [] unless excludes.empty? return true if excludes.any? {|pattern| pattern =~ base} end false end
#find_test_cases(already_gathered, descendants = TestCase::DESCENDANTS)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 76
def find_test_cases(already_gathered, descendants=TestCase::DESCENDANTS) test_cases = [] descendants.each do |test_case| next if already_gathered.key?(test_case) test_cases << test_case already_gathered[test_case] = true end test_cases end
#resolve_path(path) (private)
[ GitHub ]# File 'lib/test/unit/collector/load.rb', line 165
def resolve_path(path) if @base @base + path else Pathname(path) end end