From ce2a1ad594fc99389fe2093f856cbb7aa8de063a Mon Sep 17 00:00:00 2001 From: Kharec Date: Tue, 30 Dec 2025 23:04:45 +0100 Subject: [PATCH] test: expand configure load tests with fixture and error cases --- tests/test_configure_load.py | 68 +++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 16 deletions(-) diff --git a/tests/test_configure_load.py b/tests/test_configure_load.py index 6a487b8..9933f76 100644 --- a/tests/test_configure_load.py +++ b/tests/test_configure_load.py @@ -1,32 +1,68 @@ from pathlib import Path +from unittest.mock import patch import pytest +import yaml from skywipe.configure import Configuration -def test_configuration_load_missing_file(monkeypatch, tmp_path): +@pytest.fixture +def config_with_tmp_path(monkeypatch, tmp_path): monkeypatch.setattr(Path, "home", lambda: tmp_path) - config = Configuration() - with pytest.raises(FileNotFoundError): - config.load() + return Configuration() -def test_configuration_load_empty_file(monkeypatch, tmp_path): - monkeypatch.setattr(Path, "home", lambda: tmp_path) - config = Configuration() - config.config_file.parent.mkdir(parents=True, exist_ok=True) - config.config_file.write_text("") +def test_configuration_load_missing_file(config_with_tmp_path): + with pytest.raises(FileNotFoundError, match="Configuration file not found"): + config_with_tmp_path.load() + + +def test_configuration_load_empty_file(config_with_tmp_path): + config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True) + config_with_tmp_path.config_file.write_text("") with pytest.raises(ValueError, match="empty or invalid"): - config.load() + config_with_tmp_path.load() -def test_configuration_load_invalid_yaml(monkeypatch, tmp_path): - monkeypatch.setattr(Path, "home", lambda: tmp_path) - config = Configuration() - config.config_file.parent.mkdir(parents=True, exist_ok=True) - config.config_file.write_text(": bad") +def test_configuration_load_invalid_yaml(config_with_tmp_path): + config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True) + config_with_tmp_path.config_file.write_text(": bad") with pytest.raises(ValueError, match="Invalid YAML"): - config.load() + config_with_tmp_path.load() + + +def test_configuration_load_valid_file(config_with_tmp_path): + config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True) + config_data = { + "handle": "alice.bsky.social", + "password": "password123", + "batch_size": 10, + "delay": 1, + "verbose": True + } + with open(config_with_tmp_path.config_file, "w") as f: + yaml.dump(config_data, f) + + result = config_with_tmp_path.load() + assert result == config_data + + +def test_configuration_load_file_read_error(config_with_tmp_path): + config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True) + config_with_tmp_path.config_file.write_text("handle: alice") + + with patch("builtins.open", side_effect=IOError("Permission denied")): + with pytest.raises(ValueError, match="Failed to read configuration file"): + config_with_tmp_path.load() + + +def test_configuration_load_file_os_error(config_with_tmp_path): + config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True) + config_with_tmp_path.config_file.write_text("handle: alice") + + with patch("builtins.open", side_effect=OSError("File is locked")): + with pytest.raises(ValueError, match="Failed to read configuration file"): + config_with_tmp_path.load()