ColtenOuO commented on code in PR #72291:
URL: https://github.com/apache/airflow/pull/72291#discussion_r3889599798


##########
airflow-core/tests/unit/cli/commands/test_task_command.py:
##########
@@ -215,7 +215,18 @@ def test_cli_test_with_params(self):
             )
         )
 
-    def test_cli_test_with_env_vars(self):
+    @pytest.mark.parametrize(
+        ("env_var_args", "expected_foo"),
+        [
+            pytest.param([], "foo=None", id="without-env-vars"),
+            pytest.param(["--env-vars", '{"foo":"bar"}'], "foo=bar", 
id="with-env-vars"),
+        ],
+    )
+    def test_cli_test_with_env_vars(self, monkeypatch, env_var_args, 
expected_foo):
+        # task_test writes both keys into the real process environment and 
never restores them;
+        # clear them so this case sees only what this invocation exported.
+        monkeypatch.delenv("AIRFLOW_TEST_MODE", raising=False)
+        monkeypatch.delenv("foo", raising=False)

Review Comment:
   ```suggestion
           monkeypatch.setenv("AIRFLOW_TEST_MODE", "sentinel")
           monkeypatch.setenv("foo", "sentinel")
   ```
   
   `delenv(name, raising=False)` only records an undo entry if `name` is 
already present in `os.environ` at the time it's called. In this parametrized 
test:
   
   - `without-env-vars` runs first. `AIRFLOW_TEST_MODE` isn't set yet, so 
`delenv` has nothing to undo. `task_test()` then sets `AIRFLOW_TEST_MODE=True` 
directly on `os.environ`. At teardown, `monkeypatch` has no recorded entry for 
that key, so it's never unset -- it leaks into every subsequent test in this 
worker process.
   - `with-env-vars` runs next, sees the leaked `AIRFLOW_TEST_MODE=True`, so 
this time `delenv` does record an undo (restoring it back to `"True"` -- a 
no-op). But `foo` is new here, so the same gap applies: `foo=bar` leaks 
permanently too.
   
   Suggest swapping to `monkeypatch.setenv(name, <sentinel>)` before invoking 
`task_test`
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to