54
54
55
55
import org .graalvm .polyglot .Context ;
56
56
import org .graalvm .polyglot .HostAccess ;
57
- import org .graalvm .polyglot .PolyglotAccess ;
57
+ import org .graalvm .polyglot .PolyglotException ;
58
58
import org .graalvm .polyglot .io .IOAccess ;
59
59
import org .graalvm .python .embedding .vfs .VirtualFileSystem ;
60
60
import org .junit .Test ;
61
61
62
62
public class VirtualFileSystemTest {
63
63
64
- private static final String VFS_UNIX_MOUNT_POINT = "/test_mount_point" ;
65
- private static final String VFS_WIN_MOUNT_POINT = "X:\\ test_win_mount_point" ;
66
- private static final String VFS_MOUNT_POINT = IS_WINDOWS ? VFS_WIN_MOUNT_POINT : VFS_UNIX_MOUNT_POINT ;
64
+ static final String VFS_UNIX_MOUNT_POINT = "/test_mount_point" ;
65
+ static final String VFS_WIN_MOUNT_POINT = "X:\\ test_win_mount_point" ;
66
+ static final String VFS_MOUNT_POINT = IS_WINDOWS ? VFS_WIN_MOUNT_POINT : VFS_UNIX_MOUNT_POINT ;
67
67
68
- private static final String PYTHON = "python" ;
68
+ static final String PYTHON = "python" ;
69
69
70
70
@ Test
71
71
public void defaultValues () throws Exception {
72
72
VirtualFileSystem vfs = VirtualFileSystem .create ();
73
73
VirtualFileSystem vfs2 = VirtualFileSystem .newBuilder ().build ();
74
74
75
75
assertEquals (vfs .getMountPoint (), vfs2 .getMountPoint ());
76
- assertEquals (vfs .vfsHomePath (), vfs2 .vfsHomePath ());
77
- assertEquals (vfs .vfsProjPath (), vfs2 .vfsProjPath ());
78
- assertEquals (vfs .vfsVenvPath (), vfs2 .vfsVenvPath ());
79
76
80
77
assertEquals (IS_WINDOWS ? "X:\\ graalpy_vfs" : "/graalpy_vfs" , vfs .getMountPoint ());
81
- assertEquals (IS_WINDOWS ? "X:\\ graalpy_vfs\\ home" : "/graalpy_vfs/home" , vfs .vfsHomePath ());
82
- assertEquals (IS_WINDOWS ? "X:\\ graalpy_vfs\\ proj" : "/graalpy_vfs/proj" , vfs .vfsProjPath ());
83
- assertEquals (IS_WINDOWS ? "X:\\ graalpy_vfs\\ venv" : "/graalpy_vfs/venv" , vfs .vfsVenvPath ());
84
78
}
85
79
86
80
@ Test
@@ -90,9 +84,6 @@ public void mountPoints() throws Exception {
90
84
windowsMountPoint (VFS_WIN_MOUNT_POINT ).build ();
91
85
92
86
assertEquals (VFS_MOUNT_POINT , vfs .getMountPoint ());
93
- assertEquals (IS_WINDOWS ? VFS_WIN_MOUNT_POINT + "\\ home" : VFS_UNIX_MOUNT_POINT + "/home" , vfs .vfsHomePath ());
94
- assertEquals (IS_WINDOWS ? VFS_WIN_MOUNT_POINT + "\\ proj" : VFS_UNIX_MOUNT_POINT + "/proj" , vfs .vfsProjPath ());
95
- assertEquals (IS_WINDOWS ? VFS_WIN_MOUNT_POINT + "\\ venv" : VFS_UNIX_MOUNT_POINT + "/venv" , vfs .vfsVenvPath ());
96
87
}
97
88
98
89
@ Test
@@ -396,13 +387,16 @@ private void eval(String s) {
396
387
}
397
388
398
389
private void eval (String s , Function <VirtualFileSystem .Builder , VirtualFileSystem .Builder > builderFunction ) {
399
- String src ;
390
+ String src = patchMountPoint (s );
391
+
392
+ getContext (builderFunction ).eval (PYTHON , src );
393
+ }
394
+
395
+ private static String patchMountPoint (String src ) {
400
396
if (IS_WINDOWS ) {
401
- src = s .replace (VFS_UNIX_MOUNT_POINT , "X:\\ \\ test_win_mount_point" );
402
- } else {
403
- src = s ;
397
+ return src .replace (VFS_UNIX_MOUNT_POINT , "X:\\ \\ test_win_mount_point" );
404
398
}
405
- getContext ( builderFunction ). eval ( PYTHON , src ) ;
399
+ return src ;
406
400
}
407
401
408
402
private Context cachedContext ;
@@ -419,28 +413,46 @@ public Context getContext(Function<VirtualFileSystem.Builder, VirtualFileSystem.
419
413
builder = builderFunction .apply (builder );
420
414
}
421
415
VirtualFileSystem vfs = builder .build ();
422
- Context context = Context .newBuilder ().//
423
- allowExperimentalOptions (false ).//
424
- allowAllAccess (false ).//
425
- allowHostAccess (HostAccess .ALL ).//
426
- allowIO (IOAccess .newBuilder ().//
427
- allowHostSocketAccess (true ).//
428
- fileSystem (vfs ).//
429
- build ()).//
430
- allowCreateThread (true ).//
431
- allowNativeAccess (true ).//
432
- allowPolyglotAccess (PolyglotAccess .ALL ).//
433
- option ("python.PosixModuleBackend" , "java" ).//
434
- option ("python.DontWriteBytecodeFlag" , "true" ).//
435
- option ("python.VerboseFlag" , System .getenv ("PYTHONVERBOSE" ) != null ? "true" : "false" ).//
436
- option ("log.python.level" , System .getenv ("PYTHONVERBOSE" ) != null ? "FINE" : "SEVERE" ).//
437
- option ("python.WarnOptions" , System .getenv ("PYTHONWARNINGS" ) == null ? "" : System .getenv ("PYTHONWARNINGS" )).//
438
- option ("python.AlwaysRunExcepthook" , "true" ).//
439
- option ("python.ForceImportSite" , "true" ).//
440
- option ("engine.WarnInterpreterOnly" , "false" ).build ();
416
+ Context context = VirtualFileSystem .contextBuilder (vfs ).build ();
441
417
if (builderFunction == null ) {
442
418
cachedContext = context ;
443
419
}
444
420
return context ;
445
421
}
422
+
423
+ @ Test
424
+ public void builderTest () {
425
+ Context context = VirtualFileSystem .contextBuilder ().allowAllAccess (true ).allowHostAccess (HostAccess .ALL ).build ();
426
+ context .eval (PYTHON , "import java; java.type('java.lang.String')" );
427
+
428
+ context = VirtualFileSystem .contextBuilder ().allowAllAccess (false ).allowHostAccess (HostAccess .NONE ).build ();
429
+ context .eval (PYTHON , """
430
+ import java
431
+ try:
432
+ java.type('java.lang.String');
433
+ except NotImplementedError:
434
+ pass
435
+ else:
436
+ assert False, 'expected NotImplementedError'
437
+ """ );
438
+
439
+ VirtualFileSystem vfs = VirtualFileSystem .newBuilder ().//
440
+ unixMountPoint (VFS_MOUNT_POINT ).//
441
+ windowsMountPoint (VFS_WIN_MOUNT_POINT ).//
442
+ resourceLoadingClass (VirtualFileSystemTest .class ).build ();
443
+ context = VirtualFileSystem .contextBuilder (vfs ).build ();
444
+ context .eval (PYTHON , patchMountPoint ("from os import listdir; listdir('/test_mount_point')" ));
445
+
446
+ context = VirtualFileSystem .contextBuilder ().build ();
447
+ context .eval (PYTHON , "from os import listdir; listdir('.')" );
448
+
449
+ context = VirtualFileSystem .contextBuilder ().allowIO (IOAccess .NONE ).build ();
450
+ boolean gotPE = false ;
451
+ try {
452
+ context .eval (PYTHON , "from os import listdir; listdir('.')" );
453
+ } catch (PolyglotException pe ) {
454
+ gotPE = true ;
455
+ }
456
+ assert gotPE : "expected PolyglotException" ;
457
+ }
446
458
}
0 commit comments