diff --git a/src/tagstudio/qt/cache_manager.py b/src/tagstudio/qt/cache_manager.py index 3ac5f29a..043c7073 100644 --- a/src/tagstudio/qt/cache_manager.py +++ b/src/tagstudio/qt/cache_manager.py @@ -141,8 +141,7 @@ class CacheManager(metaclass=Singleton): self.last_lib_path = self.lib.library_dir CacheManager.folder_dict.clear() - # NOTE: The /dev/null check is a workaround for current test assumptions. - if self.last_lib_path and self.last_lib_path != Path("/dev/null"): + if self.last_lib_path: # Ensure thumbnail cache path exists. self.cache_dir().mkdir(exist_ok=True) # Registers any existing folders and counts the capacity of the most recent one. diff --git a/tests/conftest.py b/tests/conftest.py index 94bd3ba3..64f96d41 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,6 +9,7 @@ CWD = Path(__file__).parent # this needs to be above `src` imports sys.path.insert(0, str(CWD.parent)) +from tagstudio.core.constants import THUMB_CACHE_NAME, TS_FOLDER_NAME from tagstudio.core.library.alchemy.library import Library from tagstudio.core.library.alchemy.models import Entry, Tag from tagstudio.qt.ts_qt import QtDriver @@ -50,18 +51,30 @@ def file_mediatypes_library(): return lib +@pytest.fixture(scope="session") +def library_dir(): + """Creates a shared library path for tests, that cleans up after the session.""" + with TemporaryDirectory() as tmp_dir_name: + library_path = Path(tmp_dir_name) + + thumbs_path = library_path / TS_FOLDER_NAME / THUMB_CACHE_NAME + thumbs_path.mkdir(parents=True, exist_ok=True) + + yield library_path + + @pytest.fixture -def library(request): +def library(request, library_dir: Path): # when no param is passed, use the default - library_path = "/dev/null/" + library_path = library_dir if hasattr(request, "param"): if isinstance(request.param, TemporaryDirectory): - library_path = request.param.name + library_path = Path(request.param.name) else: - library_path = request.param + library_path = Path(request.param) lib = Library() - status = lib.open_library(Path(library_path), ":memory:") + status = lib.open_library(library_path, ":memory:") assert status.success tag = Tag( @@ -130,34 +143,32 @@ def entry_full(library: Library): @pytest.fixture -def qt_driver(qtbot, library): - with TemporaryDirectory() as tmp_dir: +def qt_driver(qtbot, library, library_dir: Path): + class Args: + settings_file = library_dir / "settings.toml" + cache_file = library_dir / "tagstudio.ini" + open = library_dir + ci = True - class Args: - settings_file = Path(tmp_dir) / "settings.toml" - cache_file = Path(tmp_dir) / "tagstudio.ini" - open = Path(tmp_dir) - ci = True + with patch("tagstudio.qt.ts_qt.Consumer"), patch("tagstudio.qt.ts_qt.CustomRunnable"): + driver = QtDriver(Args()) - with patch("tagstudio.qt.ts_qt.Consumer"), patch("tagstudio.qt.ts_qt.CustomRunnable"): - driver = QtDriver(Args()) + driver.app = Mock() + driver.main_window = Mock() + driver.preview_panel = Mock() + driver.flow_container = Mock() + driver.item_thumbs = [] + driver.autofill_action = Mock() - driver.app = Mock() - driver.main_window = Mock() - driver.preview_panel = Mock() - driver.flow_container = Mock() - driver.item_thumbs = [] - driver.autofill_action = Mock() + driver.copy_buffer = {"fields": [], "tags": []} + driver.copy_fields_action = Mock() + driver.paste_fields_action = Mock() - driver.copy_buffer = {"fields": [], "tags": []} - driver.copy_fields_action = Mock() - driver.paste_fields_action = Mock() - - driver.lib = library - # TODO - downsize this method and use it - # driver.start() - driver.frame_content = list(library.get_entries()) - yield driver + driver.lib = library + # TODO - downsize this method and use it + # driver.start() + driver.frame_content = list(library.get_entries()) + yield driver @pytest.fixture diff --git a/tests/qt/test_file_path_options.py b/tests/qt/test_file_path_options.py index 40dfeab1..be78224f 100644 --- a/tests/qt/test_file_path_options.py +++ b/tests/qt/test_file_path_options.py @@ -106,12 +106,14 @@ def test_file_path_display( ), ], ) -def test_title_update(qt_driver: QtDriver, filepath_option: ShowFilepathOption, expected_title): +def test_title_update( + qt_driver: QtDriver, filepath_option: ShowFilepathOption, expected_title, library_dir: Path +): base_title = qt_driver.base_title - test_path = Path("/dev/null") + open_status = LibraryStatus( success=True, - library_path=test_path, + library_path=library_dir, message="", msg_description="", ) @@ -133,7 +135,7 @@ def test_title_update(qt_driver: QtDriver, filepath_option: ShowFilepathOption, qt_driver.folders_to_tags_action = QAction(menu_bar) # Trigger the update - qt_driver.init_library(test_path, open_status) + qt_driver.init_library(library_dir, open_status) # Assert the title is updated correctly - qt_driver.main_window.setWindowTitle.assert_called_with(expected_title(test_path, base_title)) + qt_driver.main_window.setWindowTitle.assert_called_with(expected_title(library_dir, base_title)) diff --git a/tests/qt/test_global_settings.py b/tests/qt/test_global_settings.py index 25d78086..8325c154 100644 --- a/tests/qt/test_global_settings.py +++ b/tests/qt/test_global_settings.py @@ -1,34 +1,32 @@ from pathlib import Path -from tempfile import TemporaryDirectory from tagstudio.core.global_settings import GlobalSettings, Theme -def test_read_settings(): - with TemporaryDirectory() as tmp_dir: - settings_path = Path(tmp_dir) / "settings.toml" - with open(settings_path, "a") as settings_file: - settings_file.write(""" - language = "de" - open_last_loaded_on_startup = true - autoplay = true - show_filenames_in_grid = true - page_size = 1337 - show_filepath = 0 - dark_mode = 2 - date_format = "%x" - hour_format = true - zero_padding = true - """) +def test_read_settings(library_dir: Path): + settings_path = library_dir / "settings.toml" + with open(settings_path, "w") as settings_file: + settings_file.write(""" + language = "de" + open_last_loaded_on_startup = true + autoplay = true + show_filenames_in_grid = true + page_size = 1337 + show_filepath = 0 + dark_mode = 2 + date_format = "%x" + hour_format = true + zero_padding = true + """) - settings = GlobalSettings.read_settings(settings_path) - assert settings.language == "de" - assert settings.open_last_loaded_on_startup - assert settings.autoplay - assert settings.show_filenames_in_grid - assert settings.page_size == 1337 - assert settings.show_filepath == 0 - assert settings.theme == Theme.SYSTEM - assert settings.date_format == "%x" - assert settings.hour_format - assert settings.zero_padding + settings = GlobalSettings.read_settings(settings_path) + assert settings.language == "de" + assert settings.open_last_loaded_on_startup + assert settings.autoplay + assert settings.show_filenames_in_grid + assert settings.page_size == 1337 + assert settings.show_filepath == 0 + assert settings.theme == Theme.SYSTEM + assert settings.date_format == "%x" + assert settings.hour_format + assert settings.zero_padding diff --git a/tests/test_driver.py b/tests/test_driver.py index c69b9757..b5251021 100644 --- a/tests/test_driver.py +++ b/tests/test_driver.py @@ -1,10 +1,7 @@ -from os import makedirs from pathlib import Path -from tempfile import TemporaryDirectory from PySide6.QtCore import QSettings -from tagstudio.core.constants import TS_FOLDER_NAME from tagstudio.core.driver import DriverMixin from tagstudio.core.enums import SettingItems from tagstudio.core.global_settings import GlobalSettings @@ -52,22 +49,20 @@ def test_evaluate_path_last_lib_not_exists(): assert result == LibraryStatus(success=True, library_path=None, message=None) -def test_evaluate_path_last_lib_present(): +def test_evaluate_path_last_lib_present(library_dir: Path): # Given - with TemporaryDirectory() as tmpdir: - cache_file = tmpdir + "/test_settings.ini" - cache = QSettings(cache_file, QSettings.Format.IniFormat) - cache.setValue(SettingItems.LAST_LIBRARY, tmpdir) - cache.sync() + cache_file = library_dir / "test_settings.ini" + cache = QSettings(str(cache_file), QSettings.Format.IniFormat) + cache.setValue(SettingItems.LAST_LIBRARY, library_dir) + cache.sync() - settings = GlobalSettings() - settings.open_last_loaded_on_startup = True + settings = GlobalSettings() + settings.open_last_loaded_on_startup = True - makedirs(Path(tmpdir) / TS_FOLDER_NAME) - driver = TestDriver(settings, cache) + driver = TestDriver(settings, cache) - # When - result = driver.evaluate_path(None) + # When + result = driver.evaluate_path(None) - # Then - assert result == LibraryStatus(success=True, library_path=Path(tmpdir)) + # Then + assert result == LibraryStatus(success=True, library_path=library_dir)