Skip to content

Commit b3ea669

Browse files
ayhammoudaclaude
andcommitted
test(packaging): harden source-tree fallback + entry-point assertions
Fixes I3 + M2 from the PR #22 coherence review. I3: -S does not reliably suppress editable-install dist-info discovery, so the source-tree fallback test was previously passing because installed metadata happened to match pyproject. Force the fallback by monkey-patching importlib.metadata.version to raise PackageNotFoundError. M2: Replace substring assertions on entry_points.txt with configparser parsing that verifies each console script is bound to mcp_server_python_docs.__main__:main, not merely substring- present. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 8337924 commit b3ea669

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

tests/test_packaging.py

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,30 @@ def test_wheel_metadata_uses_public_distribution_name(self, built_wheel):
7777
assert f"Name: {DIST_NAME}" in content
7878

7979
def test_wheel_has_entry_point(self, built_wheel):
80-
"""PKG-01: Wheel metadata declares the entry-point."""
80+
"""PKG-01: Wheel metadata declares both console scripts, structurally."""
81+
import configparser
82+
import io
83+
8184
with zipfile.ZipFile(built_wheel) as zf:
82-
# Entry points are in the .dist-info/entry_points.txt
8385
entry_point_files = [
8486
n for n in zf.namelist() if n.endswith("entry_points.txt")
8587
]
8688
assert len(entry_point_files) == 1
8789
content = zf.read(entry_point_files[0]).decode()
88-
assert DIST_NAME in content
89-
assert LEGACY_CLI_NAME in content
90-
assert "mcp_server_python_docs.__main__:main" in content
90+
91+
parser = configparser.ConfigParser()
92+
parser.read_file(io.StringIO(content))
93+
assert parser.has_section("console_scripts"), (
94+
f"entry_points.txt missing [console_scripts]:\n{content}"
95+
)
96+
scripts = dict(parser.items("console_scripts"))
97+
target = "mcp_server_python_docs.__main__:main"
98+
assert scripts.get(DIST_NAME) == target, (
99+
f"Expected {DIST_NAME} -> {target}, got {scripts!r}"
100+
)
101+
assert scripts.get(LEGACY_CLI_NAME) == target, (
102+
f"Expected {LEGACY_CLI_NAME} -> {target}, got {scripts!r}"
103+
)
91104

92105

93106
class TestPyprojectDeps:
@@ -127,15 +140,26 @@ def test_module_version_matches_package_metadata(self):
127140
assert mcp_server_python_docs.__version__ == version(DIST_NAME)
128141

129142
def test_source_tree_import_without_installed_metadata(self, tmp_path: Path):
130-
"""Source-tree import falls back to pyproject.toml when metadata is absent."""
143+
"""Source-tree import falls back to pyproject.toml when metadata is absent.
144+
145+
Forces the fallback path by monkey-patching importlib.metadata.version
146+
to raise PackageNotFoundError, since `-S` alone does not reliably
147+
suppress editable-install dist-info discovery.
148+
"""
131149
env = os.environ.copy()
132150
env["PYTHONPATH"] = str(PROJECT_ROOT / "src")
151+
prelude = (
152+
"import importlib.metadata as m\n"
153+
"def _raise(_):\n"
154+
" raise m.PackageNotFoundError\n"
155+
"m.version = _raise\n"
156+
)
133157
result = subprocess.run(
134158
[
135159
sys.executable,
136160
"-S",
137161
"-c",
138-
"import mcp_server_python_docs; print(mcp_server_python_docs.__version__)",
162+
prelude + "import mcp_server_python_docs; print(mcp_server_python_docs.__version__)",
139163
],
140164
capture_output=True,
141165
text=True,
@@ -148,7 +172,13 @@ def test_source_tree_import_without_installed_metadata(self, tmp_path: Path):
148172
f"stdout: {result.stdout!r}\n"
149173
f"stderr: {result.stderr!r}"
150174
)
151-
assert result.stdout.strip() == version(DIST_NAME)
175+
# The fallback reads pyproject.toml directly; assert it equals
176+
# whatever pyproject currently declares (decoupled from installed metadata).
177+
import tomllib
178+
pyproject_version = tomllib.loads(
179+
(PROJECT_ROOT / "pyproject.toml").read_text()
180+
)["project"]["version"]
181+
assert result.stdout.strip() == pyproject_version
152182

153183
def test_version_flag_output(self):
154184
"""--version prints the installed package metadata version."""

0 commit comments

Comments
 (0)