tests.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_match_not_equal():
  39. assert match("2.3.7", "!=2.3.8") is True
  40. assert match("2.3.7", "!=2.3.6") is True
  41. assert match("2.3.7", "!=2.3.7") is False
  42. def test_should_not_raise_value_error_for_expected_match_expression():
  43. assert match("2.3.7", "<2.4.0") is True
  44. assert match("2.3.7", ">2.3.5") is True
  45. assert match("2.3.7", "<=2.3.9") is True
  46. assert match("2.3.7", ">=2.3.5") is True
  47. assert match("2.3.7", "==2.3.7") is True
  48. assert match("2.3.7", "!=2.3.7") is False
  49. def test_should_raise_value_error_for_unexpected_match_expression():
  50. with pytest.raises(ValueError):
  51. match("2.3.7", "=2.3.7")
  52. with pytest.raises(ValueError):
  53. match("2.3.7", "~2.3.7")
  54. with pytest.raises(ValueError):
  55. match("2.3.7", "^2.3.7")
  56. def test_should_raise_value_error_for_zero_prefixed_versions():
  57. with pytest.raises(ValueError):
  58. parse("01.2.3")
  59. with pytest.raises(ValueError):
  60. parse("1.02.3")
  61. with pytest.raises(ValueError):
  62. parse("1.2.03")
  63. def test_should_raise_value_error_for_invalid_value():
  64. with pytest.raises(ValueError):
  65. compare('foo', 'bar')
  66. with pytest.raises(ValueError):
  67. compare('1.0', '1.0.0')
  68. with pytest.raises(ValueError):
  69. compare('1.x', '1.0.0')
  70. def test_should_raise_value_error_for_invalid_match_expression():
  71. with pytest.raises(ValueError):
  72. match('1.0.0', '')
  73. with pytest.raises(ValueError):
  74. match('1.0.0', '!')
  75. with pytest.raises(ValueError):
  76. match('1.0.0', '1.0.0')
  77. def test_should_follow_specification_comparison():
  78. """
  79. produce comparison chain:
  80. 1.0.0-alpha < 1.0.0-alpha.1 < 1.0.0-beta.2 < 1.0.0-beta.11
  81. < 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
  82. < 1.3.7+build.2.b8f12d7 < 1.3.7+build.11.e0f985a
  83. and in backward too.
  84. """
  85. chain = [
  86. '1.0.0-alpha', '1.0.0-alpha.1', '1.0.0-beta.2', '1.0.0-beta.11',
  87. '1.0.0-rc.1', '1.0.0', '1.3.7+build',
  88. ]
  89. versions = zip(chain[:-1], chain[1:])
  90. for low_version, high_version in versions:
  91. assert compare(low_version, high_version) == -1, \
  92. '%s should be lesser than %s' % (low_version, high_version)
  93. assert compare(high_version, low_version) == 1, \
  94. '%s should be higher than %s' % (high_version, low_version)
  95. def test_should_compare_rc_builds():
  96. assert compare('1.0.0-beta.2', '1.0.0-beta.11') == -1
  97. def test_should_compare_release_candidate_with_release():
  98. assert compare('1.0.0-rc.1', '1.0.0') == -1
  99. assert compare('1.0.0-rc.1+build.1', '1.0.0') == -1
  100. def test_should_say_equal_versions_are_equal():
  101. assert compare('2.0.0', '2.0.0') == 0
  102. assert compare('1.1.9-rc.1', '1.1.9-rc.1') == 0
  103. assert compare('1.1.9+build.1', '1.1.9+build.1') == 0
  104. assert compare('1.1.9-rc.1+build.1', '1.1.9-rc.1+build.1') == 0
  105. def test_should_compare_versions_with_build_and_release():
  106. assert compare('1.1.9-rc.1', '1.1.9-rc.1+build.1') == 0
  107. assert compare('1.1.9-rc.1', '1.1.9+build.1') == -1
  108. def test_should_ignore_builds_on_compare():
  109. assert compare('1.0.0+build.1', '1.0.0') == 0
  110. assert compare('1.0.0-alpha.1+build.1', '1.0.0-alpha.1') == 0
  111. assert compare('1.0.0+build.1', '1.0.0-alpha.1') == 1
  112. assert compare('1.0.0+build.1', '1.0.0-alpha.1+build.1') == 1
  113. def test_should_correctly_format_version():
  114. assert format_version(3, 4, 5) == '3.4.5'
  115. assert format_version(3, 4, 5, 'rc.1') == '3.4.5-rc.1'
  116. assert format_version(3, 4, 5, prerelease='rc.1') == '3.4.5-rc.1'
  117. assert format_version(3, 4, 5, build='build.4') == '3.4.5+build.4'
  118. assert format_version(3, 4, 5, 'rc.1', 'build.4') == '3.4.5-rc.1+build.4'
  119. def test_should_bump_major():
  120. assert bump_major('3.4.5') == '4.0.0'
  121. def test_should_bump_minor():
  122. assert bump_minor('3.4.5') == '3.5.0'
  123. def test_should_bump_patch():
  124. assert bump_patch('3.4.5') == '3.4.6'
  125. def test_should_ignore_extensions_for_bump():
  126. assert bump_patch('3.4.5-rc1+build4') == '3.4.6'
  127. def test_should_get_max():
  128. assert max_ver('3.4.5', '4.0.2') == '4.0.2'
  129. def test_should_get_min():
  130. assert min_ver('3.4.5', '4.0.2') == '3.4.5'
  131. def test_should_get_min_same():
  132. assert min_ver('3.4.5', '3.4.5') == '3.4.5'
  133. def test_should_get_more_rc1():
  134. assert compare("1.0.0-rc1", "1.0.0-rc0") == 1
  135. def test_prerelease_order():
  136. assert min_ver('1.2.3-rc.2', '1.2.3-rc.10') == '1.2.3-rc.2'
  137. assert min_ver('1.2.3-rc2', '1.2.3-rc10') == '1.2.3-rc10'
  138. # identifiers with letters or hyphens are compared lexically in ASCII sort
  139. # order.
  140. assert min_ver('1.2.3-Rc10', '1.2.3-rc10') == '1.2.3-Rc10'
  141. # Numeric identifiers always have lower precedence than non-numeric
  142. # identifiers.
  143. assert min_ver('1.2.3-2', '1.2.3-rc') == '1.2.3-rc'
  144. # A larger set of pre-release fields has a higher precedence than a
  145. # smaller set, if all of the preceding identifiers are equal.
  146. assert min_ver('1.2.3-rc.2.1', '1.2.3-rc.2') == '1.2.3-rc.2'
  147. # When major, minor, and patch are equal, a pre-release version has lower
  148. # precedence than a normal version.
  149. assert min_ver('1.2.3', '1.2.3-1') == '1.2.3-1'
  150. assert min_ver('1.0.0-alpha', '1.0.0-alpha.1') == '1.0.0-alpha'
  151. def test_should_bump_prerelease():
  152. assert bump_prerelease('3.4.5-rc.9') == '3.4.5-rc.10'
  153. assert bump_prerelease('3.4.5-0009.dev') == '3.4.5-0010.dev'
  154. assert bump_prerelease('3.4.5') == '3.4.5-rc.1'
  155. def test_should_ignore_build_on_prerelease_bump():
  156. assert bump_prerelease('3.4.5-rc.1+build.4') == '3.4.5-rc.2'
  157. def test_should_bump_build():
  158. assert bump_build('3.4.5-rc.1+build.9') == '3.4.5-rc.1+build.10'
  159. assert bump_build('3.4.5-rc.1+0009.dev') == '3.4.5-rc.1+0010.dev'
  160. assert bump_build('3.4.5-rc.1') == '3.4.5-rc.1+build.1'
  161. assert bump_build('3.4.5') == '3.4.5+build.1'