File: minitest_runner.rb

package info (click to toggle)
ruby-byebug 11.1.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,252 kB
  • sloc: ruby: 8,835; ansic: 1,662; sh: 6; makefile: 4
file content (96 lines) | stat: -rw-r--r-- 1,971 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

$LOAD_PATH.unshift(File.expand_path(File.join("..", "lib"), __dir__))
$LOAD_PATH.unshift(__dir__)

require "minitest"
require "English"
require "shellwords"
require "timeout"

module Byebug
  #
  # Helper class to aid running minitest
  #
  class MinitestRunner
    def initialize
      @test_suites = extract_from_argv { |cmd_arg| test_suite?(cmd_arg) }
    end

    def run
      test_suites.each { |f| require File.expand_path(f) }

      flags = if ENV["TESTOPTS"]
                ENV["TESTOPTS"].split(" ")
              else
                ["--name=/#{filtered_methods.join('|')}/"]
              end

      Minitest.run(flags + $ARGV)
    end

    private

    def runnables
      Minitest::Runnable.runnables
    end

    def test_suite?(str)
      all_test_suites.include?(str)
    end

    def test_suites
      return all_test_suites if @test_suites.empty?

      @test_suites
    end

    def test_methods(str)
      if /test_.*/.match?(str)
        filter_runnables_by_method(str)
      else
        filter_runnables_by_class(str)
      end
    end

    def filter_runnables_by_method(str)
      filter_runnables do |runnable|
        "#{runnable}##{str}" if runnable.runnable_methods.include?(str)
      end
    end

    def filter_runnables_by_class(str)
      filter_runnables do |runnable|
        runnable.runnable_methods if runnable.name == "Byebug::#{str}"
      end
    end

    def filter_runnables
      selected = runnables.flat_map do |runnable|
        yield(runnable)
      end.compact

      return unless selected.any?

      selected
    end

    def filtered_methods
      @filtered_methods ||= extract_from_argv do |cmd_arg|
        test_methods(cmd_arg)
      end
    end

    def all_test_suites
      Dir.glob("test/**/*_test.rb")
    end

    def extract_from_argv
      matching, non_matching = $ARGV.partition { |arg| yield(arg) }

      $ARGV.replace(non_matching)

      matching
    end
  end
end