Skip to content

Commit

Permalink
Add unit tests for twine.wheel (#596)
Browse files Browse the repository at this point in the history
* Add unit tests for twine.wheel

* Use correct HTTPError in twine.utils unit tests

* Use pathlib and f-strings

* Refactor test for reading empty metadata whl file

* Add f-string to test_read_non_existent_wheel_file_name

* Fixed zipfile import and moved TESTS_DIR to test_wheel
  • Loading branch information
deveshks committed Apr 21, 2020
1 parent f61707b commit 2c01611
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
6 changes: 3 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,18 @@ def test_check_status_code_for_missing_status_code(capsys, repo_url):
response = pretend.stub(
status_code=403,
url=repo_url,
raise_for_status=pretend.raiser(requests.exceptions.HTTPError),
raise_for_status=pretend.raiser(requests.HTTPError),
text="Forbidden",
)

with pytest.raises(requests.exceptions.HTTPError):
with pytest.raises(requests.HTTPError):
utils.check_status_code(response, True)

# Different messages are printed based on the verbose level
captured = capsys.readouterr()
assert captured.out == "Content received from server:\nForbidden\n"

with pytest.raises(requests.exceptions.HTTPError):
with pytest.raises(requests.HTTPError):
utils.check_status_code(response, False)

captured = capsys.readouterr()
Expand Down
54 changes: 51 additions & 3 deletions tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,27 @@
# 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 os
import pathlib
import zipfile

import pytest

from twine import exceptions
from twine import wheel

TESTS_DIR = pathlib.Path(__file__).parent


@pytest.fixture(
params=[
"tests/fixtures/twine-1.5.0-py2.py3-none-any.whl",
"tests/alt-fixtures/twine-1.5.0-py2.py3-none-any.whl",
"fixtures/twine-1.5.0-py2.py3-none-any.whl",
"alt-fixtures/twine-1.5.0-py2.py3-none-any.whl",
]
)
def example_wheel(request):
return wheel.Wheel(request.param)
file_name = os.path.join(TESTS_DIR, request.param)
return wheel.Wheel(file_name)


def test_version_parsing(example_wheel):
Expand All @@ -45,3 +53,43 @@ def test_find_metadata_files():
]
candidates = wheel.Wheel.find_candidate_metadata_files(names)
assert expected == candidates


def test_read_valid(example_wheel):
"""Test reading a valid wheel file"""
metadata = example_wheel.read().decode().splitlines()
assert "Name: twine" in metadata
assert "Version: 1.5.0" in metadata


def test_read_non_existent_wheel_file_name():
"""Test reading a wheel file which doesn't exist"""

file_name = "/foo/bar/baz.whl"
with pytest.raises(
exceptions.InvalidDistribution, match=f"No such file: {file_name}"
):
wheel.Wheel(file_name)


def test_read_invalid_wheel_extension():
"""Test reading a wheel file without a .whl extension"""

file_name = os.path.join(os.path.dirname(__file__), "fixtures/twine-1.5.0.tar.gz")
with pytest.raises(
exceptions.InvalidDistribution, match=f"Not a known archive format: {file_name}"
):
wheel.Wheel(file_name)


def test_read_wheel_empty_metadata(tmpdir):
"""Test reading a wheel file with an empty METADATA file"""

whl_file = tmpdir.mkdir("wheel").join("not-a-wheel.whl")
with zipfile.ZipFile(whl_file, "w") as zip_file:
zip_file.writestr("METADATA", "")

with pytest.raises(
exceptions.InvalidDistribution, match=f"No METADATA in archive: {whl_file}"
):
wheel.Wheel(whl_file)

0 comments on commit 2c01611

Please sign in to comment.