Skip to content

Commit 11bd05c

Browse files
committed
A test of GR-13376.
PullRequest: graalpython/357
2 parents bf1520d + f92c2cd commit 11bd05c

File tree

1 file changed

+76
-1
lines changed

1 file changed

+76
-1
lines changed

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

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -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,60 @@ public void testGettersSetters() throws Throwable {
398404
}
399405
}
400406

407+
@Test
408+
public void testSourceFileURI() throws Throwable {
409+
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
410+
// on the mac slaves we run with symlinked directories and such and it's annoying to
411+
// cater for that
412+
return;
413+
}
414+
Path tempDir = Files.createTempDirectory("pySourceTest");
415+
try {
416+
Path importedFile = tempDir.resolve("imported.py");
417+
Path importingFile = tempDir.resolve("importing.py");
418+
Files.write(importedFile, ("def sum(a, b):\n" +
419+
" return a + b\n").getBytes());
420+
Files.write(importingFile, ("import sys\n" +
421+
"sys.path.insert(0, '" + tempDir.toString() + "')\n" +
422+
"import imported\n" +
423+
"imported.sum(2, 3)\n").getBytes());
424+
Source source = Source.newBuilder("python", importingFile.toFile()).build();
425+
try (DebuggerSession session = tester.startSession()) {
426+
Breakpoint breakpoint = Breakpoint.newBuilder(importingFile.toUri()).lineIs(4).build();
427+
session.install(breakpoint);
428+
tester.startEval(source);
429+
expectSuspended((SuspendedEvent event) -> {
430+
assertEquals(importingFile.toUri(), event.getSourceSection().getSource().getURI());
431+
DebugStackFrame frame = event.getTopStackFrame();
432+
assertEquals(4, frame.getSourceSection().getStartLine());
433+
event.prepareStepInto(1);
434+
});
435+
expectSuspended((SuspendedEvent event) -> {
436+
assertEquals(importedFile.toUri(), event.getSourceSection().getSource().getURI());
437+
DebugStackFrame frame = event.getTopStackFrame();
438+
assertEquals(2, frame.getSourceSection().getStartLine());
439+
event.prepareContinue();
440+
});
441+
}
442+
tester.expectDone();
443+
// Test that breakpoint on the imported file is hit:
444+
try (DebuggerSession session = tester.startSession()) {
445+
Breakpoint breakpoint = Breakpoint.newBuilder(importedFile.toUri()).lineIs(2).build();
446+
session.install(breakpoint);
447+
tester.startEval(source);
448+
expectSuspended((SuspendedEvent event) -> {
449+
DebugStackFrame frame = event.getTopStackFrame();
450+
assertEquals(2, frame.getSourceSection().getStartLine());
451+
checkStack(frame, "sum", "a", "2", "b", "3");
452+
event.prepareContinue();
453+
});
454+
tester.expectDone();
455+
}
456+
} finally {
457+
deleteRecursively(tempDir);
458+
}
459+
}
460+
401461
private void expectSuspended(SuspendedCallback callback) {
402462
tester.expectSuspended(callback);
403463
}
@@ -423,4 +483,19 @@ private static void checkDebugValues(String msg, Iterable<DebugValue> values, St
423483
}
424484
}
425485

486+
private static void deleteRecursively(Path path) throws IOException {
487+
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
488+
@Override
489+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
490+
Files.delete(file);
491+
return FileVisitResult.CONTINUE;
492+
}
493+
494+
@Override
495+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
496+
Files.delete(dir);
497+
return FileVisitResult.CONTINUE;
498+
}
499+
});
500+
}
426501
}

0 commit comments

Comments
 (0)