Repository: MarketSquare/robotframework-robocop
Pull request #1263 (2025-02-27 17:50:05)

Add tests for extended view for comments rules
Also adjusted some of the rules reported lines - it's a lot easier to see how the final report will look like with new extended format. 


Patch:
diff --git a/src/robocop/linter/rules/comments.py b/src/robocop/linter/rules/comments.py
index 876c2b0..b9db717 100644
--- a/src/robocop/linter/rules/comments.py
+++ b/src/robocop/linter/rules/comments.py
@@ -176,6 +176,7 @@ class BomEncodingRule(Rule):
     name = "bom-encoding-in-file"
     rule_id = "COM05"
     message = "BOM (Byte Order Mark) found in the file"
+    file_wide_rule = True
     severity = RuleSeverity.WARNING
     added_in_version = "1.7.0"
 
@@ -238,13 +239,13 @@ class CommentChecker(VisitorChecker):
                 marker=content[index : index + len(violation)],
                 lineno=token.lineno,
                 col=token.col_offset + 1 + index,
+                end_col=token.col_offset + 1 + index + len(violation),
             )
         if content.startswith("#") and not self.is_block_comment(content) and not content.startswith("# "):
             self.report(
                 self.missing_space_after_comment,
                 lineno=token.lineno,
                 col=token.col_offset + 1,
-                end_col=token.col_offset + len(content) + 1,
             )
 
     def is_block_comment(self, comment: str) -> bool:


Test patch:
diff --git a/tests/linter/rules/comments/bom_encoding_in_file/expected_extended.txt b/tests/linter/rules/comments/bom_encoding_in_file/expected_extended.txt
new file mode 100644
index 0000000..c573f04
--- /dev/null
+++ b/tests/linter/rules/comments/bom_encoding_in_file/expected_extended.txt
@@ -0,0 +1,1 @@
+bom_in_file.robot:1:1 COM05 BOM (Byte Order Mark) found in the file
\ No newline at end of file
diff --git a/tests/linter/rules/comments/bom_encoding_in_file/test_rule.py b/tests/linter/rules/comments/bom_encoding_in_file/test_rule.py
index e0553eb..931c17e 100644
--- a/tests/linter/rules/comments/bom_encoding_in_file/test_rule.py
+++ b/tests/linter/rules/comments/bom_encoding_in_file/test_rule.py
@@ -4,3 +4,6 @@ from tests.linter.utils import RuleAcceptance
 class TestRuleAcceptance(RuleAcceptance):
     def test_rule(self):
         self.check_rule(expected_file="expected_output.txt")
+
+    def test_extended(self):
+        self.check_rule(expected_file="expected_extended.txt", output_format="extended")
diff --git a/tests/linter/rules/comments/ignored_data/expected_extended.txt b/tests/linter/rules/comments/ignored_data/expected_extended.txt
new file mode 100644
index 0000000..614af33
--- /dev/null
+++ b/tests/linter/rules/comments/ignored_data/expected_extended.txt
@@ -0,0 +1,39 @@
+bom_and_two_ignored_lines.robot:2:1 COM04 Ignored data found in file
+   |
+ 1 | ignored but prefixed with bom
+ 2 | second line
+   | ^^^^^^^^^^^ COM04
+ 3 | *** Settings ***
+ 4 | Documentation     This file is created with VSCode by choosing `UTF-8 with BOM` encoding.
+   |
+
+language_header_and_other.robot:3:1 COM04 Ignored data found in file
+   |
+ 3 | I am comment that should be in the *** Comments *** section
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ COM04
+   |
+
+language_header_in_second_line.robot:1:1 COM04 Ignored data found in file
+   |
+ 1 |
+   | ^ COM04
+ 2 | language: pl
+   |
+
+with_ignored_data.robot:1:1 COM04 Ignored data found in file
+   |
+ 1 | ${var}    10    # This should throw a warning.
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ COM04
+ 2 | *** Settings ***
+ 3 | Documentation    Doc
+   |
+
+with_ignored_rule_and_data.robot:2:1 COM04 Ignored data found in file
+   |
+ 1 | # robocop: disable=1001
+ 2 | ${var}    10    # This should throw a warning.
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ COM04
+ 3 | ${var}    20    # This is not good but one warning is enough.
+ 4 | *** Settings ***
+   |
+
diff --git a/tests/linter/rules/comments/ignored_data/test_rule.py b/tests/linter/rules/comments/ignored_data/test_rule.py
index e0553eb..931c17e 100644
--- a/tests/linter/rules/comments/ignored_data/test_rule.py
+++ b/tests/linter/rules/comments/ignored_data/test_rule.py
@@ -4,3 +4,6 @@ from tests.linter.utils import RuleAcceptance
 class TestRuleAcceptance(RuleAcceptance):
     def test_rule(self):
         self.check_rule(expected_file="expected_output.txt")
