187 lines
7.4 KiB
Python
187 lines
7.4 KiB
Python
import json
|
|
import shutil
|
|
import subprocess
|
|
|
|
import pytest
|
|
|
|
from adf_codec import convert
|
|
from failure import Failure
|
|
|
|
DOC = {"type": "doc", "version": 1, "content": [{"type": "paragraph", "content": [{"type": "text", "text": "Hi"}]}]}
|
|
|
|
|
|
def fake(status: int, out: str = "", err: str = ""):
|
|
calls: list[tuple[str, str]] = []
|
|
|
|
def run(mode: str, text: str) -> convert.Ran:
|
|
calls.append((mode, text))
|
|
return convert.Ran(status, out, err)
|
|
|
|
run.calls = calls # type: ignore[attr-defined]
|
|
return run
|
|
|
|
|
|
def test_markdown_to_adf_returns_the_document_the_shim_printed():
|
|
run = fake(0, json.dumps(DOC))
|
|
assert convert.markdown_to_adf("Hi\n", run) == DOC
|
|
assert run.calls == [("to-adf", "Hi\n")]
|
|
|
|
|
|
def test_adf_to_markdown_sends_the_document_as_json_and_returns_the_text():
|
|
run = fake(0, "Hi\n")
|
|
assert convert.adf_to_markdown(DOC, run) == "Hi\n"
|
|
assert run.calls == [("to-markdown", json.dumps(DOC))]
|
|
|
|
|
|
def test_a_refusal_becomes_a_failure_with_adf_codecs_code_and_message():
|
|
refusal = {"error": {"code": "malformed-pipe-table", "message": "row has too many cells", "path": [], "position": {"line": 3, "offset": 9}}}
|
|
failure = convert.markdown_to_adf("| a |\n", fake(2, json.dumps(refusal)))
|
|
assert failure == Failure("adf-codec refused the input with malformed-pipe-table: row has too many cells (at line 3)")
|
|
|
|
|
|
def test_a_refusal_without_a_position_names_the_path():
|
|
refusal = {"error": {"code": "unsupported-node-shape", "message": "bad node", "path": ["content", 0]}}
|
|
failure = convert.adf_to_markdown(DOC, fake(2, json.dumps(refusal)))
|
|
assert failure == Failure("adf-codec refused the input with unsupported-node-shape: bad node (at content.0)")
|
|
|
|
|
|
def test_a_shim_that_could_not_run_becomes_a_failure_with_its_stderr():
|
|
failure = convert.adf_to_markdown(DOC, fake(1, "", "Unexpected token"))
|
|
assert failure == Failure("the adf-codec shim failed with exit 1: Unexpected token")
|
|
|
|
|
|
def test_a_shim_killed_for_memory_becomes_a_failure_with_its_exit_status():
|
|
failure = convert.markdown_to_adf("x", fake(137))
|
|
assert isinstance(failure, Failure) and "137" in failure.reason
|
|
|
|
|
|
def test_output_that_is_not_json_becomes_a_failure():
|
|
failure = convert.markdown_to_adf("x", fake(0, "not json"))
|
|
assert failure == Failure("the adf-codec shim printed output that is not JSON: not json")
|
|
|
|
|
|
def test_a_refusal_that_is_not_the_expected_json_becomes_a_failure():
|
|
failure = convert.markdown_to_adf("x", fake(2, "oops"))
|
|
assert failure == Failure("the adf-codec shim refused with output that is not an error: oops")
|
|
|
|
|
|
def test_a_failure_from_the_runner_passes_through():
|
|
def run(mode: str, text: str) -> Failure:
|
|
return Failure("docker is not installed")
|
|
|
|
assert convert.markdown_to_adf("x", run) == Failure("docker is not installed")
|
|
|
|
|
|
def test_the_image_tag_changes_with_the_shim_files(tmp_path):
|
|
for name in convert.SHIM_FILES:
|
|
(tmp_path / name).write_text("a")
|
|
before = convert.image_tag(tmp_path)
|
|
(tmp_path / "shim.mjs").write_text("b")
|
|
assert convert.image_tag(tmp_path) != before
|
|
|
|
|
|
def test_the_container_command_line_is_exactly_this():
|
|
assert convert.docker_args("image:1", "to-adf") == [
|
|
"docker", "run", "--rm", "-i", "--memory", "256m", "--memory-swap", "256m", "--network", "none", "--read-only", "image:1", "to-adf",
|
|
]
|
|
|
|
|
|
def test_a_refusal_at_the_root_says_the_document():
|
|
refusal = {"error": {"code": "not-an-adf-document", "message": "no type", "path": []}}
|
|
failure = convert.adf_to_markdown(DOC, fake(2, json.dumps(refusal)))
|
|
assert failure == Failure("adf-codec refused the input with not-an-adf-document: no type (at the document)")
|
|
|
|
|
|
def test_docker_passes_the_text_on_stdin_and_returns_the_output():
|
|
seen = {}
|
|
|
|
def call(args, **kwargs):
|
|
seen.update(kwargs, args=args)
|
|
return subprocess.CompletedProcess(args, 3, "out", "err")
|
|
|
|
assert convert.docker(["docker", "x"], "text", 7, call) == convert.Ran(3, "out", "err")
|
|
assert seen == {"args": ["docker", "x"], "input": "text", "capture_output": True, "text": True, "encoding": "utf-8", "timeout": 7}
|
|
|
|
|
|
def test_docker_missing_becomes_a_failure():
|
|
def call(args, **kwargs):
|
|
raise FileNotFoundError
|
|
|
|
failure = convert.docker(["docker", "x"], "", 7, call)
|
|
assert failure == Failure("docker is not installed, and the adf-codec shim runs in a container")
|
|
|
|
|
|
def test_docker_that_takes_too_long_becomes_a_failure():
|
|
def call(args, **kwargs):
|
|
raise subprocess.TimeoutExpired(args, 7)
|
|
|
|
assert convert.docker(["docker", "build", "x"], "", 7, call) == Failure("`docker build` took more than 7 seconds")
|
|
|
|
|
|
def shim_dir(tmp_path):
|
|
for name in convert.SHIM_FILES:
|
|
(tmp_path / name).write_text("a")
|
|
return tmp_path
|
|
|
|
|
|
def scripted(*answers):
|
|
lines: list[tuple[list[str], str, int]] = []
|
|
queue = list(answers)
|
|
|
|
def run_docker(args, text, seconds):
|
|
lines.append((args, text, seconds))
|
|
return queue.pop(0)
|
|
|
|
run_docker.lines = lines # type: ignore[attr-defined]
|
|
return run_docker
|
|
|
|
|
|
def test_an_image_that_exists_is_not_built(tmp_path):
|
|
run_docker = scripted(convert.Ran(0, "", ""))
|
|
tag = convert.image_tag(shim_dir(tmp_path))
|
|
assert convert.ensure_image(tmp_path, run_docker) == tag
|
|
assert run_docker.lines == [(["docker", "image", "inspect", tag], "", convert.RUN_SECONDS)]
|
|
|
|
|
|
def test_a_missing_image_is_built_from_the_shim_folder_with_a_memory_limit(tmp_path):
|
|
run_docker = scripted(convert.Ran(1, "", ""), convert.Ran(0, "", ""))
|
|
tag = convert.image_tag(shim_dir(tmp_path))
|
|
assert convert.ensure_image(tmp_path, run_docker) == tag
|
|
assert run_docker.lines[1] == (["docker", "build", "--quiet", "--memory", "1g", "--memory-swap", "1g", "--tag", tag, str(tmp_path)], "", convert.BUILD_SECONDS)
|
|
|
|
|
|
def test_a_failed_build_becomes_a_failure_with_its_stderr(tmp_path):
|
|
run_docker = scripted(convert.Ran(1, "", ""), convert.Ran(5, "", "no network\n"))
|
|
shim_dir(tmp_path)
|
|
assert convert.ensure_image(tmp_path, run_docker) == Failure("building the adf-codec shim's image failed with exit 5: no network")
|
|
|
|
|
|
def test_a_failure_to_start_docker_passes_through_inspect_and_build(tmp_path):
|
|
shim_dir(tmp_path)
|
|
down = Failure("docker is not installed")
|
|
assert convert.ensure_image(tmp_path, scripted(down)) == down
|
|
assert convert.ensure_image(tmp_path, scripted(convert.Ran(1, "", ""), down)) == down
|
|
|
|
|
|
def test_container_run_runs_the_image_on_the_text(tmp_path):
|
|
run_docker = scripted(convert.Ran(0, "", ""), convert.Ran(0, "answer", ""))
|
|
assert convert.container_run("to-adf", "text", run_docker, shim_dir(tmp_path)) == convert.Ran(0, "answer", "")
|
|
assert run_docker.lines[1] == (convert.docker_args(convert.image_tag(tmp_path), "to-adf"), "text", convert.RUN_SECONDS)
|
|
|
|
|
|
def test_container_run_passes_an_image_failure_through(tmp_path):
|
|
down = Failure("docker is not installed")
|
|
assert convert.container_run("to-adf", "text", scripted(down), shim_dir(tmp_path)) == down
|
|
|
|
|
|
@pytest.mark.skipif(shutil.which("docker") is None or subprocess.run(["docker", "info"], capture_output=True, timeout=20).returncode != 0, reason="needs a docker daemon")
|
|
def test_round_trip_through_the_real_container():
|
|
doc = convert.markdown_to_adf("# Title\n\nSome **bold** text.\n")
|
|
assert isinstance(doc, dict) and doc["type"] == "doc" and doc["content"][0]["type"] == "heading"
|
|
markdown = convert.adf_to_markdown(doc)
|
|
assert isinstance(markdown, str) and markdown.startswith("# Title") and "**bold**" in markdown
|
|
refused = convert.adf_to_markdown({"type": "nope"})
|
|
assert isinstance(refused, Failure) and "not-an-adf-document" in refused.reason
|
|
crashed = convert.answer("to-markdown", "not json", convert.container_run)
|
|
assert isinstance(crashed, Failure) and crashed.reason.startswith("the adf-codec shim failed with exit 1: ")
|