1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* The Universal Permissive License (UPL), Version 1.0
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,60 @@ public void testGettersSetters() throws Throwable {
398
404
}
399
405
}
400
406
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
+
401
461
private void expectSuspended (SuspendedCallback callback ) {
402
462
tester .expectSuspended (callback );
403
463
}
@@ -423,4 +483,19 @@ private static void checkDebugValues(String msg, Iterable<DebugValue> values, St
423
483
}
424
484
}
425
485
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
+ }
426
501
}
0 commit comments