Skip to content

Commit 54dcc97

Browse files
SONARPY-653 Ensure correct import of external issues for Flake8 plugins rules (SonarSource#686)
1 parent 4ee1f46 commit 54dcc97

6 files changed

Lines changed: 38 additions & 2 deletions

File tree

its/plugin/it-python-plugin-test/projects/flake8_project/flake8-report.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ src/file1.py:5:1: E302 expected 2 blank lines, found 1
22
src/file1.py:7:42: F821 undefined name 'random'
33
src/file1.py:7:64: E201 whitespace after '('
44
src/file1.py:7:80: E501 line too long (108 > 79 characters)
5-
src/file1.py:7:109: W292 no newline at end of file
5+
src/file1.py:10:1: C901 'bar' is too complex (6)

its/plugin/it-python-plugin-test/projects/flake8_project/src/file1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@
55
def foo(secret='secret'):
66
os.system("echo " + os.environ['HOME'])
77
return md5("%s:%s:%s" % (secret, str(random.random()), str( Util.number.long_to_bytes(42)))).hexdigest()
8+
9+
10+
def bar(things):
11+
for thing in things:
12+
if thing != 41:
13+
if thing != 43:
14+
if thing > 40:
15+
if thing < 44:
16+
print("42")

its/plugin/it-python-plugin-test/src/test/java/com/sonar/python/it/plugin/Flake8ReportTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ public void import_report() {
5858
assertThat(issue.getType()).isEqualTo(Common.RuleType.CODE_SMELL);
5959
assertThat(issue.getSeverity()).isEqualTo(Common.Severity.MAJOR);
6060
assertThat(issue.getEffort()).isEqualTo("5min");
61+
62+
// Issue for which we don't have metadata
63+
issue = issues.get(4);
64+
assertThat(issue.getComponent()).isEqualTo("flake8_project:src/file1.py");
65+
assertThat(issue.getRule()).isEqualTo("external_flake8:C901");
66+
assertThat(issue.getMessage()).isEqualTo("'bar' is too complex (6)");
67+
assertThat(issue.getType()).isEqualTo(Common.RuleType.CODE_SMELL);
68+
assertThat(issue.getSeverity()).isEqualTo(Common.Severity.MAJOR);
69+
assertThat(issue.getEffort()).isEqualTo("5min");
6170
}
6271

6372
private static List<Issues.Issue> issues() {

sonar-python-plugin/src/test/java/org/sonar/plugins/python/flake8/Flake8SensorTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void test_descriptor() {
7575
@Test
7676
public void issues_with_sonarqube_79() throws IOException {
7777
List<ExternalIssue> externalIssues = executeSensorImporting(7, 9, FLAKE_8_REPORT);
78-
assertThat(externalIssues).hasSize(2);
78+
assertThat(externalIssues).hasSize(3);
7979

8080
ExternalIssue first = externalIssues.get(0);
8181
assertThat(first.ruleKey().toString()).isEqualTo(FLAKE8_F401);
@@ -106,6 +106,14 @@ public void issues_with_sonarqube_79() throws IOException {
106106
assertThat(secondTextRange.end().line()).isEqualTo(3);
107107
assertThat(secondTextRange.end().lineOffset()).isEqualTo(1);
108108

109+
ExternalIssue third = externalIssues.get(2);
110+
assertThat(third.ruleKey().toString()).isEqualTo("external_flake8:C901");
111+
assertThat(third.type()).isEqualTo(RuleType.CODE_SMELL);
112+
assertThat(third.severity()).isEqualTo(Severity.MAJOR);
113+
IssueLocation thirdPrimaryLoc = third.primaryLocation();
114+
assertThat(thirdPrimaryLoc.inputComponent().key()).isEqualTo(FLAKE8_FILE);
115+
assertThat(thirdPrimaryLoc.message()).isEqualTo("'bar' is too complex (6)");
116+
109117
assertNoErrorWarnDebugLogs(logTester);
110118
}
111119

sonar-python-plugin/src/test/resources/org/sonar/plugins/python/flake8/file1.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,12 @@
55
def foo(secret='secret'):
66
os.system("echo " + os.environ['HOME'])
77
return md5("%s:%s:%s" % (secret, str(random.random()), str( Util.number.long_to_bytes(42)))).hexdigest()
8+
9+
10+
def bar(things):
11+
for thing in things:
12+
if thing != 41:
13+
if thing != 43:
14+
if thing > 40:
15+
if thing < 44:
16+
print("42")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
flake8/file1.py:1:1: F401 'os' imported but unused
22
flake8/file1.py:3:1: E302 expected 2 blank lines, found 1
3+
flake8/file1.py:10:1: C901 'bar' is too complex (6)

0 commit comments

Comments
 (0)