Module: Datadog::CI::Contrib::Minitest::Helpers

Defined in:
lib/datadog/ci/contrib/minitest/helpers.rb

Class Method Summary collapse

Class Method Details

.extract_runnable_source_location(klass, method_name) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/datadog/ci/contrib/minitest/helpers.rb', line 53

def self.extract_runnable_source_location(klass, method_name)
  source_location = extract_source_location_from_class(klass)
  if source_location.nil? || source_location.empty?
    return klass.instance_method(method_name).source_location
  end
  source_location
end

.extract_source_location_from_class(klass) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/datadog/ci/contrib/minitest/helpers.rb', line 66

def self.extract_source_location_from_class(klass)
  return [] if klass.nil? || klass.name.nil?

  klass.const_source_location(klass.name)
rescue
  []
end

.parallel?(klass) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/datadog/ci/contrib/minitest/helpers.rb', line 61

def self.parallel?(klass)
  klass.ancestors.include?(::Minitest::Parallel::Test) ||
    (defined?(::Minitest::Queue) && ::Minitest.singleton_class.ancestors.include?(::Minitest::Queue))
end

.start_test_suite(klass) ⇒ Object



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
# File 'lib/datadog/ci/contrib/minitest/helpers.rb', line 8

def self.start_test_suite(klass)
  method = klass.runnable_methods.first
  return nil if method.nil?

  test_suite_name = test_suite_name(klass, method)
  source_file, line_number = extract_runnable_source_location(klass, method)

  test_suite_tags = if source_file
    {
      CI::Ext::Test::TAG_SOURCE_FILE => (Git::LocalRepository.relative_to_root(source_file) if source_file),
      CI::Ext::Test::TAG_SOURCE_START => line_number&.to_s
    }
  else
    {}
  end

  test_visibility_component = Datadog.send(:components).test_visibility
  test_suite = test_visibility_component.start_test_suite(
    test_suite_name,
    tags: test_suite_tags
  )
  test_suite&.set_expected_tests!(klass.runnable_methods)

  test_suite
end

.test_suite_name(klass, method_name) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/datadog/ci/contrib/minitest/helpers.rb', line 34

def self.test_suite_name(klass, method_name)
  source_location = extract_runnable_source_location(klass, method_name)&.first

  # According to https://github.com/DataDog/datadog-ci-rb/issues/386
  # the source file path coould be relative when using minitest mixins.
  #
  # Note that it doesn't break for test suite source location in .start_test_suite method
  # because it outputs path relative to the repository root.
  #
  # For backwards compatibility, we'll continue to use the relative path from the current
  # working directory for test suite name.
  source_file_path = Pathname.new(source_location.to_s)
  if source_file_path.absolute?
    source_file_path = source_file_path.relative_path_from(Pathname.pwd).to_s
  end

  "#{klass.name} at #{source_file_path}"
end