File: test_search.py

package info (click to toggle)
python-pdoc 15.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,192 kB
  • sloc: python: 8,013; javascript: 1,156; makefile: 18; sh: 3
file content (45 lines) | stat: -rw-r--r-- 1,400 bytes parent folder | download
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