5454
5555import org .graalvm .polyglot .Context ;
5656import org .graalvm .polyglot .HostAccess ;
57- import org .graalvm .polyglot .PolyglotAccess ;
57+ import org .graalvm .polyglot .PolyglotException ;
5858import org .graalvm .polyglot .io .IOAccess ;
5959import org .graalvm .python .embedding .vfs .VirtualFileSystem ;
6060import org .junit .Test ;
6161
6262public class VirtualFileSystemTest {
6363
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 ;
6767
68- private static final String PYTHON = "python" ;
68+ static final String PYTHON = "python" ;
6969
7070 @ Test
7171 public void defaultValues () throws Exception {
7272 VirtualFileSystem vfs = VirtualFileSystem .create ();
7373 VirtualFileSystem vfs2 = VirtualFileSystem .newBuilder ().build ();
7474
7575 assertEquals (vfs .getMountPoint (), vfs2 .getMountPoint ());
76- assertEquals (vfs .vfsHomePath (), vfs2 .vfsHomePath ());
77- assertEquals (vfs .vfsProjPath (), vfs2 .vfsProjPath ());
78- assertEquals (vfs .vfsVenvPath (), vfs2 .vfsVenvPath ());
7976
8077 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 ());
8478 }
8579
8680 @ Test
@@ -90,9 +84,6 @@ public void mountPoints() throws Exception {
9084 windowsMountPoint (VFS_WIN_MOUNT_POINT ).build ();
9185
9286 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 ());
9687 }
9788
9889 @ Test
@@ -396,13 +387,16 @@ private void eval(String s) {
396387 }
397388
398389 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 ) {
400396 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" );
404398 }
405- getContext ( builderFunction ). eval ( PYTHON , src ) ;
399+ return src ;
406400 }
407401
408402 private Context cachedContext ;
@@ -419,28 +413,46 @@ public Context getContext(Function<VirtualFileSystem.Builder, VirtualFileSystem.
419413 builder = builderFunction .apply (builder );
420414 }
421415 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 ();
441417 if (builderFunction == null ) {
442418 cachedContext = context ;
443419 }
444420 return context ;
445421 }
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+ }
446458}
0 commit comments