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 1b64dcb91ee [v3-3-test] Fix DAG.cli() crashing on dags test 
--show-dagrun (#72810) (#73216)
1b64dcb91ee is described below

commit 1b64dcb91ee6bd8317f66d920e0b7ff3bf368089
Author: github-actions[bot] 
<41898282+github-actions[bot]@users.noreply.github.com>
AuthorDate: Wed Sep 16 03:45:21 2026 +0800

    [v3-3-test] Fix DAG.cli() crashing on dags test --show-dagrun (#72810) 
(#73216)
    
    * Fix DAG.cli() crashing on dags test --show-dagrun
    
    A Dag file run as a script goes through DAG.cli(), whose parser drops the
    dag_id positional and passes the Dag object to the handler instead. dag_test
    never needed dag_id from the parsed arguments until it rendered the run: the
    task instance query read args.dag_id, so --show-dagrun, --save-dagrun and
    --imgcat-dagrun raised AttributeError after the Dag had already run, while
    the plain command and the regular airflow dags test path worked.
    
    Filtering on the resolved Dag's id is the same value on the regular path and
    the only one available from DAG.cli().
    
    * Update airflow-core/tests/unit/cli/commands/test_dag_command.py
    
    
    
    * Update airflow-core/tests/unit/cli/commands/test_dag_command.py
    
    ---------
    (cherry picked from commit 63fc32fd14bbbb20abfa60301abcd2f33d49726f)
    
    Co-authored-by: Y-C <[email protected]>
    Co-authored-by: Eason09053360 
<[email protected]>
    Co-authored-by: rjgoyln <[email protected]>
    Co-authored-by: Henry Chen <[email protected]>
---
 .../src/airflow/cli/commands/dag_command.py         |  2 +-
 .../tests/unit/cli/commands/test_dag_command.py     | 21 ++++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/airflow-core/src/airflow/cli/commands/dag_command.py 
b/airflow-core/src/airflow/cli/commands/dag_command.py
index c31b989d1e6..1d5f7bba099 100644
--- a/airflow-core/src/airflow/cli/commands/dag_command.py
+++ b/airflow-core/src/airflow/cli/commands/dag_command.py
@@ -849,7 +849,7 @@ def dag_test(args, dag: DAG | None = None, *, session: 
Session = NEW_SESSION) ->
     if show_dagrun or imgcat or filename:
         tis = session.scalars(
             select(TaskInstance).where(
-                TaskInstance.dag_id == args.dag_id,
+                TaskInstance.dag_id == dag.dag_id,
                 TaskInstance.run_id == dr.run_id,
             )
         ).all()
diff --git a/airflow-core/tests/unit/cli/commands/test_dag_command.py 
b/airflow-core/tests/unit/cli/commands/test_dag_command.py
index db7bf2d14ad..d9020a4601d 100644
--- a/airflow-core/tests/unit/cli/commands/test_dag_command.py
+++ b/airflow-core/tests/unit/cli/commands/test_dag_command.py
@@ -942,9 +942,10 @@ class TestCliDags:
         )
 
     @mock.patch("airflow.cli.commands.dag_command.render_dag", 
return_value=MagicMock(source="SOURCE"))
-    @mock.patch("airflow.cli.commands.dag_command.get_bagged_dag")
+    @mock.patch("airflow.cli.commands.dag_command.get_bagged_dag", 
autospec=True)
     def test_dag_test_show_dag(self, mock_get_dag, mock_render_dag, 
stdout_capture):
         mock_get_dag.return_value.test.return_value.run_id = 
"__test_dag_test_show_dag_fake_dag_run_run_id__"
+        mock_get_dag.return_value.dag_id = "example_bash_operator"
 
         cli_args = self.parser.parse_args(
             ["dags", "test", "example_bash_operator", 
DEFAULT_DATE.isoformat(), "--show-dagrun"]
@@ -969,6 +970,24 @@ class TestCliDags:
         mock_render_dag.assert_has_calls([mock.call(mock_get_dag.return_value, 
tis=[])])
         assert "SOURCE" in output
 
+    @mock.patch("airflow.cli.commands.dag_command.render_dag", autospec=True)
+    @mock.patch.object(DAG, "test", autospec=True)
+    def test_dag_test_show_dag_from_dag_cli(self, mock_test, mock_render_dag, 
dag_maker, stdout_capture):
+        """``DAG.cli()`` passes the Dag positionally and its parser drops 
``dag_id``."""
+        with dag_maker("dag_cli_show_dagrun", schedule=None) as dag:
+            EmptyOperator(task_id="only_task")
+        mock_test.return_value = dag_maker.create_dagrun(run_id="dag_cli_run")
+
+        parser = cli_parser.get_parser(dag_parser=True)
+        with stdout_capture:
+            dag_command.dag_test(parser.parse_args(["dags", "test", 
"--show-dagrun"]), dag)
+
+        mock_render_dag.assert_called_once()
+        assert mock_render_dag.call_args.args[0] is dag
+        assert [(ti.dag_id, ti.task_id, ti.run_id) for ti in 
mock_render_dag.call_args.kwargs["tis"]] == [
+            ("dag_cli_show_dagrun", "only_task", "dag_cli_run")
+        ]
+
     @mock.patch("airflow.dag_processing.dagbag.BundleDagBag")
     def test_dag_test_with_bundle_name(self, mock_dagbag, 
configure_dag_bundles):
         """Test that DAG can be tested using bundle name."""

Reply via email to