Skip to content

Commit

Permalink
test: exec the codespell executable consistently
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
perillo committed Jan 4, 2024
1 parent f6a0fcb commit 122f744
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sys
from io import StringIO
from pathlib import Path
from shutil import copyfile
from shutil import copyfile, copytree
from typing import Any, Generator, Optional, Tuple, Union

import pytest
Expand Down Expand Up @@ -64,13 +64,15 @@ def main(


def run_codespell(
args: Tuple[Any, ...] = (),
cwd: Optional[Path] = None,
args: Tuple[Any, ...],
cwd: Path,
) -> int:
"""Run codespell."""
lib = "codespell_lib"
copytree(lib, cwd / lib, dirs_exist_ok=True)
args = tuple(str(arg) for arg in args)
proc = subprocess.run(
["codespell", "--count", *args], # noqa: S603, S607
[sys.executable, "-m", lib, "-S", lib, "--count", *args], # noqa: S603, S607
cwd=cwd,
capture_output=True,
encoding="utf-8",
Expand All @@ -83,9 +85,9 @@ def run_codespell(
def test_command(tmp_path: Path) -> None:
"""Test running the codespell executable."""
# With no arguments does "."
assert run_codespell(cwd=tmp_path) == 0
assert run_codespell(args=(), cwd=tmp_path) == 0
(tmp_path / "bad.txt").write_text("abandonned\nAbandonned\nABANDONNED\nAbAnDoNnEd")
assert run_codespell(cwd=tmp_path) == 4
assert run_codespell(args=(), cwd=tmp_path) == 4


def test_basic(
Expand Down Expand Up @@ -1196,11 +1198,13 @@ def FakeStdin(text: str) -> Generator[None, None, None]:
def run_codespell_stdin(
text: str,
args: Tuple[Any, ...],
cwd: Optional[Path] = None,
cwd: Path,
) -> int:
"""Run codespell in stdin mode and return number of lines in output."""
lib = "codespell_lib"
copytree(lib, cwd / lib, dirs_exist_ok=True)
proc = subprocess.run(
["codespell", *args, "-"], # noqa: S603, S607
[sys.executable, "-m", lib, "-S", lib, *args, "-"], # noqa: S603, S607
cwd=cwd,
input=text,
capture_output=True,
Expand Down

0 comments on commit 122f744

Please sign in to comment.