Skip to content

Commit

Permalink
refactored and added tests, changed upload and get_file_size
Browse files Browse the repository at this point in the history
  • Loading branch information
Vikram Jayanthi committed Jun 9, 2020
1 parent ed37363 commit b745b13
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
8 changes: 6 additions & 2 deletions tests/test_upload.py
Expand Up @@ -11,14 +11,14 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pretend
import pytest
import requests

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
Expand Down Expand Up @@ -74,14 +74,16 @@ def test_successs_prints_release_urls(upload_settings, stub_repository, capsys):


def test_successs_prints_uploaded_package_names_if_verbose(upload_settings, capsys):
"""Prints the path of each distribution attempting to be uploaded."""
"""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)
Expand All @@ -92,6 +94,8 @@ def test_successs_prints_uploaded_package_names_if_verbose(upload_settings, caps

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):
Expand Down
23 changes: 14 additions & 9 deletions tests/test_utils.py
Expand Up @@ -315,12 +315,17 @@ def test_check_status_code_for_missing_status_code(capsys, repo_url):
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"
@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
7 changes: 3 additions & 4 deletions twine/commands/upload.py
Expand Up @@ -61,10 +61,9 @@ def upload(upload_settings: settings.Settings, dists: List[str]) -> None:

print(f"Uploading distributions to {repository_url}")
if upload_settings.verbose:
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:.1f} {size_unit})")
for filename in uploads:
file_size = utils.get_file_size(filename)
print(f" {filename} ({file_size})")
print("\n")

repository = upload_settings.create_repository()
Expand Down
19 changes: 10 additions & 9 deletions twine/utils.py
Expand Up @@ -23,7 +23,6 @@
from typing import Dict
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import Union
from urllib.parse import urlparse
from urllib.parse import urlunparse
Expand Down Expand Up @@ -160,14 +159,16 @@ def normalize_repository_url(url: str) -> str:
return urlunparse(parsed)


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 = os.path.getsize(filepath) / (1024 * 1024)
if round(file_size, 1) == 0:
size_unit = "KB"
file_size = os.path.getsize(filepath) / 1024
return file_size, size_unit
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:
Expand Down

0 comments on commit b745b13

Please sign in to comment.