Skip to content

Commit

Permalink
Fix Cucumber JUnit 4 instrumentation to support empty scenario names (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachenko-datadog authored Aug 22, 2024
1 parent 4679c0a commit 77c6fbe
Show file tree
Hide file tree
Showing 7 changed files with 350 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public static String getVersion() {
REFLECTION.constructor("io.cucumber.junit.PickleRunners$PickleId", Pickle.class);
private static final MethodHandle PICKLE_ID_URI_GETTER =
REFLECTION.privateFieldGetter("io.cucumber.junit.PickleRunners$PickleId", "uri");
private static final MethodHandle PICKLE_ID_LINE_GETTER =
REFLECTION.privateFieldGetter("io.cucumber.junit.PickleRunners$PickleId", "pickleLine");
private static final MethodHandle PICKLE_RUNNER_GET_DESCRIPTION =
REFLECTION.method("io.cucumber.junit.PickleRunners$PickleRunner", "getDescription");

Expand All @@ -72,6 +74,11 @@ public static URI getPickleUri(Description scenarioDescription) {
return REFLECTION.invoke(PICKLE_ID_URI_GETTER, pickleId);
}

public static Integer getPickleLine(Description scenarioDescription) {
Object pickleId = JUnit4Utils.getUniqueId(scenarioDescription);
return REFLECTION.invoke(PICKLE_ID_LINE_GETTER, pickleId);
}

public static String getTestSuiteNameForFeature(Description featureDescription) {
Object uniqueId = JUnit4Utils.getUniqueId(featureDescription);
return (uniqueId != null ? uniqueId + ":" : "") + featureDescription.getClassName();
Expand Down Expand Up @@ -99,12 +106,22 @@ private static String getFeatureNameForScenario(Description scenarioDescription)
public static String getTestNameForScenario(Description scenarioDescription) {
String scenarioDescriptionString = scenarioDescription.toString();
int featureNameStart = getFeatureNameStartIdx(scenarioDescriptionString);
if (featureNameStart >= 0) {
if (featureNameStart > 0) { // if featureNameStart == 0, then test name is empty and of no use
return scenarioDescriptionString.substring(0, featureNameStart);
} else {
// fallback to default method
return scenarioDescription.getMethodName();
}

// fallback to default method
String methodName = scenarioDescription.getMethodName();
if (Strings.isNotBlank(methodName)) {
return methodName;
}

Integer pickleLine = getPickleLine(scenarioDescription);
if (pickleLine != null) {
return "LINE:" + pickleLine + "";
}

return "EMPTY_NAME";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class CucumberTest extends CiVisibilityInstrumentationTest {
"org/example/cucumber/calculator/basic_arithmetic_failed.feature"
] | 3
"test-name-with-brackets" | ["org/example/cucumber/calculator/name_with_brackets.feature"] | 2
"test-empty-name-${version()}" | ["org/example/cucumber/calculator/empty_scenario_name.feature"] | 2
}

def "test ITR #testcaseName"() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
@foo
Feature:

Background: A Calculator
Given a calculator I just turned on

Scenario:
# Try to change one of the values below to provoke a failure
When I add 4 and 5
Then the result is 9
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ {
"test_session_id" : ${content_test_session_id},
"test_suite_id" : ${content_test_suite_id},
"span_id" : ${content_span_id},
"files" : [ {
"filename" : "org/example/cucumber/calculator/empty_scenario_name.feature"
} ]
} ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
[ {
"type" : "test_suite_end",
"version" : 1,
"content" : {
"test_session_id" : ${content_test_session_id},
"test_module_id" : ${content_test_module_id},
"test_suite_id" : ${content_test_suite_id},
"service" : "worker.org.gradle.process.internal.worker.gradleworkermain",
"name" : "junit.test_suite",
"resource" : "classpath:org/example/cucumber/calculator/empty_scenario_name.feature:EMPTY_NAME",
"start" : ${content_start},
"duration" : ${content_duration},
"error" : 0,
"metrics" : { },
"meta" : {
"test.type" : "test",
"os.architecture" : ${content_meta_os_architecture},
"test.module" : "cucumber-junit-4",
"test.status" : "pass",
"runtime.name" : ${content_meta_runtime_name},
"runtime.vendor" : ${content_meta_runtime_vendor},
"env" : "none",
"os.platform" : ${content_meta_os_platform},
"dummy_ci_tag" : "dummy_ci_tag_value",
"os.version" : ${content_meta_os_version},
"library_version" : ${content_meta_library_version},
"component" : "junit",
"span.kind" : "test_suite_end",
"test.suite" : "classpath:org/example/cucumber/calculator/empty_scenario_name.feature:EMPTY_NAME",
"runtime.version" : ${content_meta_runtime_version},
"test.framework_version" : ${content_meta_test_framework_version},
"test.framework" : "cucumber"
}
}
}, {
"type" : "test",
"version" : 2,
"content" : {
"trace_id" : ${content_trace_id},
"span_id" : ${content_span_id},
"parent_id" : ${content_parent_id},
"test_session_id" : ${content_test_session_id},
"test_module_id" : ${content_test_module_id},
"test_suite_id" : ${content_test_suite_id},
"service" : "worker.org.gradle.process.internal.worker.gradleworkermain",
"name" : "junit.test",
"resource" : "classpath:org/example/cucumber/calculator/empty_scenario_name.feature:EMPTY_NAME.EMPTY_NAME",
"start" : ${content_start_2},
"duration" : ${content_duration_2},
"error" : 0,
"metrics" : {
"process_id" : ${content_metrics_process_id},
"_dd.profiling.enabled" : 0,
"_dd.trace_span_attribute_schema" : 0
},
"meta" : {
"os.architecture" : ${content_meta_os_architecture},
"test.module" : "cucumber-junit-4",
"test.status" : "pass",
"language" : "jvm",
"runtime.name" : ${content_meta_runtime_name},
"os.platform" : ${content_meta_os_platform},
"os.version" : ${content_meta_os_version},
"library_version" : ${content_meta_library_version},
"test.name" : "EMPTY_NAME",
"span.kind" : "test",
"test.suite" : "classpath:org/example/cucumber/calculator/empty_scenario_name.feature:EMPTY_NAME",
"runtime.version" : ${content_meta_runtime_version},
"runtime-id" : ${content_meta_runtime_id},
"test.type" : "test",
"test.traits" : "{\"category\":[\"foo\"]}",
"runtime.vendor" : ${content_meta_runtime_vendor},
"env" : "none",
"dummy_ci_tag" : "dummy_ci_tag_value",
"component" : "junit",
"_dd.profiling.ctx" : "test",
"test.framework_version" : ${content_meta_test_framework_version},
"test.framework" : "cucumber"
}
}
}, {
"type" : "test_session_end",
"version" : 1,
"content" : {
"test_session_id" : ${content_test_session_id},
"service" : "worker.org.gradle.process.internal.worker.gradleworkermain",
"name" : "junit.test_session",
"resource" : "cucumber-junit-4",
"start" : ${content_start_3},
"duration" : ${content_duration_3},
"error" : 0,
"metrics" : {
"process_id" : ${content_metrics_process_id},
"_dd.profiling.enabled" : 0,
"_dd.trace_span_attribute_schema" : 0
},
"meta" : {
"test.type" : "test",
"os.architecture" : ${content_meta_os_architecture},
"test.status" : "pass",
"language" : "jvm",
"runtime.name" : ${content_meta_runtime_name},
"runtime.vendor" : ${content_meta_runtime_vendor},
"env" : "none",
"os.platform" : ${content_meta_os_platform},
"dummy_ci_tag" : "dummy_ci_tag_value",
"os.version" : ${content_meta_os_version},
"library_version" : ${content_meta_library_version},
"component" : "junit",
"_dd.profiling.ctx" : "test",
"span.kind" : "test_session_end",
"runtime.version" : ${content_meta_runtime_version},
"runtime-id" : ${content_meta_runtime_id},
"test.command" : "cucumber-junit-4",
"test.framework_version" : ${content_meta_test_framework_version},
"test.framework" : "cucumber"
}
}
}, {
"type" : "test_module_end",
"version" : 1,
"content" : {
"test_session_id" : ${content_test_session_id},
"test_module_id" : ${content_test_module_id},
"service" : "worker.org.gradle.process.internal.worker.gradleworkermain",
"name" : "junit.test_module",
"resource" : "cucumber-junit-4",
"start" : ${content_start_4},
"duration" : ${content_duration_4},
"error" : 0,
"metrics" : { },
"meta" : {
"test.type" : "test",
"os.architecture" : ${content_meta_os_architecture},
"test.module" : "cucumber-junit-4",
"test.status" : "pass",
"runtime.name" : ${content_meta_runtime_name},
"runtime.vendor" : ${content_meta_runtime_vendor},
"env" : "none",
"os.platform" : ${content_meta_os_platform},
"dummy_ci_tag" : "dummy_ci_tag_value",
"os.version" : ${content_meta_os_version},
"library_version" : ${content_meta_library_version},
"component" : "junit",
"span.kind" : "test_module_end",
"runtime.version" : ${content_meta_runtime_version},
"test.framework_version" : ${content_meta_test_framework_version},
"test.framework" : "cucumber"
}
}
} ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[ {
"test_session_id" : ${content_test_session_id},
"test_suite_id" : ${content_test_suite_id},
"span_id" : ${content_span_id},
"files" : [ {
"filename" : "org/example/cucumber/calculator/empty_scenario_name.feature"
} ]
} ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
[ {
"type" : "test_suite_end",
"version" : 1,
"content" : {
"test_session_id" : ${content_test_session_id},
"test_module_id" : ${content_test_module_id},
"test_suite_id" : ${content_test_suite_id},
"service" : "worker.org.gradle.process.internal.worker.gradleworkermain",
"name" : "junit.test_suite",
"resource" : "classpath:org/example/cucumber/calculator/empty_scenario_name.feature:EMPTY_NAME",
"start" : ${content_start},
"duration" : ${content_duration},
"error" : 0,
"metrics" : { },
"meta" : {
"test.type" : "test",
"os.architecture" : ${content_meta_os_architecture},
"test.module" : "cucumber-junit-4",
"test.status" : "pass",
"runtime.name" : ${content_meta_runtime_name},
"runtime.vendor" : ${content_meta_runtime_vendor},
"env" : "none",
"os.platform" : ${content_meta_os_platform},
"dummy_ci_tag" : "dummy_ci_tag_value",
"os.version" : ${content_meta_os_version},
"library_version" : ${content_meta_library_version},
"component" : "junit",
"span.kind" : "test_suite_end",
"test.suite" : "classpath:org/example/cucumber/calculator/empty_scenario_name.feature:EMPTY_NAME",
"runtime.version" : ${content_meta_runtime_version},
"test.framework_version" : ${content_meta_test_framework_version},
"test.framework" : "cucumber"
}
}
}, {
"type" : "test",
"version" : 2,
"content" : {
"trace_id" : ${content_trace_id},
"span_id" : ${content_span_id},
"parent_id" : ${content_parent_id},
"test_session_id" : ${content_test_session_id},
"test_module_id" : ${content_test_module_id},
"test_suite_id" : ${content_test_suite_id},
"service" : "worker.org.gradle.process.internal.worker.gradleworkermain",
"name" : "junit.test",
"resource" : "classpath:org/example/cucumber/calculator/empty_scenario_name.feature:EMPTY_NAME.LINE:7",
"start" : ${content_start_2},
"duration" : ${content_duration_2},
"error" : 0,
"metrics" : {
"process_id" : ${content_metrics_process_id},
"_dd.profiling.enabled" : 0,
"_dd.trace_span_attribute_schema" : 0
},
"meta" : {
"os.architecture" : ${content_meta_os_architecture},
"test.module" : "cucumber-junit-4",
"test.status" : "pass",
"language" : "jvm",
"runtime.name" : ${content_meta_runtime_name},
"os.platform" : ${content_meta_os_platform},
"os.version" : ${content_meta_os_version},
"library_version" : ${content_meta_library_version},
"test.name" : "LINE:7",
"span.kind" : "test",
"test.suite" : "classpath:org/example/cucumber/calculator/empty_scenario_name.feature:EMPTY_NAME",
"runtime.version" : ${content_meta_runtime_version},
"runtime-id" : ${content_meta_runtime_id},
"test.type" : "test",
"test.traits" : "{\"category\":[\"foo\"]}",
"runtime.vendor" : ${content_meta_runtime_vendor},
"env" : "none",
"dummy_ci_tag" : "dummy_ci_tag_value",
"component" : "junit",
"_dd.profiling.ctx" : "test",
"test.framework_version" : ${content_meta_test_framework_version},
"test.framework" : "cucumber"
}
}
}, {
"type" : "test_session_end",
"version" : 1,
"content" : {
"test_session_id" : ${content_test_session_id},
"service" : "worker.org.gradle.process.internal.worker.gradleworkermain",
"name" : "junit.test_session",
"resource" : "cucumber-junit-4",
"start" : ${content_start_3},
"duration" : ${content_duration_3},
"error" : 0,
"metrics" : {
"process_id" : ${content_metrics_process_id},
"_dd.profiling.enabled" : 0,
"_dd.trace_span_attribute_schema" : 0
},
"meta" : {
"test.type" : "test",
"os.architecture" : ${content_meta_os_architecture},
"test.status" : "pass",
"language" : "jvm",
"runtime.name" : ${content_meta_runtime_name},
"runtime.vendor" : ${content_meta_runtime_vendor},
"env" : "none",
"os.platform" : ${content_meta_os_platform},
"dummy_ci_tag" : "dummy_ci_tag_value",
"os.version" : ${content_meta_os_version},
"library_version" : ${content_meta_library_version},
"component" : "junit",
"_dd.profiling.ctx" : "test",
"span.kind" : "test_session_end",
"runtime.version" : ${content_meta_runtime_version},
"runtime-id" : ${content_meta_runtime_id},
"test.command" : "cucumber-junit-4",
"test.framework_version" : ${content_meta_test_framework_version},
"test.framework" : "cucumber"
}
}
}, {
"type" : "test_module_end",
"version" : 1,
"content" : {
"test_session_id" : ${content_test_session_id},
"test_module_id" : ${content_test_module_id},
"service" : "worker.org.gradle.process.internal.worker.gradleworkermain",
"name" : "junit.test_module",
"resource" : "cucumber-junit-4",
"start" : ${content_start_4},
"duration" : ${content_duration_4},
"error" : 0,
"metrics" : { },
"meta" : {
"test.type" : "test",
"os.architecture" : ${content_meta_os_architecture},
"test.module" : "cucumber-junit-4",
"test.status" : "pass",
"runtime.name" : ${content_meta_runtime_name},
"runtime.vendor" : ${content_meta_runtime_vendor},
"env" : "none",
"os.platform" : ${content_meta_os_platform},
"dummy_ci_tag" : "dummy_ci_tag_value",
"os.version" : ${content_meta_os_version},
"library_version" : ${content_meta_library_version},
"component" : "junit",
"span.kind" : "test_module_end",
"runtime.version" : ${content_meta_runtime_version},
"test.framework_version" : ${content_meta_test_framework_version},
"test.framework" : "cucumber"
}
}
} ]

0 comments on commit 77c6fbe

Please sign in to comment.