tests.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import pytest # noqa
  2. from semver import compare
  3. from semver import match
  4. from semver import parse
  5. from semver import format_version
  6. from semver import bump_major
  7. from semver import bump_minor
  8. from semver import bump_patch
  9. from semver import bump_prerelease
  10. from semver import bump_build
  11. from semver import min_ver
  12. from semver import max_ver
  13. def test_should_parse_version():
  14. result = parse("1.2.3-alpha.1.2+build.11.e0f985a")
  15. assert result == {
  16. 'major': 1,
  17. 'minor': 2,
  18. 'patch': 3,
  19. 'prerelease': 'alpha.1.2',
  20. 'build': 'build.11.e0f985a',
  21. }
  22. result = parse("1.2.3-alpha-1+build.11.e0f985a")
  23. assert result == {
  24. 'major': 1,
  25. 'minor': 2,
  26. 'patch': 3,
  27. 'prerelease': 'alpha-1',
  28. 'build': 'build.11.e0f985a',
  29. }
  30. def test_should_get_less():
  31. assert compare("1.0.0", "2.0.0") == -1
  32. def test_should_get_greater():
  33. assert compare("2.0.0", "1.0.0") == 1
  34. def test_should_match_simple():
  35. assert match("2.3.7", ">=2.3.6") is True
  36. def test_should_no_match_simple():
  37. assert match("2.3.7", ">=2.3.8") is False
  38. def test_should_raise_value_error_for_zero_prefixed_versions():
  39. with pytest.raises(ValueError):
  40. parse("01.2.3")
  41. with pytest.raises(ValueError):
  42. parse("1.02.3")
  43. with pytest.raises(ValueError):
  44. parse("1.2.03")
  45. def test_should_raise_value_error_for_invalid_value():
  46. with pytest.raises(ValueError):
  47. compare('foo', 'bar')
  48. with pytest.raises(ValueError):
  49. compare('1.0', '1.0.0')
  50. with pytest.raises(ValueError):
  51. compare('1.x', '1.0.0')
  52. def test_should_raise_value_error_for_invalid_match_expression():
  53. with pytest.raises(ValueError):
  54. match('1.0.0', '')
  55. with pytest.raises(ValueError):
  56. match('1.0.0', '!')
  57. with pytest.raises(ValueError):
  58. match('1.0.0', '1.0.0')
  59. def test_should_follow_specification_comparison():
  60. """
  61. produce comparison chain:
  62. 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11
  63. < 1.0.0-rc.1 < 1.0.0-rc.1+build.1 < 1.0.0 < 1.0.0+0.3.7 < 1.3.7+build
  64. < 1.3.7+build.2.b8f12d7 < 1.3.7+build.11.e0f985a
  65. and in backward too.
  66. """
  67. chain = [
  68. '1.0.0-alpha', '1.0.0-alpha.1', '1.0.0-beta.2', '1.0.0-beta.11',
  69. '1.0.0-rc.1', '1.0.0', '1.3.7+build',
  70. ]
  71. versions = zip(chain[:-1], chain[1:])
  72. for low_version, high_version in versions:
  73. assert compare(low_version, high_version) == -1, \
  74. '%s should be lesser than %s' % (low_version, high_version)
  75. assert compare(high_version, low_version) == 1, \
  76. '%s should be higher than %s' % (high_version, low_version)
  77. def test_should_compare_rc_builds():
  78. assert compare('1.0.0-beta.2', '1.0.0-beta.11') == -1
  79. def test_should_compare_release_candidate_with_release():
  80. assert compare('1.0.0-rc.1+build.1', '1.0.0') == -1
  81. def test_should_say_equal_versions_are_equal():
  82. assert compare('2.0.0', '2.0.0') == 0
  83. assert compare('1.1.9-rc.1', '1.1.9-rc.1') == 0
  84. assert compare('1.1.9+build.1', '1.1.9+build.1') == 0
  85. assert compare('1.1.9-rc.1+build.1', '1.1.9-rc.1+build.1') == 0
  86. def test_should_compare_versions_with_build_and_release():
  87. assert compare('1.1.9-rc.1', '1.1.9-rc.1+build.1') == -1
  88. assert compare('1.1.9-rc.1', '1.1.9+build.1') == 1
  89. def test_should_correctly_format_version():
  90. assert format_version(3, 4, 5) == '3.4.5'
  91. assert format_version(3, 4, 5, 'rc.1') == '3.4.5-rc.1'
  92. assert format_version(3, 4, 5, prerelease='rc.1') == '3.4.5-rc.1'
  93. assert format_version(3, 4, 5, build='build.4') == '3.4.5+build.4'
  94. assert format_version(3, 4, 5, 'rc.1', 'build.4') == '3.4.5-rc.1+build.4'
  95. def test_should_bump_major():
  96. assert bump_major('3.4.5') == '4.0.0'
  97. def test_should_bump_minor():
  98. assert bump_minor('3.4.5') == '3.5.0'
  99. def test_should_bump_patch():
  100. assert bump_patch('3.4.5') == '3.4.6'
  101. def test_should_ignore_extensions_for_bump():
  102. assert bump_patch('3.4.5-rc1+build4') == '3.4.6'
  103. def test_should_get_max():
  104. assert max_ver('3.4.5', '4.0.2') == '4.0.2'
  105. def test_should_get_min():
  106. assert min_ver('3.4.5', '4.0.2') == '3.4.5'
  107. def test_should_get_min_same():
  108. assert min_ver('3.4.5', '3.4.5') == '3.4.5'
  109. def test_should_get_more_rc1():
  110. assert compare("1.0.0-rc1", "1.0.0-rc0") == 1
  111. def test_prerelease_order():
  112. assert min_ver('1.2.3-rc.2', '1.2.3-rc.10') == '1.2.3-rc.2'
  113. assert min_ver('1.2.3-rc2', '1.2.3-rc10') == '1.2.3-rc10'
  114. # identifiers with letters or hyphens are compared lexically in ASCII sort
  115. # order.
  116. assert min_ver('1.2.3-Rc10', '1.2.3-rc10') == '1.2.3-Rc10'
  117. # Numeric identifiers always have lower precedence than non-numeric
  118. # identifiers.
  119. assert min_ver('1.2.3-2', '1.2.3-rc') == '1.2.3-rc'
  120. # A larger set of pre-release fields has a higher precedence than a
  121. # smaller set, if all of the preceding identifiers are equal.
  122. assert min_ver('1.2.3-rc.2.1', '1.2.3-rc.2') == '1.2.3-rc.2'
  123. # When major, minor, and patch are equal, a pre-release version has lower
  124. # precedence than a normal version.
  125. assert min_ver('1.2.3', '1.2.3-1') == '1.2.3-1'
  126. assert min_ver('1.0.0-alpha', '1.0.0-alpha.1') == '1.0.0-alpha'
  127. def test_should_bump_prerelease():
  128. assert bump_prerelease('3.4.5-rc.9') == '3.4.5-rc.10'
  129. assert bump_prerelease('3.4.5-0009.dev') == '3.4.5-0010.dev'
  130. assert bump_prerelease('3.4.5') == '3.4.5-rc.1'
  131. def test_should_ignore_build_on_prerelease_bump():
  132. assert bump_prerelease('3.4.5-rc.1+build.4') == '3.4.5-rc.2'
  133. def test_should_bump_build():
  134. assert bump_build('3.4.5-rc.1+build.9') == '3.4.5-rc.1+build.10'
  135. assert bump_build('3.4.5-rc.1+0009.dev') == '3.4.5-rc.1+0010.dev'
  136. assert bump_build('3.4.5-rc.1') == '3.4.5-rc.1+build.1'
  137. assert bump_build('3.4.5') == '3.4.5+build.1'