+
+    def test_extended(self):
+        self.check_rule(expected_file="expected_extended.txt", output_format="extended")
diff --git a/tests/linter/rules/comments/missing_space_after_comment/expected_extended.txt b/tests/linter/rules/comments/missing_space_after_comment/expected_extended.txt
new file mode 100644
index 0000000..6c2b12c
--- /dev/null
+++ b/tests/linter/rules/comments/missing_space_after_comment/expected_extended.txt
@@ -0,0 +1,337 @@
+test.robot:2:21 COM02 Missing blank space after comment character
+   |
+ 1 | *** Settings ***
+ 2 | Documentation  doc  #invalid comment
+   |                     ^ COM02
+ 3 | Metadata   myvalue  #invalid comment
+ 4 | Force Tags  sometag  #invalid comment
+   |
+
+test.robot:3:21 COM02 Missing blank space after comment character
+   |
+ 1 | *** Settings ***
+ 2 | Documentation  doc  #invalid comment
+ 3 | Metadata   myvalue  #invalid comment
+   |                     ^ COM02
+ 4 | Force Tags  sometag  #invalid comment
+ 5 | Default Tags  mytag  #invalid comment
+   |
+
+test.robot:4:22 COM02 Missing blank space after comment character
+   |
+ 2 | Documentation  doc  #invalid comment
+ 3 | Metadata   myvalue  #invalid comment
+ 4 | Force Tags  sometag  #invalid comment
+   |                      ^ COM02
+ 5 | Default Tags  mytag  #invalid comment
+ 6 | Test Setup      Keyword  #invalid comment
+   |
+
+test.robot:5:22 COM02 Missing blank space after comment character
+   |
+ 3 | Metadata   myvalue  #invalid comment
+ 4 | Force Tags  sometag  #invalid comment
+ 5 | Default Tags  mytag  #invalid comment
+   |                      ^ COM02
+ 6 | Test Setup      Keyword  #invalid comment
+ 7 | Test Teardown   Keyword  #invalid comment
+   |
+
+test.robot:6:26 COM02 Missing blank space after comment character
+   |
+ 4 | Force Tags  sometag  #invalid comment
+ 5 | Default Tags  mytag  #invalid comment
+ 6 | Test Setup      Keyword  #invalid comment
+   |                          ^ COM02
+ 7 | Test Teardown   Keyword  #invalid comment
+ 8 | Suite Setup     Keyword  #invalid comment
+   |
+
+test.robot:7:26 COM02 Missing blank space after comment character
+   |
+ 5 | Default Tags  mytag  #invalid comment
+ 6 | Test Setup      Keyword  #invalid comment
+ 7 | Test Teardown   Keyword  #invalid comment
+   |                          ^ COM02
+ 8 | Suite Setup     Keyword  #invalid comment
+ 9 | Suite Teardown  Keyword  #invalid comment
+   |
+
+test.robot:8:26 COM02 Missing blank space after comment character
+   |
+ 6 | Test Setup      Keyword  #invalid comment
+ 7 | Test Teardown   Keyword  #invalid comment
+ 8 | Suite Setup     Keyword  #invalid comment
+   |                          ^ COM02
+ 9 | Suite Teardown  Keyword  #invalid comment
+10 | Test Template   Keyword  #invalid comment
+   |
+
+test.robot:9:26 COM02 Missing blank space after comment character
+   |
+ 7 | Test Teardown   Keyword  #invalid comment
+ 8 | Suite Setup     Keyword  #invalid comment
+ 9 | Suite Teardown  Keyword  #invalid comment
+   |                          ^ COM02
+10 | Test Template   Keyword  #invalid comment
+   |
+
+test.robot:10:26 COM02 Missing blank space after comment character
+    |
+  8 | Suite Setup     Keyword  #invalid comment
+  9 | Suite Teardown  Keyword  #invalid comment
+ 10 | Test Template   Keyword  #invalid comment
+    |                          ^ COM02
+    |
+
+test.robot:14:17 COM02 Missing blank space after comment character
+    |
+ 12 |
+ 13 | *** Variables ***
+ 14 | ${MY_VAR}    1  #invalid comment  with 2 spaces
+    |                 ^ COM02
+    |
+
+test.robot:18:7 COM02 Missing blank space after comment character
+    |
+ 16 |
+ 17 | *** Test Cases ***  # valid comment
+ 18 | Test  #invalid comment
+    |       ^ COM02
+ 19 |     [Documentation]  doc  # valid comment
+ 20 |     [Tags]  sometag  #invalid comment
+    |
+
+test.robot:20:22 COM02 Missing blank space after comment character
+    |
+ 18 | Test  #invalid comment
+ 19 |     [Documentation]  doc  # valid comment
+ 20 |     [Tags]  sometag  #invalid comment
+    |                      ^ COM02
+ 21 |     [Setup]  Keyword  #invalid comment
+ 22 |     [Template]  Keyword  #invalid comment
+    |
+
+test.robot:21:23 COM02 Missing blank space after comment character
+    |
+ 19 |     [Documentation]  doc  # valid comment
+ 20 |     [Tags]  sometag  #invalid comment
+ 21 |     [Setup]  Keyword  #invalid comment
+    |                       ^ COM02
+ 22 |     [Template]  Keyword  #invalid comment
+ 23 |     [Timeout]  10  #invalid comment
+    |
+
+test.robot:22:26 COM02 Missing blank space after comment character
+    |
+ 20 |     [Tags]  sometag  #invalid comment
+ 21 |     [Setup]  Keyword  #invalid comment
+ 22 |     [Template]  Keyword  #invalid comment
+    |                          ^ COM02
+ 23 |     [Timeout]  10  #invalid comment
+ 24 |     Pass
+    |
+
+test.robot:23:20 COM02 Missing blank space after comment character
+    |
+ 21 |     [Setup]  Keyword  #invalid comment
+ 22 |     [Template]  Keyword  #invalid comment
+ 23 |     [Timeout]  10  #invalid comment
+    |                    ^ COM02
+ 24 |     Pass
+ 25 |     Keyword  # valid comment
+    |
+
+test.robot:27:1 COM02 Missing blank space after comment character
+    |
+ 25 |     Keyword  # valid comment
+ 26 |     One More
+ 27 | #invalid comment
+    | ^ COM02
+    |
+
+test.robot:29:19 COM02 Missing blank space after comment character
+    |
+ 29 | *** Keywords ***  #invalid comment
+    |                   ^ COM02
+ 30 | Keyword  #invalid comment
+ 31 |     [Documentation]  this is doc  #invalid comment
+    |
+
+test.robot:30:10 COM02 Missing blank space after comment character
+    |
+ 28 |
+ 29 | *** Keywords ***  #invalid comment
+ 30 | Keyword  #invalid comment
+    |          ^ COM02
+ 31 |     [Documentation]  this is doc  #invalid comment
+ 32 |     [Arguments]  ${arg}  #invalid comment
+    |
+
+test.robot:31:35 COM02 Missing blank space after comment character
+    |
+ 29 | *** Keywords ***  #invalid comment
+ 30 | Keyword  #invalid comment
+ 31 |     [Documentation]  this is doc  #invalid comment
+    |                                   ^ COM02
+ 32 |     [Arguments]  ${arg}  #invalid comment
+ 33 |     [Timeout]  10  #invalid comment
+    |
+
+test.robot:32:26 COM02 Missing blank space after comment character
+    |
+ 30 | Keyword  #invalid comment
+ 31 |     [Documentation]  this is doc  #invalid comment
+ 32 |     [Arguments]  ${arg}  #invalid comment
+    |                          ^ COM02
+ 33 |     [Timeout]  10  #invalid comment
+ 34 |     No Operation  #invalid comment
+    |
+
+test.robot:33:20 COM02 Missing blank space after comment character
+    |
+ 31 |     [Documentation]  this is doc  #invalid comment
+ 32 |     [Arguments]  ${arg}  #invalid comment
+ 33 |     [Timeout]  10  #invalid comment
+    |                    ^ COM02
+ 34 |     No Operation  #invalid comment
+ 35 |     Pass
+    |
+
+test.robot:34:19 COM02 Missing blank space after comment character
+    |
+ 32 |     [Arguments]  ${arg}  #invalid comment
+ 33 |     [Timeout]  10  #invalid comment
+ 34 |     No Operation  #invalid comment
+    |                   ^ COM02
+ 35 |     Pass
+ 36 |     No Operation
+    |
+
+test.robot:38:17 COM02 Missing blank space after comment character
+    |
+ 36 |     No Operation
+ 37 |     Fail
+ 38 |     IF  ${var}  #invalid comment
+    |                 ^ COM02
+ 39 |         My Keyword
+ 40 |     ELSE IF  ${another_var}  #invalid comment
+    |
+
+test.robot:40:30 COM02 Missing blank space after comment character
+    |
+ 38 |     IF  ${var}  #invalid comment
+ 39 |         My Keyword
+ 40 |     ELSE IF  ${another_var}  #invalid comment
+    |                              ^ COM02
+ 41 |         Not My Keyword
+ 42 |     ELSE  #invalid comment
+    |
+
+test.robot:42:11 COM02 Missing blank space after comment character
+    |
+ 40 |     ELSE IF  ${another_var}  #invalid comment
+ 41 |         Not My Keyword
+ 42 |     ELSE  #invalid comment
+    |           ^ COM02
+ 43 |         Banned Keyword
+ 44 |     END
+    |
+
+test.robot:45:31 COM02 Missing blank space after comment character
+    |
+ 43 |         Banned Keyword
+ 44 |     END
+ 45 |     FOR  ${var}  IN  @{list}  #invalid comment
+    |                               ^ COM02
+ 46 |         My Keyword
+ 47 |     END
+    |
+
+test.robot:48:23 COM02 Missing blank space after comment character
+    |
+ 46 |         My Keyword
+ 47 |     END
+ 48 |     [Return]  ${val}  #invalid comment
+    |                       ^ COM02
+    |
+
+test.robot:51:26 COM02 Missing blank space after comment character
+    |
+ 49 |
+ 50 | RF 5 syntax
+ 51 |     WHILE    $condition  #invalid comment
+    |                          ^ COM02
+ 52 |         TRY  #invalid comment
+ 53 |             Keyword
+    |
+
+test.robot:52:14 COM02 Missing blank space after comment character
+    |
+ 50 | RF 5 syntax
+ 51 |     WHILE    $condition  #invalid comment
+ 52 |         TRY  #invalid comment
+    |              ^ COM02
+ 53 |             Keyword
+ 54 |         EXCEPT  #invalid comment
+    |
+
+test.robot:54:17 COM02 Missing blank space after comment character
+    |
+ 52 |         TRY  #invalid comment
+ 53 |             Keyword
+ 54 |         EXCEPT  #invalid comment
+    |                 ^ COM02
+ 55 |             Keyword
+ 56 |         FINALLY  #invalid comment
+    |
+
+test.robot:56:18 COM02 Missing blank space after comment character
+    |
+ 54 |         EXCEPT  #invalid comment
+ 55 |             Keyword
+ 56 |         FINALLY  #invalid comment
+    |                  ^ COM02
+ 57 |             Keyword
+ 58 |         ELSE  #invalid comment
+    |
+
+test.robot:58:15 COM02 Missing blank space after comment character
+    |
+ 56 |         FINALLY  #invalid comment
+ 57 |             Keyword
+ 58 |         ELSE  #invalid comment
+    |               ^ COM02
+ 59 |             Keyword
+ 60 |         END  #invalid comment
+    |
+
+test.robot:60:14 COM02 Missing blank space after comment character
+    |
+ 58 |         ELSE  #invalid comment
+ 59 |             Keyword
+ 60 |         END  #invalid comment
+    |              ^ COM02
+ 61 |         CONTINUE  #invalid comment
+ 62 |         BREAK  #invalid comment
+    |
+
+test.robot:61:19 COM02 Missing blank space after comment character
+    |
+ 59 |             Keyword
+ 60 |         END  #invalid comment
+ 61 |         CONTINUE  #invalid comment
+    |                   ^ COM02
+ 62 |         BREAK  #invalid comment
+ 63 |     END
+    |
+
+test.robot:62:16 COM02 Missing blank space after comment character
+    |
+ 60 |         END  #invalid comment
+ 61 |         CONTINUE  #invalid comment
+ 62 |         BREAK  #invalid comment
+    |                ^ COM02
+ 63 |     END
+    |
+
diff --git a/tests/linter/rules/comments/missing_space_after_comment/test_rule.py b/tests/linter/rules/comments/missing_space_after_comment/test_rule.py
index 3fd6441..1451082 100644
--- a/tests/linter/rules/comments/missing_space_after_comment/test_rule.py
+++ b/tests/linter/rules/comments/missing_space_after_comment/test_rule.py
@@ -5,6 +5,9 @@ class TestRuleAcceptance(RuleAcceptance):
     def test_rule(self):
         self.check_rule(src_files=["test.robot"], expected_file="expected_output.txt")
 
+    def test_extended(self):
+        self.check_rule(src_files=["test.robot"], expected_file="expected_extended.txt", output_format="extended")
+
     def test_configured_block(self):
         self.check_rule(
             configure=["missing-space-after-comment.block=^#[*]+"],
diff --git a/tests/linter/rules/comments/todo_in_comment/expected_extended.txt b/tests/linter/rules/comments/todo_in_comment/expected_extended.txt
new file mode 100644
index 0000000..5a6ec38
--- /dev/null
+++ b/tests/linter/rules/comments/todo_in_comment/expected_extended.txt
@@ -0,0 +1,45 @@
+test.robot:2:23 COM01 Found a marker 'todo' in the comments
+   |
+ 1 | *** Settings ***
+ 2 | Documentation  doc  # todo so many things
+   |                       ^^^^ COM01
+   |
+
+test.robot:13:3 COM01 Found a marker 'fixme' in the comments
+    |
+ 13 | # fixme: this whole section
+    |   ^^^^^ COM01
+ 14 | *** Keywords ***  # TODO do this
+ 15 | Keyword  # FIXME now!
+    |
+
+test.robot:14:21 COM01 Found a marker 'TODO' in the comments
+    |
+ 12 |
+ 13 | # fixme: this whole section
+ 14 | *** Keywords ***  # TODO do this
+    |                     ^^^^ COM01
+ 15 | Keyword  # FIXME now!
+ 16 |     [Documentation]  this is doc
+    |
+
+test.robot:15:12 COM01 Found a marker 'FIXME' in the comments
+    |
+ 13 | # fixme: this whole section
+ 14 | *** Keywords ***  # TODO do this
+ 15 | Keyword  # FIXME now!
+    |            ^^^^^ COM01
+ 16 |     [Documentation]  this is doc
+ 17 |     No Operation  # todo: do something
+    |
+
+test.robot:17:21 COM01 Found a marker 'todo' in the comments
+    |
+ 15 | Keyword  # FIXME now!
+ 16 |     [Documentation]  this is doc
+ 17 |     No Operation  # todo: do something
+    |                     ^^^^ COM01
+ 18 |     Pass
+ 19 |     No Operation
+    |
+
diff --git a/tests/linter/rules/comments/todo_in_comment/test_rule.py b/tests/linter/rules/comments/todo_in_comment/test_rule.py
index 3a60950..7a79a84 100644
--- a/tests/linter/rules/comments/todo_in_comment/test_rule.py
+++ b/tests/linter/rules/comments/todo_in_comment/test_rule.py
@@ -5,6 +5,9 @@ class TestRuleAcceptance(RuleAcceptance):
     def test_rule(self):
         self.check_rule(src_files=["test.robot"], expected_file="expected_output.txt")
 
+    def test_extended(self):
+        self.check_rule(src_files=["test.robot"], expected_file="expected_extended.txt", output_format="extended")
+
     def test_added_terms(self):
         self.check_rule(
             configure=["todo-in-comment.markers=todo,fixme,OMG"],
diff --git a/tests/linter/utils/__init__.py b/tests/linter/utils/__init__.py
index ac4e396..00e9bcd 100644
--- a/tests/linter/utils/__init__.py
+++ b/tests/linter/utils/__init__.py
@@ -156,6 +156,9 @@ class RuleAcceptance:
                     present_in_expected = "\n    ".join(missing_actual)
                     error += f"Expected issues not found in actual:\n    {present_in_expected}"
             else:
+                # false alarm (rare occurence if OS have random file order), we don't sort at first to save time
+                if sorted(actual) == sorted(expected):
+                    return
                 display_lines_diff(expected, actual)
             pytest.fail(error, pytrace=False)