Skip to content

Commit 0d8066b

Browse files
mhlidddevflow.devflow-routing-intake
andauthored
Update JUnit Tests to run with STRICT_TEST mode by default (#11045)
updating config helper to have descriptive exception in strict mode init fixing test_base issues and adding drain to confighelpre suppressforbidden move ConfigInversionExtension to config-utils simplifying extension Merge branch 'master' into mhlidd/junit_strict_mode Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent 70b27c7 commit 0d8066b

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

buildSrc/src/main/kotlin/dd-trace-java.configure-tests.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ tasks.withType<Test>().configureEach {
4646
// Use a task-specific user prefs directory
4747
systemProperty("java.util.prefs.userRoot", layout.buildDirectory.dir("tmp/userPrefs/$name").get().asFile.absolutePath)
4848

49+
// Enable JUnit 5 auto-detection so ConfigInversionExtension (STRICT mode) is loaded automatically
50+
systemProperty("junit.jupiter.extensions.autodetection.enabled", "true")
51+
4952
// Split up tests that want to run forked in their own separate JVM for generated tasks
5053
if (name.startsWith("forkedTest") || name.endsWith("ForkedTest")) {
5154
setExcludes(emptyList())

utils/config-utils/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
plugins {
22
`java-library`
3+
`java-test-fixtures`
34
id("dd-trace-java.supported-config-generator")
45
}
56

@@ -60,6 +61,8 @@ dependencies {
6061
implementation(libs.slf4j)
6162
implementation("org.snakeyaml", "snakeyaml-engine", "2.9")
6263

64+
testFixturesImplementation(libs.junit.jupiter)
65+
6366
testImplementation(project(":utils:test-utils"))
6467
testImplementation("org.snakeyaml:snakeyaml-engine:2.9")
6568
testImplementation("com.squareup.okhttp3:mockwebserver:${libs.versions.okhttp.legacy.get()}")

utils/config-utils/src/test/java/datadog/trace/config/inversion/ConfigHelperTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
67

78
import datadog.trace.test.util.ControllableEnvironmentVariables;
89
import java.util.ArrayList;
@@ -167,6 +168,14 @@ void testInconsistentAliasesAndAliasMapping() {
167168
assertFalse(result.containsKey(NEW_ALIAS_TARGET));
168169
}
169170

171+
@Test
172+
void testStrictTestThrowsForUnsupportedConfig() {
173+
env.set("DD_FAKE_VAR", "banana");
174+
175+
// STRICT_TEST mode should throw for unsupported DD_ variables
176+
assertThrows(IllegalArgumentException.class, () -> ConfigHelper.env("DD_FAKE_VAR"));
177+
}
178+
170179
@Test
171180
void testUnsupportedEnvWarningNotInTestMode() {
172181
ConfigHelper.get().setConfigInversionStrict(ConfigHelper.StrictnessPolicy.TEST);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package datadog.trace.test;
2+
3+
import datadog.trace.config.inversion.ConfigHelper;
4+
import datadog.trace.config.inversion.ConfigHelper.StrictnessPolicy;
5+
import java.util.List;
6+
import org.junit.jupiter.api.extension.AfterAllCallback;
7+
import org.junit.jupiter.api.extension.BeforeAllCallback;
8+
import org.junit.jupiter.api.extension.ExtensionContext;
9+
10+
/**
11+
* JUnit 5 extension that enforces {@link StrictnessPolicy#STRICT_TEST} mode on {@link ConfigHelper}
12+
* during test execution. Any access to an unsupported configuration key will throw {@link
13+
* IllegalArgumentException} and be collected for assertion in {@link #afterAll}.
14+
*
15+
* <p>Registered via {@code META-INF/services/org.junit.jupiter.api.extension.Extension} for
16+
* automatic discovery. Requires {@code junit.jupiter.extensions.autodetection.enabled=true}.
17+
*/
18+
public class ConfigInversionExtension implements BeforeAllCallback, AfterAllCallback {
19+
20+
private StrictnessPolicy previousPolicy;
21+
22+
@Override
23+
public void beforeAll(ExtensionContext ctx) {
24+
previousPolicy = ConfigHelper.get().configInversionStrictFlag();
25+
ConfigHelper.get().setConfigInversionStrict(StrictnessPolicy.STRICT_TEST);
26+
}
27+
28+
@Override
29+
public void afterAll(ExtensionContext ctx) {
30+
List<String> unsupported = ConfigHelper.get().drainUnsupportedConfigs();
31+
ConfigHelper.get().setConfigInversionStrict(previousPolicy);
32+
33+
if (!unsupported.isEmpty()) {
34+
throw new AssertionError(
35+
"Unsupported configurations found during test. "
36+
+ "Add these to metadata/supported-configurations.json or opt out with StrictnessPolicy.TEST:\n "
37+
+ String.join("\n ", unsupported));
38+
}
39+
}
40+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
datadog.trace.test.ConfigInversionExtension

0 commit comments

Comments
 (0)