-
Notifications
You must be signed in to change notification settings - Fork 1
add: background upgrade hint at startup (rate-limited, non-blocking) #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| """Tests for background_update_hint() in solvent/upgrade.py.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import sys | ||
| import threading | ||
| import time | ||
| from io import StringIO | ||
| from pathlib import Path | ||
| from unittest import mock | ||
|
|
||
| import pytest | ||
|
|
||
| from solvent.upgrade import background_update_hint | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Helpers | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def _join_threads(timeout: float = 2.0) -> None: | ||
| """Wait for all non-main daemon threads to finish.""" | ||
| for t in threading.enumerate(): | ||
| if t is not threading.main_thread(): | ||
| t.join(timeout=timeout) | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Env-var guard | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_env_var_suppresses_hint(tmp_path: Path) -> None: | ||
| """SOLVENT_NO_UPDATE_CHECK=1 must prevent any thread from starting.""" | ||
| with mock.patch.dict("os.environ", {"SOLVENT_NO_UPDATE_CHECK": "1"}): | ||
| with mock.patch("solvent.upgrade.latest_pypi_version") as mock_pypi: | ||
| background_update_hint() | ||
| _join_threads() | ||
| mock_pypi.assert_not_called() | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Happy-path: outdated version prints to stderr | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_prints_hint_when_outdated(tmp_path: Path) -> None: | ||
| with mock.patch.dict( | ||
| "os.environ", | ||
| {"SOLVENT_NO_UPDATE_CHECK": "", "SOLVENT_HOME": str(tmp_path)}, | ||
| clear=False, | ||
| ): | ||
| with mock.patch("solvent.upgrade.latest_pypi_version", return_value="999.0.0"): | ||
| with mock.patch("solvent.upgrade.current_version", return_value="0.1.0"): | ||
| with mock.patch("solvent.paths.data_dir", return_value=tmp_path): | ||
| stderr = StringIO() | ||
| with mock.patch("sys.stderr", stderr): | ||
| background_update_hint() | ||
| _join_threads() | ||
| output = stderr.getvalue() | ||
| assert "999.0.0" in output | ||
| assert "0.1.0" in output | ||
| assert "pip install" in output | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Up-to-date: no output | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_no_hint_when_up_to_date(tmp_path: Path) -> None: | ||
| with mock.patch.dict("os.environ", {"SOLVENT_NO_UPDATE_CHECK": ""}, clear=False): | ||
| with mock.patch("solvent.upgrade.latest_pypi_version", return_value="0.1.0"): | ||
| with mock.patch("solvent.upgrade.current_version", return_value="0.1.0"): | ||
| with mock.patch("solvent.paths.data_dir", return_value=tmp_path): | ||
| stderr = StringIO() | ||
| with mock.patch("sys.stderr", stderr): | ||
| background_update_hint() | ||
| _join_threads() | ||
| output = stderr.getvalue() | ||
| assert output == "" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # PyPI unreachable: no crash, no output | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_no_hint_when_pypi_unreachable(tmp_path: Path) -> None: | ||
| with mock.patch.dict("os.environ", {"SOLVENT_NO_UPDATE_CHECK": ""}, clear=False): | ||
| with mock.patch("solvent.upgrade.latest_pypi_version", return_value=None): | ||
| with mock.patch("solvent.paths.data_dir", return_value=tmp_path): | ||
| stderr = StringIO() | ||
| with mock.patch("sys.stderr", stderr): | ||
| background_update_hint() | ||
| _join_threads() | ||
| output = stderr.getvalue() | ||
| assert output == "" | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Rate-limiting: second call within interval is suppressed | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_rate_limit_suppresses_second_call(tmp_path: Path) -> None: | ||
| stamp = tmp_path / ".upgrade_check" | ||
| stamp.write_text(str(time.time())) # pretend we checked just now | ||
|
|
||
| with mock.patch.dict("os.environ", {"SOLVENT_NO_UPDATE_CHECK": ""}, clear=False): | ||
| with mock.patch("solvent.upgrade.latest_pypi_version") as mock_pypi: | ||
| with mock.patch("solvent.paths.data_dir", return_value=tmp_path): | ||
| background_update_hint() | ||
| _join_threads() | ||
| mock_pypi.assert_not_called() | ||
|
|
||
|
|
||
| def test_rate_limit_allows_call_after_interval(tmp_path: Path) -> None: | ||
| stamp = tmp_path / ".upgrade_check" | ||
| old_ts = time.time() - 90000 # 25 hours ago | ||
| stamp.write_text(str(old_ts)) | ||
|
|
||
| with mock.patch.dict("os.environ", {"SOLVENT_NO_UPDATE_CHECK": ""}, clear=False): | ||
| with mock.patch("solvent.upgrade.latest_pypi_version", return_value="999.0.0"): | ||
| with mock.patch("solvent.upgrade.current_version", return_value="0.1.0"): | ||
| with mock.patch("solvent.paths.data_dir", return_value=tmp_path): | ||
| stderr = StringIO() | ||
| with mock.patch("sys.stderr", stderr): | ||
| background_update_hint() | ||
| _join_threads() | ||
| output = stderr.getvalue() | ||
| assert "999.0.0" in output | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Stamp file is written after a successful check | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_stamp_written_after_check(tmp_path: Path) -> None: | ||
| stamp = tmp_path / ".upgrade_check" | ||
| assert not stamp.exists() | ||
|
|
||
| with mock.patch.dict("os.environ", {"SOLVENT_NO_UPDATE_CHECK": ""}, clear=False): | ||
| with mock.patch("solvent.upgrade.latest_pypi_version", return_value="0.1.0"): | ||
| with mock.patch("solvent.upgrade.current_version", return_value="0.1.0"): | ||
| with mock.patch("solvent.paths.data_dir", return_value=tmp_path): | ||
| background_update_hint() | ||
| _join_threads() | ||
|
|
||
| assert stamp.exists() | ||
| ts = float(stamp.read_text()) | ||
| assert abs(ts - time.time()) < 5 | ||
|
|
||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Exceptions inside thread must not propagate | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| def test_exception_in_thread_does_not_raise(tmp_path: Path) -> None: | ||
| with mock.patch.dict("os.environ", {"SOLVENT_NO_UPDATE_CHECK": ""}, clear=False): | ||
| with mock.patch("solvent.paths.data_dir", side_effect=RuntimeError("boom")): | ||
| background_update_hint() | ||
| _join_threads() # no exception should surface here |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In source checkouts,
solvent.paths.data_dir()resolves to<repo>/data, and.gitignoredoes not ignoredata/.upgrade_check, so this write makes the repository dirty just by running the no-subcommand CLI path (I also hit it when the routing test exercisedentry.main()). Store the rate-limit stamp in an already-ignored/cache location or add this file to the ignore rules so normal CLI/test usage does not leave untracked files behind.Useful? React with 👍 / 👎.