-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARPY-2406 Fix coverage by moving files between packages (#2208)
- Loading branch information
1 parent
b138625
commit efcd828
Showing
19 changed files
with
151 additions
and
78 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
python-frontend/src/main/java/org/sonar/plugins/python/NotebookTestUtils.java
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,35 @@ | ||
/* | ||
* SonarQube Python Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the Sonar Source-Available License for more details. | ||
* | ||
* You should have received a copy of the Sonar Source-Available License | ||
* along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
*/ | ||
package org.sonar.plugins.python; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import org.sonar.python.EscapeCharPositionInfo; | ||
|
||
public class NotebookTestUtils { | ||
|
||
private NotebookTestUtils() { | ||
// Utility class | ||
} | ||
|
||
public static List<EscapeCharPositionInfo> mapToColumnMappingList(Map<Integer, Integer> map) { | ||
return map.entrySet().stream() | ||
.sorted(Map.Entry.comparingByKey()) | ||
.map(entry -> new EscapeCharPositionInfo(entry.getKey(), entry.getValue())) | ||
.toList(); | ||
} | ||
} |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
97 changes: 97 additions & 0 deletions
97
...-python-plugin/src/test/java/org/sonar/plugins/python/IpynbNotebookParserScannerTest.java
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,97 @@ | ||
/* | ||
* SonarQube Python Plugin | ||
* Copyright (C) 2011-2024 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the Sonar Source-Available License for more details. | ||
* | ||
* You should have received a copy of the Sonar Source-Available License | ||
* along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
*/ | ||
package org.sonar.plugins.python; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import org.junit.jupiter.api.Test; | ||
import org.sonar.api.batch.fs.InputFile; | ||
import org.sonar.plugins.python.api.IssueLocation; | ||
import org.sonar.plugins.python.api.PythonVisitorContext; | ||
import org.sonar.python.TestPythonVisitorRunner; | ||
import org.sonar.python.checks.TrailingWhitespaceCheck; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.sonar.plugins.python.TestUtils.createInputFile; | ||
|
||
class IpynbNotebookParserScannerTest { | ||
|
||
private final File baseDir = new File("src/test/resources/org/sonar/plugins/python"); | ||
|
||
@Test | ||
void trailing_whitespace() throws IOException { | ||
var inputFile = createInputFile(baseDir, "notebook_trailing_whitespace.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN); | ||
var result = IpynbNotebookParser.parseNotebook(inputFile).get(); | ||
var check = new TrailingWhitespaceCheck(); | ||
var context = new PythonVisitorContext( | ||
TestPythonVisitorRunner.parseNotebookFile(result.locationMap(), result.contents()), | ||
SonarQubePythonFile.create(result), | ||
null, | ||
""); | ||
check.scanFile(context); | ||
|
||
var issues = context.getIssues(); | ||
assertThat(issues).hasSize(3); | ||
|
||
assertThat(issues.get(0).primaryLocation().startLine()).isEqualTo(14); | ||
assertThat(issues.get(0).primaryLocation().endLine()).isEqualTo(14); | ||
assertThat(issues.get(0).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); | ||
assertThat(issues.get(0).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); | ||
|
||
assertThat(issues.get(1).primaryLocation().startLine()).isEqualTo(17); | ||
assertThat(issues.get(1).primaryLocation().endLine()).isEqualTo(17); | ||
assertThat(issues.get(1).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); | ||
assertThat(issues.get(1).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); | ||
|
||
assertThat(issues.get(2).primaryLocation().startLine()).isEqualTo(20); | ||
assertThat(issues.get(2).primaryLocation().endLine()).isEqualTo(20); | ||
assertThat(issues.get(2).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); | ||
assertThat(issues.get(2).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); | ||
} | ||
|
||
@Test | ||
void trailing_whitespace_compressed() throws IOException { | ||
var inputFile = createInputFile(baseDir, "notebook_trailing_whitespace_compressed.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN); | ||
var result = IpynbNotebookParser.parseNotebook(inputFile).get(); | ||
var check = new TrailingWhitespaceCheck(); | ||
var context = new PythonVisitorContext( | ||
TestPythonVisitorRunner.parseNotebookFile(result.locationMap(), result.contents()), | ||
SonarQubePythonFile.create(result), | ||
null, | ||
""); | ||
check.scanFile(context); | ||
|
||
var issues = context.getIssues(); | ||
assertThat(issues).hasSize(3); | ||
|
||
assertThat(issues.get(0).primaryLocation().startLine()).isEqualTo(1); | ||
assertThat(issues.get(0).primaryLocation().endLine()).isEqualTo(1); | ||
assertThat(issues.get(0).primaryLocation().startLineOffset()).isEqualTo(142); | ||
assertThat(issues.get(0).primaryLocation().endLineOffset()).isEqualTo(157); // Should be 154 | ||
|
||
assertThat(issues.get(1).primaryLocation().startLine()).isEqualTo(1); | ||
assertThat(issues.get(1).primaryLocation().endLine()).isEqualTo(1); | ||
assertThat(issues.get(1).primaryLocation().startLineOffset()).isEqualTo(199); | ||
assertThat(issues.get(1).primaryLocation().endLineOffset()).isEqualTo(207); // Should be 204 | ||
|
||
assertThat(issues.get(2).primaryLocation().startLine()).isEqualTo(1); | ||
assertThat(issues.get(2).primaryLocation().endLine()).isEqualTo(1); | ||
assertThat(issues.get(2).primaryLocation().startLineOffset()).isEqualTo(249); | ||
assertThat(issues.get(2).primaryLocation().endLineOffset()).isEqualTo(249); // Should be 255 | ||
} | ||
|
||
} |
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
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