From d341be83e5e4d8cd8465f55b187b662b60c27eff Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Fri, 6 Oct 2023 23:19:49 +0200 Subject: [PATCH] VersionRange: improve error message for empty range (#40345) --- lib/spack/spack/test/versions.py | 5 +++-- lib/spack/spack/version/__init__.py | 2 ++ lib/spack/spack/version/common.py | 4 ++++ lib/spack/spack/version/version_types.py | 8 ++++++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/spack/spack/test/versions.py b/lib/spack/spack/test/versions.py index c606fad8ed..1dcf28cd71 100644 --- a/lib/spack/spack/test/versions.py +++ b/lib/spack/spack/test/versions.py @@ -17,6 +17,7 @@ import spack.package_base import spack.spec from spack.version import ( + EmptyRangeError, GitVersion, StandardVersion, Version, @@ -695,9 +696,9 @@ def test_version_range_nonempty(): def test_empty_version_range_raises(): - with pytest.raises(ValueError): + with pytest.raises(EmptyRangeError, match="2:1.0 is an empty range"): assert VersionRange("2", "1.0") - with pytest.raises(ValueError): + with pytest.raises(EmptyRangeError, match="2:1.0 is an empty range"): assert ver("2:1.0") diff --git a/lib/spack/spack/version/__init__.py b/lib/spack/spack/version/__init__.py index 25745a94fd..b25048f052 100644 --- a/lib/spack/spack/version/__init__.py +++ b/lib/spack/spack/version/__init__.py @@ -16,6 +16,7 @@ """ from .common import ( + EmptyRangeError, VersionChecksumError, VersionError, VersionLookupError, @@ -54,5 +55,6 @@ "VersionError", "VersionChecksumError", "VersionLookupError", + "EmptyRangeError", "any_version", ] diff --git a/lib/spack/spack/version/common.py b/lib/spack/spack/version/common.py index e26339d132..28dced815c 100644 --- a/lib/spack/spack/version/common.py +++ b/lib/spack/spack/version/common.py @@ -35,3 +35,7 @@ class VersionChecksumError(VersionError): class VersionLookupError(VersionError): """Raised for errors looking up git commits as versions.""" + + +class EmptyRangeError(VersionError): + """Raised when constructing an empty version range.""" diff --git a/lib/spack/spack/version/version_types.py b/lib/spack/spack/version/version_types.py index 223cad40d6..87f4d26308 100644 --- a/lib/spack/spack/version/version_types.py +++ b/lib/spack/spack/version/version_types.py @@ -12,6 +12,7 @@ from .common import ( COMMIT_VERSION, + EmptyRangeError, VersionLookupError, infinity_versions, is_git_version, @@ -595,14 +596,17 @@ def up_to(self, index) -> StandardVersion: class ClosedOpenRange: def __init__(self, lo: StandardVersion, hi: StandardVersion): if hi < lo: - raise ValueError(f"{lo}:{hi} is an empty range") + raise EmptyRangeError(f"{lo}..{hi} is an empty range") self.lo: StandardVersion = lo self.hi: StandardVersion = hi @classmethod def from_version_range(cls, lo: StandardVersion, hi: StandardVersion): """Construct ClosedOpenRange from lo:hi range.""" - return ClosedOpenRange(lo, next_version(hi)) + try: + return ClosedOpenRange(lo, next_version(hi)) + except EmptyRangeError as e: + raise EmptyRangeError(f"{lo}:{hi} is an empty range") from e def __str__(self): # This simplifies 3.1:<3.2 to 3.1:3.1 to 3.1