diff --git a/src/tagstudio/core/library/refresh.py b/src/tagstudio/core/library/refresh.py index 824cae527..2789fc5f7 100644 --- a/src/tagstudio/core/library/refresh.py +++ b/src/tagstudio/core/library/refresh.py @@ -91,27 +91,29 @@ def __get_dir_list(self, library_dir: Path, ignore_patterns: list[str]) -> list[ with open(compiled_ignore_path, "w") as pattern_file: pattern_file.write("\n".join(ignore_patterns)) - result = silent_run( - " ".join( + try: + result = silent_run( [ - "rg", + rg_path, "--files", "--follow", "--hidden", "--ignore-file", - f'"{str(compiled_ignore_path)}"', - ] - ), - cwd=library_dir, - capture_output=True, - shell=True, - encoding="UTF-8", - ) - compiled_ignore_path.unlink() + str(compiled_ignore_path), + ], + cwd=library_dir, + capture_output=True, + encoding="UTF-8", + ) + finally: + compiled_ignore_path.unlink() if result.stderr: logger.error(result.stderr) + if result.returncode != 0: + return None + return result.stdout.splitlines() # pyright: ignore [reportReturnType] logger.warning("[Refresh: ripgrep not found on system]") diff --git a/tests/macros/test_refresh_dir.py b/tests/macros/test_refresh_dir.py index 01578aad8..f94d0b746 100644 --- a/tests/macros/test_refresh_dir.py +++ b/tests/macros/test_refresh_dir.py @@ -3,6 +3,7 @@ from pathlib import Path +from subprocess import CompletedProcess from tempfile import TemporaryDirectory import pytest @@ -48,3 +49,24 @@ def test_refresh_multi_byte_filenames(library: Library): assert Path("em–dash.txt") in registry.files_not_in_library assert Path("apostrophe’.txt") in registry.files_not_in_library assert Path("umlaute äöü.txt") in registry.files_not_in_library + + +@pytest.mark.parametrize("library", [TemporaryDirectory()], indirect=True) +def test_refresh_falls_back_when_ripgrep_fails(library: Library, monkeypatch: pytest.MonkeyPatch): + library_dir = unwrap(library.library_dir) + registry = RefreshTracker(library=library) + library.included_files.clear() + (library_dir / ".TagStudio").mkdir() + (library_dir / "new-file.txt").touch() + + monkeypatch.setattr("tagstudio.core.library.refresh.shutil.which", lambda _: "rg") + monkeypatch.setattr( + "tagstudio.core.library.refresh.silent_run", + lambda *args, **kwargs: CompletedProcess( + args=args, returncode=1, stdout="", stderr="rg failed" + ), + ) + + list(registry.refresh_dir(library_dir)) + + assert Path("new-file.txt") in registry.files_not_in_library