Skip to content

Commit 8f1e767

Browse files
Fix tracing skipped suites in MUnit 1.0.1 (#7605)
1 parent a7d9a79 commit 8f1e767

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

dd-java-agent/instrumentation/junit-4.10/munit-junit-4/build.gradle

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ muzzle {
55
pass {
66
group = 'org.scalameta'
77
module = 'munit_2.13'
8-
versions = '[0.7.28,1.0.0]'
8+
versions = '[0.7.28,)'
99
}
1010
}
1111

@@ -28,10 +28,7 @@ dependencies {
2828
testImplementation group: 'org.scala-lang', name: 'scala-library', version: '2.13.10'
2929
testImplementation group: 'org.scalameta', name: 'munit_2.13', version: '0.7.28'
3030

31-
// latest version as of august 2024 is 1.0.1, but that version changes which notifications are sent when a test is skipped,
32-
// making the tests fail. See https://github.com/scalameta/munit/issues/813 and https://github.com/DataDog/dd-trace-java/pull/7502/commits/ecda25e
33-
// TODO replace the fixed version with '+' once the github issue is resolved OR the code/tests are updated to accept the new behavior.
34-
latestDepTestImplementation group: 'org.scalameta', name: 'munit_2.13', version: '1.0.0'
31+
latestDepTestImplementation group: 'org.scalameta', name: 'munit_2.13', version: '+'
3532
}
3633

3734
compileTestGroovy {

dd-java-agent/instrumentation/junit-4.10/munit-junit-4/src/main/java/datadog/trace/instrumentation/junit4/MUnitTracingListener.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
1010
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
1111
import datadog.trace.bootstrap.instrumentation.api.InternalSpanTypes;
12+
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
1213
import datadog.trace.util.Strings;
1314
import java.lang.annotation.Annotation;
1415
import java.util.ArrayList;
@@ -149,7 +150,7 @@ public void testIgnored(final Description description) {
149150

150151
if (Strings.isNotBlank(testName)) {
151152
TestDescriptor testDescriptor = MUnitUtils.toTestDescriptor(description);
152-
if (!isTestInProgress()) {
153+
if (!isSpanInProgress(InternalSpanTypes.TEST)) {
153154
// earlier versions of MUnit (e.g. 0.7.28) trigger "testStarted" event for ignored tests,
154155
// while newer versions don't
155156
TestSuiteDescriptor suiteDescriptor = MUnitUtils.toSuiteDescriptor(description);
@@ -173,21 +174,41 @@ public void testIgnored(final Description description) {
173174

174175
} else if (testClass != null) {
175176
TestSuiteDescriptor suiteDescriptor = MUnitUtils.toSuiteDescriptor(description);
177+
178+
boolean suiteStarted = isSpanInProgress(InternalSpanTypes.TEST_SUITE_END);
179+
if (!suiteStarted) {
180+
// there is a bug in MUnit 1.0.1+: start/finish events are not fired for skipped suites
181+
List<String> categories = getCategories(description);
182+
TestEventsHandlerHolder.TEST_EVENTS_HANDLER.onTestSuiteStart(
183+
suiteDescriptor,
184+
testSuiteName,
185+
FRAMEWORK_NAME,
186+
FRAMEWORK_VERSION,
187+
testClass,
188+
categories,
189+
false,
190+
TestFrameworkInstrumentation.MUNIT);
191+
}
192+
176193
TestEventsHandlerHolder.TEST_EVENTS_HANDLER.onTestSuiteSkip(suiteDescriptor, null);
177194
for (Description child : description.getChildren()) {
178195
testCaseIgnored(child);
179196
}
197+
198+
if (!suiteStarted) {
199+
TestEventsHandlerHolder.TEST_EVENTS_HANDLER.onTestSuiteFinish(suiteDescriptor);
200+
}
180201
}
181202
}
182203

183-
private boolean isTestInProgress() {
204+
private static boolean isSpanInProgress(UTF8BytesString type) {
184205
final AgentScope scope = AgentTracer.activeScope();
185206
if (scope == null) {
186207
return false;
187208
}
188209
AgentSpan scopeSpan = scope.span();
189210
String spanType = scopeSpan.getSpanType();
190-
return spanType != null && spanType.contentEquals(InternalSpanTypes.TEST);
211+
return spanType != null && spanType.contentEquals(type);
191212
}
192213

193214
private void testCaseIgnored(final Description description) {

0 commit comments

Comments
 (0)