1
0

test_pypi.py 4.8 KB

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