Skip to content

Commit 2c504ee

Browse files
committed
path_belongs_to_site_packages code review adjust
1 parent d953a1c commit 2c504ee

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

codeflash/code_utils/code_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def get_run_tmp_file(file_path: Path) -> Path:
246246

247247
def path_belongs_to_site_packages(file_path: Path) -> bool:
248248
file_path_resolved = file_path.resolve()
249-
site_packages = [Path(p) for p in site.getsitepackages()]
249+
site_packages = [Path(p).resolve() for p in site.getsitepackages()]
250250
return any(file_path_resolved.is_relative_to(site_package_path) for site_package_path in site_packages)
251251

252252

tests/test_code_utils.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,66 @@ def test_path_belongs_to_site_packages_with_relative_path(monkeypatch: pytest.Mo
277277
assert path_belongs_to_site_packages(file_path) is False
278278

279279

280+
def test_path_belongs_to_site_packages_with_symlinked_site_packages(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
281+
real_site_packages = tmp_path / "real_site_packages"
282+
real_site_packages.mkdir()
283+
284+
symlinked_site_packages = tmp_path / "symlinked_site_packages"
285+
symlinked_site_packages.symlink_to(real_site_packages)
286+
287+
package_file = real_site_packages / "some_package" / "__init__.py"
288+
package_file.parent.mkdir()
289+
package_file.write_text("# package file")
290+
291+
monkeypatch.setattr(site, "getsitepackages", lambda: [str(symlinked_site_packages)])
292+
293+
assert path_belongs_to_site_packages(package_file) is True
294+
295+
symlinked_package_file = symlinked_site_packages / "some_package" / "__init__.py"
296+
assert path_belongs_to_site_packages(symlinked_package_file) is True
297+
298+
299+
def test_path_belongs_to_site_packages_with_complex_symlinks(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
300+
real_site_packages = tmp_path / "real" / "lib" / "python3.9" / "site-packages"
301+
real_site_packages.mkdir(parents=True)
302+
303+
link1 = tmp_path / "link1"
304+
link1.symlink_to(real_site_packages.parent.parent.parent)
305+
306+
link2 = tmp_path / "link2"
307+
link2.symlink_to(link1)
308+
309+
package_file = real_site_packages / "test_package" / "module.py"
310+
package_file.parent.mkdir()
311+
package_file.write_text("# test module")
312+
313+
site_packages_via_links = link2 / "lib" / "python3.9" / "site-packages"
314+
monkeypatch.setattr(site, "getsitepackages", lambda: [str(site_packages_via_links)])
315+
316+
assert path_belongs_to_site_packages(package_file) is True
317+
318+
file_via_links = site_packages_via_links / "test_package" / "module.py"
319+
assert path_belongs_to_site_packages(file_via_links) is True
320+
321+
322+
def test_path_belongs_to_site_packages_resolved_paths_normalization(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
323+
site_packages_dir = tmp_path / "lib" / "python3.9" / "site-packages"
324+
site_packages_dir.mkdir(parents=True)
325+
326+
package_dir = site_packages_dir / "mypackage"
327+
package_dir.mkdir()
328+
package_file = package_dir / "module.py"
329+
package_file.write_text("# module")
330+
331+
complex_site_packages_path = tmp_path / "lib" / "python3.9" / "other" / ".." / "site-packages" / "."
332+
monkeypatch.setattr(site, "getsitepackages", lambda: [str(complex_site_packages_path)])
333+
334+
assert path_belongs_to_site_packages(package_file) is True
335+
336+
complex_file_path = tmp_path / "lib" / "python3.9" / "site-packages" / "other" / ".." / "mypackage" / "module.py"
337+
assert path_belongs_to_site_packages(complex_file_path) is True
338+
339+
280340
# tests for is_class_defined_in_file
281341
def test_is_class_defined_in_file_with_existing_class(tmp_path: Path) -> None:
282342
test_file = tmp_path / "test_file.py"

0 commit comments

Comments
 (0)