Module: DiffTest::Integrations::Minitest::Lifecycle::InstanceMethods

Defined in:
lib/diff_test/integrations/minitest/lifecycle.rb

Instance Method Summary collapse

Instance Method Details

#relative_test_file_path(runnable_class, runnable_method) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/diff_test/integrations/minitest/lifecycle.rb', line 10

def relative_test_file_path(runnable_class, runnable_method)
  value   = Helper.relative_const_source_path_from_project_root(runnable_class.name) rescue nil

  unbound_method = runnable_class.instance_method(runnable_method) if runnable_method
  value ||= Helper.relative_path_from_project_root(unbound_method.source_location.first) if unbound_method

  value
rescue
  nil
end

#runObject



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
# File 'lib/diff_test/integrations/minitest/lifecycle.rb', line 21

def run
  return super unless DiffTest.configuration.enabled?

  begin
    unless DiffTest::TestSuiteExecution.current
      tests = ::Minitest::Test.runnables.flat_map do |runnable|
        next if runnable.runnable_methods.empty?

        test_file_path = relative_test_file_path(runnable, runnable.runnable_methods.first)
        next unless test_file_path

        runnable.runnable_methods.map do |test_name|
          DiffTest::Helper.test_id(test_file_path, test_name)
        end
      end.compact

      DiffTest::TestSuiteExecution.find_or_create(tests)

      at_exit do
        DiffTest::TestSuiteExecution.current.save
      end
    end
  rescue => e
    DiffTest.error("An error ocurred while trying to create a test suite execution on DiffTest. Please report this issue.\nException: #{e}")
    return super
  end

  DiffTest.error("DiffTest: Expected DiffTest::TestExecution to be nil, but was #{DiffTest::TestExecution.current}. Please report this issue.") if DiffTest::TestExecution.current

  return super unless DiffTest::TestSuiteExecution.current.valid?

  begin
    test_file_path = relative_test_file_path(self.class, self.name)
    return super if test_file_path.nil?
    test_name = self.name

    DiffTest::TestExecution.current = execution =  DiffTest::TestExecution.new(test_file_path:, test_name:)
    should_run = execution.should_run?
    DiffTest::TestSuiteExecution.current.ensure_application_eager_loaded! if should_run
  rescue => e
    DiffTest.error("An error ocurred while trying to create a test execution. Please report this issue.\nException: #{e}")
    return super
  end

  if should_run
    execution.start
    super
  else
    # Skip raises an exception too, which is handled by `capture_exception`
    capture_exceptions do
      skip("Impacted files have not changed. Skipping...")
    end
    self.time = execution.previous_runtime_s
    ::Minitest::Result.from(self)
  end
ensure
  if execution
    if execution.should_run?
      if skipped?
        execution.skipped!
      elsif passed?
        execution.passed!
      else
        execution.failed!
      end
      execution.stop
    else
      execution.not_run!
      execution.finish
    end
    DiffTest::TestExecution.current = nil
  end
end