test_pypi.py 4.8 KB

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