tests: remove pyfakefs due to issues with pathlib and sqlite in favor of a simple tempfolder where to store test files

This commit is contained in:
cătălin 2023-11-29 18:42:30 +01:00
commit c6d361f649
Signed by: catalin
GPG key ID: 0178DF42F43E5FD2
6 changed files with 45 additions and 29 deletions

View file

@ -1,2 +0,0 @@
[flake8]
max-line-length = 89

View file

@ -1 +1 @@
__version__ = "0.4.2a2" __version__ = "0.4.3"

View file

@ -1,6 +1,6 @@
import nox import nox
VERSIONS = ["3.10", "3.11"] VERSIONS = ["3.10", "3.11", "3.12"]
@nox.session(python=VERSIONS) @nox.session(python=VERSIONS)

View file

@ -41,11 +41,26 @@ def ssh_recipient(halig_ssh_public_key: str) -> Recipient:
return Recipient.from_str(halig_ssh_public_key) 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() @pytest.fixture()
def halig_path(fs, halig_ssh_public_key, halig_ssh_private_key) -> Path: def halig_ssh_path(tmp_path: Path, halig_ssh_public_key, halig_ssh_private_key) -> Path:
fs.add_real_paths(["/etc/localtime"]) ssh_path = tmp_path / ".ssh"
ssh_path = Path("~/.ssh").expanduser() ssh_path.mkdir()
ssh_path.mkdir(parents=True)
with (ssh_path / "id_ed25519").open("w") as f: with (ssh_path / "id_ed25519").open("w") as f:
f.write(halig_ssh_private_key) 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: with (ssh_path / "id_ed25519.pub").open("w") as f:
f.write(halig_ssh_public_key) 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) halig_path.mkdir(parents=True)
return halig_path return halig_path
@pytest.fixture() @pytest.fixture()
def notebooks_path(halig_path) -> Path: def notebooks_path(tmp_path) -> Path:
notebooks_path = Path("~/Notebooks").expanduser() notebooks_path = tmp_path / "Notebooks"
notebooks_path.mkdir(parents=True) notebooks_path.mkdir()
return notebooks_path return notebooks_path
@pytest.fixture() @pytest.fixture()
def settings(notebooks_path: Path) -> Settings: def settings(notebooks_path: Path, halig_ssh_path) -> Settings:
return Settings(notebooks_root_path=notebooks_path) return Settings(
notebooks_root_path=notebooks_path,
identity_paths=[halig_ssh_path / "id_ed25519"],
recipient_paths=[halig_ssh_path / "id_ed25519.pub"]
)
@pytest.fixture() @pytest.fixture()
def settings_file_path(halig_path: Path, notebooks_path: Path) -> Path: def settings_file_path(halig_config_path: Path, notebooks_path: Path) -> Path:
yaml_file = halig_path / "halig.yml" yaml_file = halig_config_path / "halig.yml"
yaml_file.touch() yaml_file.touch()
s = Settings(notebooks_root_path=notebooks_path) s = Settings(notebooks_root_path=notebooks_path)
# `.dict()` doesn't serialize some fields that yaml doesn't understand # `.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() @pytest.fixture()
def empty_file_path(halig_path: Path) -> Path: def empty_file_path(halig_config_path: Path) -> Path:
empty_path = halig_path / "empty" empty_path = halig_config_path / "empty"
empty_path.touch() empty_path.touch()
return empty_path return empty_path

View file

@ -4,27 +4,26 @@ from halig.encryption import Encryptor
from halig.settings import Settings 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 = [] identity_paths = []
recipient_paths = [] recipient_paths = []
identities = [] identities = []
for i in range(5): for i in range(5):
identity = x25519.Identity.generate() identity = x25519.Identity.generate()
identities.append(identity) identities.append(identity)
identity_path = halig_path / f"identity_{i}.key" identity_path = halig_config_path / f"identity_{i}.key"
identity_path.touch() identity_path.touch()
with identity_path.open("w") as f: with identity_path.open("w") as f:
f.write(str(identity)) f.write(str(identity))
identity_paths.append(identity_path) 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() recipient_path.touch()
with recipient_path.open("w") as f: with recipient_path.open("w") as f:
f.write(str(identity.to_public())) f.write(str(identity.to_public()))
recipient_paths.append(recipient_path) recipient_paths.append(recipient_path)
# cache_path = platformdirs.user_cache_path("halig", ensure_exists=True)
settings = Settings( settings = Settings(
notebooks_root_path=notebooks_path, notebooks_root_path=notebooks_path,
identity_paths=identity_paths, identity_paths=identity_paths,

View file

@ -20,17 +20,12 @@ def test_load_from_file(notebooks_path: Path, settings_file_path: Path):
assert settings.notebooks_root_path == notebooks_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): 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"): with pytest.raises(ValueError, match=f"File {empty_file_path} is empty"):
load_from_file(empty_file_path) load_from_file(empty_file_path)
def test_load_from_non_existing_file_path_raises_file_not_found_error(halig_path: Path): def test_load_from_non_existing_file_path_raises_file_not_found_error(halig_config_path: Path):
file = halig_path / "some_invalid_file.yml" file = halig_config_path / "some_invalid_file.yml"
with pytest.raises(FileNotFoundError, match=f"File {file} does not exist"): with pytest.raises(FileNotFoundError, match=f"File {file} does not exist"):
load_from_file(file) load_from_file(file)