Skip to content

Commit d053f76

Browse files
Remove exact archive suffixes when locating extracted sdists
Signed-off-by: Ali Zulfiqar <codewithfourtix@gmail.com>
1 parent a841c7c commit d053f76

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/python_inspector/resolution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ async def fetch_and_extract_sdist(
222222

223223
def get_sdist_file_path_from_filename(sdist):
224224
if sdist.endswith(".tar.gz"):
225-
sdist_file = sdist.rstrip(".tar.gz")
225+
sdist_file = sdist.removesuffix(".tar.gz")
226226
with tarfile.open(os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)) as file:
227227
file.extractall(
228228
os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file)
229229
)
230230
elif sdist.endswith(".zip"):
231-
sdist_file = sdist.rstrip(".zip")
231+
sdist_file = sdist.removesuffix(".zip")
232232
with ZipFile(os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)) as zip:
233233
zip.extractall(
234234
os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file)

tests/test_sdist_directory_name.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import io
2+
import tarfile
3+
import zipfile
4+
from pathlib import Path
5+
6+
import pytest
7+
8+
from python_inspector import resolution
9+
10+
@pytest.mark.parametrize("stem,extension", [("demo-1.0a", ".tar.gz"), ("demo-1.0+zip", ".zip")])
11+
def test_sdist_extracted_directory_name(tmp_path, monkeypatch, stem, extension):
12+
monkeypatch.setattr(resolution.settings, "CACHE_THIRDPARTY_DIR", str(tmp_path))
13+
name = stem + extension
14+
member = stem + "/setup.py"
15+
if extension == ".tar.gz":
16+
with tarfile.open(tmp_path / name, "w:gz") as archive:
17+
info = tarfile.TarInfo(member)
18+
info.size = 4
19+
archive.addfile(info, io.BytesIO(b"pass"))
20+
else:
21+
with zipfile.ZipFile(tmp_path / name, "w") as archive:
22+
archive.writestr(member, "pass")
23+
result = Path(resolution.get_sdist_file_path_from_filename(name))
24+
assert result.name == stem
25+
assert (result / "setup.py").read_text() == "pass"

0 commit comments

Comments
 (0)