dns-blocklist-mirror/tests/test_cli.py
XLion 7d99d7e4e8
All checks were successful
Checks / checks (push) Successful in 45s
Deploy Pages / deploy (push) Successful in 20s
Split tests by responsibility
2026-08-15 22:34:31 +08:00

194 lines
5.7 KiB
Python

from __future__ import annotations
from pathlib import Path
import pytest
from dns_blocklist_mirror.cli import _ownership_path, _publish_output
from tests._support import Response, run_cli, serve_https
def test_cli_mirrors_one_https_source_without_changing_bytes(tmp_path: Path) -> None:
expected = b"[Adblock Plus]\n||ads.example^\r\n"
with serve_https(tmp_path, {"/pro.txt": Response(body=expected)}) as (
base_url,
ca_path,
):
config_path = tmp_path / "mirrors.yaml"
config_path.write_text(
f"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- {base_url}/pro.txt
""",
encoding="utf-8",
)
output_path = tmp_path / "public"
result = run_cli(config_path, output_path, ca_path)
assert result.returncode == 0, result.stderr
assert (output_path / "hagezi" / "pro.txt").read_bytes() == expected
assert "hagezi/pro.txt: source 1/1 succeeded" in result.stdout
def test_cli_preserves_the_previous_output_and_reports_every_failed_file(
tmp_path: Path,
) -> None:
routes = {
"/available.txt": Response(body=b"new content\n"),
"/missing-one.txt": Response(body=b"missing\n", status=404),
"/missing-one-fallback.txt": Response(body=b"missing\n", status=410),
"/missing-two.txt": Response(body=b"missing\n", status=404),
}
with serve_https(tmp_path, routes) as (base_url, ca_path):
config_path = tmp_path / "mirrors.yaml"
config_path.write_text(
f"""\
version: 1
mirrors:
hagezi:
- destination: available.txt
sources:
- {base_url}/available.txt
- destination: missing-one.txt
sources:
- {base_url}/missing-one.txt
- {base_url}/missing-one-fallback.txt
other:
- destination: missing-two.txt
sources:
- {base_url}/missing-two.txt
""",
encoding="utf-8",
)
output_path = tmp_path / "public"
previous_path = output_path / "previous.txt"
previous_path.parent.mkdir()
previous_path.write_bytes(b"previous deployment\n")
_ownership_path(output_path).write_text(
"dns-blocklist-mirror output\n",
encoding="utf-8",
)
result = run_cli(config_path, output_path, ca_path)
assert result.returncode == 1
assert previous_path.read_bytes() == b"previous deployment\n"
assert sorted(path.relative_to(output_path) for path in output_path.rglob("*")) == [
Path("previous.txt")
]
assert "Mirror build failed: 2 files could not be downloaded." in result.stderr
assert (
f"- source 1/2 ({base_url}/missing-one.txt): HTTP 404 Not Found"
in result.stderr
)
assert (
f"- source 2/2 ({base_url}/missing-one-fallback.txt): HTTP 410 Gone"
in result.stderr
)
assert "- other/missing-two.txt" in result.stderr
assert (
f"- source 1/1 ({base_url}/missing-two.txt): HTTP 404 Not Found"
in result.stderr
)
assert "No files were published." in result.stderr
def test_cli_refuses_to_replace_a_symlink_output(tmp_path: Path) -> None:
config_path = tmp_path / "mirrors.yaml"
config_path.write_text(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- https://example.com/pro.txt
""",
encoding="utf-8",
)
target_path = tmp_path / "target"
target_path.mkdir()
sentinel_path = target_path / "sentinel.txt"
sentinel_path.write_bytes(b"keep me\n")
output_path = tmp_path / "public"
output_path.symlink_to(target_path, target_is_directory=True)
result = run_cli(config_path, output_path)
assert result.returncode == 2
assert (
f'Output error: refusing symlink output path "{output_path}"' in result.stderr
)
assert sentinel_path.read_bytes() == b"keep me\n"
def test_cli_refuses_to_replace_an_unowned_output_directory(tmp_path: Path) -> None:
config_path = tmp_path / "mirrors.yaml"
config_path.write_text(
"""\
version: 1
mirrors:
hagezi:
- destination: pro.txt
sources:
- https://example.com/pro.txt
""",
encoding="utf-8",
)
output_path = tmp_path / "public"
output_path.mkdir()
sentinel_path = output_path / "sentinel.txt"
sentinel_path.write_bytes(b"keep me\n")
result = run_cli(config_path, output_path)
assert result.returncode == 2
assert (
"refusing to replace an output directory not created by this tool"
in result.stderr
)
assert sentinel_path.read_bytes() == b"keep me\n"
def test_publish_restores_the_previous_output_if_replacement_fails(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
staging_path = tmp_path / "staging"
(staging_path / "hagezi").mkdir(parents=True)
(staging_path / "hagezi" / "pro.txt").write_bytes(b"new\n")
output_path = tmp_path / "public"
output_path.mkdir()
previous_path = output_path / "previous.txt"
previous_path.write_bytes(b"previous\n")
_ownership_path(output_path).write_text(
"dns-blocklist-mirror output\n",
encoding="utf-8",
)
import dns_blocklist_mirror.cli as cli
real_copytree = cli.shutil.copytree
copy_count = 0
def fail_replacement(source: Path, destination: Path) -> Path:
nonlocal copy_count
copy_count += 1
if copy_count == 2:
raise OSError("simulated replacement failure")
return real_copytree(source, destination)
monkeypatch.setattr(cli.shutil, "copytree", fail_replacement)
with pytest.raises(OSError, match="previous output was restored"):
_publish_output(staging_path, output_path)
assert previous_path.read_bytes() == b"previous\n"
assert not (output_path / "hagezi" / "pro.txt").exists()