| 1 | # Copyright 2026 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | from importlib.metadata import PackageNotFoundError |
| 16 | from typer.testing import CliRunner |
| 17 | from unittest.mock import patch |
| 18 | |
| 19 | from colab_cli.cli import app |
| 20 | |
| 21 | runner = CliRunner() |
| 22 | |
| 23 | |
| 24 | def test_version_installed(): |
| 25 | with patch("colab_cli.auto_update.installed_version") as mock_version: |
| 26 | mock_version.return_value = "0.2.0" |
| 27 | result = runner.invoke(app, ["version"]) |
| 28 | assert result.exit_code == 0 |
| 29 | assert "Version: 0.2.0" in result.output |
| 30 | # Guard against a silent regression if the PyPI distribution name |
| 31 | # changes again: importlib.metadata.version() must be called with |
| 32 | # the distribution name exactly as it appears in pyproject.toml. |
| 33 | mock_version.assert_called_with("google-colab-cli") |
| 34 | |
| 35 | |
| 36 | def test_version_git_fallback(): |
| 37 | with patch("colab_cli.auto_update.installed_version") as mock_version: |
| 38 | mock_version.side_effect = PackageNotFoundError |
| 39 | |
| 40 | with patch("subprocess.check_output") as mock_git: |
| 41 | mock_git.return_value = "abc1234" |
| 42 | result = runner.invoke(app, ["version"]) |
| 43 | assert result.exit_code == 0 |
| 44 | assert "Version: abc1234" in result.output |
| 45 | |
| 46 | |
| 47 | def test_version_unknown(): |
| 48 | with patch("colab_cli.auto_update.installed_version") as mock_version: |
| 49 | mock_version.side_effect = PackageNotFoundError |
| 50 | |
| 51 | with patch("subprocess.check_output") as mock_git: |
| 52 | mock_git.side_effect = Exception("git not found") |
| 53 | result = runner.invoke(app, ["version"]) |
| 54 | assert result.exit_code == 0 |
| 55 | assert "Version: unknown" in result.output |