You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Eason09053360 (via GitHub)" <gi...@apache.org> on 2026/08/30 09:38:51 UTC

[PR] Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars [airflow]

Eason09053360 opened a new pull request, #72291:
URL: https://github.com/apache/airflow/pull/72291

   `airflow tasks test` never exported `AIRFLOW_TEST_MODE` unless `--env-vars` was also passed: the `os.environ.update()` call sat inside the `if args.env_vars:` guard, while the dict it writes was built outside it. The variable has been hostage to an unrelated flag since it was introduced in #7639 (2020).
   
   This matters more than it looks on Airflow 3: the `test_mode` task-context variable is currently commented out in `task_runner.py`, so `AIRFLOW_TEST_MODE` is the only working way for a task to tell it is running under a test command — and it is the only channel that crosses the process boundary into `BashOperator` shells and user subprocesses. The official `example_passing_params_via_test_command` Dag prints it and has been showing `None` the whole time.
   
   The `monkeypatch.delenv` calls in the test are load-bearing: `task_test` writes to the real process environment, so without clearing the keys first the `with-env-vars` case leaks `AIRFLOW_TEST_MODE=True` and the new case would pass against unfixed code — recreating the same blind spot.
   
   Dag code that reads `AIRFLOW_TEST_MODE` during `tasks test` will now see `True` where it previously saw nothing. 
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   - [X] Yes — Claude Code (Opus 5)
   
   Generated-by: Claude Code (Opus 5) following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars [airflow]

Posted by "henry3260 (via GitHub)" <gi...@apache.org>.
henry3260 merged PR #72291:
URL: https://github.com/apache/airflow/pull/72291


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars [airflow]

Posted by "Eason09053360 (via GitHub)" <gi...@apache.org>.
Eason09053360 commented on PR #72291:
URL: https://github.com/apache/airflow/pull/72291#issuecomment-5468949317

   > Looks good, LGTM
   
   Thanks for reviewing!


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #72291:
URL: https://github.com/apache/airflow/pull/72291#issuecomment-5476028539

   ### Backport successfully created: v3-3-test
   
   Note: As of [Merging PRs targeted for Airflow 3.X](https://github.com/apache/airflow/blob/main/dev/README_AIRFLOW3_DEV.md#merging-prs-targeted-for-airflow-3x)
   the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.
   
   In matter of doubt please ask in [#release-management](https://apache-airflow.slack.com/archives/C03G9H97MM2) Slack channel.
   
   <table>
           <tr>
           <th>Status</th>
           <th>Branch</th>
           <th>Result</th>
       </tr>
       <tr>
           <td>✅</td>
           <td>v3-3-test</td>
           <td><a href="https://github.com/apache/airflow/pull/72320"><img src="https://img.shields.io/badge/PR-72320-blue" alt="PR Link"></a></td>
       </tr>
   </table>


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars [airflow]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #72291:
URL: https://github.com/apache/airflow/pull/72291#issuecomment-5476019798

   Hi maintainer, this PR was merged without a milestone set.
   We've automatically set the milestone to **[Airflow 3.3.2](https://github.com/apache/airflow/milestone/150)** based on: backport label targeting v3-3-test
   If this milestone is not correct, please update it to the appropriate milestone.
   
   > This comment was generated by [Milestone Tag Assistant](https://github.com/apache/airflow/blob/main/.github/workflows/milestone-tag-assistant.yml).
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars [airflow]

Posted by "Eason09053360 (via GitHub)" <gi...@apache.org>.
Eason09053360 commented on code in PR #72291:
URL: https://github.com/apache/airflow/pull/72291#discussion_r3890297427


##########
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:
   Good catch!
   
   Applied with some adjustment:  as-written fails `without-env-vars`,
   because the example Dag prints `foo=sentinel` while that case asserts `foo=None`.
   
   Measured before/after on this test:
   
   |          | `AIRFLOW_TEST_MODE` | `foo`         |
   | -------- | ------------------- | ------------- |
   | `delenv` | leaked `True`       | leaked `bar`  |
   | `setenv` | clean               | clean         |
   
   The sentinel also strengthens the assertion — the command now has to overwrite a
   pre-existing value rather than merely populate an absent one. Regression detection
   still holds: with the fix reverted, `without-env-vars` fails in both parametrize orders.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Export AIRFLOW_TEST_MODE from airflow tasks test without --env-vars [airflow]

Posted by "ColtenOuO (via GitHub)" <gi...@apache.org>.
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org