HexCasting/doc/test/test_snapshots.py

73 lines
2 KiB
Python
Raw Normal View History

import subprocess
import sys
from dataclasses import dataclass
2023-06-06 15:24:18 +02:00
from pathlib import Path
from typing import Any, Iterator
2023-06-06 15:24:18 +02:00
import pytest
from main import Args, main
2023-06-06 15:24:18 +02:00
from syrupy.assertion import SnapshotAssertion
from syrupy.extensions.amber import AmberSnapshotExtension
from syrupy.types import SerializedData
class NoDiffSnapshotExtension(AmberSnapshotExtension):
def diff_snapshots(
self, serialized_data: SerializedData, snapshot_data: SerializedData
) -> SerializedData:
return "diff-is-disabled".encode()
def diff_lines(
self, serialized_data: SerializedData, snapshot_data: SerializedData
) -> Iterator[str]:
return iter(["diff-is-disabled"])
2023-06-06 15:24:18 +02:00
@dataclass
class DocgenArgs:
out_path: Path
snapshot: SnapshotAssertion
argv: list[str]
def assert_out_path(self):
2023-06-11 01:14:53 +02:00
actual = self.out_path.read_text("utf-8")
assert actual == self.snapshot
@pytest.fixture
def docgen(tmp_path: Path, snapshot: SnapshotAssertion) -> DocgenArgs:
# arguments we want to pass to the docgen
2023-06-06 15:24:18 +02:00
out_path = tmp_path / "out.html"
return DocgenArgs(
out_path,
snapshot.use_extension(NoDiffSnapshotExtension),
[
"../Common/src/main/resources",
"hexcasting",
"thehexbook",
"template.html",
out_path.as_posix(),
],
)
def test_file(docgen: DocgenArgs):
# generate output docs html file and assert it hasn't changed vs. the snapshot
main(Args().parse_args(docgen.argv))
docgen.assert_out_path()
def test_cmd(docgen: DocgenArgs):
# as above, but running the command we actually want to be using
subprocess.run(
[sys.executable, "src/main.py"] + docgen.argv,
stdout=sys.stdout,
stderr=sys.stderr,
)
docgen.assert_out_path()
2023-06-07 05:01:25 +02:00
def test_stdout(docgen: DocgenArgs, capsys: pytest.CaptureFixture[str]):
main(Args().parse_args(docgen.argv[:-1]))
assert capsys.readouterr() == docgen.snapshot