123456789_123456789_123456789_123456789_123456789_

Class: Rails::TestUnit::Runner

Relationships & Source Files
Inherits: Object
Defined in: railties/lib/rails/test_unit/runner.rb

Constant Summary

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Class Attribute Details

.filters (readonly) Also known as: #filters

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 24

mattr_reader :filters, default: []

Class Method Details

.attach_before_load_options(opts)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 27

def attach_before_load_options(opts)
  opts.on("--warnings", "-w", "Run with Ruby warnings enabled") { }
  opts.on("-e", "--environment ENV", "Run tests in the ENV environment") { }
end

.compose_filter(runnable, filter)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 78

def compose_filter(runnable, filter)
  filter = normalize_declarative_test_filter(filter)

  if filters.any? { |_, lines| lines.any? }
    CompositeFilter.new(runnable, filter, filters)
  else
    filter
  end
end

.default_test_exclude_glob (private)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 113

def default_test_exclude_glob
  ENV["DEFAULT_TEST_EXCLUDE"] || "test/{system,dummy,fixtures}/**/*_test.rb"
end

.default_test_glob (private)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 109

def default_test_glob
  ENV["DEFAULT_TEST"] || "test/**/*_test.rb"
end

.extract_filters(argv) (private)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 89

def extract_filters(argv)
  # Extract absolute and relative paths but skip -n /.*/ regexp filters.
  argv.filter_map do |path|
    next unless path_argument?(path)

    path = path.tr("\\", "/")
    case
    when /(:\d(-\d)?)+$/.match?(path)
      file, *lines = path.split(":")
      filters << [ file, lines ]
      file
    when Dir.exist?(path)
      "#{path}/**/*_test.rb"
    else
      filters << [ path, [] ]
      path
    end
  end
end

.list_tests(patterns) (private)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 125

def list_tests(patterns)
  tests = Rake::FileList[patterns.any? ? patterns : default_test_glob]
  tests.exclude(default_test_exclude_glob) if patterns.empty?
  tests.exclude(%r{test/isolation/assets/node_modules})
  tests
end

.load_tests(argv)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 57

def load_tests(argv)
  patterns = extract_filters(argv)
  tests = list_tests(patterns)
  tests.to_a.each do |path|
    abs_path = File.expand_path(path)
    require abs_path
  rescue LoadError => exception
    if exception.path == abs_path
      all_tests = list_tests([default_test_glob])
      corrections = DidYouMean::SpellChecker.new(dictionary: all_tests).correct(path)

      if corrections.empty?
        raise exception
      end
      raise InvalidTestError.new(path, DidYouMean::Formatter.message_for(corrections))
    else
      raise
    end
  end
end

.normalize_declarative_test_filter(filter) (private)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 132

def normalize_declarative_test_filter(filter)
  if filter.is_a?(String)
    if regexp_filter?(filter)
      # Minitest::Spec::DSL#it does not replace whitespace in method
      # names, so match unmodified method names as well.
      filter = filter.gsub(/\s+/, "_").delete_suffix("/") + "|" + filter.delete_prefix("/")
    elsif !filter.start_with?("test_")
      filter = "test_#{filter.gsub(/\s+/, "_")}"
    end
  end
  filter
end

.parse_options(argv)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 32

def parse_options(argv)
  # Perform manual parsing and cleanup since option parser raises on unknown options.
  env_index = argv.index("--environment") || argv.index("-e")
  if env_index
    argv.delete_at(env_index)
    environment = argv.delete_at(env_index).strip
  end
  ENV["RAILS_ENV"] = environment || "test"

  w_index = argv.index("--warnings") || argv.index("-w")
  $VERBOSE = argv.delete_at(w_index) if w_index
end

.path_argument?(arg) ⇒ Boolean (private)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 121

def path_argument?(arg)
  PATH_ARGUMENT_PATTERN.match?(arg)
end

.regexp_filter?(arg) ⇒ Boolean (private)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 117

def regexp_filter?(arg)
  arg.start_with?("/") && arg.end_with?("/")
end

.run(argv = [])

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 51

def run(argv = [])
  load_tests(argv)

  require "active_support/testing/autorun"
end

.run_from_rake(test_command, argv = [])

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 45

def run_from_rake(test_command, argv = [])
  # Ensure the tests run during the Rake Task action, not when the process exits
  success = system("rails", test_command, *argv, *Shellwords.split(ENV["TESTOPTS"] || ""))
  success || exit(false)
end

Instance Attribute Details

#filters (readonly)

[ GitHub ]

  
# File 'railties/lib/rails/test_unit/runner.rb', line 24

mattr_reader :filters, default: []