|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import pytest |
| 4 | +from click.testing import CliRunner |
| 5 | + |
| 6 | +from cycode import __version__ |
| 7 | +from cycode.cli.commands.main_cli import main_cli |
| 8 | +from cycode.cli.commands.version.version_checker import VersionChecker |
| 9 | +from tests.conftest import CLI_ENV_VARS |
| 10 | + |
| 11 | +_NEW_LATEST_VERSION = '999.0.0' # Simulate a newer version available |
| 12 | +_UPDATE_MESSAGE_PART = 'new version of cycode available' |
| 13 | + |
| 14 | + |
| 15 | +@patch.object(VersionChecker, 'check_for_update') |
| 16 | +def test_version_check_with_json_output(mock_check_update: patch) -> None: |
| 17 | + # When output is JSON, version check should be skipped |
| 18 | + mock_check_update.return_value = _NEW_LATEST_VERSION |
| 19 | + |
| 20 | + args = ['--output', 'json', 'version'] |
| 21 | + result = CliRunner().invoke(main_cli, args, env=CLI_ENV_VARS) |
| 22 | + |
| 23 | + # Version check message should not be present in JSON output |
| 24 | + assert _UPDATE_MESSAGE_PART not in result.output.lower() |
| 25 | + mock_check_update.assert_not_called() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def mock_auth_info() -> 'patch': |
| 30 | + # Mock the authorization info to avoid API calls |
| 31 | + with patch('cycode.cli.commands.status.status_command.get_authorization_info', return_value=None) as mock: |
| 32 | + yield mock |
| 33 | + |
| 34 | + |
| 35 | +@pytest.mark.parametrize('command', ['version', 'status']) |
| 36 | +@patch.object(VersionChecker, 'check_for_update') |
| 37 | +def test_version_check_for_special_commands(mock_check_update: patch, mock_auth_info: patch, command: str) -> None: |
| 38 | + # Version and status commands should always check the version without cache |
| 39 | + mock_check_update.return_value = _NEW_LATEST_VERSION |
| 40 | + |
| 41 | + result = CliRunner().invoke(main_cli, [command], env=CLI_ENV_VARS) |
| 42 | + |
| 43 | + # Version information should be present in output |
| 44 | + assert _UPDATE_MESSAGE_PART in result.output.lower() |
| 45 | + # Version check must be called without a cache |
| 46 | + mock_check_update.assert_called_once_with(__version__, False) |
| 47 | + |
| 48 | + |
| 49 | +@patch.object(VersionChecker, 'check_for_update') |
| 50 | +def test_version_check_with_text_output(mock_check_update: patch) -> None: |
| 51 | + # Regular commands with text output should check the version using cache |
| 52 | + mock_check_update.return_value = _NEW_LATEST_VERSION |
| 53 | + |
| 54 | + args = ['version'] |
| 55 | + result = CliRunner().invoke(main_cli, args, env=CLI_ENV_VARS) |
| 56 | + |
| 57 | + # Version check message should be present in JSON output |
| 58 | + assert _UPDATE_MESSAGE_PART in result.output.lower() |
| 59 | + |
| 60 | + |
| 61 | +@patch.object(VersionChecker, 'check_for_update') |
| 62 | +def test_version_check_disabled(mock_check_update: patch) -> None: |
| 63 | + # When --no-update-notifier is used, version check should be skipped |
| 64 | + mock_check_update.return_value = _NEW_LATEST_VERSION |
| 65 | + |
| 66 | + args = ['--no-update-notifier', 'version'] |
| 67 | + result = CliRunner().invoke(main_cli, args, env=CLI_ENV_VARS) |
| 68 | + |
| 69 | + # Version check message should not be present |
| 70 | + assert _UPDATE_MESSAGE_PART not in result.output.lower() |
| 71 | + mock_check_update.assert_not_called() |
0 commit comments