Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mycli/packages/special/favoritequeries.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,10 @@ def reload(self) -> None:
raise FavoriteQueryReloadError(f'invalid [{self.section_name}] section in system configuration files')
queries: dict[str, str] = {}
if self.shared_favorites_file is not None:
queries.update(self._reload_queries(self.shared_favorites_file, 'shared favorites'))
try:
queries.update(self._reload_queries(self.shared_favorites_file, 'shared favorites'))
except FavoriteQueryReloadError as exc:
log(logger, logging.WARNING, str(exc))
queries.update(system_queries)
queries.update(user_queries)
self.config[self.section_name] = queries
Expand Down
54 changes: 44 additions & 10 deletions test/pytests/test_favoritequeries.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,24 +220,41 @@ def test_reload_keeps_startup_shared_favorites_path(tmp_path: Path) -> None:
assert favorites.get('local') == 'select 10'


@pytest.mark.parametrize('broken_source', ['user', 'shared'])
def test_reload_failure_preserves_runtime_favorites(tmp_path: Path, broken_source: str) -> None:
def test_reload_user_failure_preserves_runtime_favorites(tmp_path: Path) -> None:
user_file = tmp_path / 'myclirc'
shared_file = tmp_path / 'shared-myclirc'
user_file.write_text('[favorite_queries]\nlocal = select 1\n', encoding='utf-8')
shared_file.write_text('[favorite_queries]\nshared = select 2\n', encoding='utf-8')
config = DummyConfig({'favorite_queries': {'runtime': 'select 3'}})
favorites = FavoriteQueries.from_config(config, str(user_file), str(shared_file))
before_reload = dict(config['favorite_queries'])
broken_file = user_file if broken_source == 'user' else shared_file
broken_file.write_text('[favorite_queries\ninvalid = select 4\n', encoding='utf-8')
user_file.write_text('[favorite_queries\ninvalid = select 4\n', encoding='utf-8')

with pytest.raises(FavoriteQueryReloadError, match=f'unable to read {broken_source}'):
with pytest.raises(FavoriteQueryReloadError, match='unable to read user'):
favorites.reload()

assert config['favorite_queries'] == before_reload


def test_reload_shared_failure_warns_and_uses_user_favorites(
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
user_file = tmp_path / 'myclirc'
shared_file = tmp_path / 'shared-myclirc'
user_file.write_text('[favorite_queries]\nlocal = select 1\n', encoding='utf-8')
shared_file.write_text('[favorite_queries]\nshared = select 2\n', encoding='utf-8')
config = DummyConfig({'favorite_queries': {'runtime': 'select 3'}})
favorites = FavoriteQueries.from_config(config, str(user_file), str(shared_file))
shared_file.write_text('[favorite_queries\ninvalid = select 4\n', encoding='utf-8')

with caplog.at_level(logging.WARNING, logger='mycli.packages.special.favoritequeries'):
favorites.reload()

assert 'unable to read shared favorites' in caplog.text
assert config['favorite_queries'] == {'local': 'select 1'}


@pytest.mark.parametrize(
('contents', 'error_pattern'),
[
Expand All @@ -262,24 +279,41 @@ def test_reload_invalid_config_preserves_runtime_favorites(
assert config['favorite_queries'] == {'runtime': 'select 3'}


@pytest.mark.parametrize('missing_source', ['user', 'shared'])
def test_reload_missing_file_preserves_runtime_favorites(tmp_path: Path, missing_source: str) -> None:
def test_reload_missing_user_file_preserves_runtime_favorites(tmp_path: Path) -> None:
user_file = tmp_path / 'myclirc'
shared_file = tmp_path / 'shared-myclirc'
user_file.write_text('[favorite_queries]\nlocal = select 1\n', encoding='utf-8')
shared_file.write_text('[favorite_queries]\nshared = select 2\n', encoding='utf-8')
config = DummyConfig({'favorite_queries': {'runtime': 'select 3'}})
favorites = FavoriteQueries.from_config(config, str(user_file), str(shared_file))
before_reload = dict(config['favorite_queries'])
missing_file = user_file if missing_source == 'user' else shared_file
missing_file.unlink()
user_file.unlink()

with pytest.raises(FavoriteQueryReloadError, match=f'unable to read {missing_source}'):
with pytest.raises(FavoriteQueryReloadError, match='unable to read user'):
favorites.reload()

assert config['favorite_queries'] == before_reload


def test_reload_missing_shared_file_warns_and_uses_user_favorites(
tmp_path: Path,
caplog: pytest.LogCaptureFixture,
) -> None:
user_file = tmp_path / 'myclirc'
shared_file = tmp_path / 'shared-myclirc'
user_file.write_text('[favorite_queries]\nlocal = select 1\n', encoding='utf-8')
shared_file.write_text('[favorite_queries]\nshared = select 2\n', encoding='utf-8')
config = DummyConfig({'favorite_queries': {'runtime': 'select 3'}})
favorites = FavoriteQueries.from_config(config, str(user_file), str(shared_file))
shared_file.unlink()

with caplog.at_level(logging.WARNING, logger='mycli.packages.special.favoritequeries'):
favorites.reload()

assert 'unable to read shared favorites' in caplog.text
assert config['favorite_queries'] == {'local': 'select 1'}


def test_reload_unreadable_file_preserves_runtime_favorites(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
Expand Down
Loading