Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make core:warnings_as_errors accept a list #15297

Merged
merged 5 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions conan/api/output.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fnmatch
import sys

from colorama import Fore, Style
Expand Down Expand Up @@ -51,7 +52,7 @@ class ConanOutput:
# Singleton
_conan_output_level = LEVEL_STATUS
_silent_warn_tags = []
_warnings_as_errors = False
_warnings_as_errors = []

def __init__(self, scope=""):
self.stream = sys.stderr
Expand All @@ -62,7 +63,7 @@ def __init__(self, scope=""):

@classmethod
def define_silence_warnings(cls, warnings):
cls._silent_warn_tags = warnings or []
cls._silent_warn_tags = warnings

@classmethod
def set_warnings_as_errors(cls, value):
Expand Down Expand Up @@ -208,17 +209,20 @@ def success(self, msg):
self._write_message(msg, fg=Color.BRIGHT_GREEN)
return self

@staticmethod
def _warn_tag_matches(warn_tag, patterns):
lookup_tag = warn_tag or "unknown"
return any(fnmatch.fnmatch(lookup_tag, pattern) for pattern in patterns)

def warning(self, msg, warn_tag=None):
def _skip_warning():
return (warn_tag is not None and warn_tag in self._silent_warn_tags
or warn_tag is None and "unknown" in self._silent_warn_tags)
if self._conan_output_level <= LEVEL_WARNING or (self._warnings_as_errors and self._conan_output_level <= LEVEL_ERROR):
if _skip_warning():
_treat_as_error = self._warn_tag_matches(warn_tag, self._warnings_as_errors)
if self._conan_output_level <= LEVEL_WARNING or (_treat_as_error and self._conan_output_level <= LEVEL_ERROR):
if self._warn_tag_matches(warn_tag, self._silent_warn_tags):
return self
warn_tag_msg = "" if warn_tag is None else f"{warn_tag}: "
output = f"{warn_tag_msg}{msg}"

if self._warnings_as_errors:
if _treat_as_error:
self.error(output)
else:
self._write_message(f"WARN: {output}", Color.YELLOW)
Expand Down
5 changes: 3 additions & 2 deletions conan/internal/conan_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ def __init__(self, cache_folder, global_conf):
@staticmethod
def _configure(global_conf):
ConanOutput.set_warnings_as_errors(global_conf.get("core:warnings_as_errors",
default=False, check_type=bool))
ConanOutput.define_silence_warnings(global_conf.get("core:skip_warnings", check_type=list))
default=[], check_type=list))
ConanOutput.define_silence_warnings(global_conf.get("core:skip_warnings",
default=[], check_type=list))
4 changes: 2 additions & 2 deletions conans/model/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
BUILT_IN_CONFS = {
"core:required_conan_version": "Raise if current version does not match the defined range.",
"core:non_interactive": "Disable interactive user input, raises error if input necessary",
"core:warnings_as_errors": "Treat warnings as errors",
"core:skip_warnings": "Do not show warnings in this list",
"core:warnings_as_errors": "Treat warnings matching any of the patterns in this list as errors",
"core:skip_warnings": "Do not show warnings matching any of the patterns in this list",
"core:default_profile": "Defines the default host profile ('default' by default)",
"core:default_build_profile": "Defines the default build profile ('default' by default)",
"core:allow_uppercase_pkg_names": "Temporarily (will be removed in 2.X) allow uppercase names",
Expand Down
16 changes: 8 additions & 8 deletions conans/test/integration/command_v2/test_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,31 +186,31 @@ def test_output_level_envvar():

class TestWarningHandling:
warning_lines = ("self.output.warning('Tagged warning', warn_tag='tag')",
"self.output.warning('Untagged warning')")
"self.output.warning('Untagged warning')")
error_lines = ("self.output.error('Tagged error', error_type='exception')",
"self.output.error('Untagged error')")

def test_warning_as_error(self):
def test_warning_as_error_deprecated_syntax(self):
t = TestClient(light=True)
t.save({"conanfile.py": GenConanfile("foo", "1.0").with_package(*self.warning_lines)})

t.save_home({"global.conf": "core:warnings_as_errors=False"})
t.save_home({"global.conf": "core:warnings_as_errors=[]"})
t.run("create . -vwarning")
assert "WARN: Untagged warning" in t.out
assert "WARN: tag: Tagged warning" in t.out

t.save_home({"global.conf": "core:warnings_as_errors=True"})
t.save_home({"global.conf": "core:warnings_as_errors=['*']"})
t.run("create . -vwarning", assert_error=True)
assert "ConanException: tag: Tagged warning" in t.out
# We bailed early, didn't get a chance to print this one
assert "Untagged warning" not in t.out

t.save_home({"global.conf": """core:warnings_as_errors=True\ncore:skip_warnings=["tag"]"""})
t.save_home({"global.conf": """core:warnings_as_errors=['*']\ncore:skip_warnings=["tag"]"""})
t.run("create . -verror", assert_error=True)
assert "ConanException: Untagged warning" in t.out
assert "Tagged warning" not in t.out

t.save_home({"global.conf": "core:warnings_as_errors=False"})
t.save_home({"global.conf": "core:warnings_as_errors=[]"})
t.run("create . -verror")
assert "ERROR: Untagged warning" not in t.out
assert "ERROR: tag: Tagged warning" not in t.out
Expand Down Expand Up @@ -243,12 +243,12 @@ def test_exception_errors(self):
t = TestClient(light=True)
t.save({"conanfile.py": GenConanfile("foo", "1.0").with_package(*self.error_lines)})

t.save_home({"global.conf": "core:warnings_as_errors=False"})
t.save_home({"global.conf": "core:warnings_as_errors=[]"})
t.run("create .")
assert "ERROR: Tagged error" in t.out
assert "ERROR: Untagged error" in t.out

t.save_home({"global.conf": "core:warnings_as_errors=True"})
t.save_home({"global.conf": "core:warnings_as_errors=['*']"})
t.run("create .", assert_error=True)
assert "ERROR: Tagged error" in t.out
assert "ConanException: Untagged error" in t.out