From c6d361f64977f4ee37fada691b8a5c94608f6cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?c=C4=83t=C4=83lin?= Date: Wed, 29 Nov 2023 18:42:30 +0100 Subject: [PATCH] tests: remove pyfakefs due to issues with pathlib and sqlite in favor of a simple tempfolder where to store test files --- .flake8 | 2 -- halig/__version__.py | 2 +- noxfile.py | 2 +- tests/conftest.py | 52 +++++++++++++++++++++++++++++----------- tests/test_encryption.py | 7 +++--- tests/test_settings.py | 9 ++----- 6 files changed, 45 insertions(+), 29 deletions(-) delete mode 100644 .flake8 diff --git a/.flake8 b/.flake8 deleted file mode 100644 index e3a2867..0000000 --- a/.flake8 +++ /dev/null @@ -1,2 +0,0 @@ -[flake8] -max-line-length = 89 diff --git a/halig/__version__.py b/halig/__version__.py index 5d3f424..f6b7e26 100644 --- a/halig/__version__.py +++ b/halig/__version__.py @@ -1 +1 @@ -__version__ = "0.4.2a2" +__version__ = "0.4.3" diff --git a/noxfile.py b/noxfile.py index 466ad9d..9e04ac2 100644 --- a/noxfile.py +++ b/noxfile.py @@ -1,6 +1,6 @@ import nox -VERSIONS = ["3.10", "3.11"] +VERSIONS = ["3.10", "3.11", "3.12"] @nox.session(python=VERSIONS) diff --git a/tests/conftest.py b/tests/conftest.py index a5486f9..d4e09fa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -41,11 +41,26 @@ def ssh_recipient(halig_ssh_public_key: str) -> Recipient: return Recipient.from_str(halig_ssh_public_key) +# @pytest.fixture() +# def halig_path(fs, halig_ssh_public_key, halig_ssh_private_key) -> Path: +# fs.add_real_paths(["/etc/localtime"]) +# ssh_path = Path("~/.ssh").expanduser() +# ssh_path.mkdir(parents=True) +# +# with (ssh_path / "id_ed25519").open("w") as f: +# f.write(halig_ssh_private_key) +# +# with (ssh_path / "id_ed25519.pub").open("w") as f: +# f.write(halig_ssh_public_key) +# +# halig_path = Path("~/.config/halig").expanduser() +# halig_path.mkdir(parents=True) +# return halig_path + @pytest.fixture() -def halig_path(fs, halig_ssh_public_key, halig_ssh_private_key) -> Path: - fs.add_real_paths(["/etc/localtime"]) - ssh_path = Path("~/.ssh").expanduser() - ssh_path.mkdir(parents=True) +def halig_ssh_path(tmp_path: Path, halig_ssh_public_key, halig_ssh_private_key) -> Path: + ssh_path = tmp_path / ".ssh" + ssh_path.mkdir() with (ssh_path / "id_ed25519").open("w") as f: f.write(halig_ssh_private_key) @@ -53,26 +68,35 @@ def halig_path(fs, halig_ssh_public_key, halig_ssh_private_key) -> Path: with (ssh_path / "id_ed25519.pub").open("w") as f: f.write(halig_ssh_public_key) - halig_path = Path("~/.config/halig").expanduser() + return ssh_path + + +@pytest.fixture() +def halig_config_path(tmp_path: Path): + halig_path = tmp_path / ".config/halig" halig_path.mkdir(parents=True) return halig_path @pytest.fixture() -def notebooks_path(halig_path) -> Path: - notebooks_path = Path("~/Notebooks").expanduser() - notebooks_path.mkdir(parents=True) +def notebooks_path(tmp_path) -> Path: + notebooks_path = tmp_path / "Notebooks" + notebooks_path.mkdir() return notebooks_path @pytest.fixture() -def settings(notebooks_path: Path) -> Settings: - return Settings(notebooks_root_path=notebooks_path) +def settings(notebooks_path: Path, halig_ssh_path) -> Settings: + return Settings( + notebooks_root_path=notebooks_path, + identity_paths=[halig_ssh_path / "id_ed25519"], + recipient_paths=[halig_ssh_path / "id_ed25519.pub"] + ) @pytest.fixture() -def settings_file_path(halig_path: Path, notebooks_path: Path) -> Path: - yaml_file = halig_path / "halig.yml" +def settings_file_path(halig_config_path: Path, notebooks_path: Path) -> Path: + yaml_file = halig_config_path / "halig.yml" yaml_file.touch() s = Settings(notebooks_root_path=notebooks_path) # `.dict()` doesn't serialize some fields that yaml doesn't understand @@ -83,8 +107,8 @@ def settings_file_path(halig_path: Path, notebooks_path: Path) -> Path: @pytest.fixture() -def empty_file_path(halig_path: Path) -> Path: - empty_path = halig_path / "empty" +def empty_file_path(halig_config_path: Path) -> Path: + empty_path = halig_config_path / "empty" empty_path.touch() return empty_path diff --git a/tests/test_encryption.py b/tests/test_encryption.py index 8f7435f..dfb78c0 100644 --- a/tests/test_encryption.py +++ b/tests/test_encryption.py @@ -4,27 +4,26 @@ from halig.encryption import Encryptor from halig.settings import Settings -def test_instance_encryptor_from_age_keys(halig_path, notebooks_path): +def test_instance_encryptor_from_age_keys(notebooks_path, halig_config_path): identity_paths = [] recipient_paths = [] identities = [] for i in range(5): identity = x25519.Identity.generate() identities.append(identity) - identity_path = halig_path / f"identity_{i}.key" + identity_path = halig_config_path / f"identity_{i}.key" identity_path.touch() with identity_path.open("w") as f: f.write(str(identity)) identity_paths.append(identity_path) - recipient_path = halig_path / f"recipient_{i}.key" + recipient_path = halig_config_path / f"recipient_{i}.key" recipient_path.touch() with recipient_path.open("w") as f: f.write(str(identity.to_public())) recipient_paths.append(recipient_path) - # cache_path = platformdirs.user_cache_path("halig", ensure_exists=True) settings = Settings( notebooks_root_path=notebooks_path, identity_paths=identity_paths, diff --git a/tests/test_settings.py b/tests/test_settings.py index fde6712..e77c887 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -20,17 +20,12 @@ def test_load_from_file(notebooks_path: Path, settings_file_path: Path): assert settings.notebooks_root_path == notebooks_path -def test_load_from_existing_standard_file(settings_file_path: Path, settings: Settings): - standard_settings = load_from_file() - assert standard_settings.notebooks_root_path == settings.notebooks_root_path - - def test_load_from_empty_file_raises_value_error(empty_file_path: Path): with pytest.raises(ValueError, match=f"File {empty_file_path} is empty"): load_from_file(empty_file_path) -def test_load_from_non_existing_file_path_raises_file_not_found_error(halig_path: Path): - file = halig_path / "some_invalid_file.yml" +def test_load_from_non_existing_file_path_raises_file_not_found_error(halig_config_path: Path): + file = halig_config_path / "some_invalid_file.yml" with pytest.raises(FileNotFoundError, match=f"File {file} does not exist"): load_from_file(file)