From 220fcc54370ec59ce86b30bc19eddbc8acc87094 Mon Sep 17 00:00:00 2001 From: memsharded Date: Thu, 11 May 2023 00:23:36 +0200 Subject: [PATCH] fix accepted ref patterns --- conans/model/recipe_ref.py | 4 +- .../conanfile/set_name_version_test.py | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/conans/model/recipe_ref.py b/conans/model/recipe_ref.py index a294c2dea15..9d9f99f270b 100644 --- a/conans/model/recipe_ref.py +++ b/conans/model/recipe_ref.py @@ -135,9 +135,9 @@ def validate_ref(self, allow_uppercase=False): if len(self_str) > 200: raise ConanException(f"Package reference too long >200 {self_str}") if not allow_uppercase: - validation_pattern = re.compile(r"^[a-z0-9_][a-z0-9_+.-]{1,100}$") + validation_pattern = re.compile(r"^[a-z0-9_][a-z0-9_+.-]{1,100}\Z") else: - validation_pattern = re.compile(r"^[a-zA-Z0-9_][a-zA-Z0-9_+.-]{1,100}$") + validation_pattern = re.compile(r"^[a-zA-Z0-9_][a-zA-Z0-9_+.-]{1,100}\Z") if validation_pattern.match(self.name) is None: raise ConanException(f"Invalid package name '{self.name}'") if validation_pattern.match(str(self.version)) is None: diff --git a/conans/test/integration/conanfile/set_name_version_test.py b/conans/test/integration/conanfile/set_name_version_test.py index 7d09c412315..f052f5ecaae 100644 --- a/conans/test/integration/conanfile/set_name_version_test.py +++ b/conans/test/integration/conanfile/set_name_version_test.py @@ -4,6 +4,7 @@ from parameterized import parameterized +from conans.test.assets.genconanfile import GenConanfile from conans.test.utils.tools import TestClient, NO_SETTINGS_PACKAGE_ID from conans.util.files import mkdir @@ -150,3 +151,42 @@ def set_version(self): client.save({"version.txt": "2.1"}, clean_first=True) client.run("export .. ") self.assertIn("pkg/2.1: Exported", client.out) + + +def test_set_version_forbidden_chars(): + c = TestClient() + c.save({"conanfile.py": GenConanfile("pkg", "$$$")}) + c.run("export .", assert_error=True) + assert "ERROR: Invalid package version '$$$'" in c.out + + conanfile = textwrap.dedent(""" + from conan import ConanFile + class Lib(ConanFile): + name = "pkg" + def set_version(self): + self.version = "$$$" + """) + c.save({"conanfile.py": conanfile}) + c.run("export .", assert_error=True) + assert "ERROR: Invalid package version '$$$'" in c.out + + +def test_set_version_carriage_return(): + c = TestClient() + c.save({"conanfile.py": GenConanfile("pkg", r"1.0\n")}) + c.run("export .", assert_error=True) + assert "ERROR: Invalid package version '1.0" in c.out + + conanfile = textwrap.dedent(""" + import os + from conan import ConanFile + from conan.tools.files import load + class Lib(ConanFile): + name = "pkg" + def set_version(self): + self.version = load(self, "version.txt") + """) + c.save({"conanfile.py": conanfile, + "version.txt": "1.0\n"}) + c.run("export .", assert_error=True) + assert "ERROR: Invalid package version '1.0" in c.out