test_pypi.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. from os.path import join as pjoin
  2. from pathlib import Path
  3. import pytest
  4. from testpath import assert_isfile
  5. from nsist.wheels import (
  6. WheelLocator, extract_wheel, CachedRelease, merge_dir_to, NoWheelError,
  7. CompatibilityScorer,
  8. )
  9. # To exclude tests requiring network on an unplugged machine, use: pytest -m "not network"
  10. @pytest.mark.network
  11. def test_download(tmpdir):
  12. tmpdir = str(tmpdir)
  13. wd = WheelLocator("astsearch==0.1.2", CompatibilityScorer("3.5.1", "win_amd64"))
  14. wheel = wd.fetch()
  15. assert_isfile(wheel)
  16. extract_wheel(wheel, target_dir=tmpdir)
  17. assert_isfile(pjoin(tmpdir, 'astsearch.py'))
  18. assert_isfile(pjoin(tmpdir, 'astsearch-0.1.2.dist-info', 'METADATA'))
  19. @pytest.mark.network
  20. def test_bad_name():
  21. # Packages can't be named after stdlib modules like os
  22. wl = WheelLocator("os==1.0", "3.5.1", 64)
  23. with pytest.raises(NoWheelError):
  24. wl.get_from_pypi()
  25. @pytest.mark.network
  26. def test_bad_version():
  27. wl = WheelLocator("pynsist==0.99.99", "3.5.1", 64)
  28. with pytest.raises(NoWheelError):
  29. wl.get_from_pypi()
  30. def test_extra_sources(tmpdir):
  31. src1 = Path(str(tmpdir.mkdir('src1')))
  32. src2 = Path(str(tmpdir.mkdir('src2')))
  33. # First one found wins, even if a later one is more specific.
  34. expected = (src1 / 'astsearch-0.1.2-py3-none-any.whl')
  35. expected.touch()
  36. (src2 / 'astsearch-0.1.2-py3-none-win_amd64.whl').touch()
  37. scorer = CompatibilityScorer("3.5.1", "win_amd64")
  38. wl = WheelLocator("astsearch==0.1.2", scorer, extra_sources=[src1, src2])
  39. assert wl.check_extra_sources() == expected
  40. wl = WheelLocator("astsearch==0.2.0", scorer, extra_sources=[src1, src2])
  41. assert wl.check_extra_sources() is None
  42. def test_pick_best_wheel():
  43. wd37 = WheelLocator("astsearch==0.1.2", CompatibilityScorer("3.7.1", "win_amd64"))
  44. wd38 = WheelLocator("astsearch==0.1.2", CompatibilityScorer("3.8.0", "win_amd64"))
  45. # Some of the wheel filenames below are impossible combinations - they are
  46. # there to test the scoring and ranking machinery.
  47. # Prefer Windows-specific wheel
  48. releases = [
  49. CachedRelease('astsearch-0.1.2-py3-none-any.whl'),
  50. CachedRelease('astsearch-0.1.2-py3-none-win_amd64.whl'),
  51. ]
  52. assert wd37.pick_best_wheel(releases) == releases[1]
  53. # Wrong Windows bitness
  54. releases = [
  55. CachedRelease('astsearch-0.1.2-py3-none-any.whl'),
  56. CachedRelease('astsearch-0.1.2-py3-none-win_32.whl'),
  57. ]
  58. assert wd37.pick_best_wheel(releases) == releases[0]
  59. # Prefer more specific Python version
  60. releases = [
  61. CachedRelease('astsearch-0.1.2-cp37-none-any.whl'),
  62. CachedRelease('astsearch-0.1.2-py3-none-any.whl'),
  63. ]
  64. assert wd37.pick_best_wheel(releases) == releases[0]
  65. # Prefer more specific Python version
  66. releases = [
  67. CachedRelease('astsearch-0.1.2-py34.py37-none-any.whl'),
  68. CachedRelease('astsearch-0.1.2-py3-none-any.whl'),
  69. ]
  70. assert wd37.pick_best_wheel(releases) == releases[0]
  71. # Incompatible Python version
  72. releases = [
  73. CachedRelease('astsearch-0.1.2-cp33-none-any.whl'),
  74. CachedRelease('astsearch-0.1.2-py3-none-any.whl'),
  75. ]
  76. assert wd37.pick_best_wheel(releases) == releases[1]
  77. # Prefer more specific ABI version
  78. releases = [
  79. CachedRelease('astsearch-0.1.2-py3-abi3-any.whl'),
  80. CachedRelease('astsearch-0.1.2-py3-none-any.whl'),
  81. ]
  82. assert wd37.pick_best_wheel(releases) == releases[0]
  83. # ABI suffix on Python <3.8
  84. releases = [
  85. CachedRelease('astsearch-0.1.2-cp37-cp37-any.whl'),
  86. CachedRelease('astsearch-0.1.2-cp37-cp37m-any.whl'),
  87. ]
  88. assert wd37.pick_best_wheel(releases) == releases[1]
  89. # No ABI suffix on Python >=3.8
  90. releases = [
  91. CachedRelease('astsearch-0.1.2-cp38-cp38-any.whl'),
  92. CachedRelease('astsearch-0.1.2-cp38-cp38m-any.whl'),
  93. ]
  94. assert wd38.pick_best_wheel(releases) == releases[0]
  95. # Incompatible ABI version
  96. releases = [
  97. CachedRelease('astsearch-0.1.2-cp37-abi4-win_amd64.whl'),
  98. CachedRelease('astsearch-0.1.2-py3-none-any.whl'),
  99. ]
  100. assert wd37.pick_best_wheel(releases) == releases[1]
  101. # Platform has priority over other attributes
  102. releases = [
  103. CachedRelease('astsearch-0.1.2-cp37-abi3-any.whl'),
  104. CachedRelease('astsearch-0.1.2-py2.py3-none-win_amd64.whl'),
  105. ]
  106. assert wd37.pick_best_wheel(releases) == releases[1]
  107. def test_merge_dir_to(tmpdir):
  108. td1 = Path(str(tmpdir.mkdir('one')))
  109. td2 = Path(str(tmpdir.mkdir('two')))
  110. with (td1 / 'ab').open('w') as f:
  111. f.write(u"original")
  112. with (td2 / 'ab').open('w') as f:
  113. f.write(u"alternate")
  114. (td1 / 'subdir').mkdir()
  115. with (td1 / 'subdir' / 'foo').open('w'): pass
  116. (td2 / 'subdir').mkdir()
  117. with (td2 / 'subdir' / 'bar').open('w'): pass
  118. merge_dir_to(td2, td1)
  119. assert_isfile(td1 / 'subdir' / 'foo')
  120. assert_isfile(td1 / 'subdir' / 'bar')
  121. with (td1 / 'ab').open() as f:
  122. assert f.read() == u"alternate"
  123. # Test with conflicts
  124. (td1 / 'conflict').mkdir()
  125. with (td2 / 'conflict').open('w'):
  126. pass
  127. with pytest.raises(RuntimeError):
  128. merge_dir_to(td2, td1)
  129. with pytest.raises(RuntimeError):
  130. merge_dir_to(td1, td2)