From ff0f32a557034727f7b1c23b36c319fd66acb175 Mon Sep 17 00:00:00 2001 From: Andrey Date: Tue, 24 Sep 2024 12:10:24 +0200 Subject: [PATCH] test suite source location support for Cucumber --- lib/datadog/ci/contrib/cucumber/formatter.rb | 28 +++++++++++++++++-- sig/datadog/ci/contrib/cucumber/formatter.rbs | 4 ++- .../contrib/cucumber/instrumentation_spec.rb | 12 ++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/datadog/ci/contrib/cucumber/formatter.rb b/lib/datadog/ci/contrib/cucumber/formatter.rb index ef85c8c7..95d7aa4d 100644 --- a/lib/datadog/ci/contrib/cucumber/formatter.rb +++ b/lib/datadog/ci/contrib/cucumber/formatter.rb @@ -70,7 +70,12 @@ def on_test_case_started(event) tags[CI::Ext::Test::TAG_PARAMETERS] = Utils::TestRun.test_parameters(arguments: parameters) end - start_test_suite(test_suite_name) unless same_test_suite_as_current?(test_suite_name) + unless same_test_suite_as_current?(test_suite_name) + start_test_suite( + test_suite_name, + tags: test_suite_source_file_tags(event.test_case) + ) + end test_span = test_visibility_component.trace_test( event.test_case.name, @@ -146,10 +151,10 @@ def finish_session(result) test_session.finish end - def start_test_suite(test_suite_name) + def start_test_suite(test_suite_name, tags: {}) finish_current_test_suite - @current_test_suite = test_visibility_component.start_test_suite(test_suite_name) + @current_test_suite = test_visibility_component.start_test_suite(test_suite_name, tags: tags) end def finish_current_test_suite @@ -201,6 +206,23 @@ def configuration def test_visibility_component Datadog.send(:components).test_visibility end + + def test_suite_source_file_tags(test_case) + if test_case.respond_to?(:parent_locations) + # supported in cucumber >= 9.0 + source_file = test_case.parent_locations.file + line_number = test_case.parent_locations.line.to_s + else + # fallback for cucumber < 9.0 + source_file = test_case.location.file + line_number = "1" + end + + { + CI::Ext::Test::TAG_SOURCE_FILE => Git::LocalRepository.relative_to_root(source_file), + CI::Ext::Test::TAG_SOURCE_START => line_number.to_s + } + end end end end diff --git a/sig/datadog/ci/contrib/cucumber/formatter.rbs b/sig/datadog/ci/contrib/cucumber/formatter.rbs index 23510e04..5fce73fe 100644 --- a/sig/datadog/ci/contrib/cucumber/formatter.rbs +++ b/sig/datadog/ci/contrib/cucumber/formatter.rbs @@ -35,7 +35,7 @@ module Datadog def test_suite_name: (untyped test_case) -> String - def start_test_suite: (String test_suite_name) -> void + def start_test_suite: (String test_suite_name, ?tags: Hash[String, String]) -> void def finish_current_test_suite: () -> void @@ -52,6 +52,8 @@ module Datadog def configuration: () -> untyped def test_visibility_component: () -> Datadog::CI::TestVisibility::Component + + def test_suite_source_file_tags: (untyped test_case) -> Hash[String, String] end end end diff --git a/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb b/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb index d9f45f9e..5eaf4eb3 100644 --- a/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb +++ b/spec/datadog/ci/contrib/cucumber/instrumentation_spec.rb @@ -209,6 +209,18 @@ :framework_version, Datadog::CI::Contrib::Cucumber::Integration.version.to_s ) + + expect(first_test_suite_span).to have_test_tag( + :source_file, + "passing.feature" + ) + expect(first_test_suite_span).to have_test_tag(:source_start, "1") + + expect(first_test_suite_span).to have_test_tag( + :codeowners, + "[\"@test-owner\"]" + ) + expect(first_test_suite_span).to have_pass_status end