|
| 1 | +from sphinx_js.analyzer_utils import search_node_modules |
| 2 | +import pytest |
| 3 | +from pathlib import Path |
| 4 | +from sphinx.errors import SphinxError |
| 5 | + |
| 6 | +@pytest.fixture |
| 7 | +def global_install(tmp_path_factory, monkeypatch): |
| 8 | + tmpdir = tmp_path_factory.mktemp("my_program_global") |
| 9 | + my_program = tmpdir/ "my_program" |
| 10 | + my_program.write_text("") |
| 11 | + my_program.chmod(0o777) |
| 12 | + monkeypatch.setenv("PATH", tmpdir, prepend=":") |
| 13 | + return tmpdir |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def no_local_install(tmp_path_factory): |
| 18 | + my_program = tmp_path_factory.mktemp("my_program_local") |
| 19 | + working_dir = my_program / "a" / "b" / "c" |
| 20 | + return working_dir |
| 21 | + |
| 22 | +my_prog_path = Path("my_program/sub/bin.js") |
| 23 | + |
| 24 | +@pytest.fixture |
| 25 | +def local_install(no_local_install): |
| 26 | + working_dir = no_local_install |
| 27 | + bin_path = working_dir.parents[1] / "node_modules" / my_prog_path |
| 28 | + bin_path.parent.mkdir(parents=True) |
| 29 | + bin_path.write_text("") |
| 30 | + return (working_dir, bin_path) |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def env_install(monkeypatch): |
| 34 | + env_path = Path("/a/b/c") |
| 35 | + monkeypatch.setenv("SPHINX_JS_NODE_MODULES", env_path) |
| 36 | + return env_path / my_prog_path |
| 37 | + |
| 38 | + |
| 39 | +def test_global(global_install, no_local_install): |
| 40 | + # If no env or local, use global |
| 41 | + working_dir = no_local_install |
| 42 | + assert search_node_modules("my_program", my_prog_path, working_dir) == str(global_install / "my_program") |
| 43 | + |
| 44 | +def test_node_modules1(global_install, local_install): |
| 45 | + # If local and global, use local |
| 46 | + [working_dir, bin_path] = local_install |
| 47 | + assert search_node_modules("my_program", my_prog_path, working_dir) == str(bin_path) |
| 48 | + |
| 49 | +def test_node_modules2(local_install): |
| 50 | + # If local only, use local |
| 51 | + [working_dir, bin_path] = local_install |
| 52 | + assert search_node_modules("my_program", my_prog_path, working_dir) == str(bin_path) |
| 53 | + |
| 54 | +def test_env1(env_install): |
| 55 | + # If env only, use env |
| 56 | + assert search_node_modules("my_program", my_prog_path, "/x/y/z") == str(env_install) |
| 57 | + |
| 58 | +def test_env2(env_install, local_install, global_install): |
| 59 | + # If env, local, and global, use env |
| 60 | + [working_dir, _] = local_install |
| 61 | + assert search_node_modules("my_program", my_prog_path, working_dir) == str(env_install) |
| 62 | + |
| 63 | +def test_err(): |
| 64 | + with pytest.raises(SphinxError, match='my_program was not found. Install it using "npm install my_program"'): |
| 65 | + search_node_modules("my_program", my_prog_path, "/a/b/c") |
0 commit comments