|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.IOException;
|
| 21 | +import java.nio.charset.StandardCharsets; |
| 22 | +import java.nio.file.Files; |
21 | 23 | import java.util.List;
|
22 | 24 | import java.util.Map;
|
23 | 25 | import org.junit.jupiter.api.Test;
|
24 | 26 | import org.sonar.api.batch.fs.InputFile;
|
| 27 | +import org.sonar.api.batch.fs.internal.TestInputFileBuilder; |
25 | 28 | import org.sonar.api.internal.apachecommons.lang3.StringUtils;
|
26 |
| -import org.sonar.plugins.python.api.IssueLocation; |
27 |
| -import org.sonar.plugins.python.api.PythonVisitorContext; |
28 | 29 | import org.sonar.python.IPythonLocation;
|
29 |
| -import org.sonar.python.TestPythonVisitorRunner; |
30 |
| -import org.sonar.python.checks.TrailingWhitespaceCheck; |
31 | 30 |
|
32 | 31 | import static org.assertj.core.api.Assertions.assertThat;
|
33 | 32 | import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
34 |
| -import static org.sonar.plugins.python.TestUtils.createInputFile; |
35 |
| -import static org.sonar.plugins.python.TestUtils.mapToColumnMappingList; |
| 33 | +import static org.sonar.plugins.python.NotebookTestUtils.mapToColumnMappingList; |
36 | 34 |
|
37 | 35 | class IpynbNotebookParserTest {
|
38 | 36 | private final File baseDir = new File("src/test/resources/org/sonar/plugins/python").getAbsoluteFile();
|
39 | 37 |
|
| 38 | + public static PythonInputFile createInputFile(File baseDir, String name, InputFile.Status status, InputFile.Type type) { |
| 39 | + try { |
| 40 | + return new PythonInputFileImpl(TestInputFileBuilder.create("moduleKey", name) |
| 41 | + .setModuleBaseDir(baseDir.toPath()) |
| 42 | + .setCharset(StandardCharsets.UTF_8) |
| 43 | + .setStatus(status) |
| 44 | + .setType(type) |
| 45 | + .setLanguage("py") |
| 46 | + .initMetadata(Files.readString(new File(baseDir, name).toPath())) |
| 47 | + .build()); |
| 48 | + } catch (IOException e) { |
| 49 | + throw new IllegalStateException("Cannot read " + name + " from base directory" + baseDir, e); |
| 50 | + } |
| 51 | + } |
40 | 52 | @Test
|
41 | 53 | void testParseNotebook() throws IOException {
|
42 | 54 | var inputFile = createInputFile(baseDir, "notebook.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
|
@@ -238,65 +250,4 @@ void testParseNotebook1() throws IOException {
|
238 | 250 | assertThat(result.contents()).isEmpty();
|
239 | 251 | }
|
240 | 252 |
|
241 |
| - @Test |
242 |
| - void trailing_whitespace() throws IOException { |
243 |
| - var inputFile = createInputFile(baseDir, "notebook_trailing_whitespace.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN); |
244 |
| - var result = IpynbNotebookParser.parseNotebook(inputFile).get(); |
245 |
| - var check = new TrailingWhitespaceCheck(); |
246 |
| - var context = new PythonVisitorContext( |
247 |
| - TestPythonVisitorRunner.parseNotebookFile(result.locationMap(), result.contents()), |
248 |
| - SonarQubePythonFile.create(result), |
249 |
| - null, |
250 |
| - ""); |
251 |
| - check.scanFile(context); |
252 |
| - |
253 |
| - var issues = context.getIssues(); |
254 |
| - assertThat(issues).hasSize(3); |
255 |
| - |
256 |
| - assertThat(issues.get(0).primaryLocation().startLine()).isEqualTo(14); |
257 |
| - assertThat(issues.get(0).primaryLocation().endLine()).isEqualTo(14); |
258 |
| - assertThat(issues.get(0).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); |
259 |
| - assertThat(issues.get(0).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); |
260 |
| - |
261 |
| - assertThat(issues.get(1).primaryLocation().startLine()).isEqualTo(17); |
262 |
| - assertThat(issues.get(1).primaryLocation().endLine()).isEqualTo(17); |
263 |
| - assertThat(issues.get(1).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); |
264 |
| - assertThat(issues.get(1).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); |
265 |
| - |
266 |
| - assertThat(issues.get(2).primaryLocation().startLine()).isEqualTo(20); |
267 |
| - assertThat(issues.get(2).primaryLocation().endLine()).isEqualTo(20); |
268 |
| - assertThat(issues.get(2).primaryLocation().startLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); |
269 |
| - assertThat(issues.get(2).primaryLocation().endLineOffset()).isEqualTo(IssueLocation.UNDEFINED_OFFSET); |
270 |
| - } |
271 |
| - |
272 |
| - @Test |
273 |
| - void trailing_whitespace_compressed() throws IOException { |
274 |
| - var inputFile = createInputFile(baseDir, "notebook_trailing_whitespace_compressed.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN); |
275 |
| - var result = IpynbNotebookParser.parseNotebook(inputFile).get(); |
276 |
| - var check = new TrailingWhitespaceCheck(); |
277 |
| - var context = new PythonVisitorContext( |
278 |
| - TestPythonVisitorRunner.parseNotebookFile(result.locationMap(), result.contents()), |
279 |
| - SonarQubePythonFile.create(result), |
280 |
| - null, |
281 |
| - ""); |
282 |
| - check.scanFile(context); |
283 |
| - |
284 |
| - var issues = context.getIssues(); |
285 |
| - assertThat(issues).hasSize(3); |
286 |
| - |
287 |
| - assertThat(issues.get(0).primaryLocation().startLine()).isEqualTo(1); |
288 |
| - assertThat(issues.get(0).primaryLocation().endLine()).isEqualTo(1); |
289 |
| - assertThat(issues.get(0).primaryLocation().startLineOffset()).isEqualTo(142); |
290 |
| - assertThat(issues.get(0).primaryLocation().endLineOffset()).isEqualTo(157); // Should be 154 |
291 |
| - |
292 |
| - assertThat(issues.get(1).primaryLocation().startLine()).isEqualTo(1); |
293 |
| - assertThat(issues.get(1).primaryLocation().endLine()).isEqualTo(1); |
294 |
| - assertThat(issues.get(1).primaryLocation().startLineOffset()).isEqualTo(199); |
295 |
| - assertThat(issues.get(1).primaryLocation().endLineOffset()).isEqualTo(207); // Should be 204 |
296 |
| - |
297 |
| - assertThat(issues.get(2).primaryLocation().startLine()).isEqualTo(1); |
298 |
| - assertThat(issues.get(2).primaryLocation().endLine()).isEqualTo(1); |
299 |
| - assertThat(issues.get(2).primaryLocation().startLineOffset()).isEqualTo(249); |
300 |
| - assertThat(issues.get(2).primaryLocation().endLineOffset()).isEqualTo(249); // Should be 255 |
301 |
| - } |
302 | 253 | }
|
0 commit comments