Skip to content

Commit 195d8bd

Browse files
committed
fix check_and_notify_update without cache usage
1 parent b32b2de commit 195d8bd

File tree

3 files changed

+12
-15
lines changed

3 files changed

+12
-15
lines changed

cycode/cli/commands/main_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def check_latest_version_on_close(context: click.Context) -> None:
111111
return
112112

113113
# we always want to check the latest version for "version" and "status" commands
114-
bypass_version_check_cache = context.invoked_subcommand in {'version', 'status'}
114+
should_use_cache = context.invoked_subcommand not in {'version', 'status'}
115115
version_checker.check_and_notify_update(
116-
current_version=__version__, use_color=context.color, bypass_cache=bypass_version_check_cache
116+
current_version=__version__, use_color=context.color, use_cache=should_use_cache
117117
)

cycode/cli/commands/version/version_checker.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,14 @@ def _update_last_check(self) -> None:
151151
except IOError:
152152
pass
153153

154-
def check_for_update(self, current_version: str) -> Optional[str]:
154+
def check_for_update(self, current_version: str, use_cache: bool = True) -> Optional[str]:
155155
"""Check if an update is available for the current version.
156156
157-
Respects the update check frequency (daily/weekly) based on version type
157+
Respects the update check frequency (daily/weekly) based on the version type
158158
159159
Args:
160160
current_version: The current version string of the CLI
161+
use_cache: If True, use the cached timestamp to determine if an update check is needed
161162
162163
Returns:
163164
str | None: The latest version string if an update is recommended,
@@ -166,20 +167,20 @@ def check_for_update(self, current_version: str) -> Optional[str]:
166167
current_parts, current_is_pre = self._parse_version(current_version)
167168

168169
# Check if we should perform the update check based on frequency
169-
if not self._should_check_update(current_is_pre):
170+
if use_cache and not self._should_check_update(current_is_pre):
170171
return None
171172

172173
latest_version = self.get_latest_version()
173174
if not latest_version:
174175
return None
175176

176177
# Update the last check timestamp
177-
self._update_last_check()
178+
use_cache and self._update_last_check()
178179

179180
latest_parts, latest_is_pre = self._parse_version(latest_version)
180181
return _compare_versions(current_parts, latest_parts, current_is_pre, latest_is_pre, latest_version)
181182

182-
def check_and_notify_update(self, current_version: str, use_color: bool = True, bypass_cache: bool = False) -> None:
183+
def check_and_notify_update(self, current_version: str, use_color: bool = True, use_cache: bool = True) -> None:
183184
"""Check for updates and display a notification if a new version is available.
184185
185186
Performs the version check and displays a formatted message with update instructions
@@ -191,15 +192,10 @@ def check_and_notify_update(self, current_version: str, use_color: bool = True,
191192
Args:
192193
current_version: Current version of the CLI
193194
use_color: If True, use colored output in the terminal
194-
bypass_cache: If True, ignore the cache and check for updates immediately
195+
use_cache: If True, use the cached timestamp to determine if an update check is needed
195196
"""
196-
if bypass_cache:
197-
latest_version = self.get_latest_version()
198-
should_update = latest_version and latest_version != current_version
199-
else:
200-
latest_version = self.check_for_update(current_version)
201-
should_update = bool(latest_version)
202-
197+
latest_version = self.check_for_update(current_version, use_cache)
198+
should_update = bool(latest_version)
203199
if should_update:
204200
update_message = (
205201
'\nNew version of cycode available! '

tests/cli/commands/version/test_version_checker.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +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
9091
],
9192
)
9293
def test_check_for_update_scenarios(

0 commit comments

Comments
 (0)