Skip to content

Commit 269d714

Browse files
joke1196ghislainpiot
authored andcommitted
SONARPY-2066: IPython help command should be parsed correctly (#1926)
1 parent b7d4efb commit 269d714

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+38
-16
lines changed

python-frontend/src/main/java/org/sonar/python/api/IPythonGrammarBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ protected void iPythonRules(LexerfulGrammarBuilder b) {
8989
TEST,
9090
QUESTION_MARK,
9191
b.optional(QUESTION_MARK)
92-
)
92+
),
93+
QUESTION_MARK
9394
));
9495
}
9596

python-frontend/src/test/java/org/sonar/python/parser/PythonParserTest.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,36 @@
2626
import org.junit.jupiter.api.Test;
2727

2828
import static java.nio.charset.StandardCharsets.UTF_8;
29+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
2930

3031
class PythonParserTest {
3132

3233
private final PythonParser parser = PythonParser.create();
3334

35+
private final PythonParser notebookParser = PythonParser.createIPythonParser();
36+
3437
@Test
35-
void test() throws Exception {
36-
Collection<File> files = listFiles();
38+
void test_python() throws Exception {
39+
Collection<File> files = listFiles("python");
3740
for (File file : files) {
3841
String fileContent = new String(Files.readAllBytes(file.toPath()), UTF_8);
39-
parser.parse(fileContent);
42+
assertDoesNotThrow(() -> parser.parse(fileContent));
4043
}
4144
}
4245

43-
private static Collection<File> listFiles() {
44-
File dir = new File("src/test/resources/parser/");
46+
private static Collection<File> listFiles(String folderName) {
47+
File dir = new File(String.format("src/test/resources/parser/%s/", folderName));
4548
return FileUtils.listFiles(dir, new String[]{"py"}, true);
4649
}
4750

51+
52+
@Test
53+
void test_notebook() throws Exception {
54+
Collection<File> files = listFiles("notebooks");
55+
for (File file : files) {
56+
String fileContent = new String(Files.readAllBytes(file.toPath()), UTF_8);
57+
assertDoesNotThrow(() -> notebookParser.parse(fileContent));
58+
}
59+
}
60+
4861
}

python-frontend/src/test/java/org/sonar/python/tree/IPythonTreeMakerTest.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,19 @@ void cellMagicUntilEndOfFile() {
134134

135135
@Test
136136
void dynamicObjectInfo() {
137-
var file = parseIPython("a = A()\n" +
138-
"??a.foo\n" +
139-
"?a.foo\n" +
140-
"?a.foo?\n" +
141-
"a.foo?\n" +
142-
"a.foo??\n" +
143-
"??a.foo()??\n" +
144-
"b = a.foo()", treeMaker::fileInput);
137+
var file = parseIPython("""
138+
a = A()
139+
??a.foo
140+
?a.foo
141+
?a.foo?
142+
a.foo?
143+
a.foo??
144+
??a.foo()??
145+
?
146+
b = a.foo()""", treeMaker::fileInput);
145147
var statementList = findFirstChildWithKind(file, Tree.Kind.STATEMENT_LIST);
146148
var statements = statementList.children();
147-
assertThat(statements).hasSize(8);
149+
assertThat(statements).hasSize(9);
148150

149151
assertThat(statements.get(0).getKind()).isEqualTo(Tree.Kind.ASSIGNMENT_STMT);
150152
checkDynamicObjectInfo(statements.get(1), 2, 0);
@@ -153,7 +155,8 @@ void dynamicObjectInfo() {
153155
checkDynamicObjectInfo(statements.get(4), 0, 1);
154156
checkDynamicObjectInfo(statements.get(5), 0, 2);
155157
checkDynamicObjectInfo(statements.get(6), 2, 2);
156-
assertThat(statements.get(7).getKind()).isEqualTo(Tree.Kind.ASSIGNMENT_STMT);
158+
checkDynamicObjectInfo(statements.get(7), 0, 0);
159+
assertThat(statements.get(8).getKind()).isEqualTo(Tree.Kind.ASSIGNMENT_STMT);
157160
}
158161

159162
private void checkDynamicObjectInfo(Tree dynamicObjectInfo, int questionMarksBefore, int questionMarksAfter) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
?
2+
#SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER
3+
import collections
4+
collections.Counter??
5+
#SONAR_PYTHON_NOTEBOOK_CELL_DELIMITER

0 commit comments

Comments
 (0)