45
45
import static org .junit .Assert .assertNotNull ;
46
46
import static org .junit .Assert .assertTrue ;
47
47
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 ;
48
54
import java .util .Arrays ;
49
55
import java .util .HashMap ;
50
56
import java .util .Map ;
@@ -398,6 +404,52 @@ public void testGettersSetters() throws Throwable {
398
404
}
399
405
}
400
406
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
+
401
453
private void expectSuspended (SuspendedCallback callback ) {
402
454
tester .expectSuspended (callback );
403
455
}
@@ -423,4 +475,19 @@ private static void checkDebugValues(String msg, Iterable<DebugValue> values, St
423
475
}
424
476
}
425
477
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
+ }
426
493
}
0 commit comments