Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bigos/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ def parse(
else _BACKENDS[backend_name](cache=cache)
)
mime = mimetypes.guess_type(path)[0] or "application/octet-stream"
source = Source(uri=path.as_uri(), mime_type=mime, sha256=sha256_file(path))
abs_path = path.resolve()
source = Source(uri=abs_path.as_uri(), mime_type=mime, sha256=sha256_file(abs_path))
t0 = time.perf_counter()
doc = asyncio.run(backend.run(source))
elapsed = time.perf_counter() - t0
Expand Down
44 changes: 44 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from pathlib import Path

import pytest
Expand Down Expand Up @@ -31,6 +32,49 @@ def test_cli_unknown_format(tmp_path: Path) -> None:
assert "Unknown format" in result.stderr


def test_cli_relative_path_does_not_crash_with_value_error(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Regression: ``bigos parse some/relative.pdf`` previously raised
``ValueError: relative path can't be expressed as a file URI`` because the
CLI passed a non-resolved Path straight to ``Path.as_uri()``."""
from bigos import cli as cli_mod
from bigos.schema import Block, Document, Source

pdf = _minimal_pdf(tmp_path)
monkeypatch.chdir(tmp_path)
rel = Path(pdf.name)
assert not rel.is_absolute()

captured: dict[str, Source] = {}

class _StubBackend:
name = "stub"
version = "0"

def __init__(self, *args: object, **kwargs: object) -> None:
pass

async def run(self, source: Source) -> Document:
captured["source"] = source
return Document(
source=source, blocks=[Block(kind="paragraph", text="ok")]
)

monkeypatch.setitem(cli_mod._BACKENDS, "stub", _StubBackend) # type: ignore[arg-type]

result = runner.invoke(
app,
["parse", str(rel), "--no-cache", "--backend=stub", "--format=md"],
catch_exceptions=False,
)
assert result.exit_code == 0
assert "ok" in result.stdout
src = captured["source"]
assert src.uri.startswith("file://")
assert os.path.isabs(src.uri.removeprefix("file://"))


@pytest.mark.slow
def test_cli_parse_md(simple_text_pdf: Path) -> None:
result = runner.invoke(app, ["parse", str(simple_text_pdf), "--format=md"])
Expand Down
Loading