Skip to content

Commit

Permalink
Better messages for partial indentation in L003 (#3634)
Browse files Browse the repository at this point in the history
* Better messages for partial indentation in L003

* Calculate has_partial_indent in L003 once

Co-authored-by: Barry Hart <barrywhart@yahoo.com>
  • Loading branch information
pdebelak and barrywhart committed Jul 20, 2022
1 parent e4d2373 commit 2c59a93
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
28 changes: 20 additions & 8 deletions src/sqlfluff/rules/L003.py
Expand Up @@ -5,13 +5,13 @@

from sqlfluff.core.parser import WhitespaceSegment
from sqlfluff.core.parser.segments import BaseSegment
from sqlfluff.core.rules.functional import rsp, sp, Segments
from sqlfluff.core.rules.base import BaseRule, LintResult, LintFix, RuleContext
from sqlfluff.core.rules.base import BaseRule, LintFix, LintResult, RuleContext
from sqlfluff.core.rules.doc_decorators import (
document_configuration,
document_fix_compatible,
document_groups,
)
from sqlfluff.core.rules.functional import Segments, rsp, sp
from sqlfluff.core.templaters import TemplatedFile
from sqlfluff.core.templaters.base import RawFileSlice

Expand Down Expand Up @@ -513,7 +513,10 @@ def _process_working_state(
return LintResult(memory=memory)
prev_line_no = prev_line.line_no
indent_diff = this_line.anchor_indent_balance - prev_line.anchor_indent_balance
this_indent_num = this_line.indent_size // self.tab_space_size
this_indent_num, this_indent_rem = divmod(
this_line.indent_size, self.tab_space_size
)
has_partial_indent = bool(this_indent_rem)
comp_indent_num = prev_line.indent_size // self.tab_space_size
# Is the indent balance the same?
if indent_diff == 0:
Expand Down Expand Up @@ -548,6 +551,7 @@ def _process_working_state(
description=_Desc(
expected=comp_indent_num,
found=this_indent_num,
has_partial_indent=has_partial_indent,
compared_to=prev_line.line_no,
),
fixes=fixes,
Expand Down Expand Up @@ -596,6 +600,7 @@ def _process_working_state(
description=_Desc(
expected=len(desired_indent) // self.tab_space_size,
found=this_indent_num,
has_partial_indent=has_partial_indent,
compared_to=prev_line.line_no,
),
fixes=fixes,
Expand Down Expand Up @@ -634,6 +639,7 @@ def _process_working_state(
description=_Desc(
expected=this_indent_num + 1,
found=this_indent_num,
has_partial_indent=has_partial_indent,
compared_to=prev_line.line_no,
),
# Add in an extra bit of whitespace for the indent
Expand Down Expand Up @@ -672,6 +678,7 @@ def _process_working_state(
description=_Desc(
expected=comp_indent_num,
found=this_indent_num,
has_partial_indent=has_partial_indent,
compared_to=prev_line.line_no,
),
fixes=fixes,
Expand Down Expand Up @@ -1022,11 +1029,16 @@ def _find_matching_start_line(


def _Desc(
expected: int,
found: int,
compared_to: int,
expected: int, found: int, compared_to: int, has_partial_indent: bool = False
) -> str:
indentations = "indentation" if expected == 1 else "indentations"
if found >= expected and has_partial_indent:
found_explanation = f"more than {found}"
elif found < expected and has_partial_indent:
found_explanation = f"less than {found + 1}"
else:
found_explanation = str(found)
return (
f"Expected {expected} indentations,"
f" found {found} [compared to line {compared_to:02}]"
f"Expected {expected} {indentations},"
f" found {found_explanation} [compared to line {compared_to:02}]"
)
15 changes: 8 additions & 7 deletions test/cli/commands_test.py
Expand Up @@ -7,24 +7,24 @@
import re
import shutil
import stat
import subprocess
import sys
import tempfile
import textwrap
from unittest.mock import MagicMock, patch

import yaml
import subprocess
import chardet
import sys

# Testing libraries
import pytest
import yaml
from click.testing import CliRunner

# We import the library directly here to get the version
import sqlfluff
from sqlfluff.cli.commands import lint, version, rules, fix, parse, dialects, get_config
from sqlfluff.core.rules.base import BaseRule, LintFix, LintResult
from sqlfluff.cli.commands import dialects, fix, get_config, lint, parse, rules, version
from sqlfluff.core.parser.segments.raw import CommentSegment
from sqlfluff.core.rules.base import BaseRule, LintFix, LintResult

re_ansi_escape = re.compile(r"\x1b[^m]*m")

Expand Down Expand Up @@ -62,7 +62,8 @@ def invoke_assert_code(


expected_output = """== [test/fixtures/linter/indentation_error_simple.sql] FAIL
L: 2 | P: 4 | L003 | Expected 1 indentations, found 0 [compared to line 01]
L: 2 | P: 4 | L003 | Expected 1 indentation, found less than 1 [compared to
| line 01]
L: 5 | P: 10 | L010 | Keywords must be consistently upper case.
L: 5 | P: 13 | L031 | Avoid aliases in from clauses and join conditions.
"""
Expand Down Expand Up @@ -1190,7 +1191,7 @@ def test__cli__command_lint_serialize_multiple_files(serialize, write_file, tmp_
result_payload = result.output

if serialize == "human":
assert len(result_payload.split("\n")) == 29 if write_file else 30
assert len(result_payload.split("\n")) == 33 if write_file else 32
elif serialize == "json":
result = json.loads(result_payload)
assert len(result) == 2
Expand Down
37 changes: 35 additions & 2 deletions test/rules/std_L003_test.py
@@ -1,7 +1,7 @@
"""Tests the python routines within L003."""
import pytest

from sqlfluff.rules.L003 import Rule_L003
from sqlfluff.rules.L003 import Rule_L003, _Desc


@pytest.mark.parametrize(
Expand Down Expand Up @@ -45,6 +45,39 @@ def is_type(self, *seg_type):
],
)
def test__rules__std_L003_indent_size(tab_space_size, segments, result):
"""Test Rule_L003._make_indent."""
"""Test Rule_L003._indent_size."""
res = Rule_L003._indent_size(segments=segments, tab_space_size=tab_space_size)
assert res == result


@pytest.mark.parametrize(
"expected,found,compared_to,has_partial_indent,expected_message",
[
(
1,
1,
4,
True,
"Expected 1 indentation, found more than 1 [compared to line 04]",
),
(
2,
1,
10,
False,
"Expected 2 indentations, found 1 [compared to line 10]",
),
(
2,
1,
11,
True,
"Expected 2 indentations, found less than 2 [compared to line 11]",
),
],
)
def test__rules__std_L003_desc(
expected, found, compared_to, has_partial_indent, expected_message
):
"""Test Rule_L003 error description."""
assert _Desc(expected, found, compared_to, has_partial_indent) == expected_message

0 comments on commit 2c59a93

Please sign in to comment.