88here = Path (__file__ ).parent
99
1010
11+ def test_node_executable (monkeypatch ):
12+ monkeypatch .setattr (
13+ shutil , "which" , lambda x : "/usr/bin/nodejs" if x == "nodejs" else None
14+ )
15+ search .node_executable .cache_clear ()
16+ assert search .node_executable () == "nodejs"
17+
18+ monkeypatch .setattr (
19+ shutil , "which" , lambda x : "/usr/bin/node" if x == "node" else None
20+ )
21+ search .node_executable .cache_clear ()
22+ assert search .node_executable () == "node"
23+
24+ monkeypatch .setattr (shutil , "which" , lambda _ : None )
25+ search .node_executable .cache_clear ()
26+ assert search .node_executable () is None
27+
28+
1129def test_precompile_index (monkeypatch , capsys ):
1230 docs = [
1331 {
@@ -18,28 +36,25 @@ def test_precompile_index(monkeypatch, capsys):
1836 "doc" : "a" * 3 * 1024 * 1024 , # we only warn if index size is meaningful.
1937 }
2038 ]
39+ raw = json .dumps (docs )
2140 compile_js = here / ".." / "pdoc" / "templates" / "build-search-index.js"
2241
2342 monkeypatch .setattr (subprocess , "check_output" , lambda * _ , ** __ : '{"foo": 42}' )
43+ monkeypatch .setattr (search , "node_executable" , lambda : "nodejs" )
2444 assert (
2545 search .precompile_index (docs , compile_js )
2646 == '{"foo": 42, "_isPrebuiltIndex": true}'
2747 )
2848
29- monkeypatch .setattr (search , "node_executable" , lambda : "C:\\ nodejs.exe" )
30- assert (
31- search .precompile_index (docs , compile_js )
32- == '{"foo": 42, "_isPrebuiltIndex": true}'
33- )
3449 monkeypatch .setattr (search , "node_executable" , lambda : None )
35- assert (
36- search .precompile_index (docs , compile_js )
37- == json .dumps (docs )
38- )
50+ assert search .precompile_index (docs , compile_js ) == raw
3951
4052 def _raise (* _ , ** __ ):
4153 raise subprocess .CalledProcessError (- 1 , ["cmd" ], b"nodejs error" )
4254
55+ monkeypatch .setattr (search , "node_executable" , lambda : "node" )
4356 monkeypatch .setattr (subprocess , "check_output" , _raise )
44- assert search .precompile_index (docs , compile_js ) == json .dumps (docs )
45- assert "pdoc failed to precompile the search index" in capsys .readouterr ().out
57+ assert search .precompile_index (docs , compile_js ) == raw
58+ out = capsys .readouterr ().out
59+ assert "pdoc failed to precompile the search index" in out
60+ assert "Node.js Output" in out
0 commit comments