fix(tests): fix tests failing on Windows (#903)

* create and delete tmp directory for tests

* Update tests/conftest.py

Co-authored-by: Travis Abendshien <46939827+CyanVoxel@users.noreply.github.com>

* Update tests/conftest.py

Co-authored-by: Travis Abendshien <46939827+CyanVoxel@users.noreply.github.com>

* remove unused import

* cleanup and added type hints

* fix mypy assignment error

* swap gettempdir() with TemporaryDirectory()

* reuse temp library for more tests + cleanup

---------

Co-authored-by: Travis Abendshien <46939827+CyanVoxel@users.noreply.github.com>
This commit is contained in:
Tony
2025-05-04 20:23:36 -04:00
committed by GitHub
parent 97ee43c875
commit 36f3b45e66
4 changed files with 79 additions and 73 deletions

View File

@@ -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

View File

@@ -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))

View File

@@ -1,28 +1,26 @@
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
""")
def test_read_settings(library_dir: Path):
settings_path = library_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
""")
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
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

View File

@@ -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)