Skip to content

Commit 258e379

Browse files
committed
A test of GR-13376.
1 parent c31bc60 commit 258e379

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/debug/PythonDebugTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@
4545
import static org.junit.Assert.assertNotNull;
4646
import static org.junit.Assert.assertTrue;
4747

48+
import java.io.IOException;
49+
import java.nio.file.FileVisitResult;
50+
import java.nio.file.Files;
51+
import java.nio.file.Path;
52+
import java.nio.file.SimpleFileVisitor;
53+
import java.nio.file.attribute.BasicFileAttributes;
4854
import java.util.Arrays;
4955
import java.util.HashMap;
5056
import java.util.Map;
@@ -398,6 +404,52 @@ public void testGettersSetters() throws Throwable {
398404
}
399405
}
400406

407+
@Test
408+
public void testSourceFileURI() throws Throwable {
409+
Path tempDir = Files.createTempDirectory("pySourceTest");
410+
try {
411+
Path importedFile = tempDir.resolve("imported.py");
412+
Path importingFile = tempDir.resolve("importing.py");
413+
Files.write(importedFile, ("def sum(a, b):\n" +
414+
" return a + b\n").getBytes());
415+
Files.write(importingFile, ("import imported\n" +
416+
"imported.sum(2, 3)\n").getBytes());
417+
Source source = Source.newBuilder("python", importingFile.toFile()).build();
418+
try (DebuggerSession session = tester.startSession()) {
419+
session.suspendNextExecution();
420+
tester.startEval(source);
421+
expectSuspended((SuspendedEvent event) -> {
422+
assertEquals(importingFile.toUri(), event.getSourceSection().getSource().getURI());
423+
DebugStackFrame frame = event.getTopStackFrame();
424+
assertEquals(1, frame.getSourceSection().getStartLine());
425+
event.prepareStepInto(2);
426+
});
427+
expectSuspended((SuspendedEvent event) -> {
428+
assertEquals(importedFile.toUri(), event.getSourceSection().getSource().getURI());
429+
DebugStackFrame frame = event.getTopStackFrame();
430+
assertEquals(2, frame.getSourceSection().getStartLine());
431+
event.prepareContinue();
432+
});
433+
}
434+
tester.expectDone();
435+
// Test that breakpoint on the imported file is hit:
436+
try (DebuggerSession session = tester.startSession()) {
437+
Breakpoint breakpoint = Breakpoint.newBuilder(importedFile.toUri()).lineIs(2).build();
438+
session.install(breakpoint);
439+
tester.startEval(source);
440+
expectSuspended((SuspendedEvent event) -> {
441+
DebugStackFrame frame = event.getTopStackFrame();
442+
assertEquals(2, frame.getSourceSection().getStartLine());
443+
checkStack(frame, "sum", "a", "2", "b", "3");
444+
event.prepareContinue();
445+
});
446+
tester.expectDone();
447+
}
448+
} finally {
449+
deleteRecursively(tempDir);
450+
}
451+
}
452+
401453
private void expectSuspended(SuspendedCallback callback) {
402454
tester.expectSuspended(callback);
403455
}
@@ -423,4 +475,19 @@ private static void checkDebugValues(String msg, Iterable<DebugValue> values, St
423475
}
424476
}
425477

478+
private static void deleteRecursively(Path path) throws IOException {
479+
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
480+
@Override
481+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
482+
Files.delete(file);
483+
return FileVisitResult.CONTINUE;
484+
}
485+
486+
@Override
487+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
488+
Files.delete(dir);
489+
return FileVisitResult.CONTINUE;
490+
}
491+
});
492+
}
426493
}

0 commit comments

Comments
 (0)