Skip to content

Commit d3e25cc

Browse files
committed
test: exec the codespell executable consistently
Currently, in test_basic.py, the run_codespell and run_codespell_stdin functions exec the codespell script from PATH, not the one from the current directory. This means that the codespell_lib package MUST first be installed, before running pytest. The current behavior is unsafe, since an user running pytest in the global environment may get an error or, worse, may actually try to test an old version. Additionally this behavior is not documented in README.md. Update the run_codespell and run_codespell_stdin functions to exec the codespell script via `python -m codespell_lib`. This change will ensure that python will try to search the __main__ module from the current directory first. Copy the codespell_lib directory to the cwd directory, and configure codespell to ignore it using the `-S` option, in order to make the environment clean. Ensure that the cwd parameter in run_codespell and run_codespell_stdin is never None, since the codespell script must not be executed from the current directory. For consistency, also make the args parameter required.
1 parent f6a0fcb commit d3e25cc

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

codespell_lib/tests/test_basic.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import sys
88
from io import StringIO
99
from pathlib import Path
10-
from shutil import copyfile
11-
from typing import Any, Generator, Optional, Tuple, Union
10+
from shutil import copyfile, copytree
11+
from typing import Any, Generator, Tuple, Union
1212

1313
import pytest
1414

@@ -64,13 +64,15 @@ def main(
6464

6565

6666
def run_codespell(
67-
args: Tuple[Any, ...] = (),
68-
cwd: Optional[Path] = None,
67+
args: Tuple[Any, ...],
68+
cwd: Path,
6969
) -> int:
7070
"""Run codespell."""
71+
lib = "codespell_lib"
72+
copytree(lib, cwd / lib, dirs_exist_ok=True)
7173
args = tuple(str(arg) for arg in args)
7274
proc = subprocess.run(
73-
["codespell", "--count", *args], # noqa: S603, S607
75+
[sys.executable, "-m", lib, "-S", lib, "--count", *args], # noqa: S603, S607
7476
cwd=cwd,
7577
capture_output=True,
7678
encoding="utf-8",
@@ -83,9 +85,9 @@ def run_codespell(
8385
def test_command(tmp_path: Path) -> None:
8486
"""Test running the codespell executable."""
8587
# With no arguments does "."
86-
assert run_codespell(cwd=tmp_path) == 0
88+
assert run_codespell(args=(), cwd=tmp_path) == 0
8789
(tmp_path / "bad.txt").write_text("abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd")
88-
assert run_codespell(cwd=tmp_path) == 4
90+
assert run_codespell(args=(), cwd=tmp_path) == 4
8991

9092

9193
def test_basic(
@@ -1196,11 +1198,13 @@ def FakeStdin(text: str) -> Generator[None, None, None]:
11961198
def run_codespell_stdin(
11971199
text: str,
11981200
args: Tuple[Any, ...],
1199-
cwd: Optional[Path] = None,
1201+
cwd: Path,
12001202
) -> int:
12011203
"""Run codespell in stdin mode and return number of lines in output."""
1204+
lib = "codespell_lib"
1205+
copytree(lib, cwd / lib, dirs_exist_ok=True)
12021206
proc = subprocess.run(
1203-
["codespell", *args, "-"], # noqa: S603, S607
1207+
[sys.executable, "-m", lib, "-S", lib, *args, "-"], # noqa: S603, S607
12041208
cwd=cwd,
12051209
input=text,
12061210
capture_output=True,

0 commit comments

Comments
 (0)