This is an automated email from the ASF dual-hosted git repository.

henry3260 pushed a commit to branch v3-3-test
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/v3-3-test by this push:
     new f543ffdd479 [v3-3-test] Export AIRFLOW_TEST_MODE from airflow tasks 
test without --env-vars (#72291) (#72320)
f543ffdd479 is described below

commit f543ffdd479637c30bdce985583e1657d5c5fc18
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Mon Aug 31 19:24:27 2026 +0800

    [v3-3-test] Export AIRFLOW_TEST_MODE from airflow tasks test without 
--env-vars (#72291) (#72320)
    
    * Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars
    
    The test-mode signal has been hostage to an unrelated flag since it was
    introduced in 2020, so Dag code that branches on it never saw it during a
    plain `airflow tasks test` run. In Airflow 3 this is the only working
    test-mode signal, because the `test_mode` task-context variable is
    currently disabled.
    
    * Stop the env-vars test leaking its writes into the pytest session
    
    monkeypatch.delenv records an undo entry only when the key is already set,
    so on a clean worker the values task_test writes to the real process
    environment survived teardown. Seeding a sentinel instead also tightens the
    assertion: the command now has to overwrite a pre-existing value rather than
    merely populate an absent one.
    
    ---------
    (cherry picked from commit 6402213dc52540cce6094f9a300d1145d7460000)
    
    Co-authored-by: Y-C <[email protected]>
    Co-authored-by: Eason09053360 
<[email protected]>
---
 airflow-core/src/airflow/cli/commands/task_command.py  |  2 +-
 .../tests/unit/cli/commands/test_task_command.py       | 18 ++++++++++++++----
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/airflow-core/src/airflow/cli/commands/task_command.py 
b/airflow-core/src/airflow/cli/commands/task_command.py
index af944526184..ca9b786ba70 100644
--- a/airflow-core/src/airflow/cli/commands/task_command.py
+++ b/airflow-core/src/airflow/cli/commands/task_command.py
@@ -413,7 +413,7 @@ def task_test(args, dag: DAG | None = None) -> None:
     env_vars = {"AIRFLOW_TEST_MODE": "True"}
     if args.env_vars:
         env_vars.update(args.env_vars)
-        os.environ.update(env_vars)
+    os.environ.update(env_vars)
 
     if dag:
         sdk_dag = dag
diff --git a/airflow-core/tests/unit/cli/commands/test_task_command.py 
b/airflow-core/tests/unit/cli/commands/test_task_command.py
index c99e82ca21d..00584f05e37 100644
--- a/airflow-core/tests/unit/cli/commands/test_task_command.py
+++ b/airflow-core/tests/unit/cli/commands/test_task_command.py
@@ -215,7 +215,18 @@ class TestCliTasks:
             )
         )
 
-    def test_cli_test_with_env_vars(self):
+    @pytest.mark.parametrize(
+        ("env_var_args", "expected_foo"),
+        [
+            pytest.param([], "foo=sentinel", 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):
+        # setenv (unlike delenv) always records an undo entry, so task_test's 
writes to the real
+        # process environment cannot leak out; the sentinel proves the command 
overwrote the key.
+        monkeypatch.setenv("AIRFLOW_TEST_MODE", "sentinel")
+        monkeypatch.setenv("foo", "sentinel")
         with redirect_stdout(io.StringIO()) as stdout:
             task_command.task_test(
                 self.parser.parse_args(
@@ -225,13 +236,12 @@ class TestCliTasks:
                         "example_passing_params_via_test_command",
                         "env_var_test_task",
                         DEFAULT_DATE.isoformat(),
-                        "--env-vars",
-                        '{"foo":"bar"}',
+                        *env_var_args,
                     ]
                 )
             )
         output = stdout.getvalue()
-        assert "foo=bar" in output
+        assert expected_foo in output
         assert "AIRFLOW_TEST_MODE=True" in output
 
     @mock.patch(

Reply via email to