test_pypi.py 4.4 KB

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