Skip to content

Commit 36f3b45

Browse files
zfbxCyanVoxel
andauthored
fix(tests): fix tests failing on Windows (TagStudioDev#903)
* create and delete tmp directory for tests * Update tests/conftest.py Co-authored-by: Travis Abendshien <[email protected]> * Update tests/conftest.py Co-authored-by: Travis Abendshien <[email protected]> * 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 <[email protected]>
1 parent 97ee43c commit 36f3b45

File tree

4 files changed

+83
-77
lines changed

4 files changed

+83
-77
lines changed

tests/conftest.py

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# this needs to be above `src` imports
1010
sys.path.insert(0, str(CWD.parent))
1111

12+
from tagstudio.core.constants import THUMB_CACHE_NAME, TS_FOLDER_NAME
1213
from tagstudio.core.library.alchemy.library import Library
1314
from tagstudio.core.library.alchemy.models import Entry, Tag
1415
from tagstudio.qt.ts_qt import QtDriver
@@ -50,18 +51,30 @@ def file_mediatypes_library():
5051
return lib
5152

5253

54+
@pytest.fixture(scope="session")
55+
def library_dir():
56+
"""Creates a shared library path for tests, that cleans up after the session."""
57+
with TemporaryDirectory() as tmp_dir_name:
58+
library_path = Path(tmp_dir_name)
59+
60+
thumbs_path = library_path / TS_FOLDER_NAME / THUMB_CACHE_NAME
61+
thumbs_path.mkdir(parents=True, exist_ok=True)
62+
63+
yield library_path
64+
65+
5366
@pytest.fixture
54-
def library(request):
67+
def library(request, library_dir: Path):
5568
# when no param is passed, use the default
56-
library_path = "/dev/null/"
69+
library_path = library_dir
5770
if hasattr(request, "param"):
5871
if isinstance(request.param, TemporaryDirectory):
59-
library_path = request.param.name
72+
library_path = Path(request.param.name)
6073
else:
61-
library_path = request.param
74+
library_path = Path(request.param)
6275

6376
lib = Library()
64-
status = lib.open_library(Path(library_path), ":memory:")
77+
status = lib.open_library(library_path, ":memory:")
6578
assert status.success
6679

6780
tag = Tag(
@@ -130,34 +143,32 @@ def entry_full(library: Library):
130143

131144

132145
@pytest.fixture
133-
def qt_driver(qtbot, library):
134-
with TemporaryDirectory() as tmp_dir:
135-
136-
class Args:
137-
settings_file = Path(tmp_dir) / "settings.toml"
138-
cache_file = Path(tmp_dir) / "tagstudio.ini"
139-
open = Path(tmp_dir)
140-
ci = True
141-
142-
with patch("tagstudio.qt.ts_qt.Consumer"), patch("tagstudio.qt.ts_qt.CustomRunnable"):
143-
driver = QtDriver(Args())
144-
145-
driver.app = Mock()
146-
driver.main_window = Mock()
147-
driver.preview_panel = Mock()
148-
driver.flow_container = Mock()
149-
driver.item_thumbs = []
150-
driver.autofill_action = Mock()
151-
152-
driver.copy_buffer = {"fields": [], "tags": []}
153-
driver.copy_fields_action = Mock()
154-
driver.paste_fields_action = Mock()
155-
156-
driver.lib = library
157-
# TODO - downsize this method and use it
158-
# driver.start()
159-
driver.frame_content = list(library.get_entries())
160-
yield driver
146+
def qt_driver(qtbot, library, library_dir: Path):
147+
class Args:
148+
settings_file = library_dir / "settings.toml"
149+
cache_file = library_dir / "tagstudio.ini"
150+
open = library_dir
151+
ci = True
152+
153+
with patch("tagstudio.qt.ts_qt.Consumer"), patch("tagstudio.qt.ts_qt.CustomRunnable"):
154+
driver = QtDriver(Args())
155+
156+
driver.app = Mock()
157+
driver.main_window = Mock()
158+
driver.preview_panel = Mock()
159+
driver.flow_container = Mock()
160+
driver.item_thumbs = []
161+
driver.autofill_action = Mock()
162+
163+
driver.copy_buffer = {"fields": [], "tags": []}
164+
driver.copy_fields_action = Mock()
165+
driver.paste_fields_action = Mock()
166+
167+
driver.lib = library
168+
# TODO - downsize this method and use it
169+
# driver.start()
170+
driver.frame_content = list(library.get_entries())
171+
yield driver
161172

162173

163174
@pytest.fixture

tests/qt/test_file_path_options.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,14 @@ def test_file_path_display(
106106
),
107107
],
108108
)
109-
def test_title_update(qt_driver: QtDriver, filepath_option: ShowFilepathOption, expected_title):
109+
def test_title_update(
110+
qt_driver: QtDriver, filepath_option: ShowFilepathOption, expected_title, library_dir: Path
111+
):
110112
base_title = qt_driver.base_title
111-
test_path = Path("/dev/null")
113+
112114
open_status = LibraryStatus(
113115
success=True,
114-
library_path=test_path,
116+
library_path=library_dir,
115117
message="",
116118
msg_description="",
117119
)
@@ -133,7 +135,7 @@ def test_title_update(qt_driver: QtDriver, filepath_option: ShowFilepathOption,
133135
qt_driver.folders_to_tags_action = QAction(menu_bar)
134136

135137
# Trigger the update
136-
qt_driver.init_library(test_path, open_status)
138+
qt_driver.init_library(library_dir, open_status)
137139

138140
# Assert the title is updated correctly
139-
qt_driver.main_window.setWindowTitle.assert_called_with(expected_title(test_path, base_title))
141+
qt_driver.main_window.setWindowTitle.assert_called_with(expected_title(library_dir, base_title))

tests/qt/test_global_settings.py

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
from pathlib import Path
2-
from tempfile import TemporaryDirectory
32

43
from tagstudio.core.global_settings import GlobalSettings, Theme
54

65

7-
def test_read_settings():
8-
with TemporaryDirectory() as tmp_dir:
9-
settings_path = Path(tmp_dir) / "settings.toml"
10-
with open(settings_path, "a") as settings_file:
11-
settings_file.write("""
12-
language = "de"
13-
open_last_loaded_on_startup = true
14-
autoplay = true
15-
show_filenames_in_grid = true
16-
page_size = 1337
17-
show_filepath = 0
18-
dark_mode = 2
19-
""")
6+
def test_read_settings(library_dir: Path):
7+
settings_path = library_dir / "settings.toml"
8+
with open(settings_path, "a") as settings_file:
9+
settings_file.write("""
10+
language = "de"
11+
open_last_loaded_on_startup = true
12+
autoplay = true
13+
show_filenames_in_grid = true
14+
page_size = 1337
15+
show_filepath = 0
16+
dark_mode = 2
17+
""")
2018

21-
settings = GlobalSettings.read_settings(settings_path)
22-
assert settings.language == "de"
23-
assert settings.open_last_loaded_on_startup
24-
assert settings.autoplay
25-
assert settings.show_filenames_in_grid
26-
assert settings.page_size == 1337
27-
assert settings.show_filepath == 0
28-
assert settings.theme == Theme.SYSTEM
19+
settings = GlobalSettings.read_settings(settings_path)
20+
assert settings.language == "de"
21+
assert settings.open_last_loaded_on_startup
22+
assert settings.autoplay
23+
assert settings.show_filenames_in_grid
24+
assert settings.page_size == 1337
25+
assert settings.show_filepath == 0
26+
assert settings.theme == Theme.SYSTEM

tests/test_driver.py

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
from os import makedirs
21
from pathlib import Path
3-
from tempfile import TemporaryDirectory
42

53
from PySide6.QtCore import QSettings
64

7-
from tagstudio.core.constants import TS_FOLDER_NAME
85
from tagstudio.core.driver import DriverMixin
96
from tagstudio.core.enums import SettingItems
107
from tagstudio.core.global_settings import GlobalSettings
@@ -52,22 +49,20 @@ def test_evaluate_path_last_lib_not_exists():
5249
assert result == LibraryStatus(success=True, library_path=None, message=None)
5350

5451

55-
def test_evaluate_path_last_lib_present():
52+
def test_evaluate_path_last_lib_present(library_dir: Path):
5653
# Given
57-
with TemporaryDirectory() as tmpdir:
58-
cache_file = tmpdir + "/test_settings.ini"
59-
cache = QSettings(cache_file, QSettings.Format.IniFormat)
60-
cache.setValue(SettingItems.LAST_LIBRARY, tmpdir)
61-
cache.sync()
54+
cache_file = library_dir / "test_settings.ini"
55+
cache = QSettings(str(cache_file), QSettings.Format.IniFormat)
56+
cache.setValue(SettingItems.LAST_LIBRARY, library_dir)
57+
cache.sync()
6258

63-
settings = GlobalSettings()
64-
settings.open_last_loaded_on_startup = True
59+
settings = GlobalSettings()
60+
settings.open_last_loaded_on_startup = True
6561

66-
makedirs(Path(tmpdir) / TS_FOLDER_NAME)
67-
driver = TestDriver(settings, cache)
62+
driver = TestDriver(settings, cache)
6863

69-
# When
70-
result = driver.evaluate_path(None)
64+
# When
65+
result = driver.evaluate_path(None)
7166

72-
# Then
73-
assert result == LibraryStatus(success=True, library_path=Path(tmpdir))
67+
# Then
68+
assert result == LibraryStatus(success=True, library_path=library_dir)

0 commit comments

Comments
 (0)