|
| 1 | +from dataclasses import dataclass |
| 2 | +from pathlib import Path |
| 3 | +from typing import Callable |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from refred.configuration import saving_configuration |
| 8 | + |
| 9 | + |
| 10 | +### Helpers to fake the MainGui |
| 11 | +@dataclass |
| 12 | +class DummyParent: |
| 13 | + path_config: str |
| 14 | + config_saved: bool = False |
| 15 | + |
| 16 | + |
| 17 | +def fake_extension_factory(expected_input: Path, sanitized_output: str) -> Callable[[str], str]: |
| 18 | + def _fake_extension(path: str) -> str: |
| 19 | + assert path == str(expected_input) |
| 20 | + return sanitized_output |
| 21 | + |
| 22 | + return _fake_extension |
| 23 | + |
| 24 | + |
| 25 | +def fake_status_handler_factory(call_collector: list[tuple[str, bool]]): |
| 26 | + class _FakeStatusMessageHandler: |
| 27 | + def __init__(self, parent: DummyParent, message: str, is_threaded: bool): |
| 28 | + call_collector.append((message, is_threaded)) |
| 29 | + |
| 30 | + return _FakeStatusMessageHandler |
| 31 | + |
| 32 | + |
| 33 | +def fake_export_config_factory() -> type: |
| 34 | + class _FakeExportXMLConfig: |
| 35 | + def __init__(self, parent: DummyParent): |
| 36 | + self.parent: DummyParent = parent |
| 37 | + |
| 38 | + def save(self, filename: str): |
| 39 | + path = Path(filename) |
| 40 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 41 | + _ = path.write_text("<Reduction />", encoding="utf-8") |
| 42 | + |
| 43 | + return _FakeExportXMLConfig |
| 44 | + |
| 45 | + |
| 46 | +def fake_gui_utility_factory(sanitized_path: str) -> type: |
| 47 | + class _FakeGuiUtility: |
| 48 | + def __init__(self, parent: DummyParent): |
| 49 | + self.parent: DummyParent = parent |
| 50 | + |
| 51 | + def new_config_file_loaded(self, config_file_name: str): |
| 52 | + assert config_file_name == sanitized_path |
| 53 | + self.parent.config_saved = True |
| 54 | + |
| 55 | + def gui_not_modified(self): |
| 56 | + pass |
| 57 | + |
| 58 | + return _FakeGuiUtility |
| 59 | + |
| 60 | + |
| 61 | +### Tests |
| 62 | + |
| 63 | + |
| 64 | +def test_saving_configuration_good_path(monkeypatch: pytest.MonkeyPatch, tmp_path: Path): |
| 65 | + parent = DummyParent(path_config=str(tmp_path)) |
| 66 | + provided_path = tmp_path / "test_config" |
| 67 | + sanitized_path = f"{provided_path}.xml" |
| 68 | + |
| 69 | + monkeypatch.setattr( |
| 70 | + saving_configuration, |
| 71 | + "makeSureFileHasExtension", |
| 72 | + fake_extension_factory(provided_path, sanitized_path), |
| 73 | + ) |
| 74 | + |
| 75 | + status_messages: list[tuple[str, bool]] = [] |
| 76 | + monkeypatch.setattr( |
| 77 | + saving_configuration, |
| 78 | + "StatusMessageHandler", |
| 79 | + fake_status_handler_factory(status_messages), |
| 80 | + ) |
| 81 | + |
| 82 | + monkeypatch.setattr( |
| 83 | + saving_configuration, |
| 84 | + "ExportXMLConfig", |
| 85 | + fake_export_config_factory(), |
| 86 | + ) |
| 87 | + |
| 88 | + monkeypatch.setattr( |
| 89 | + saving_configuration, |
| 90 | + "GuiUtility", |
| 91 | + fake_gui_utility_factory(sanitized_path), |
| 92 | + ) |
| 93 | + |
| 94 | + saver = saving_configuration.SavingConfiguration(parent=parent, filename=str(provided_path)) # type: ignore[arg-type] |
| 95 | + saver.run() |
| 96 | + |
| 97 | + saved_file = Path(sanitized_path) |
| 98 | + assert saved_file.exists() |
| 99 | + assert parent.path_config == str(provided_path.parent) |
| 100 | + assert parent.config_saved is True |
| 101 | + assert status_messages == [("Saving config ...", False), ("Done!", True)] |
| 102 | + |
| 103 | + |
| 104 | +def test_saving_configuration_permission_error(monkeypatch: pytest.MonkeyPatch, tmp_path: Path): |
| 105 | + parent = DummyParent(path_config=str(tmp_path)) |
| 106 | + provided_path = tmp_path / "forbidden" / "config" |
| 107 | + sanitized_path = f"{provided_path}.xml" |
| 108 | + |
| 109 | + monkeypatch.setattr( |
| 110 | + saving_configuration, |
| 111 | + "makeSureFileHasExtension", |
| 112 | + fake_extension_factory(provided_path, sanitized_path), |
| 113 | + ) |
| 114 | + |
| 115 | + status_messages: list[tuple[str, bool]] = [] |
| 116 | + monkeypatch.setattr( |
| 117 | + saving_configuration, |
| 118 | + "StatusMessageHandler", |
| 119 | + fake_status_handler_factory(status_messages), |
| 120 | + ) |
| 121 | + |
| 122 | + class _PermissionErrorExportXMLConfig: |
| 123 | + def __init__(self, parent: DummyParent): |
| 124 | + self.parent: DummyParent = parent |
| 125 | + |
| 126 | + def save(self, filename: str): |
| 127 | + raise PermissionError("No permission to write file.") |
| 128 | + |
| 129 | + monkeypatch.setattr( |
| 130 | + saving_configuration, |
| 131 | + "ExportXMLConfig", |
| 132 | + _PermissionErrorExportXMLConfig, |
| 133 | + ) |
| 134 | + |
| 135 | + monkeypatch.setattr( |
| 136 | + saving_configuration, |
| 137 | + "GuiUtility", |
| 138 | + fake_gui_utility_factory(sanitized_path), |
| 139 | + ) |
| 140 | + |
| 141 | + saver = saving_configuration.SavingConfiguration(parent=parent, filename=str(provided_path)) # type: ignore[arg-type] |
| 142 | + saver.run() |
| 143 | + |
| 144 | + assert parent.path_config == str(provided_path.parent) |
| 145 | + assert parent.config_saved is False |
| 146 | + assert status_messages == [ |
| 147 | + ("Saving config ...", False), |
| 148 | + ("Error: No permission to write file.", True), |
| 149 | + ] |
0 commit comments