This is an automated email from the ASF dual-hosted git repository.
henry3260 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git
The following commit(s) were added to refs/heads/main by this push:
new 6402213dc52 Export AIRFLOW_TEST_MODE from airflow tasks test without
--env-vars (#72291)
6402213dc52 is described below
commit 6402213dc52540cce6094f9a300d1145d7460000
Author: Y-C <[email protected]>
AuthorDate: Mon Aug 31 16:49:32 2026 +0800
Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars (#72291)
* 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.
---------
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 8d27e58a885..f44c1576ed4 100644
--- a/airflow-core/src/airflow/cli/commands/task_command.py
+++ b/airflow-core/src/airflow/cli/commands/task_command.py
@@ -417,7 +417,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(