Skip to content

Commit 77f3610

Browse files
committed
cover check_latest_version_on_close with tests
1 parent 195d8bd commit 77f3610

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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()

tests/cli/commands/version/test_version_checker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def test_should_check_update_prerelease_daily(self, version_checker_cached: 'Ver
8787
# Edge cases
8888
('1.0.0dev1', '1.0.0', '1.0.0'), # Pre-release to same version stable
8989
('2.0.0', '2.0.0dev1', None), # Stable to same version pre-release
90-
('2.2.1.dev4', '2.2.0', None) # Pre-release to lower stable
90+
('2.2.1.dev4', '2.2.0', None), # Pre-release to lower stable
9191
],
9292
)
9393
def test_check_for_update_scenarios(

0 commit comments

Comments
 (0)