Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions check_dist/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,20 @@ def check_present(files: list[str], patterns: list[str], dist_type: str) -> list
return errors


def check_absent(files: list[str], patterns: list[str], dist_type: str) -> list[str]:
"""Return error strings for any *patterns* found in *files*."""
def check_absent(files: list[str], patterns: list[str], dist_type: str, *, present_patterns: list[str] | None = None) -> list[str]:
"""Return error strings for any *patterns* found in *files*.

When *present_patterns* is given, files nested inside a directory
that matches a present pattern are not flagged. This avoids false
positives like ``lerna/tests/fake_package/pyproject.toml`` being
flagged as unwanted when ``lerna`` is a required present pattern.
"""
errors: list[str] = []
for pattern in patterns:
translated = translate_extension(pattern)
matching = [f for f in files if matches_pattern(f, pattern)]
if matching and present_patterns:
matching = [f for f in matching if not any(matches_pattern(f, pp) for pp in present_patterns)]
if matching:
msg = f"{dist_type}: unwanted pattern '{pattern}' matched: {', '.join(matching)}"
if translated != pattern:
Expand Down Expand Up @@ -745,7 +753,7 @@ def check_dist(
messages.append(f" Warning: could not compare against VCS: {exc}")

errors.extend(check_present(sdist_files, config["sdist"]["present"], "sdist"))
errors.extend(check_absent(sdist_files, config["sdist"]["absent"], "sdist"))
errors.extend(check_absent(sdist_files, config["sdist"]["absent"], "sdist", present_patterns=config["sdist"]["present"]))
errors.extend(check_wrong_platform_extensions(sdist_files, "sdist"))

# ── wheel checks ─────────────────────────────────────────
Expand All @@ -757,7 +765,7 @@ def check_dist(
messages.append(f" {f}")

errors.extend(check_present(wheel_files, config["wheel"]["present"], "wheel"))
errors.extend(check_absent(wheel_files, config["wheel"]["absent"], "wheel"))
errors.extend(check_absent(wheel_files, config["wheel"]["absent"], "wheel", present_patterns=config["wheel"]["present"]))
errors.extend(check_wrong_platform_extensions(wheel_files, "wheel"))
finally:
if pre_built is None:
Expand Down
30 changes: 30 additions & 0 deletions check_dist/tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,36 @@ def test_docs_directory(self):
assert len(errors) == 1
assert "docs" in errors[0]

def test_nested_in_present_pattern_skipped(self):
"""Files nested inside a 'present' dir are not flagged as absent."""
files = [
"lerna/__init__.py",
"lerna/test_utils/configs/missing_init_py/.gitignore",
"lerna/tests/fake_package/pyproject.toml",
"lerna/tests/fake_package2/pyproject.toml",
"pyproject.toml", # top-level: should still be caught
]
errors = check_absent(
files,
[".gitignore", "pyproject.toml"],
"wheel",
present_patterns=["lerna"],
)
assert len(errors) == 1
assert "pyproject.toml" in errors[0]
# Only the top-level pyproject.toml, not the nested ones
assert "lerna/" not in errors[0]

def test_present_patterns_none_flags_all(self):
"""Without present_patterns, nested files are still flagged."""
files = [
"lerna/tests/fake_package/pyproject.toml",
"pyproject.toml",
]
errors = check_absent(files, ["pyproject.toml"], "wheel")
assert len(errors) == 1
assert "lerna/tests/fake_package/pyproject.toml" in errors[0]


# ── check_wrong_platform_extensions ──────────────────────────────────

Expand Down