1
0

test_pypi.py 4.5 KB

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