Skip to content

Commit 6402213

Browse files
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 <185830721+Eason09053360@users.noreply.github.com>
1 parent b072197 commit 6402213

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

airflow-core/src/airflow/cli/commands/task_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ def task_test(args, dag: DAG | None = None) -> None:
417417
env_vars = {"AIRFLOW_TEST_MODE": "True"}
418418
if args.env_vars:
419419
env_vars.update(args.env_vars)
420-
os.environ.update(env_vars)
420+
os.environ.update(env_vars)
421421

422422
if dag:
423423
sdk_dag = dag

airflow-core/tests/unit/cli/commands/test_task_command.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,18 @@ def test_cli_test_with_params(self):
215215
)
216216
)
217217

218-
def test_cli_test_with_env_vars(self):
218+
@pytest.mark.parametrize(
219+
("env_var_args", "expected_foo"),
220+
[
221+
pytest.param([], "foo=sentinel", id="without-env-vars"),
222+
pytest.param(["--env-vars", '{"foo":"bar"}'], "foo=bar", id="with-env-vars"),
223+
],
224+
)
225+
def test_cli_test_with_env_vars(self, monkeypatch, env_var_args, expected_foo):
226+
# setenv (unlike delenv) always records an undo entry, so task_test's writes to the real
227+
# process environment cannot leak out; the sentinel proves the command overwrote the key.
228+
monkeypatch.setenv("AIRFLOW_TEST_MODE", "sentinel")
229+
monkeypatch.setenv("foo", "sentinel")
219230
with redirect_stdout(io.StringIO()) as stdout:
220231
task_command.task_test(
221232
self.parser.parse_args(
@@ -225,13 +236,12 @@ def test_cli_test_with_env_vars(self):
225236
"example_passing_params_via_test_command",
226237
"env_var_test_task",
227238
DEFAULT_DATE.isoformat(),
228-
"--env-vars",
229-
'{"foo":"bar"}',
239+
*env_var_args,
230240
]
231241
)
232242
)
233243
output = stdout.getvalue()
234-
assert "foo=bar" in output
244+
assert expected_foo in output
235245
assert "AIRFLOW_TEST_MODE=True" in output
236246

237247
@mock.patch(

0 commit comments

Comments
 (0)