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

potiuk 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 0772b1ebac4 bugfix(airflowctl): handle version command exception and 
make tests unified (#53553)
0772b1ebac4 is described below

commit 0772b1ebac49f34bef05eb45349046d5793e9f33
Author: Bugra Ozturk <[email protected]>
AuthorDate: Mon Jul 21 00:13:38 2025 +0300

    bugfix(airflowctl): handle version command exception and make tests unified 
(#53553)
    
    * fix(airflowctl): handle version command exception and make tests similar 
to all implementation
    
    * fix(airflowctl): update assignment logic
    
    * fix(airflowctl): return non-zero exit code for failures after printing
---
 airflow-ctl/src/airflowctl/ctl/cli_config.py       |  5 +++++
 .../src/airflowctl/ctl/commands/version_command.py | 15 +++++++++----
 .../ctl/commands/test_version_command.py           | 26 ++++++++++++++++------
 3 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/airflow-ctl/src/airflowctl/ctl/cli_config.py 
b/airflow-ctl/src/airflowctl/ctl/cli_config.py
index fa8c736cb5f..0d22aceacf9 100644
--- a/airflow-ctl/src/airflowctl/ctl/cli_config.py
+++ b/airflow-ctl/src/airflowctl/ctl/cli_config.py
@@ -62,14 +62,19 @@ def lazy_load_command(import_path: str) -> Callable:
 
 
 def safe_call_command(function: Callable, args: Iterable[Arg]) -> None:
+    import sys
+
     try:
         function(args)
     except AirflowCtlCredentialNotFoundException as e:
         rich.print(f"command failed due to {e}")
+        sys.exit(1)
     except AirflowCtlConnectionException as e:
         rich.print(f"command failed due to {e}")
+        sys.exit(1)
     except AirflowCtlNotFoundException as e:
         rich.print(f"command failed due to {e}")
+        sys.exit(1)
 
 
 class DefaultHelpParser(argparse.ArgumentParser):
diff --git a/airflow-ctl/src/airflowctl/ctl/commands/version_command.py 
b/airflow-ctl/src/airflowctl/ctl/commands/version_command.py
index 2c8e103c937..8eae1fcea39 100644
--- a/airflow-ctl/src/airflowctl/ctl/commands/version_command.py
+++ b/airflow-ctl/src/airflowctl/ctl/commands/version_command.py
@@ -16,6 +16,8 @@
 # under the License.
 from __future__ import annotations
 
+import sys
+
 import rich
 
 from airflowctl import __version__ as airflowctl_version
@@ -25,7 +27,12 @@ from airflowctl.api.client import NEW_API_CLIENT, 
ClientKind, provide_api_client
 @provide_api_client(kind=ClientKind.CLI)
 def version_info(arg, api_client=NEW_API_CLIENT):
     """Get version information."""
-    version_response = api_client.version.get()
-    version_dict = version_response.model_dump()
-    version_dict["airflowctl_version"] = airflowctl_version
-    rich.print(version_dict)
+    version_dict = {"airflowctl_version": airflowctl_version}
+    try:
+        version_response = api_client.version.get()
+        version_dict.update(version_response.model_dump())
+        rich.print(version_dict)
+    except Exception as e:
+        rich.print(f"[red]Error fetching version information: {e}[/red]")
+        rich.print(version_dict)
+        sys.exit(1)
diff --git a/airflow-ctl/tests/airflow_ctl/ctl/commands/test_version_command.py 
b/airflow-ctl/tests/airflow_ctl/ctl/commands/test_version_command.py
index 389348b4ec4..b5deacabb1b 100644
--- a/airflow-ctl/tests/airflow_ctl/ctl/commands/test_version_command.py
+++ b/airflow-ctl/tests/airflow_ctl/ctl/commands/test_version_command.py
@@ -43,12 +43,24 @@ def mock_client():
     return client
 
 
-parser = cli_parser.get_parser()
+class TestVersionCommand:
+    """Test the version command."""
 
+    parser = cli_parser.get_parser()
 
-def test_ctl_version(mock_client):
-    with redirect_stdout(StringIO()) as stdout:
-        version_info(parser.parse_args(["version"]), api_client=mock_client)
-        assert "version" in stdout.getvalue()
-        assert "git_version" in stdout.getvalue()
-        assert "airflowctl_version" in stdout.getvalue()
+    def test_ctl_version(self, mock_client):
+        with redirect_stdout(StringIO()) as stdout:
+            version_info(self.parser.parse_args(["version"]), 
api_client=mock_client)
+            assert "version" in stdout.getvalue()
+            assert "git_version" in stdout.getvalue()
+            assert "airflowctl_version" in stdout.getvalue()
+
+    def test_ctl_version_exception(self, mock_client):
+        """Test the version command with an exception."""
+        mock_client.version.get.side_effect = Exception("Test exception")
+        with redirect_stdout(StringIO()) as stdout:
+            with pytest.raises(SystemExit):
+                version_info(self.parser.parse_args(["version"]), 
api_client=mock_client)
+            output = stdout.getvalue()
+        assert "Error fetching version information: Test exception" in output
+        assert "airflowctl_version" in output

Reply via email to