From a31956f3496713a6616032d5b67358742a21133d Mon Sep 17 00:00:00 2001 From: arimu1 <19286898+arimu1@users.noreply.github.com> Date: Sat, 29 Aug 2026 09:57:57 +0700 Subject: [PATCH] fix: keep EnvironmentVariables buffer in sync during tests Always update the internal buffer when set/clear is called, including while a test statement is executing. Previously, changes made during a test only modified the live environment map, so a later clear() did not override buffered values from rule configuration and the variable could reappear when the next test started. Fixes #86 --- .../lang/system/EnvironmentVariables.java | 3 +- ...ironmentVariablesClearAcrossTestsTest.java | 82 +++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/junit/contrib/java/lang/system/EnvironmentVariablesClearAcrossTestsTest.java diff --git a/src/main/java/org/junit/contrib/java/lang/system/EnvironmentVariables.java b/src/main/java/org/junit/contrib/java/lang/system/EnvironmentVariables.java index 229cfe19..e88616dd 100644 --- a/src/main/java/org/junit/contrib/java/lang/system/EnvironmentVariables.java +++ b/src/main/java/org/junit/contrib/java/lang/system/EnvironmentVariables.java @@ -58,10 +58,9 @@ public class EnvironmentVariables implements TestRule { * @return the rule itself. */ public EnvironmentVariables set(String name, String value) { + writeVariableToBuffer(name, value); if (statementIsExecuting) writeVariableToEnvMap(name, value); - else - writeVariableToBuffer(name, value); return this; } diff --git a/src/test/java/org/junit/contrib/java/lang/system/EnvironmentVariablesClearAcrossTestsTest.java b/src/test/java/org/junit/contrib/java/lang/system/EnvironmentVariablesClearAcrossTestsTest.java new file mode 100644 index 00000000..6855ceaf --- /dev/null +++ b/src/test/java/org/junit/contrib/java/lang/system/EnvironmentVariablesClearAcrossTestsTest.java @@ -0,0 +1,82 @@ +package org.junit.contrib.java.lang.system; + +import org.junit.Before; +import org.junit.FixMethodOrder; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.runners.Enclosed; +import org.junit.runner.RunWith; +import org.junit.runners.MethodSorters; + +import static java.lang.System.getenv; +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Reproduces #86. + */ +@RunWith(Enclosed.class) +public class EnvironmentVariablesClearAcrossTestsTest { + /** + * Variables set in {@code @Before} can be cleared in one test method without + * being restored from the rule's buffer in later tests. + */ + @FixMethodOrder(MethodSorters.NAME_ASCENDING) + public static class clearInBeforeConfiguredTestClass { + @Rule + public final EnvironmentVariables environmentVariables = new EnvironmentVariables() + .set("someValue", "fromRuleInitializer"); + + @Before + public void setUp() { + environmentVariables.set("someValue", "testLink"); + } + + @Test + public void test01_usesEnvironmentVariableFromBefore() { + assertThat(getenv("someValue")).isEqualTo("testLink"); + } + + @Test + public void test02_clearsEnvironmentVariableSetInBefore() { + environmentVariables.clear("someValue"); + assertThat(getenv("someValue")).isNull(); + } + + @Test + public void test03_environmentVariableIsSetAgainAfterClearTest() { + assertThat(getenv("someValue")).isEqualTo("testLink"); + } + } + + /** + * The rule instance is shared across tests (same pattern as configuring common + * variables on the rule). Without syncing {@code clear()} to the buffer, a + * variable cleared in one test is restored from the buffer when a later test + * starts. + */ + @FixMethodOrder(MethodSorters.NAME_ASCENDING) + public static class clearedVariableIsNotRestoredFromBuffer { + private static final EnvironmentVariables SHARED_ENVIRONMENT_VARIABLES + = new EnvironmentVariables().set("someValue", "fromRuleInitializer"); + + @Rule + public final EnvironmentVariables environmentVariables = SHARED_ENVIRONMENT_VARIABLES; + + @Test + public void test01_setVariable() { + environmentVariables.set("someValue", "testLink"); + assertThat(getenv("someValue")).isEqualTo("testLink"); + } + + @Test + public void test02_clearVariable() { + environmentVariables.clear("someValue"); + assertThat(getenv("someValue")).isNull(); + } + + @Test + public void test03_clearedVariableIsNotRestoredFromBuffer() { + assertThat(getenv("someValue")).isNull(); + } + } +}