Class: Nicetest::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/nicetest/cli.rb

Defined Under Namespace

Classes: Opts

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Cli

Returns a new instance of Cli.



11
12
13
14
15
# File 'lib/nicetest/cli.rb', line 11

def initialize(argv)
  @argv = argv
  @logger = Logger.new($stderr)
  adjust_load_path!
end

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/nicetest/cli.rb', line 17

def run
  cli_options = Opts.parse!(@argv)
  if (dir = cli_options.cd)
    run_in_directory(dir)
  elsif File.exist?("Gemfile") && !File.read("Gemfile").include?("nicetest")
    run_in_directory(".")
  else
    run_tests(cli_options)
  end
end

#run_tests(cli_options = Opts.parse!(@argv)) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nicetest/cli.rb', line 28

def run_tests(cli_options = Opts.parse!(@argv))
  disable_autorun!

  args = @argv.dup
  argv_test_files = select_file_args(args)
  argv_test_files = glob_test_files("test") if argv_test_files.empty?
  args -= argv_test_files
  finder = TestFinder.new
  filters = Set.new

  if cli_options.name
    name = cli_options.name
    name = name[1..-1] if name.start_with?("/")
    name = name[0..-2] if name.end_with?("/")
    filters << name
  end

  # Gross hack to allow for plugins to be loaded before we require the test files
  Minitest.load_plugins unless args.delete("--no-plugins") || ENV["MT_NO_PLUGINS"]
  processed_args = Minitest.process_args(args)

  required_files = argv_test_files.map do |pattern|
    file_or_dir, filter = finder.filter_for(pattern)
    filters << filter if filter && !cli_options.name
    require_path_or_dir(file_or_dir)
  end

  @logger.fatal!("no test files found") if required_files.compact.empty?

  if filters.any?
    processed_args[:filter] = Regexp.union(*filters.map do |filter|
      Regexp.new("^#{filter}$")
    end)
    processed_args[:args] = "#{processed_args[:args]} --name=/^#{processed_args[:filter].source}$/"
  end

  patch_minitest_process_args!(processed_args)

  Minitest.run(args)
end