test_basic.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. """
  2. Basic wheel tests.
  3. """
  4. import os
  5. import pkg_resources
  6. import json
  7. import sys
  8. from pkg_resources import resource_filename
  9. import wheel.util
  10. import wheel.tool
  11. from wheel import egg2wheel
  12. from wheel.install import WheelFile
  13. from zipfile import ZipFile
  14. from shutil import rmtree
  15. test_distributions = ("complex-dist", "simple.dist", "headers.dist")
  16. def teardown_module():
  17. """Delete eggs/wheels created by tests."""
  18. base = pkg_resources.resource_filename('wheel.test', '')
  19. for dist in test_distributions:
  20. for subdir in ('build', 'dist'):
  21. try:
  22. rmtree(os.path.join(base, dist, subdir))
  23. except OSError:
  24. pass
  25. def setup_module():
  26. build_wheel()
  27. build_egg()
  28. def build_wheel():
  29. """Build wheels from test distributions."""
  30. for dist in test_distributions:
  31. pwd = os.path.abspath(os.curdir)
  32. distdir = pkg_resources.resource_filename('wheel.test', dist)
  33. os.chdir(distdir)
  34. try:
  35. sys.argv = ['', 'bdist_wheel']
  36. exec(compile(open('setup.py').read(), 'setup.py', 'exec'))
  37. finally:
  38. os.chdir(pwd)
  39. def build_egg():
  40. """Build eggs from test distributions."""
  41. for dist in test_distributions:
  42. pwd = os.path.abspath(os.curdir)
  43. distdir = pkg_resources.resource_filename('wheel.test', dist)
  44. os.chdir(distdir)
  45. try:
  46. sys.argv = ['', 'bdist_egg']
  47. exec(compile(open('setup.py').read(), 'setup.py', 'exec'))
  48. finally:
  49. os.chdir(pwd)
  50. def test_findable():
  51. """Make sure pkg_resources can find us."""
  52. assert pkg_resources.working_set.by_key['wheel'].version
  53. def test_egg_re():
  54. """Make sure egg_info_re matches."""
  55. egg_names = open(pkg_resources.resource_filename('wheel', 'eggnames.txt'))
  56. for line in egg_names:
  57. line = line.strip()
  58. if not line:
  59. continue
  60. assert egg2wheel.egg_info_re.match(line), line
  61. def test_compatibility_tags():
  62. """Test compatibilty tags are working."""
  63. wf = WheelFile("package-1.0.0-cp32.cp33-noabi-noarch.whl")
  64. assert (list(wf.compatibility_tags) ==
  65. [('cp32', 'noabi', 'noarch'), ('cp33', 'noabi', 'noarch')])
  66. assert (wf.arity == 2)
  67. wf2 = WheelFile("package-1.0.0-1st-cp33-noabi-noarch.whl")
  68. wf2_info = wf2.parsed_filename.groupdict()
  69. assert wf2_info['build'] == '1st', wf2_info
  70. def test_convert_egg():
  71. base = pkg_resources.resource_filename('wheel.test', '')
  72. for dist in test_distributions:
  73. distdir = os.path.join(base, dist, 'dist')
  74. eggs = [e for e in os.listdir(distdir) if e.endswith('.egg')]
  75. wheel.tool.convert(eggs, distdir, verbose=False)
  76. def test_unpack():
  77. """
  78. Make sure 'wheel unpack' works.
  79. This also verifies the integrity of our testing wheel files.
  80. """
  81. for dist in test_distributions:
  82. distdir = pkg_resources.resource_filename('wheel.test',
  83. os.path.join(dist, 'dist'))
  84. for wheelfile in (w for w in os.listdir(distdir) if w.endswith('.whl')):
  85. wheel.tool.unpack(os.path.join(distdir, wheelfile), distdir)
  86. def test_no_scripts():
  87. """Make sure entry point scripts are not generated."""
  88. dist = "complex-dist"
  89. basedir = pkg_resources.resource_filename('wheel.test', dist)
  90. for (dirname, subdirs, filenames) in os.walk(basedir):
  91. for filename in filenames:
  92. if filename.endswith('.whl'):
  93. whl = ZipFile(os.path.join(dirname, filename))
  94. for entry in whl.infolist():
  95. assert not '.data/scripts/' in entry.filename
  96. def test_pydist():
  97. """Make sure pydist.json exists and validates against our schema."""
  98. # XXX this test may need manual cleanup of older wheels
  99. import jsonschema
  100. def open_json(filename):
  101. return json.loads(open(filename, 'rb').read().decode('utf-8'))
  102. pymeta_schema = open_json(resource_filename('wheel.test',
  103. 'pydist-schema.json'))
  104. valid = 0
  105. for dist in ("simple.dist", "complex-dist"):
  106. basedir = pkg_resources.resource_filename('wheel.test', dist)
  107. for (dirname, subdirs, filenames) in os.walk(basedir):
  108. for filename in filenames:
  109. if filename.endswith('.whl'):
  110. whl = ZipFile(os.path.join(dirname, filename))
  111. for entry in whl.infolist():
  112. if entry.filename.endswith('/metadata.json'):
  113. pymeta = json.loads(whl.read(entry).decode('utf-8'))
  114. jsonschema.validate(pymeta, pymeta_schema)
  115. valid += 1
  116. assert valid > 0, "No metadata.json found"
  117. def test_util():
  118. """Test functions in util.py."""
  119. for i in range(10):
  120. before = b'*' * i
  121. encoded = wheel.util.urlsafe_b64encode(before)
  122. assert not encoded.endswith(b'=')
  123. after = wheel.util.urlsafe_b64decode(encoded)
  124. assert before == after
  125. def test_pick_best():
  126. """Test the wheel ranking algorithm."""
  127. def get_tags(res):
  128. info = res[-1].parsed_filename.groupdict()
  129. return info['pyver'], info['abi'], info['plat']
  130. cand_tags = [('py27', 'noabi', 'noarch'), ('py26', 'noabi', 'noarch'),
  131. ('cp27', 'noabi', 'linux_i686'),
  132. ('cp26', 'noabi', 'linux_i686'),
  133. ('cp27', 'noabi', 'linux_x86_64'),
  134. ('cp26', 'noabi', 'linux_x86_64')]
  135. cand_wheels = [WheelFile('testpkg-1.0-%s-%s-%s.whl' % t)
  136. for t in cand_tags]
  137. supported = [('cp27', 'noabi', 'linux_i686'), ('py27', 'noabi', 'noarch')]
  138. supported2 = [('cp27', 'noabi', 'linux_i686'), ('py27', 'noabi', 'noarch'),
  139. ('cp26', 'noabi', 'linux_i686'), ('py26', 'noabi', 'noarch')]
  140. supported3 = [('cp26', 'noabi', 'linux_i686'), ('py26', 'noabi', 'noarch'),
  141. ('cp27', 'noabi', 'linux_i686'), ('py27', 'noabi', 'noarch')]
  142. for supp in (supported, supported2, supported3):
  143. context = lambda: list(supp)
  144. for wheel in cand_wheels:
  145. wheel.context = context
  146. best = max(cand_wheels)
  147. assert list(best.tags)[0] == supp[0]
  148. # assert_equal(
  149. # list(map(get_tags, pick_best(cand_wheels, supp, top=False))), supp)