|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import os |
| 4 | +import shlex |
4 | 5 | import shutil |
5 | 6 | import subprocess |
6 | 7 | import sys |
7 | 8 | from argparse import Namespace |
| 9 | +from types import SimpleNamespace |
| 10 | +from typing import TYPE_CHECKING |
8 | 11 |
|
9 | 12 | import pytest |
10 | 13 |
|
11 | 14 | from virtualenv.activation import FishActivator |
12 | 15 | from virtualenv.info import IS_WIN |
13 | 16 |
|
| 17 | +if TYPE_CHECKING: |
| 18 | + from collections.abc import Callable |
| 19 | + from pathlib import Path |
| 20 | + |
14 | 21 | FISH = shutil.which("fish") |
15 | 22 |
|
16 | 23 |
|
| 24 | +@pytest.fixture |
| 25 | +def rendered_activate_fish(tmp_path: Path) -> Callable[[str | None, str | None], Path]: |
| 26 | + def render(tcl_lib: str | None, tk_lib: str | None) -> Path: |
| 27 | + creator = SimpleNamespace( |
| 28 | + dest=tmp_path, |
| 29 | + bin_dir=tmp_path / "bin", |
| 30 | + interpreter=SimpleNamespace(tcl_lib=tcl_lib, tk_lib=tk_lib), |
| 31 | + pyenv_cfg={}, |
| 32 | + env_name="my-env", |
| 33 | + ) |
| 34 | + creator.bin_dir.mkdir() |
| 35 | + FishActivator(Namespace(prompt=None)).generate(creator) |
| 36 | + return creator.bin_dir / "activate.fish" |
| 37 | + |
| 38 | + return render |
| 39 | + |
| 40 | + |
17 | 41 | @pytest.mark.parametrize( |
18 | 42 | ("tcl_lib", "tk_lib", "present"), |
19 | 43 | [ |
20 | 44 | ("/path/to/tcl", "/path/to/tk", True), |
| 45 | + ("/Program Files/tcl", "/Program Files/tk", True), |
21 | 46 | (None, None, False), |
22 | 47 | ], |
23 | 48 | ) |
24 | | -def test_fish_tkinter_generation(tmp_path, tcl_lib, tk_lib, present) -> None: |
25 | | - # GIVEN |
26 | | - class MockInterpreter: |
27 | | - pass |
28 | | - |
29 | | - interpreter = MockInterpreter() |
30 | | - interpreter.tcl_lib = tcl_lib |
31 | | - interpreter.tk_lib = tk_lib |
32 | | - |
33 | | - class MockCreator: |
34 | | - def __init__(self, dest) -> None: |
35 | | - self.dest = dest |
36 | | - self.bin_dir = dest / "bin" |
37 | | - self.bin_dir.mkdir() |
38 | | - self.interpreter = interpreter |
39 | | - self.pyenv_cfg = {} |
40 | | - self.env_name = "my-env" |
41 | | - |
42 | | - creator = MockCreator(tmp_path) |
43 | | - options = Namespace(prompt=None) |
44 | | - activator = FishActivator(options) |
45 | | - |
46 | | - # WHEN |
47 | | - activator.generate(creator) |
48 | | - content = (creator.bin_dir / "activate.fish").read_text(encoding="utf-8") |
49 | | - |
50 | | - # THEN |
51 | | - # PKG_CONFIG_PATH is always set |
| 49 | +def test_fish_tkinter_generation( |
| 50 | + rendered_activate_fish: Callable[[str | None, str | None], Path], |
| 51 | + tcl_lib: str | None, |
| 52 | + tk_lib: str | None, |
| 53 | + present: bool, |
| 54 | +) -> None: |
| 55 | + content = rendered_activate_fish(tcl_lib, tk_lib).read_text(encoding="utf-8") |
| 56 | + |
52 | 57 | assert 'set -gx _OLD_PKG_CONFIG_PATH "$PKG_CONFIG_PATH"' in content |
53 | 58 | assert 'set -gx PKG_CONFIG_PATH "$VIRTUAL_ENV/lib/pkgconfig:$PKG_CONFIG_PATH"' in content |
54 | 59 | assert "set -e _OLD_PKG_CONFIG_PATH" in content |
55 | 60 |
|
56 | 61 | if present: |
57 | | - assert "set -gx TCL_LIBRARY '/path/to/tcl'" in content |
58 | | - assert "set -gx TK_LIBRARY '/path/to/tk'" in content |
| 62 | + assert f"set -gx TCL_LIBRARY {shlex.quote(tcl_lib)}\n" in content |
| 63 | + assert f"set -gx TK_LIBRARY {shlex.quote(tk_lib)}\n" in content |
59 | 64 | else: |
60 | 65 | assert "if test -n ''\n set -gx _OLD_VIRTUAL_TCL_LIBRARY" in content |
61 | 66 | assert "if test -n ''\n set -gx _OLD_VIRTUAL_TK_LIBRARY" in content |
62 | 67 |
|
63 | 68 |
|
| 69 | +@pytest.mark.skipif(IS_WIN, reason="fish is not available on Windows") |
| 70 | +@pytest.mark.skipif(FISH is None, reason="fish is not installed") |
| 71 | +def test_fish_tkinter_path_does_not_run_commands( |
| 72 | + rendered_activate_fish: Callable[[str | None, str | None], Path], tmp_path: Path |
| 73 | +) -> None: |
| 74 | + marker = tmp_path / "PWNED" |
| 75 | + script = rendered_activate_fish(f"/tcl/(touch {marker})/lib", "/tk/lib") |
| 76 | + |
| 77 | + subprocess.run([FISH, "-c", f"source '{script}'"], capture_output=True, text=True, timeout=60, check=False) |
| 78 | + |
| 79 | + assert not marker.exists() |
| 80 | + |
| 81 | + |
64 | 82 | @pytest.mark.skipif(IS_WIN, reason="fish is not available on Windows") |
65 | 83 | @pytest.mark.skipif(FISH is None, reason="fish is not installed") |
66 | 84 | def test_fish_prompt_survives_shadowed_source(activation_python, tmp_path) -> None: |
|
0 commit comments