Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import sys
import subprocess
import shutil
from wheel.pep425tags import get_abi_tag, get_platform
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
def run(cmd):
print(cmd)
try:
subprocess.check_call(cmd)
except subprocess.CalledProcessError as e:
sys.exit(e.returncode)
venv = "test-venv-{}-{}".format(get_abi_tag(), get_platform())
if not exists(venv):
print("-- Creating venv in {} --".format(venv))
run([sys.executable, "-m", "virtualenv", "-p", sys.executable, venv])
python_candidates = [
join(venv, "bin", "python"),
join(venv, "Scripts", "python.exe"),
]
for python_candidate in python_candidates:
if exists(python_candidate):
python_exe = python_candidate
break
else:
raise RuntimeError("I don't understand this platform's virtualenv layout")
def test_write_str(wheel_path):
with WheelFile(wheel_path, 'w') as wf:
wf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
wf.writestr(native('hello/h,ll,.py'), as_bytes('print("Héllö, world!")\n'))
with ZipFile(wheel_path, 'r') as zf:
infolist = zf.infolist()
assert len(infolist) == 3
assert infolist[0].filename == native('hello/héllö.py')
assert infolist[0].file_size == 25
assert infolist[1].filename == native('hello/h,ll,.py')
assert infolist[1].file_size == 25
assert infolist[2].filename == 'test-1.0.dist-info/RECORD'
record = zf.read('test-1.0.dist-info/RECORD')
assert record == as_bytes(
'hello/héllö.py,sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n'
'"hello/h,ll,.py",sha256=bv-QV3RciQC2v3zL8Uvhd_arp40J5A9xmyubN34OVwo,25\n'
def test_weak_hash_algorithm(wheel_path, algorithm, digest):
hash_string = '{}={}'.format(algorithm, digest)
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, w0rld!")\n'))
zf.writestr('test-1.0.dist-info/RECORD',
as_bytes('hello/héllö.py,{},25'.format(hash_string)))
exc = pytest.raises(WheelError, WheelFile, wheel_path)
exc.match(r"^Weak hash algorithm \({}\) is not permitted by PEP 427$".format(algorithm))
def test_licenses_deprecated(dummy_dist, monkeypatch, tmpdir):
dummy_dist.join('setup.cfg').write('[metadata]\nlicense_file=licenses/DUMMYFILE')
monkeypatch.chdir(dummy_dist)
subprocess.check_call([sys.executable, 'setup.py', 'bdist_wheel', '-b', str(tmpdir),
'--universal'])
with WheelFile('dist/dummy_dist-1.0-py2.py3-none-any.whl') as wf:
license_files = {'dummy_dist-1.0.dist-info/DUMMYFILE'}
assert set(wf.namelist()) == DEFAULT_FILES | license_files
def test_testzip_missing_hash(wheel_path):
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
zf.writestr('test-1.0.dist-info/RECORD', '')
with WheelFile(wheel_path) as wf:
exc = pytest.raises(WheelError, wf.testzip)
exc.match(native("^No hash found for file 'hello/héllö.py'$"))
def test_attributes(tmpdir_factory, wheel_path):
# With the change from ZipFile.write() to .writestr(), we need to manually
# set member attributes.
build_dir = tmpdir_factory.mktemp('build')
files = (('foo', 0o644), ('bar', 0o755))
for filename, mode in files:
path = build_dir.join(filename)
path.write(filename + '\n')
path.chmod(mode)
with WheelFile(wheel_path, 'w') as wf:
wf.write_files(str(build_dir))
with ZipFile(wheel_path, 'r') as zf:
for filename, mode in files:
info = zf.getinfo(filename)
assert info.external_attr == (mode | 0o100000) << 16
assert info.compress_type == ZIP_DEFLATED
info = zf.getinfo('test-1.0.dist-info/RECORD')
permissions = (info.external_attr >> 16) & 0o777
assert permissions == 0o664
def test_timestamp(tmpdir_factory, wheel_path, monkeypatch):
# An environment variable can be used to influence the timestamp on
# TarInfo objects inside the zip. See issue #143.
build_dir = tmpdir_factory.mktemp('build')
for filename in ('one', 'two', 'three'):
build_dir.join(filename).write(filename + '\n')
# The earliest date representable in TarInfos, 1980-01-01
monkeypatch.setenv(native('SOURCE_DATE_EPOCH'), native('315576060'))
with WheelFile(wheel_path, 'w') as wf:
wf.write_files(str(build_dir))
with ZipFile(wheel_path, 'r') as zf:
for info in zf.infolist():
assert info.date_time[:3] == (1980, 1, 1)
assert info.compress_type == ZIP_DEFLATED
def test_testzip(wheel_path, algorithm, digest):
hash_string = '{}={}'.format(algorithm, digest)
with ZipFile(wheel_path, 'w') as zf:
zf.writestr(native('hello/héllö.py'), as_bytes('print("Héllö, world!")\n'))
zf.writestr('test-1.0.dist-info/RECORD',
as_bytes('hello/héllö.py,{},25'.format(hash_string)))
with WheelFile(wheel_path) as wf:
wf.testzip()
def test_licenses_default(dummy_dist, monkeypatch, tmpdir):
monkeypatch.chdir(dummy_dist)
subprocess.check_call([sys.executable, 'setup.py', 'bdist_wheel', '-b', str(tmpdir),
'--universal'])
with WheelFile('dist/dummy_dist-1.0-py2.py3-none-any.whl') as wf:
license_files = {'dummy_dist-1.0.dist-info/' + fname for fname in DEFAULT_LICENSE_FILES}
assert set(wf.namelist()) == DEFAULT_FILES | license_files
def test_wheelfile_re(tmpdir):
# Regression test for #208
path = tmpdir.join('foo-2-py3-none-any.whl')
with WheelFile(str(path), 'w') as wf:
assert wf.parsed_filename.group('namever') == 'foo-2'