diff --git a/src/python_inspector/resolution.py b/src/python_inspector/resolution.py index bee47473..ea241dfe 100644 --- a/src/python_inspector/resolution.py +++ b/src/python_inspector/resolution.py @@ -222,13 +222,13 @@ async def fetch_and_extract_sdist( def get_sdist_file_path_from_filename(sdist): if sdist.endswith(".tar.gz"): - sdist_file = sdist.rstrip(".tar.gz") + sdist_file = sdist.removesuffix(".tar.gz") with tarfile.open(os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)) as file: file.extractall( os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file) ) elif sdist.endswith(".zip"): - sdist_file = sdist.rstrip(".zip") + sdist_file = sdist.removesuffix(".zip") with ZipFile(os.path.join(settings.CACHE_THIRDPARTY_DIR, sdist)) as zip: zip.extractall( os.path.join(settings.CACHE_THIRDPARTY_DIR, "extracted_sdists", sdist_file) diff --git a/tests/test_sdist_directory_name.py b/tests/test_sdist_directory_name.py new file mode 100644 index 00000000..c2c67bd7 --- /dev/null +++ b/tests/test_sdist_directory_name.py @@ -0,0 +1,30 @@ +# Copyright (c) nexB Inc. and others. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 +# See https://github.com/aboutcode-org/python-inspector for support or download. + +import io +import tarfile +import zipfile +from pathlib import Path + +import pytest + +from python_inspector import resolution + + +@pytest.mark.parametrize("stem,extension", [("demo-1.0a", ".tar.gz"), ("demo-1.0+zip", ".zip")]) +def test_sdist_extracted_directory_name(tmp_path, monkeypatch, stem, extension): + monkeypatch.setattr(resolution.settings, "CACHE_THIRDPARTY_DIR", str(tmp_path)) + name = stem + extension + member = stem + "/setup.py" + if extension == ".tar.gz": + with tarfile.open(tmp_path / name, "w:gz") as archive: + info = tarfile.TarInfo(member) + info.size = 4 + archive.addfile(info, io.BytesIO(b"pass")) + else: + with zipfile.ZipFile(tmp_path / name, "w") as archive: + archive.writestr(member, "pass") + result = Path(resolution.get_sdist_file_path_from_filename(name)) + assert result.name == stem + assert (result / "setup.py").read_text() == "pass"