Files
utils/test/test_filedialogs.py
2026-10-01 20:36:59 +02:00

56 lines
1.9 KiB
Python

"""Hermetic tests for ``utils.filedialogs``.
These tests never open a real dialog: every tkinter touchpoint is mocked,
so they run headless and even on machines without Tk system libraries
(e.g. the CI runner, where ``import tkinter`` fails on a missing
``libtk8.6.so``). ``conftest.py`` registers a tkinter stub in that case,
so ``utils.filedialogs`` can be imported at all.
For tests that open a real dialog, see ``test_filedialogs_live.py``
(marker ``live``, excluded from the default run).
"""
from unittest import mock
from utils.filedialogs import get_dir_path_from_dialog, get_file_path_from_dialog
def test_get_dir_path_from_dialog_returns_selected_path():
with (
mock.patch("tkinter.Tk"),
mock.patch(
"tkinter.filedialog.askdirectory", return_value="/tmp/somewhere"
) as dialog,
):
result = get_dir_path_from_dialog(initialdir="/tmp", title="Pick")
assert result == "/tmp/somewhere"
dialog.assert_called_once_with(initialdir="/tmp", title="Pick")
def test_get_dir_path_from_dialog_returns_empty_string_on_cancel():
with (
mock.patch("tkinter.Tk"),
mock.patch("tkinter.filedialog.askdirectory", return_value=""),
):
assert get_dir_path_from_dialog() == ""
def test_get_file_path_from_dialog_returns_selected_path():
with (
mock.patch("tkinter.Tk"),
mock.patch(
"tkinter.filedialog.askopenfilename", return_value="/tmp/file.txt"
) as dialog,
):
result = get_file_path_from_dialog(filetypes=[("Text", "*.txt")])
assert result == "/tmp/file.txt"
dialog.assert_called_once_with(filetypes=[("Text", "*.txt")])
def test_get_file_path_from_dialog_returns_empty_string_on_cancel():
with (
mock.patch("tkinter.Tk"),
mock.patch("tkinter.filedialog.askopenfilename", return_value=""),
):
assert get_file_path_from_dialog() == ""