1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import json
from pathlib import Path
import shutil
import subprocess
from pdoc import search
here = Path(__file__).parent
def test_precompile_index(monkeypatch, capsys):
docs = [
{
"fullname": "test.Test",
"modulename": "test",
"qualname": "Test",
"type": "class",
"doc": "a" * 3 * 1024 * 1024, # we only warn if index size is meaningful.
}
]
compile_js = here / ".." / "pdoc" / "templates" / "build-search-index.js"
monkeypatch.setattr(subprocess, "check_output", lambda *_, **__: '{"foo": 42}')
assert (
search.precompile_index(docs, compile_js)
== '{"foo": 42, "_isPrebuiltIndex": true}'
)
monkeypatch.setattr(shutil, "which", lambda _: "C:\\nodejs.exe")
assert (
search.precompile_index(docs, compile_js)
== '{"foo": 42, "_isPrebuiltIndex": true}'
)
monkeypatch.setattr(shutil, "which", lambda _: None)
assert (
search.precompile_index(docs, compile_js)
== '{"foo": 42, "_isPrebuiltIndex": true}'
)
def _raise(*_, **__):
raise subprocess.CalledProcessError(-1, ["cmd"], b"nodejs error")
monkeypatch.setattr(subprocess, "check_output", _raise)
assert search.precompile_index(docs, compile_js) == json.dumps(docs)
assert "pdoc failed to precompile the search index" in capsys.readouterr().out
|