Skip to content

Commit

Permalink
Added get_file_size test and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikram Jayanthi committed Jun 8, 2020
1 parent 93b1b27 commit ed37363
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
11 changes: 11 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,14 @@ 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"


def test_get_file_size_unit_determination():
"""Test if get_file_size returns a non zero value in KB for a file < .01 MB."""
test_file = helpers.SDIST_FIXTURE

file_size, size_unit = utils.get_file_size(test_file)
print(file_size)
print(size_unit)
assert file_size != 0
assert size_unit == "KB"
2 changes: 1 addition & 1 deletion twine/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
for dist_to_upload in uploads:
# If the file size is <.1 MB we display in KB
file_size, size_unit = utils.get_file_size(dist_to_upload)
print(f" {dist_to_upload} ({file_size} {size_unit})")
print(f" {dist_to_upload} ({file_size:.1f} {size_unit})")
print("\n")

repository = upload_settings.create_repository()
Expand Down
6 changes: 3 additions & 3 deletions twine/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ def normalize_repository_url(url: str) -> str:
def get_file_size(filepath: str) -> Tuple[float, str]:
"""Return the file size and unit of size following PyPI's file size format."""
size_unit = "MB"
file_size = round(os.path.getsize(filepath) / (1024 * 1024), 1)
if not file_size:
file_size = os.path.getsize(filepath) / (1024 * 1024)
if round(file_size, 1) == 0:
size_unit = "KB"
file_size = round(os.path.getsize(filepath) / (1024), 1)
file_size = os.path.getsize(filepath) / 1024
return file_size, size_unit


Expand Down

0 comments on commit ed37363

Please sign in to comment.