From 9d99aa1902efa845b24a599b788c033915bc4b30 Mon Sep 17 00:00:00 2001 From: Aritra Basu <24430013+aritra24@users.noreply.github.com> Date: Thu, 17 Aug 2023 23:03:48 +0530 Subject: [PATCH] project: output version value with error message (#4284) on version error output the erroneous version along with error message mentioning the versioning convention already being output Co-authored-by: Sergio Schvezov --- snapcraft/projects.py | 8 +++--- tests/unit/test_projects.py | 51 +++++++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/snapcraft/projects.py b/snapcraft/projects.py index c9a441efbf..1c822efb32 100644 --- a/snapcraft/projects.py +++ b/snapcraft/projects.py @@ -549,10 +549,10 @@ def _validate_version(cls, version, values): r"^[a-zA-Z0-9](?:[a-zA-Z0-9:.+~-]*[a-zA-Z0-9+~])?$", version ): raise ValueError( - "Snap versions consist of upper- and lower-case alphanumeric characters, " - "as well as periods, colons, plus signs, tildes, and hyphens. They cannot " - "begin with a period, colon, plus sign, tilde, or hyphen. They cannot end " - "with a period, colon, or hyphen" + f"Invalid version '{version}': Snap versions consist of upper- and lower-case " + "alphanumeric characters, as well as periods, colons, plus signs, tildes, " + "and hyphens. They cannot begin with a period, colon, plus sign, tilde, or " + "hyphen. They cannot end with a period, colon, or hyphen" ) return version diff --git a/tests/unit/test_projects.py b/tests/unit/test_projects.py index 28164cc928..48c5ccbb03 100644 --- a/tests/unit/test_projects.py +++ b/tests/unit/test_projects.py @@ -257,16 +257,47 @@ def test_project_version_valid(self, version, project_yaml_data): @pytest.mark.parametrize( "version,error", [ - ("1_0", "Snap versions consist of"), # _ is an invalid character - ("1=1", "Snap versions consist of"), # = is an invalid character - (".1", "Snap versions consist of"), # cannot start with period - (":1", "Snap versions consist of"), # cannot start with colon - ("+1", "Snap versions consist of"), # cannot start with plus sign - ("~1", "Snap versions consist of"), # cannot start with tilde - ("-1", "Snap versions consist of"), # cannot start with hyphen - ("1.", "Snap versions consist of"), # cannot end with period - ("1:", "Snap versions consist of"), # cannot end with colon - ("1-", "Snap versions consist of"), # cannot end with hyphen + ( + "1_0", + "Invalid version '1_0': Snap versions consist of", + ), # _ is an invalid character + ( + "1=1", + "Invalid version '1=1': Snap versions consist of", + ), # = is an invalid character + ( + ".1", + "Invalid version '.1': Snap versions consist of", + ), # cannot start with period + ( + ":1", + "Invalid version ':1': Snap versions consist of", + ), # cannot start with colon + ( + "+1", + r"Invalid version '\+1': Snap versions consist of", + ), # cannot start with plus sign + # escaping + from the regex string to match. + ( + "~1", + "Invalid version '~1': Snap versions consist of", + ), # cannot start with tilde + ( + "-1", + "Invalid version '-1': Snap versions consist of", + ), # cannot start with hyphen + ( + "1.", + "Invalid version '1.': Snap versions consist of", + ), # cannot end with period + ( + "1:", + "Invalid version '1:': Snap versions consist of", + ), # cannot end with colon + ( + "1-", + "Invalid version '1-': Snap versions consist of", + ), # cannot end with hyphen ( "123456789012345678901234567890123", "ensure this value has at most 32 characters",