Skip to content

Commit 5d850a0

Browse files
committed
Fix "# noqa: F401" placement for coverage by import tests
1 parent 9701c26 commit 5d850a0

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

src/pynguin/generator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,8 @@ def _export_chromosome(
10221022
module_ast,
10231023
target_file,
10241024
format_with_black=config.configuration.test_case_output.format_with_black,
1025-
coverage_by_import_only=coverage_by_import_only,
1025+
module_name_with_coverage=config.configuration.module_name
1026+
if coverage_by_import_only
1027+
else None,
10261028
)
10271029
_LOGGER.info("Written %i test cases to %s", chromosome.size(), target_file)

src/pynguin/testcase/export.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import dataclasses
1111
import importlib.util
1212
import logging
13+
import re
1314
import sys
1415
from pathlib import Path
1516

@@ -315,7 +316,7 @@ def save_module_to_file(
315316
target: Path,
316317
*,
317318
format_with_black: bool = True,
318-
coverage_by_import_only: bool = False,
319+
module_name_with_coverage: str | None = None,
319320
) -> None:
320321
"""Saves an AST module to a file.
321322
@@ -324,7 +325,7 @@ def save_module_to_file(
324325
module: The AST module
325326
format_with_black: ast.unparse is not PEP-8 compliant, so we apply black
326327
on the result.
327-
coverage_by_import_only: If True, adds a comment explaining that importing
328+
module_name_with_coverage: If provided, adds a comment explaining that importing
328329
the module achieves coverage, and appends '# noqa: F401' to the import
329330
statement to prevent it from being removed by linters.
330331
"""
@@ -343,8 +344,10 @@ def save_module_to_file(
343344
except black.parsing.InvalidInput as e:
344345
_LOGGER.warning("Could not format the module '%s' with black: %s", target, e)
345346

346-
if coverage_by_import_only:
347+
if module_name_with_coverage:
347348
file.write(_COVERAGE_BY_IMPORT_COMMENT)
348-
output = output.rstrip("\n") + " # noqa: F401\n"
349+
# Add
350+
pattern = re.compile(rf"^import {re.escape(module_name_with_coverage)}\b", re.MULTILINE)
351+
output = pattern.sub(rf"import {module_name_with_coverage} # noqa: F401", output)
349352

350353
file.write(output)

tests/testcase/export/test_export.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def test_case_1():
281281
module_ast,
282282
target_file,
283283
format_with_black=config.configuration.test_case_output.format_with_black,
284-
coverage_by_import_only=coverage_by_import_only,
284+
module_name_with_coverage=module_name if coverage_by_import_only else None,
285285
)
286286
assert target_file.exists()
287287
content = target_file.read_text(encoding="utf-8")
@@ -347,7 +347,7 @@ def _import_execute_export(
347347
module_ast,
348348
export_path,
349349
format_with_black=config.configuration.test_case_output.format_with_black,
350-
coverage_by_import_only=coverage_by_import_only,
350+
module_name_with_coverage=module_name if coverage_by_import_only else None,
351351
)
352352

353353
exported = export_path.read_text(encoding="utf-8")
@@ -532,8 +532,8 @@ def test_import_export_no_xfail(
532532

533533
def test_coverage_by_import_only(tmp_path: Path) -> None:
534534
"""When no test cases exist, SUT is imported with coverage comment and an empty test."""
535-
sut_module_name = "tests.fixtures.accessibles.accessible"
536-
visitor = export.PyTestChromosomeToAstVisitor(sut_module_name=sut_module_name)
535+
module_name = "tests.fixtures.accessibles.accessible"
536+
visitor = export.PyTestChromosomeToAstVisitor(sut_module_name=module_name)
537537

538538
module_ast, coverage_by_import_only = visitor.to_module()
539539

@@ -544,13 +544,12 @@ def test_coverage_by_import_only(tmp_path: Path) -> None:
544544
module_ast,
545545
target_file,
546546
format_with_black=False,
547-
coverage_by_import_only=coverage_by_import_only,
547+
module_name_with_coverage=module_name if coverage_by_import_only else None,
548548
)
549549

550550
content = target_file.read_text(encoding="utf-8")
551551

552552
assert "# Importing this module achieves coverage." in content
553-
assert "# noqa: F401" in content
554-
assert "import tests.fixtures.accessibles.accessible" in content
553+
assert "import tests.fixtures.accessibles.accessible # noqa: F401" in content
555554
assert "def test_empty():" in content
556555
assert " pass" in content

0 commit comments

Comments
 (0)