Rule names in warnings logic (#6459)

Co-authored-by: Alan Cruickshank <alanmcruickshank@gmail.com>
This commit is contained in:
Luigi Cerone
2024-11-21 16:55:51 +01:00
committed by GitHub
parent 65ef3d0ec3
commit 787b58e58d
4 changed files with 43 additions and 1 deletions

View File

@@ -162,6 +162,8 @@ This is particularly useful as a transitional tool when considering
the introduction on new rules on a project where you might want to
make users aware of issues without blocking their workflow (yet).
You can use either rule code or rule name for this setting.
Layout & Spacing Configuration
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@@ -37,6 +37,7 @@ class SQLBaseError(ValueError):
"""Base Error Class for all violations."""
_code: Optional[str] = None
_name: Optional[str] = None
_identifier = "base"
_warning = False # The default value for `warning`
@@ -90,6 +91,10 @@ class SQLBaseError(ValueError):
"""Fetch the code of the rule which cause this error."""
return self._code or "????"
def rule_name(self) -> str:
"""Fetch the name of the rule which cause this error."""
return self._name or "????"
def desc(self) -> str:
"""Fetch a description of this violation."""
if self.description:
@@ -141,7 +146,7 @@ class SQLBaseError(ValueError):
Returns:
None
"""
if self.rule_code() in warning_iterable:
if self.rule_code() in warning_iterable or self.rule_name() in warning_iterable:
self.warning = True
@@ -337,6 +342,10 @@ class SQLLintError(SQLBaseError):
"""Fetch the code of the rule which cause this error."""
return self.rule.code
def rule_name(self) -> str:
"""Fetch the name of the rule which cause this error."""
return self.rule.name
def source_signature(self) -> Tuple[Any, ...]:
"""Return hashable source signature for deduplication.

View File

@@ -830,6 +830,33 @@ def test__cli__command_lint_warning():
)
def test__cli__command_lint_warning_name_rule():
"""Test that configuring warnings works.
For this test the warnings are configured using
inline config in the file. That's more for simplicity
however the code paths should be the same if it's
configured in a file.
"""
runner = CliRunner()
result = runner.invoke(
lint,
[
"test/fixtures/cli/warning_name_a.sql",
],
)
# Because we're only warning. The command should pass.
assert result.exit_code == 0
# The output should still say PASS.
assert "PASS" in result.output.strip()
# But should also contain the warnings.
# NOTE: Not including the whole description because it's too long.
assert (
"L: 4 | P: 9 | LT01 | WARNING: Expected single whitespace"
in result.output.strip()
)
def test__cli__command_versioning():
"""Check version command."""
# Get the package version info

View File

@@ -0,0 +1,4 @@
-- This file should fail _only_ for spacing around +
-- We explicit configure that rule to only warn.
-- sqlfluff:warnings:layout.spacing
SELECT 1+2