feat: add import command

This commit is contained in:
cătălin 2023-05-17 19:00:42 +02:00
commit be20284f78
Signed by: catalin
GPG key ID: 0178DF42F43E5FD2
9 changed files with 133 additions and 10 deletions

View file

@ -29,6 +29,24 @@ def notes(notebooks_path: Path):
dt = dt.subtract(days=day_offset)
(dailies / f"{dt.date()}.age").touch()
@pytest.fixture()
def unencrypted_notes(notebooks_path):
unencrypted_root_path = notebooks_path / "unencrypted"
unencrypted_root_path.mkdir()
for i in range(5):
note = unencrypted_root_path / f"note_{i}.md"
note.touch()
subnote_path = unencrypted_root_path / f"inner_{i}"
subnote_path.mkdir()
for j in range(2):
subnote = subnote_path / f"note_{i}_{j}.md"
subnote.touch()
with subnote.open("w") as f:
f.write(f"subnote {i} {j}")
with note.open("w") as f:
f.write(f"note {i}")
return unencrypted_root_path
@pytest.fixture
def notebooks_command(settings: Settings):

View file

@ -0,0 +1,36 @@
import pytest
from pathlib import Path
from halig.commands.import_unencrypted import ImportCommand
from halig.encryption import Encryptor
@pytest.fixture()
def command(settings) -> ImportCommand:
return ImportCommand(settings)
def test_get_importables(unencrypted_notes: Path, command: ImportCommand):
notes = list(command.get_importables())
assert len(notes) == 15
assert notes == list(unencrypted_notes.glob("**/*.md"))
def test_import(unencrypted_notes: Path, command: ImportCommand, encryptor: Encryptor):
command.run()
notes = command.get_importables()
encrypted_notes = list(unencrypted_notes.glob("**/*.age"))
assert len(encrypted_notes) == len(list(notes)) == 15
for note in encrypted_notes:
with note.open("rb") as f:
encrypted_data = f.read()
data = encryptor.decrypt(encrypted_data)
if "inner" in str(note):
assert f'sub{note.name.replace(".age", "").replace("_"," ")}' == data.decode()
else:
assert note.name.replace(".age", "").replace("_"," ") == data.decode()
def test_import_unlink(unencrypted_notes: Path, command: ImportCommand, encryptor: Encryptor):
command.unlink = True
command.run()
notes = command.get_importables()
encrypted_notes = list(unencrypted_notes.glob("**/*.age"))
assert len(encrypted_notes) == 15
assert len(list(unencrypted_notes.glob("**/*.md"))) == 0