auto-archiver/tests/test_implementation.py

83 wiersze
2.5 KiB
Python
Czysty Zwykły widok Historia

2025-02-10 12:27:11 +00:00
import sys
import pytest
from auto_archiver.__main__ import main
2025-02-10 12:43:21 +00:00
2025-02-10 12:27:11 +00:00
@pytest.fixture
2025-02-10 12:43:21 +00:00
def orchestration_file_path(tmp_path):
folder = tmp_path / "secrets"
folder.mkdir(exist_ok=True)
return (folder / "example_orch.yaml").as_posix()
2025-02-10 12:27:11 +00:00
2025-03-10 18:44:54 +00:00
2025-02-10 12:27:11 +00:00
@pytest.fixture
2025-02-10 12:43:21 +00:00
def orchestration_file(orchestration_file_path):
2025-03-10 18:44:54 +00:00
def _orchestration_file(content=""):
2025-02-10 12:43:21 +00:00
with open(orchestration_file_path, "w") as f:
f.write(content)
return orchestration_file_path
2025-03-10 18:44:54 +00:00
2025-02-10 12:43:21 +00:00
return _orchestration_file
2025-03-10 18:44:54 +00:00
2025-02-10 12:43:21 +00:00
@pytest.fixture
def autoarchiver(tmp_path, monkeypatch, request):
def _autoarchiver(args=[]):
def cleanup():
from loguru import logger
2025-03-10 18:44:54 +00:00
2025-02-10 12:43:21 +00:00
if not logger._core.handlers.get(0):
logger._core.handlers_count = 0
logger.add(sys.stderr)
request.addfinalizer(cleanup)
(tmp_path / "secrets").mkdir(exist_ok=True)
2025-02-10 12:27:11 +00:00
# change dir to tmp_path
monkeypatch.chdir(tmp_path)
with monkeypatch.context() as m:
m.setattr(sys, "argv", ["auto-archiver"] + args)
return main()
2025-02-10 12:43:21 +00:00
2025-02-10 12:27:11 +00:00
return _autoarchiver
def test_run_auto_archiver_no_args(caplog, autoarchiver):
with pytest.raises(SystemExit):
2025-02-10 12:43:21 +00:00
autoarchiver()
2025-02-10 12:27:11 +00:00
assert "provide at least one URL via the command line, or set up an alternative feeder" in caplog.text
2025-03-10 18:44:54 +00:00
2025-02-10 12:43:21 +00:00
def test_run_auto_archiver_invalid_file(caplog, autoarchiver):
# exec 'auto-archiver' on the command lin
with pytest.raises(SystemExit):
autoarchiver(["--config", "nonexistent_file.yaml"])
assert "Make sure the file exists and try again, or run without th" in caplog.text
2025-02-10 12:27:11 +00:00
2025-03-10 18:44:54 +00:00
2025-02-10 12:43:21 +00:00
def test_run_auto_archiver_empty_file(caplog, autoarchiver, orchestration_file):
# create a valid (empty) orchestration file
path = orchestration_file(content="")
2025-02-10 12:27:11 +00:00
# exec 'auto-archiver' on the command lin
with pytest.raises(SystemExit):
2025-02-10 12:43:21 +00:00
autoarchiver(["--config", path])
2025-02-10 12:27:11 +00:00
2025-02-10 12:43:21 +00:00
# should treat an empty file as if there is no file at all
assert " No URLs provided. Please provide at least one URL via the com" in caplog.text
2025-03-10 18:44:54 +00:00
def test_call_autoarchiver_main(caplog, monkeypatch, tmp_path):
from auto_archiver.__main__ import main
# monkey patch to change the current working directory, so that we don't use the user's real config file
monkeypatch.chdir(tmp_path)
(tmp_path / "secrets").mkdir(exist_ok=True)
with monkeypatch.context() as m:
m.setattr(sys, "argv", ["auto-archiver"])
with pytest.raises(SystemExit):
main()
2025-03-10 18:44:54 +00:00
assert "No URLs provided. Please provide at least one" in caplog.text