diff --git a/tests/test_upload.py b/tests/test_upload.py index 42939f93..b30fe583 100644 --- a/tests/test_upload.py +++ b/tests/test_upload.py @@ -18,6 +18,7 @@ from twine import cli from twine import exceptions from twine import package as package_file +from twine import utils from twine.commands import upload from . import helpers @@ -72,6 +73,31 @@ def test_successs_prints_release_urls(upload_settings, stub_repository, capsys): assert captured.out.count(NEW_RELEASE_URL) == 1 +def test_successs_prints_uploaded_package_names_if_verbose(upload_settings, capsys): + """Print the path and file size of each distribution attempting to be uploaded.""" + dists_to_upload = [ + helpers.WHEEL_FIXTURE, + helpers.SDIST_FIXTURE, + helpers.NEW_SDIST_FIXTURE, + helpers.NEW_WHEEL_FIXTURE, + ] + + sizes_of_dists_to_upload = [utils.get_file_size(dist) for dist in dists_to_upload] + + upload_settings.verbose = True + + result = upload.upload(upload_settings, dists_to_upload) + + assert result is None + + captured = capsys.readouterr() + + for dist_to_upload in dists_to_upload: + assert captured.out.count(dist_to_upload) == 1 + for size_of_dist_to_upload in sizes_of_dists_to_upload: + assert captured.out.count(size_of_dist_to_upload) == 1 + + def test_success_with_pre_signed_distribution(upload_settings, stub_repository): """Adds GPG signature provided by user to uploaded package.""" # Upload a pre-signed distribution diff --git a/tests/test_utils.py b/tests/test_utils.py index 55d96706..b8ae34ca 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -313,3 +313,19 @@ def test_check_status_code_for_missing_status_code(capsys, repo_url): captured = capsys.readouterr() assert captured.out == "NOTE: Try --verbose to see response content.\n" + + +@pytest.mark.parametrize( + ("filename, expected"), + [ + (helpers.WHEEL_FIXTURE, "15.4 KB"), + (helpers.SDIST_FIXTURE, "20.8 KB"), + (helpers.NEW_SDIST_FIXTURE, "26.1 KB"), + (helpers.NEW_WHEEL_FIXTURE, "21.9 KB"), + ], +) +def test_get_file_size(filename, expected): + """Compare the size of a file to the expected value.""" + file_size = utils.get_file_size(filename) + + assert file_size == expected diff --git a/twine/commands/upload.py b/twine/commands/upload.py index a37e8bd0..ef7ff67f 100644 --- a/twine/commands/upload.py +++ b/twine/commands/upload.py @@ -60,6 +60,11 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None: repository_url = cast(str, upload_settings.repository_config["repository"]) print(f"Uploading distributions to {repository_url}") + if upload_settings.verbose: + for filename in uploads: + file_size = utils.get_file_size(filename) + print(f" {filename} ({file_size})") + print("\n") repository = upload_settings.create_repository() uploaded_packages = [] diff --git a/twine/utils.py b/twine/utils.py index f49360de..d859904e 100644 --- a/twine/utils.py +++ b/twine/utils.py @@ -159,6 +159,18 @@ def normalize_repository_url(url: str) -> str: return urlunparse(parsed) +def get_file_size(filename: str) -> str: + """Return the size of a file, in KB if < .1 MB.""" + file_size = os.path.getsize(filename) / 1024 + size_unit = "KB" + + if file_size > 1024: + file_size = file_size / 1024 + size_unit = "MB" + + return f"{file_size:.1f} {size_unit}" + + def check_status_code(response: requests.Response, verbose: bool) -> None: """Generate a helpful message based on the response from the repository.