Fix broken semver regex (#39414)

This commit is contained in:
Harmen Stoppels 2023-08-14 15:38:06 +02:00 committed by GitHub
parent c882214273
commit da46b63a34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 9 deletions

View file

@ -26,6 +26,7 @@
is_git_version,
ver,
)
from spack.version.git_ref_lookup import SEMVER_REGEX
def assert_ver_lt(a, b):
@ -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 version, we should get VersionLookupError, not a generic AttributeError.
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

@ -22,11 +22,16 @@
from .lookup import AbstractRefLookup
# regular expression for semantic versioning
SEMVER_REGEX = re.compile(
".+(?P<semver>([0-9]+)[.]([0-9]+)[.]([0-9]+)"
"(?:-([0-9A-Za-z-]+(?:[.][0-9A-Za-z-]+)*))?"
"(?:[+][0-9A-Za-z-]+)?)"
)
_VERSION_CORE = r"\d+\.\d+\.\d+"
_IDENT = r"[0-9A-Za-z-]+"
_SEPARATED_IDENT = rf"{_IDENT}(?:\.{_IDENT})*"
_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}$")
class GitRefLookup(AbstractRefLookup):
@ -186,11 +191,10 @@ def lookup_ref(self, ref) -> Tuple[Optional[str], int]:
commit_to_version[tag_commit] = v
break
else:
# try to parse tag to copare versions spack does not know
match = SEMVER_REGEX.match(tag)
# try to parse tag to compare versions spack does not know
match = SEMVER_REGEX.search(tag)
if match:
semver = match.groupdict()["semver"]
commit_to_version[tag_commit] = semver
commit_to_version[tag_commit] = match.group()
ancestor_commits = []
for tag_commit in commit_to_version: