-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rules checking that builtin and custom libraries are imported in …
…alphabetical order. (#1088) * Add rules checking that builtin and custom libraries are imported in alphabetical order. * Move rule checking order of non builtin libraries imports to community rules. * Add community rule checking order of Resource imports * Reformat Rules definitions to be consistence with existing
- Loading branch information
1 parent
afb711a
commit 6d7ee3b
Showing
22 changed files
with
288 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
from robot.api import Token | ||
from robot.libraries import STDLIBS | ||
|
||
from robocop.checkers import VisitorChecker | ||
from robocop.rules import Rule, RuleSeverity | ||
|
||
RULE_CATEGORY_ID = "01" | ||
|
||
|
||
rules = { | ||
"10101": Rule( | ||
rule_id="10101", | ||
name="non-builtin-imports-not-sorted", | ||
msg="Non builtin library import '{{ custom_import }}' should be placed before '{{ previous_custom_import }}'", | ||
severity=RuleSeverity.WARNING, | ||
added_in_version="5.2.0", | ||
enabled=False, | ||
docs=""" | ||
Example of rule violation: | ||
*** Settings *** | ||
Library Collections | ||
Library CustomLibrary | ||
Library AnotherCustomLibrary # AnotherCustomLibrary library defined after custom CustomLibrary | ||
""", | ||
), | ||
"10102": Rule( | ||
rule_id="10102", | ||
name="resources-imports-not-sorted", | ||
msg="Resource import '{{ resource_import }}' should be placed before '{{ previous_resource_import }}'", | ||
severity=RuleSeverity.WARNING, | ||
added_in_version="5.2.0", | ||
enabled=False, | ||
docs=""" | ||
Example of rule violation: | ||
*** Settings *** | ||
Resource CustomResource.resource | ||
Resource AnotherFile.resource | ||
""", | ||
), | ||
} | ||
|
||
|
||
class NonBuiltinLibrariesImportOrderChecker(VisitorChecker): | ||
""" | ||
Find and report Non Builtin Libraries or Resources imported not in alphabetical order. | ||
""" | ||
|
||
reports = ( | ||
"non-builtin-imports-not-sorted", | ||
"resources-imports-not-sorted", | ||
) | ||
|
||
def __init__(self): | ||
self.non_builtin_libraries = [] | ||
self.resources = [] | ||
super().__init__() | ||
|
||
def visit_File(self, node): # noqa | ||
self.non_builtin_libraries = [] | ||
self.resources = [] | ||
self.generic_visit(node) | ||
previous = None | ||
for library in self.non_builtin_libraries: | ||
if previous is not None and library.name < previous.name: | ||
lib_name = library.get_token(Token.NAME) | ||
self.report( | ||
"non-builtin-imports-not-sorted", | ||
custom_import=library.name, | ||
previous_custom_import=previous.name, | ||
node=library, | ||
col=lib_name.col_offset + 1, | ||
end_col=lib_name.end_col_offset + 1, | ||
) | ||
previous = library | ||
previous = None | ||
for resource in self.resources: | ||
if previous is not None and resource.name < previous.name: | ||
resource_name = resource.get_token(Token.NAME) | ||
self.report( | ||
"resources-imports-not-sorted", | ||
resource_import=resource.name, | ||
previous_resource_import=previous.name, | ||
node=resource, | ||
col=resource_name.col_offset + 1, | ||
end_col=resource_name.end_col_offset + 1, | ||
) | ||
previous = resource | ||
|
||
def visit_LibraryImport(self, node): # noqa | ||
if node.name and node.name not in STDLIBS: | ||
self.non_builtin_libraries.append(node) | ||
|
||
def visit_ResourceImport(self, node): # noqa | ||
if not node.name: | ||
return | ||
self.resources.append(node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
2 changes: 2 additions & 0 deletions
2
tests/atest/rules/community_rules/misc/non_builtin_imports_not_sorted/expected_output.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
no_builtins.robot:4:12 [W] 10101 Non builtin library import 'OwnLib.py' should be placed before 'RequestsLibrary' | ||
test.robot:9:12 [W] 10101 Non builtin library import 'AnotherLibrary' should be placed before 'RequestsLibrary' |
11 changes: 11 additions & 0 deletions
11
tests/atest/rules/community_rules/misc/non_builtin_imports_not_sorted/no_builtins.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
*** Settings *** | ||
Library RequestsLibrary | ||
Resource ..${/}Libs${/}MyResource.robot | ||
Library OwnLib.py | ||
Variables variables.py | ||
Test Template Keyword | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
9 changes: 9 additions & 0 deletions
9
tests/atest/rules/community_rules/misc/non_builtin_imports_not_sorted/no_imports.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*** Settings *** | ||
Resource ..${/}Libs${/}MyResource.robot | ||
Variables variables.py | ||
Test Template Keyword | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
12 changes: 12 additions & 0 deletions
12
tests/atest/rules/community_rules/misc/non_builtin_imports_not_sorted/only_builtins.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
*** Settings *** | ||
Library Collections | ||
Library String | ||
Resource ..${/}Libs${/}MyResource.robot | ||
Variables variables.py | ||
Test Template Keyword | ||
Library BuiltIn | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
18 changes: 18 additions & 0 deletions
18
tests/atest/rules/community_rules/misc/non_builtin_imports_not_sorted/test.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
*** Settings *** | ||
Library DateTime | ||
Library Collections | ||
Library OperatingSystem | ||
Library XML | ||
Library String | ||
Library OwnLib.py | ||
Library RequestsLibrary | ||
Library AnotherLibrary | ||
Resource ..${/}Libs${/}MyResource.robot | ||
|
||
Variables variables.py | ||
Test Template Keyword | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
6 changes: 6 additions & 0 deletions
6
tests/atest/rules/community_rules/misc/non_builtin_imports_not_sorted/test_rule.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from tests.atest.utils import RuleAcceptance | ||
|
||
|
||
class TestRuleAcceptance(RuleAcceptance): | ||
def test_rule(self): | ||
self.check_rule(expected_file="expected_output.txt") |
Empty file.
2 changes: 2 additions & 0 deletions
2
tests/atest/rules/community_rules/misc/resources_imports_not_sorted/expected_output.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test.robot:5:12 [W] 10102 Resource import 'AnotherFile.resource' should be placed before 'CustomResource.resource' | ||
test.robot:6:12 [W] 10102 Resource import '..\${/}Libs\${/}MyResource.robot' should be placed before 'AnotherFile.resource' |
9 changes: 9 additions & 0 deletions
9
tests/atest/rules/community_rules/misc/resources_imports_not_sorted/no_resources.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*** Settings *** | ||
Library RequestsLibrary | ||
Variables variables.py | ||
Test Template Keyword | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
14 changes: 14 additions & 0 deletions
14
tests/atest/rules/community_rules/misc/resources_imports_not_sorted/test.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
*** Settings *** | ||
Library DateTime | ||
Library RequestsLibrary | ||
Resource CustomResource.resource | ||
Resource AnotherFile.resource | ||
Resource ..${/}Libs${/}MyResource.robot | ||
|
||
Variables variables.py | ||
Test Template Keyword | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
6 changes: 6 additions & 0 deletions
6
tests/atest/rules/community_rules/misc/resources_imports_not_sorted/test_rule.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from tests.atest.utils import RuleAcceptance | ||
|
||
|
||
class TestRuleAcceptance(RuleAcceptance): | ||
def test_rule(self): | ||
self.check_rule(expected_file="expected_output.txt") |
Empty file.
4 changes: 4 additions & 0 deletions
4
tests/atest/rules/misc/builtin_imports_not_sorted/expected_output.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
only_builtins.robot:4:12 [W] 0926 BuiltIn library import 'String' should be placed before 'XML' | ||
only_builtins.robot:8:12 [W] 0926 BuiltIn library import 'BuiltIn' should be placed before 'String' | ||
test.robot:3:12 [W] 0926 BuiltIn library import 'Collections' should be placed before 'DateTime' | ||
test.robot:6:12 [W] 0926 BuiltIn library import 'String' should be placed before 'XML' |
11 changes: 11 additions & 0 deletions
11
tests/atest/rules/misc/builtin_imports_not_sorted/no_builtins.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
*** Settings *** | ||
Library RequestsLibrary | ||
Resource ..${/}Libs${/}MyResource.robot | ||
Library OwnLib.py | ||
Variables variables.py | ||
Test Template Keyword | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
9 changes: 9 additions & 0 deletions
9
tests/atest/rules/misc/builtin_imports_not_sorted/no_imports.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*** Settings *** | ||
Resource ..${/}Libs${/}MyResource.robot | ||
Variables variables.py | ||
Test Template Keyword | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
13 changes: 13 additions & 0 deletions
13
tests/atest/rules/misc/builtin_imports_not_sorted/only_builtins.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
*** Settings *** | ||
Library Collections | ||
Library XML | ||
Library String | ||
Resource ..${/}Libs${/}MyResource.robot | ||
Variables variables.py | ||
Test Template Keyword | ||
Library BuiltIn | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
16 changes: 16 additions & 0 deletions
16
tests/atest/rules/misc/builtin_imports_not_sorted/test.robot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
*** Settings *** | ||
Library DateTime | ||
Library Collections | ||
Library OperatingSystem | ||
Library XML | ||
Library String | ||
Library RequestsLibrary | ||
Resource ..${/}Libs${/}MyResource.robot | ||
Library OwnLib.py | ||
Variables variables.py | ||
Test Template Keyword | ||
|
||
|
||
*** Keywords *** | ||
Keyword | ||
No Operation |
6 changes: 6 additions & 0 deletions
6
tests/atest/rules/misc/builtin_imports_not_sorted/test_rule.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from tests.atest.utils import RuleAcceptance | ||
|
||
|
||
class TestRuleAcceptance(RuleAcceptance): | ||
def test_rule(self): | ||
self.check_rule(expected_file="expected_output.txt") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters