Skip to content

Commit c238536

Browse files
joke1196ghislainpiot
authored andcommitted
SONARPY-2012: IPynbParser should return a GeneratedIPythonFile (#1875)
1 parent 7794c9a commit c238536

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

sonar-python-plugin/src/main/java/org/sonar/plugins/python/GeneratedIPythonFile.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,28 @@
2020
package org.sonar.plugins.python;
2121

2222
import java.io.IOException;
23+
import java.util.Map;
2324
import org.sonar.api.batch.fs.InputFile;
25+
import org.sonar.plugins.python.IpynbNotebookParser.IPythonLocation;
2426

2527
public class GeneratedIPythonFile implements PythonInputFile {
2628

2729
InputFile wrappedFile;
2830

2931
private String pythonContent;
3032

31-
public GeneratedIPythonFile(InputFile wrappedFile, String pythonContent) {
33+
private Map<Integer, IPythonLocation> locationMap;
34+
35+
public GeneratedIPythonFile(InputFile wrappedFile, String pythonContent, Map<Integer, IPythonLocation> locationMap) {
36+
this.locationMap = locationMap;
3237
this.wrappedFile = wrappedFile;
3338
this.pythonContent = pythonContent;
3439
}
35-
40+
41+
public Map<Integer, IPythonLocation> locationMap() {
42+
return locationMap;
43+
}
44+
3645
@Override
3746
public InputFile wrappedFile() {
3847
return wrappedFile;

sonar-python-plugin/src/main/java/org/sonar/plugins/python/IpynbNotebookParser.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class IpynbNotebookParser {
3232

3333
public static final String SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER = "\n#SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER\n";
3434

35-
public static ParseResult parseNotebook(PythonInputFile inputFile) {
35+
public static GeneratedIPythonFile parseNotebook(PythonInputFile inputFile) {
3636
try {
3737
return new IpynbNotebookParser(inputFile).parseNotebook();
3838
} catch (IOException e) {
@@ -51,7 +51,7 @@ private IpynbNotebookParser(PythonInputFile inputFile) {
5151
private final Map<Integer, IPythonLocation> locationMap = new HashMap<>();
5252
private int aggregatedSourceLine = 1;
5353

54-
public ParseResult parseNotebook() throws IOException {
54+
public GeneratedIPythonFile parseNotebook() throws IOException {
5555
String content = inputFile.wrappedFile().contents();
5656
JsonFactory factory = new JsonFactory();
5757
try (JsonParser jParser = factory.createParser(content)) {
@@ -69,7 +69,7 @@ public ParseResult parseNotebook() throws IOException {
6969
}
7070
}
7171

72-
return new ParseResult(inputFile, aggregatedSource.toString(), locationMap);
72+
return new GeneratedIPythonFile(inputFile.wrappedFile(), aggregatedSource.toString(), locationMap);
7373
}
7474

7575
private void processCodeCell(JsonParser jParser) throws IOException {
@@ -164,9 +164,6 @@ private static Map<Integer, Integer> countEscapeCharacters(String sourceLine, Ma
164164
return colMap;
165165
}
166166

167-
public record ParseResult(PythonInputFile inputFile, String aggregatedSource, Map<Integer, IPythonLocation> locationMap) {
168-
}
169-
170167
public record IPythonLocation(int line, int column, Map<Integer, Integer> colOffset) {
171168
}
172169

sonar-python-plugin/src/test/java/org/sonar/plugins/python/GeneratedIPythonFileTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.sonar.plugins.python;
2121

2222
import java.io.IOException;
23+
import java.util.Map;
2324
import org.junit.jupiter.api.Test;
2425
import org.sonar.api.batch.fs.InputFile;
2526
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
@@ -31,28 +32,28 @@ class GeneratedIPythonFileTest {
3132

3233
@Test
3334
void shouldHaveIPythonKind() {
34-
PythonInputFile inputFile = new GeneratedIPythonFile(createWrappedFile(), "");
35+
PythonInputFile inputFile = new GeneratedIPythonFile(createWrappedFile(), "", Map.of());
3536
assertThat(inputFile.kind()).isEqualTo(Kind.IPYTHON);
3637
}
3738

3839
@Test
3940
void shouldReturnTheWrappedFile() {
4041
InputFile wrappedFile = createWrappedFile();
41-
GeneratedIPythonFile inputFile = new GeneratedIPythonFile(wrappedFile, "");
42+
GeneratedIPythonFile inputFile = new GeneratedIPythonFile(wrappedFile, "", Map.of());
4243
assertThat(inputFile.wrappedFile()).isEqualTo(wrappedFile);
4344
}
4445

4546
@Test
4647
void shouldHaveTheWrappedFileToString() {
4748
InputFile wrappedFile = createWrappedFile();
48-
PythonInputFile inputFile = new GeneratedIPythonFile(wrappedFile, "");
49+
PythonInputFile inputFile = new GeneratedIPythonFile(wrappedFile, "", Map.of());
4950
assertThat(inputFile).hasToString(wrappedFile.toString());
5051
}
5152

5253
@Test
5354
void shouldHaveTheContentPassed() throws IOException {
5455
InputFile wrappedFile = createWrappedFile();
55-
PythonInputFile inputFile = new GeneratedIPythonFile(wrappedFile, "test");
56+
PythonInputFile inputFile = new GeneratedIPythonFile(wrappedFile, "test", Map.of());
5657
assertThat(inputFile.contents()).isEqualTo("test");
5758

5859
}

sonar-python-plugin/src/test/java/org/sonar/plugins/python/IpynbNotebookParserTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.sonar.plugins.python;
2121

2222
import java.io.File;
23+
import java.io.IOException;
2324
import java.util.Map;
2425
import org.junit.jupiter.api.Test;
2526
import org.sonar.api.batch.fs.InputFile;
@@ -33,23 +34,22 @@ class IpynbNotebookParserTest {
3334
private final File baseDir = new File("src/test/resources/org/sonar/plugins/python").getAbsoluteFile();
3435

3536
@Test
36-
void testParseNotebook() {
37+
void testParseNotebook() throws IOException {
3738
var inputFile = createInputFile(baseDir, "notebook.ipynb", InputFile.Status.CHANGED, InputFile.Type.MAIN);
3839

3940
var result = IpynbNotebookParser.parseNotebook(inputFile);
4041

4142
assertThat(result.locationMap().keySet()).hasSize(20);
42-
assertThat(StringUtils.countMatches(result.aggregatedSource(), IpynbNotebookParser.SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER))
43+
assertThat(StringUtils.countMatches(result.contents(), IpynbNotebookParser.SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER))
4344
.isEqualTo(7);
4445
assertThat(result.locationMap()).extracting(map -> map.get(17)).isEqualTo(new IpynbNotebookParser.IPythonLocation(64, 27, Map.of(6, 21, 20, 37, -1, 3)));
4546

46-
// The wrapped file changes the lines of the notebook
47-
assertThat(result.locationMap()).extracting(map -> map.get(22)).isEqualTo(new IpynbNotebookParser.IPythonLocation(84, 15, Map.of(6, 21, 15, 32, -1, 3)));
48-
assertThat(result.locationMap()).extracting(map -> map.get(23)).isEqualTo(new IpynbNotebookParser.IPythonLocation(84, 37, Map.of(6, 21, 23, 40, -1, 3)));
47+
assertThat(result.locationMap()).extracting(map -> map.get(22)).isEqualTo(new IpynbNotebookParser.IPythonLocation(83, 15, Map.of(6, 21, 15, 32, -1, 3)));
48+
assertThat(result.locationMap()).extracting(map -> map.get(23)).isEqualTo(new IpynbNotebookParser.IPythonLocation(83, 37, Map.of(6, 21, 23, 40, -1, 3)));
4949

5050
assertThat(result.locationMap()).extracting(map -> map.get(25))
51-
.isEqualTo(new IpynbNotebookParser.IPythonLocation(91, 15, Map.of(4, 19, 39, 62, 41, 64, 42, 65, 46, 71, -1, 7)));
52-
assertThat(result.locationMap()).extracting(map -> map.get(26)).isEqualTo(new IpynbNotebookParser.IPythonLocation(91, 71, Map.of(-1, 0)));
51+
.isEqualTo(new IpynbNotebookParser.IPythonLocation(90, 15, Map.of(4, 19, 39, 62, 41, 64, 42, 65, 46, 71, -1, 7)));
52+
assertThat(result.locationMap()).extracting(map -> map.get(26)).isEqualTo(new IpynbNotebookParser.IPythonLocation(90, 71, Map.of(-1, 0)));
5353
}
5454

5555
@Test

sonar-python-plugin/src/test/resources/org/sonar/plugins/python/notebook.ipynb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@
7272
"metadata": {},
7373
"outputs": [],
7474
"source": [
75-
"print(\"My\\ntext\")\n",
76-
"print(\"Something else\\n\")"
75+
"print(\"My\\ntext\")\n", "print(\"Something else\\n\")"
7776
]
7877
},
7978
{

0 commit comments

Comments
 (0)