From 787b58e58d68ad0ca92c0f7f789b18a4cd0e6a65 Mon Sep 17 00:00:00 2001 From: Luigi Cerone Date: Thu, 21 Nov 2024 16:55:51 +0100 Subject: [PATCH] Rule names in warnings logic (#6459) Co-authored-by: Alan Cruickshank --- .../configuration/rule_configuration.rst | 2 ++ src/sqlfluff/core/errors.py | 11 +++++++- test/cli/commands_test.py | 27 +++++++++++++++++++ test/fixtures/cli/warning_name_a.sql | 4 +++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/cli/warning_name_a.sql diff --git a/docs/source/configuration/rule_configuration.rst b/docs/source/configuration/rule_configuration.rst index dd49662cf..ffafb56b2 100644 --- a/docs/source/configuration/rule_configuration.rst +++ b/docs/source/configuration/rule_configuration.rst @@ -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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/sqlfluff/core/errors.py b/src/sqlfluff/core/errors.py index db964c5bb..9a886cfc6 100644 --- a/src/sqlfluff/core/errors.py +++ b/src/sqlfluff/core/errors.py @@ -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. diff --git a/test/cli/commands_test.py b/test/cli/commands_test.py index f9751c96e..97567b293 100644 --- a/test/cli/commands_test.py +++ b/test/cli/commands_test.py @@ -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 diff --git a/test/fixtures/cli/warning_name_a.sql b/test/fixtures/cli/warning_name_a.sql new file mode 100644 index 000000000..5382c8504 --- /dev/null +++ b/test/fixtures/cli/warning_name_a.sql @@ -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