Skip to content

Commit b751296

Browse files
Address code review: narrower exception, extract test helpers, cleaner script
- Catch PackageNotFoundError instead of bare Exception in version.py - Extract _venv_python() and _create_venv_and_install() helpers in test - Use multi-line script string instead of semicolons Agent-Logs-Url: https://github.com/codeSamuraii/pyfuse/sessions/92c23804-a854-46b3-978f-2599f056b232 Co-authored-by: codeSamuraii <17270548+codeSamuraii@users.noreply.github.com>
1 parent dfb269e commit b751296

2 files changed

Lines changed: 37 additions & 65 deletions

File tree

pyfuse/core/version.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
from importlib.metadata import PackageNotFoundError
12
from importlib.metadata import version as _pkg_version
23

34
_FALLBACK_VERSION = "0.4.0"
45

56
try:
67
_VERSION: str = _pkg_version("pyfuse")
7-
except Exception:
8+
except PackageNotFoundError:
89
# Not installed as a package (e.g. running from source checkout).
910
_VERSION = _FALLBACK_VERSION

tests/test_packaging.py

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,29 @@ def _project_root() -> Path:
1818
return root
1919

2020

21+
def _venv_python(venv_dir: Path) -> str:
22+
"""Return the path to the Python executable inside a venv."""
23+
if sys.platform == "win32":
24+
return str(venv_dir / "Scripts" / "python.exe")
25+
return str(venv_dir / "bin" / "python")
26+
27+
28+
def _create_venv_and_install(tmp_path: Path) -> str:
29+
"""Create a venv at *tmp_path*/venv, install pyfuse, return python path."""
30+
venv_dir = tmp_path / "venv"
31+
venv.create(str(venv_dir), with_pip=True)
32+
python = _venv_python(venv_dir)
33+
34+
result = subprocess.run(
35+
[python, "-m", "pip", "install", str(_project_root()), "--quiet"],
36+
capture_output=True,
37+
text=True,
38+
timeout=120,
39+
)
40+
assert result.returncode == 0, f"pip install failed:\n{result.stderr}"
41+
return python
42+
43+
2144
class TestVersionConsistency:
2245
"""Ensure the package exposes a consistent version."""
2346

@@ -54,35 +77,17 @@ class TestIsolatedInstallation:
5477

5578
def test_install_and_import_no_extras(self, tmp_path: Path) -> None:
5679
"""Package installs and imports without optional dependencies."""
57-
venv_dir = tmp_path / "venv"
58-
venv.create(str(venv_dir), with_pip=True)
59-
60-
if sys.platform == "win32":
61-
python = str(venv_dir / "Scripts" / "python.exe")
62-
else:
63-
python = str(venv_dir / "bin" / "python")
64-
65-
root = _project_root()
66-
67-
# Install the package (no extras)
68-
result = subprocess.run(
69-
[python, "-m", "pip", "install", str(root), "--quiet"],
70-
capture_output=True,
71-
text=True,
72-
timeout=120,
80+
python = _create_venv_and_install(tmp_path)
81+
82+
script = (
83+
"import pyfuse\n"
84+
"print(pyfuse.__version__)\n"
85+
"print(pyfuse.serialize)\n"
86+
"print(pyfuse.trace)\n"
87+
"print(pyfuse.get_graph())\n"
7388
)
74-
assert result.returncode == 0, f"pip install failed:\n{result.stderr}"
75-
76-
# Verify import works
7789
result = subprocess.run(
78-
[
79-
python, "-c",
80-
"import pyfuse; "
81-
"print(pyfuse.__version__); "
82-
"print(pyfuse.serialize); "
83-
"print(pyfuse.trace); "
84-
"print(pyfuse.get_graph()); "
85-
],
90+
[python, "-c", script],
8691
capture_output=True,
8792
text=True,
8893
timeout=30,
@@ -102,30 +107,12 @@ def test_install_version_matches(self, tmp_path: Path) -> None:
102107
except ModuleNotFoundError:
103108
import tomli as tomllib # type: ignore[no-redef]
104109

105-
venv_dir = tmp_path / "venv"
106-
venv.create(str(venv_dir), with_pip=True)
107-
108-
if sys.platform == "win32":
109-
python = str(venv_dir / "Scripts" / "python.exe")
110-
else:
111-
python = str(venv_dir / "bin" / "python")
112-
113-
root = _project_root()
110+
python = _create_venv_and_install(tmp_path)
114111

115-
pyproject = root / "pyproject.toml"
112+
pyproject = _project_root() / "pyproject.toml"
116113
with open(pyproject, "rb") as f:
117114
expected_version = tomllib.load(f)["project"]["version"]
118115

119-
# Install
120-
result = subprocess.run(
121-
[python, "-m", "pip", "install", str(root), "--quiet"],
122-
capture_output=True,
123-
text=True,
124-
timeout=120,
125-
)
126-
assert result.returncode == 0, f"pip install failed:\n{result.stderr}"
127-
128-
# Check version
129116
result = subprocess.run(
130117
[python, "-c", "import pyfuse; print(pyfuse.__version__)"],
131118
capture_output=True,
@@ -137,23 +124,7 @@ def test_install_version_matches(self, tmp_path: Path) -> None:
137124

138125
def test_cli_entrypoint(self, tmp_path: Path) -> None:
139126
"""The ``pyfuse`` CLI entry point is installed and functional."""
140-
venv_dir = tmp_path / "venv"
141-
venv.create(str(venv_dir), with_pip=True)
142-
143-
if sys.platform == "win32":
144-
python = str(venv_dir / "Scripts" / "python.exe")
145-
else:
146-
python = str(venv_dir / "bin" / "python")
147-
148-
root = _project_root()
149-
150-
result = subprocess.run(
151-
[python, "-m", "pip", "install", str(root), "--quiet"],
152-
capture_output=True,
153-
text=True,
154-
timeout=120,
155-
)
156-
assert result.returncode == 0, f"pip install failed:\n{result.stderr}"
127+
python = _create_venv_and_install(tmp_path)
157128

158129
# Run ``pyfuse info`` via the entry point
159130
result = subprocess.run(

0 commit comments

Comments
 (0)