Skip to content

Commit

Permalink
warn if special chars in exported refs (#12053)
Browse files Browse the repository at this point in the history
  • Loading branch information
memsharded authored Sep 7, 2022
1 parent 4bff533 commit 8f8a53e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
13 changes: 12 additions & 1 deletion conans/model/recipe_ref.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
from functools import total_ordering

from conan.api.output import ConanOutput
from conans.errors import ConanException
from conans.model.version import Version
from conans.util.dates import timestamp_to_str
Expand Down Expand Up @@ -124,7 +125,7 @@ def validate_ref(self):
if self_str != self_str.lower():
raise ConanException(f"Conan packages names '{self_str}' must be all lowercase")
if len(self_str) > 200:
raise ConanException(f"Package reference too long >250 {self_str}")
raise ConanException(f"Package reference too long >200 {self_str}")
validation_pattern = re.compile(r"^[a-z0-9_][a-z0-9_+.-]{1,100}$")
if validation_pattern.match(self.name) is None:
raise ConanException(f"Invalid package name '{self.name}'")
Expand All @@ -135,6 +136,16 @@ def validate_ref(self):
if self.channel and validation_pattern.match(self.channel) is None:
raise ConanException(f"Invalid package channel '{self.channel}'")

# Warn if they use .+- in the name/user/channel, as it can be problematic for generators
pattern = re.compile(r'[.+-]')
if pattern.search(self.name):
ConanOutput().warning(f"Name containing special chars is discouraged '{self.name}'")
if self.user and pattern.search(self.user):
ConanOutput().warning(f"User containing special chars is discouraged '{self.user}'")
if self.channel and pattern.search(self.channel):
ConanOutput().warning(f"Channel containing special chars is discouraged "
f"'{self.channel}'")

def matches(self, pattern, is_consumer):
negate = False
if pattern.startswith("!"):
Expand Down
11 changes: 11 additions & 0 deletions conans/test/integration/command/export/export_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,17 @@ def test_export_invalid_refs():
assert "ERROR: Invalid package channel 'channel%'" in c.out


def test_warn_special_chars_refs():
c = TestClient()
c.save({"conanfile.py": GenConanfile()})
c.run("export . --name=pkg.name --version=0.1")
assert "WARN: Name containing special chars is discouraged 'pkg.name'" in c.out
c.run("export . --name=name --version=0.1 --user=user+some")
assert "WARN: User containing special chars is discouraged 'user+some'" in c.out
c.run("export . --name=pkg.name --version=0.1 --user=user --channel=channel-some")
assert "WARN: Channel containing special chars is discouraged 'channel-some'" in c.out


def test_export_json():
c = TestClient()
c.save({"conanfile.py": GenConanfile()})
Expand Down

0 comments on commit 8f8a53e

Please sign in to comment.