Skip to content

Commit

Permalink
add test suite source location support for minitest
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 23, 2024
1 parent 3670369 commit 415c5b1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
8 changes: 4 additions & 4 deletions lib/datadog/ci/contrib/minitest/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Contrib
module Minitest
module Helpers
def self.test_suite_name(klass, method_name)
source_location = extract_source_location_from_class(klass)
source_location = extract_source_location_from_class(klass)&.first
# if we are in anonymous class, fallback to the method source location
if source_location.nil?
source_location, = klass.instance_method(method_name).source_location
Expand All @@ -23,11 +23,11 @@ def self.parallel?(klass)
end

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

klass.const_source_location(klass.name)&.first
klass.const_source_location(klass.name)
rescue
nil
[]
end
end
end
Expand Down
11 changes: 9 additions & 2 deletions lib/datadog/ci/contrib/minitest/runnable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ def run(*args)
return super if method.nil?

test_suite_name = Helpers.test_suite_name(self, method)

test_suite = test_visibility_component.start_test_suite(test_suite_name)
source_file, line_number = Helpers.extract_source_location_from_class(self)

test_suite = test_visibility_component.start_test_suite(
test_suite_name,
tags: {
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
}
)

results = super
return results unless test_suite
Expand Down
2 changes: 1 addition & 1 deletion sig/datadog/ci/contrib/minitest/helpers.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Datadog

def self.parallel?: (untyped klass) -> bool

def self.extract_source_location_from_class: (untyped klass) -> String?
def self.extract_source_location_from_class: (untyped klass) -> Array[::String]
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/datadog/ci/contrib/minitest/instrumentation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,17 @@ def test_pass_other
:framework_version,
Datadog::CI::Contrib::Minitest::Integration.version.to_s
)

expect(first_test_suite_span).to have_test_tag(
:source_file,
"spec/datadog/ci/contrib/minitest/instrumentation_spec.rb"
)
expect(first_test_suite_span).to have_test_tag(:source_start, "415")
expect(first_test_suite_span).to have_test_tag(
:codeowners,
"[\"@DataDog/ruby-guild\", \"@DataDog/ci-app-libraries\"]"
)

expect(first_test_suite_span).to have_pass_status
end

Expand Down

0 comments on commit 415c5b1

Please sign in to comment.