Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions astrbot/cli/utils/version_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def compare_version(v1: str, v2: str) -> int:

Returns 1 if v1 > v2, -1 if v1 < v2, 0 if v1 == v2.
"""
v1 = v1.lower().replace("v", "")
v2 = v2.lower().replace("v", "")
v1 = v1[1:] if v1.startswith(("v", "V")) else v1
v2 = v2[1:] if v2.startswith(("v", "V")) else v2

def split_version(version):
match = re.match(
Expand Down
4 changes: 2 additions & 2 deletions astrbot/core/utils/version_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ def compare_version(v1: str, v2: str) -> int:

返回 1 表示 v1 > v2,返回 -1 表示 v1 < v2,返回 0 表示 v1 = v2。
"""
v1 = v1.lower().replace("v", "")
v2 = v2.lower().replace("v", "")
v1 = v1[1:] if v1.startswith(("v", "V")) else v1
v2 = v2[1:] if v2.startswith(("v", "V")) else v2

def split_version(version):
match = re.match(
Expand Down
77 changes: 77 additions & 0 deletions tests/test_version_comparator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import runpy
from pathlib import Path

import pytest


@pytest.fixture(params=["core", "cli"])
def comparator(request):
"""Load the actual comparator without initializing databases and providers.

Args:
request: Pytest request selecting the core or CLI implementation.

Returns:
The production VersionComparator class for the selected implementation.
"""
module_path = (
Path(__file__).resolve().parents[1]
/ "astrbot"
/ request.param
/ "utils"
/ "version_comparator.py"
)
return runpy.run_path(str(module_path))["VersionComparator"]


@pytest.mark.parametrize(
("left", "right", "expected"),
[
("1.0.0-dev", "1.0.0-delta", 1),
("1.0.0-v1", "1.0.0-1", 1),
("1.0.0-BETA", "1.0.0-alpha", -1),
("1.0.0-RC.1", "1.0.0-rc.1", -1),
("v1.0.0-dev", "V1.0.0-delta", 1),
("v1.0.0", "V1.0.0", 0),
("1.0.0-dev+build.v1", "1.0.0-dev+build.V2", 0),
("1.0", "1.0.0", 0),
("1.9", "1.10", -1),
("1.0.0.1", "1.0.0", 1),
("1.0.0-beta.2", "1.0.0-beta.11", -1),
("1.0.0-alpha", "1.0.0", -1),
("1.0.0+v1", "1.0.0+V2", 0),
],
)
def test_compare_versions(comparator, left, right, expected):
"""Check version precedence in both directions.

Args:
comparator: Production core or CLI comparator class.
left: First version string.
right: Second version string.
expected: Expected comparison result for left against right.
"""
assert comparator.compare_version(left, right) == expected
assert comparator.compare_version(right, left) == -expected


def test_semver_precedence_example(comparator):
"""Check the ordering example from SemVer section 11.

Args:
comparator: Production core or CLI comparator class.
"""
versions = [
"1.0.0-alpha",
"1.0.0-alpha.1",
"1.0.0-alpha.beta",
"1.0.0-beta",
"1.0.0-beta.2",
"1.0.0-beta.11",
"1.0.0-rc.1",
"1.0.0",
]

for left, right in zip(versions, versions[1:]):
assert comparator.compare_version(left, right) == -1
assert comparator.compare_version(right, left) == 1
Loading