Fix broken semver regex (#39414)

This commit is contained in:
Harmen Stoppels 2023-08-14 15:38:06 +02:00
parent 0e905f5e64
commit 12cb071fe2
2 changed files with 36 additions and 9 deletions

View File

@ -17,6 +17,7 @@
import spack.package_base import spack.package_base
import spack.spec import spack.spec
from spack.version import ( from spack.version import (
SEMVER_REGEX,
GitVersion, GitVersion,
StandardVersion, StandardVersion,
Version, Version,
@ -976,3 +977,25 @@ def test_unresolvable_git_versions_error(config, mock_packages):
# The package exists, but does not have a git property set. When dereferencing # The package exists, but does not have a git property set. When dereferencing
# the version, we should get VersionLookupError, not a generic AttributeError. # the version, we should get VersionLookupError, not a generic AttributeError.
spack.spec.Spec(f"git-test-commit@{'a' * 40}").version.ref_version spack.spec.Spec(f"git-test-commit@{'a' * 40}").version.ref_version
@pytest.mark.parametrize(
"tag,expected",
[
("v100.2.3", "100.2.3"),
("v1.2.3", "1.2.3"),
("v1.2.3-pre.release+build.1", "1.2.3-pre.release+build.1"),
("v1.2.3+build.1", "1.2.3+build.1"),
("v1.2.3+build_1", None),
("v1.2.3-pre.release", "1.2.3-pre.release"),
("v1.2.3-pre_release", None),
("1.2.3", "1.2.3"),
("1.2.3.", None),
],
)
def test_semver_regex(tag, expected):
result = SEMVER_REGEX.search(tag)
if expected is None:
assert result is None
else:
assert result.group() == expected

View File

@ -40,11 +40,16 @@
SEGMENT_REGEX = re.compile(r"(?:(?P<num>[0-9]+)|(?P<str>[a-zA-Z]+))(?P<sep>[_.-]*)") SEGMENT_REGEX = re.compile(r"(?:(?P<num>[0-9]+)|(?P<str>[a-zA-Z]+))(?P<sep>[_.-]*)")
# regular expression for semantic versioning # regular expression for semantic versioning
SEMVER_REGEX = re.compile( _VERSION_CORE = r"\d+\.\d+\.\d+"
".+(?P<semver>([0-9]+)[.]([0-9]+)[.]([0-9]+)" _IDENT = r"[0-9A-Za-z-]+"
"(?:-([0-9A-Za-z-]+(?:[.][0-9A-Za-z-]+)*))?" _SEPARATED_IDENT = rf"{_IDENT}(?:\.{_IDENT})*"
"(?:[+][0-9A-Za-z-]+)?)" _PRERELEASE = rf"\-{_SEPARATED_IDENT}"
) _BUILD = rf"\+{_SEPARATED_IDENT}"
_SEMVER = rf"{_VERSION_CORE}(?:{_PRERELEASE})?(?:{_BUILD})?"
# clamp on the end, so versions like v1.2.3-rc1 will match
# without the leading 'v'.
SEMVER_REGEX = re.compile(rf"{_SEMVER}$")
# Infinity-like versions. The order in the list implies the comparison rules # Infinity-like versions. The order in the list implies the comparison rules
infinity_versions = ["stable", "trunk", "head", "master", "main", "develop"] infinity_versions = ["stable", "trunk", "head", "master", "main", "develop"]
@ -1319,11 +1324,10 @@ def lookup_ref(self, ref) -> Tuple[Optional[str], int]:
commit_to_version[tag_commit] = v commit_to_version[tag_commit] = v
break break
else: else:
# try to parse tag to copare versions spack does not know # try to parse tag to compare versions spack does not know
match = SEMVER_REGEX.match(tag) match = SEMVER_REGEX.search(tag)
if match: if match:
semver = match.groupdict()["semver"] commit_to_version[tag_commit] = match.group()
commit_to_version[tag_commit] = semver
ancestor_commits = [] ancestor_commits = []
for tag_commit in commit_to_version: for tag_commit in commit_to_version: