class Test::Unit::Collector::Load - test-unit: Ruby Standard Library Documentation

# class Test::Unit::Collector::Load

### Attributes

base[R]
default_test_paths[R]
excludes[R]
patterns[R]

### Public Class Methods

new() click to toggle source
Calls superclass method `Test::Unit::Collector::new`

    # File test-unit-3.6.7/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, /[_\-]test\.rb\z/]
      @excludes = []
      @base = nil
      @default_test_paths = []
      @require_failed_infos = []
    end

### Private Class Methods

name() click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 174
    def name
      "RequireFailedErrors"
    end

### Public Instance Methods

base=(base) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 26
    def base=(base)
      base = Pathname(base) unless base.nil?
      @base = base
    end

collect(*froms) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 37
    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

default_test_paths=(paths) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 31
    def default_test_paths=(paths)
      @default_test_paths = paths.collect do |path|
        Pathname(path)
      end
    end

find_test_cases(already_gathered) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 70
    def find_test_cases(already_gathered)
      test_cases = []
      TestCase::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

### Private Instance Methods

add_load_path(path) { || ... } click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 134
    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) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 167
    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]
          define_method("test_require_#{normalized_path}") do
            raise(exception.class,
                  "failed to load <#{path}>: #{exception.message}",
                  exception.backtrace)
          end
        end

        def priority
          100
        end
      end

      add_suite(test_suites, require_failed_errors.suite)
    end

collect_file(path, test_suites, already_gathered) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 112
    def collect_file(path, test_suites, already_gathered)
      @program_file ||= File.expand_path($0)
      expanded_path = path.expand_path
      return if @program_file == expanded_path.to_s
      add_load_path(expanded_path.dirname) do
        begin
          require(expanded_path.to_s)
        rescue LoadError
          @require_failed_infos << {:path => expanded_path, :exception => $!}
        end
        add_test_cases(test_suites, find_test_cases(already_gathered))
      end
    end

collect_recursive(path, already_gathered) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 81
    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) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 147
    def excluded_directory?(base)
      @system_directory_excludes.any? {|pattern| pattern =~ base}
    end

excluded_file?(base) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 151
    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

priority() click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 191
    def priority
      100
    end

resolve_path(path) click to toggle source

    # File test-unit-3.6.7/lib/test/unit/collector/load.rb, line 126
    def resolve_path(path)
      if @base
        @base + path
      else
        Pathname(path)
      end
    end