test_version_range.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from build.plugins.lib.nots.semver import Version, Operator, VersionRange
  2. def test_from_str():
  3. checklist = [
  4. (">= 1.2.3", VersionRange, Operator.GE),
  5. (">=1.2.3", VersionRange, Operator.GE),
  6. (">= 1.2.3", VersionRange, Operator.GE),
  7. (" >= 1.2.3 ", VersionRange, Operator.GE),
  8. ("= 1.2.3", VersionRange, Operator.EQ),
  9. ("=1.2.3", VersionRange, Operator.EQ),
  10. ("= 1.2.3", VersionRange, Operator.EQ),
  11. (" = 1.2.3 ", VersionRange, Operator.EQ),
  12. (" 1.2.3", VersionRange, Operator.EQ),
  13. ("1.2.3", VersionRange, Operator.EQ),
  14. (" 1.2.3", VersionRange, Operator.EQ),
  15. (" 1.2.3 ", VersionRange, Operator.EQ),
  16. ]
  17. for range_str, expected_class, expected_operator in checklist:
  18. range = VersionRange.from_str(range_str)
  19. assert isinstance(range, expected_class), f"unexpected class for '{range_str}': '{type(range)}'"
  20. assert range.operator == expected_operator, f"unexpected operator for '{range_str}': '{range.operator}'"
  21. def test_from_str_error():
  22. error_template = "Unsupported version range: '{}'. Currently we only support ranges with stable versions and GE / EQ: '>= 1.2.3' / '= 1.2.3' / '1.2.3'"
  23. checklist = [
  24. (r"¯\_(ツ)_/¯", ValueError, error_template),
  25. ("<= 1.2.3", ValueError, error_template),
  26. ("<=1.2.3", ValueError, error_template),
  27. ("<= 1.2.3", ValueError, error_template),
  28. (" <= 1.2.3 ", ValueError, error_template),
  29. ("< 1.2.3", ValueError, error_template),
  30. ("<1.2.3", ValueError, error_template),
  31. ("< 1.2.3", ValueError, error_template),
  32. (" < 1.2.3 ", ValueError, error_template),
  33. ("> 1.2.3", ValueError, error_template),
  34. (">1.2.3", ValueError, error_template),
  35. ("> 1.2.3", ValueError, error_template),
  36. (" > 1.2.3 ", ValueError, error_template),
  37. ("0.0.1-beta", ValueError, error_template),
  38. ]
  39. for range_str, expected_class, expected_msg_template in checklist:
  40. try:
  41. VersionRange.from_str(range_str)
  42. except Exception as exception:
  43. error = exception
  44. assert isinstance(error, expected_class), f"unexpected error class for '{range_str}': '{type(error)}'"
  45. assert str(error) == expected_msg_template.format(
  46. range_str
  47. ), f"unexpected error message for '{range_str}': '{error}'"
  48. def test_init():
  49. checklist = [
  50. (Operator.GE, "1.2.3", Operator.GE, Version(1, 2, 3)),
  51. (Operator.GE, " 1.2.3 ", Operator.GE, Version(1, 2, 3)),
  52. (Operator.GE, "0.0.1", Operator.GE, Version(0, 0, 1)),
  53. (Operator.EQ, "1.2.3", Operator.EQ, Version(1, 2, 3)),
  54. (Operator.EQ, " 1.2.3 ", Operator.EQ, Version(1, 2, 3)),
  55. (Operator.EQ, "0.0.1", Operator.EQ, Version(0, 0, 1)),
  56. (None, "1.2.3", Operator.EQ, Version(1, 2, 3)),
  57. (None, " 1.2.3 ", Operator.EQ, Version(1, 2, 3)),
  58. (None, "0.0.1", Operator.EQ, Version(0, 0, 1)),
  59. ]
  60. for operator_provided, version_provided, expected_operator, expected_version in checklist:
  61. range = VersionRange(operator_provided, Version.from_str(version_provided))
  62. assert (
  63. range.operator == expected_operator
  64. ), f"unexpected operator for '{operator_provided}', '{version_provided}': '{range.operator}'"
  65. assert (
  66. range.version == expected_version
  67. ), f"unexpected result version for '{operator_provided}', '{version_provided}': '{range.version}'"
  68. def test_is_satisfied():
  69. checklist = [
  70. (">= 1.2.3", "1.2.3", True),
  71. (">= 1.2.3", "1.2.4", True),
  72. (">= 1.2.3", "1.3.0", True),
  73. (">= 1.2.3", "2.0.0", True),
  74. (">= 1.2.3", "5.8.2", True),
  75. (">= 1.2.3", "1.2.2", False),
  76. (">= 1.2.3", "0.100.200", False),
  77. ("= 1.2.3", "1.2.3", True),
  78. ("1.2.3", "1.2.3", True),
  79. ("1.2.3", "1.2.2", False),
  80. ("1.2.3", "1.3.3", False),
  81. ("1.2.3", "2.2.3", False),
  82. ("12345.45634.456234", "12345.45634.456234", True),
  83. ("0.0.0", "0.0.0", True),
  84. ]
  85. for range_provided, version_provided, expected_result in checklist:
  86. version = Version.from_str(version_provided)
  87. range = VersionRange.from_str(range_provided)
  88. assert (
  89. range.is_satisfied_by(version) == expected_result
  90. ), f"Unexpected is_satisfied_by result for '{range_provided}', '{version_provided}': {(not expected_result)}"