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, is_git_version,
ver, ver,
) )
from spack.version.git_ref_lookup import SEMVER_REGEX
def assert_ver_lt(a, b): 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 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

@ -22,11 +22,16 @@
from .lookup import AbstractRefLookup from .lookup import AbstractRefLookup
# 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}$")
class GitRefLookup(AbstractRefLookup): class GitRefLookup(AbstractRefLookup):
@ -186,11 +191,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: