|
| 1 | +"""Benchmarks for pyautoenv's main function.""" |
| 2 | + |
| 3 | +from io import StringIO |
| 4 | +from pathlib import Path |
| 5 | +from typing import Union |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +import pyautoenv |
| 10 | +from benches.conftest import PoetryVenvFixture |
| 11 | +from benches.tools import make_venv, venv_active, working_directory |
| 12 | +from tests.tools import clear_lru_caches |
| 13 | + |
| 14 | + |
| 15 | +class ResettingStream(StringIO): |
| 16 | + """ |
| 17 | + A writable stream that resets its position to 0 after each write. |
| 18 | +
|
| 19 | + We can use this in benchmarks to check what's written to the stream |
| 20 | + in the final iteration. |
| 21 | + """ |
| 22 | + |
| 23 | + def write(self, s): |
| 24 | + r = super().write(s) |
| 25 | + self.seek(0) |
| 26 | + return r |
| 27 | + |
| 28 | + |
| 29 | +def run_main_benchmark(benchmark, *, shell: Union[str, None] = None): |
| 30 | + stream = ResettingStream() |
| 31 | + argv = [] |
| 32 | + if shell: |
| 33 | + argv.append(f"--{shell}") |
| 34 | + benchmark(pyautoenv.main, argv, stdout=stream) |
| 35 | + clear_lru_caches(pyautoenv) |
| 36 | + return stream.getvalue() |
| 37 | + |
| 38 | + |
| 39 | +def test_no_activation(benchmark, tmp_path: Path): |
| 40 | + with working_directory(tmp_path): |
| 41 | + assert not run_main_benchmark(benchmark) |
| 42 | + |
| 43 | + |
| 44 | +def test_deactivate(benchmark, venv: Path, tmp_path: Path): |
| 45 | + with venv_active(venv), working_directory(tmp_path): |
| 46 | + assert run_main_benchmark(benchmark) == "deactivate" |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize("shell", [None, "fish", "pwsh"]) |
| 50 | +def test_venv_activate(shell, benchmark, venv: Path): |
| 51 | + with working_directory(venv): |
| 52 | + output = run_main_benchmark(benchmark, shell=shell) |
| 53 | + |
| 54 | + assert all(s in output.lower() for s in ["activate", str(venv).lower()]), ( |
| 55 | + output |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def test_venv_already_active(benchmark, venv: Path): |
| 60 | + with venv_active(venv), working_directory(venv): |
| 61 | + assert not run_main_benchmark(benchmark) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("shell", [None, "fish", "pwsh"]) |
| 65 | +def test_venv_switch_venv(shell, benchmark, venv: Path, tmp_path: Path): |
| 66 | + make_venv(tmp_path) |
| 67 | + |
| 68 | + with venv_active(venv), working_directory(tmp_path): |
| 69 | + output = run_main_benchmark(benchmark, shell=shell) |
| 70 | + |
| 71 | + assert all( |
| 72 | + s in output for s in ["deactivate", "&&", str(tmp_path), "activate"] |
| 73 | + ), output |
| 74 | + |
| 75 | + |
| 76 | +@pytest.mark.parametrize("shell", [None, "fish", "pwsh"]) |
| 77 | +def test_poetry_activate(shell, benchmark, poetry_venv: PoetryVenvFixture): |
| 78 | + with working_directory(poetry_venv.project_dir): |
| 79 | + output = run_main_benchmark(benchmark, shell=shell) |
| 80 | + |
| 81 | + assert "activate" in output.lower() |
| 82 | + assert str(poetry_venv.venv_dir).lower() in output.lower() |
| 83 | + |
| 84 | + |
| 85 | +def test_poetry_already_active(benchmark, poetry_venv: PoetryVenvFixture): |
| 86 | + with ( |
| 87 | + venv_active(poetry_venv.venv_dir), |
| 88 | + working_directory(poetry_venv.project_dir), |
| 89 | + ): |
| 90 | + assert not run_main_benchmark(benchmark) |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize("shell", [None, "fish", "pwsh"]) |
| 94 | +def test_venv_switch_to_poetry( |
| 95 | + shell, benchmark, poetry_venv: PoetryVenvFixture, venv: Path |
| 96 | +): |
| 97 | + with venv_active(venv), working_directory(poetry_venv.project_dir): |
| 98 | + output = run_main_benchmark(benchmark, shell=shell) |
| 99 | + |
| 100 | + assert all( |
| 101 | + s in output |
| 102 | + for s in ["deactivate", "&&", str(poetry_venv.venv_dir), "activate"] |
| 103 | + ), output |
| 104 | + |
| 105 | + |
| 106 | +@pytest.mark.parametrize("shell", [None, "fish", "pwsh"]) |
| 107 | +def test_poetry_switch_to_venv( |
| 108 | + shell, benchmark, poetry_venv: PoetryVenvFixture, venv: Path |
| 109 | +): |
| 110 | + with venv_active(poetry_venv.venv_dir), working_directory(venv): |
| 111 | + output = run_main_benchmark(benchmark, shell=shell) |
| 112 | + |
| 113 | + assert all( |
| 114 | + s in output for s in ["deactivate", "&&", str(venv), "activate"] |
| 115 | + ), output |
0 commit comments