45
45
import static org .junit .Assert .assertEquals ;
46
46
import static org .junit .Assert .assertTrue ;
47
47
48
+ import java .io .BufferedReader ;
48
49
import java .io .File ;
49
50
import java .io .IOException ;
51
+ import java .io .InputStream ;
52
+ import java .io .InputStreamReader ;
50
53
import java .nio .file .Files ;
51
54
import java .nio .file .Path ;
52
55
import java .util .List ;
55
58
import org .graalvm .polyglot .Context ;
56
59
import org .graalvm .polyglot .HostAccess ;
57
60
import org .graalvm .polyglot .PolyglotException ;
61
+ import org .graalvm .polyglot .io .FileSystem ;
58
62
import org .graalvm .polyglot .io .IOAccess ;
59
- import org .graalvm .python .embedding .vfs .VirtualFileSystem ;
63
+ import org .graalvm .python .embedding .utils .GraalPyResources ;
64
+ import org .graalvm .python .embedding .utils .VirtualFileSystem ;
60
65
import org .junit .Test ;
61
66
62
67
public class VirtualFileSystemTest {
@@ -69,71 +74,72 @@ public class VirtualFileSystemTest {
69
74
70
75
@ Test
71
76
public void defaultValues () throws Exception {
72
- VirtualFileSystem vfs = VirtualFileSystem .create ();
73
- VirtualFileSystem vfs2 = VirtualFileSystem .newBuilder (). build ();
77
+ VirtualFileSystem fs = VirtualFileSystem .create ();
78
+ VirtualFileSystem fs2 = VirtualFileSystem .create ();
74
79
75
- assertEquals (vfs .getMountPoint (), vfs2 .getMountPoint ());
80
+ assertEquals (fs .getMountPoint (), fs2 .getMountPoint ());
76
81
77
- assertEquals (IS_WINDOWS ? "X:\\ graalpy_vfs" : "/graalpy_vfs" , vfs .getMountPoint ());
82
+ assertEquals (IS_WINDOWS ? "X:\\ graalpy_vfs" : "/graalpy_vfs" , fs .getMountPoint ());
78
83
}
79
84
80
85
@ Test
81
86
public void mountPoints () throws Exception {
82
- VirtualFileSystem vfs = VirtualFileSystem .newBuilder ().//
87
+ VirtualFileSystem fs = VirtualFileSystem .newBuilder ().//
83
88
unixMountPoint (VFS_MOUNT_POINT ).//
84
89
windowsMountPoint (VFS_WIN_MOUNT_POINT ).build ();
85
90
86
- assertEquals (VFS_MOUNT_POINT , vfs .getMountPoint ());
91
+ assertEquals (VFS_MOUNT_POINT , fs .getMountPoint ());
87
92
}
88
93
89
94
@ Test
90
95
public void toRealPath () throws Exception {
91
- VirtualFileSystem vfs = VirtualFileSystem .newBuilder ().//
96
+ FileSystem fs = VirtualFileSystem .newBuilder ().//
92
97
unixMountPoint (VFS_MOUNT_POINT ).//
93
98
windowsMountPoint (VFS_WIN_MOUNT_POINT ).//
94
99
extractFilter (p -> p .getFileName ().toString ().equals ("file1" )).//
95
100
resourceLoadingClass (VirtualFileSystemTest .class ).build ();
96
101
// check regular resource dir
97
- assertEquals ("dir1" , vfs .toRealPath (Path .of ("dir1" )).toString ());
98
- assertEquals (VFS_MOUNT_POINT + File .separator + "dir1" , vfs .toRealPath (Path .of (VFS_MOUNT_POINT + File .separator + "dir1" )).toString ());
102
+ assertEquals ("dir1" , fs .toRealPath (Path .of ("dir1" )).toString ());
103
+ assertEquals (VFS_MOUNT_POINT + File .separator + "dir1" , fs .toRealPath (Path .of (VFS_MOUNT_POINT + File .separator + "dir1" )).toString ());
99
104
// check regular resource file
100
- assertEquals ("SomeFile" , vfs .toRealPath (Path .of ("SomeFile" )).toString ());
101
- assertEquals (VFS_MOUNT_POINT + File .separator + "SomeFile" , vfs .toRealPath (Path .of (VFS_MOUNT_POINT + File .separator + "SomeFile" )).toString ());
105
+ assertEquals ("SomeFile" , fs .toRealPath (Path .of ("SomeFile" )).toString ());
106
+ assertEquals (VFS_MOUNT_POINT + File .separator + "SomeFile" , fs .toRealPath (Path .of (VFS_MOUNT_POINT + File .separator + "SomeFile" )).toString ());
102
107
// check to be extracted file
103
- checkExtractedFile (vfs .toRealPath (Path .of ("file1" )), new String []{"text1" , "text2" });
104
- checkExtractedFile (vfs .toRealPath (Path .of (VFS_MOUNT_POINT + File .separator + "file1" )), new String []{"text1" , "text2" });
108
+ checkExtractedFile (fs .toRealPath (Path .of ("file1" )), new String []{"text1" , "text2" });
109
+ checkExtractedFile (fs .toRealPath (Path .of (VFS_MOUNT_POINT + File .separator + "file1" )), new String []{"text1" , "text2" });
105
110
}
106
111
107
112
@ Test
108
113
public void toAbsolutePath () throws Exception {
109
- VirtualFileSystem vfs = VirtualFileSystem .newBuilder ().//
114
+ FileSystem fs = VirtualFileSystem .newBuilder ().//
110
115
unixMountPoint (VFS_MOUNT_POINT ).//
111
116
windowsMountPoint (VFS_WIN_MOUNT_POINT ).//
112
117
extractFilter (p -> p .getFileName ().toString ().equals ("file1" ) || p .getFileName ().toString ().equals ("file2" )).//
113
118
resourceLoadingClass (VirtualFileSystemTest .class ).build ();
114
119
// check regular resource dir
115
- assertEquals (VFS_MOUNT_POINT + File .separator + "dir1" , vfs .toAbsolutePath (Path .of ("dir1" )).toString ());
116
- assertEquals (VFS_MOUNT_POINT + File .separator + "dir1" , vfs .toAbsolutePath (Path .of (VFS_MOUNT_POINT + File .separator + "dir1" )).toString ());
120
+ assertEquals (VFS_MOUNT_POINT + File .separator + "dir1" , fs .toAbsolutePath (Path .of ("dir1" )).toString ());
121
+ assertEquals (VFS_MOUNT_POINT + File .separator + "dir1" , fs .toAbsolutePath (Path .of (VFS_MOUNT_POINT + File .separator + "dir1" )).toString ());
117
122
// check regular resource file
118
- assertEquals (VFS_MOUNT_POINT + File .separator + "SomeFile" , vfs .toAbsolutePath (Path .of ("SomeFile" )).toString ());
119
- assertEquals (VFS_MOUNT_POINT + File .separator + "SomeFile" , vfs .toAbsolutePath (Path .of (VFS_MOUNT_POINT + File .separator + "SomeFile" )).toString ());
123
+ assertEquals (VFS_MOUNT_POINT + File .separator + "SomeFile" , fs .toAbsolutePath (Path .of ("SomeFile" )).toString ());
124
+ assertEquals (VFS_MOUNT_POINT + File .separator + "SomeFile" , fs .toAbsolutePath (Path .of (VFS_MOUNT_POINT + File .separator + "SomeFile" )).toString ());
120
125
// check to be extracted file
121
- checkExtractedFile (vfs .toAbsolutePath (Path .of ("file1" )), new String []{"text1" , "text2" });
122
- checkExtractedFile (vfs .toAbsolutePath (Path .of (VFS_MOUNT_POINT + File .separator + "file1" )), new String []{"text1" , "text2" });
123
- checkExtractedFile (vfs .toAbsolutePath (Path .of ("dir1/file2" )), null );
124
- checkExtractedFile (vfs .toAbsolutePath (Path .of (VFS_MOUNT_POINT + File .separator + "dir1/file2" )), null );
126
+ checkExtractedFile (fs .toAbsolutePath (Path .of ("file1" )), new String []{"text1" , "text2" });
127
+ checkExtractedFile (fs .toAbsolutePath (Path .of (VFS_MOUNT_POINT + File .separator + "file1" )), new String []{"text1" , "text2" });
128
+ checkExtractedFile (fs .toAbsolutePath (Path .of ("dir1/file2" )), null );
129
+ checkExtractedFile (fs .toAbsolutePath (Path .of (VFS_MOUNT_POINT + File .separator + "dir1/file2" )), null );
125
130
}
126
131
127
132
@ Test
128
133
public void libsExtract () throws Exception {
129
- VirtualFileSystem vfs = VirtualFileSystem .newBuilder ().//
134
+ FileSystem fs = VirtualFileSystem .newBuilder ().//
130
135
unixMountPoint (VFS_MOUNT_POINT ).//
131
136
windowsMountPoint (VFS_WIN_MOUNT_POINT ).//
132
137
extractFilter (p -> p .getFileName ().toString ().endsWith (".tso" )).//
133
138
resourceLoadingClass (VirtualFileSystemTest .class ).build ();
134
- Path p = vfs .toAbsolutePath (Path .of ("site-packages/testpkg/file.tso" ));
139
+ Path p = fs .toAbsolutePath (Path .of ("site-packages/testpkg/file.tso" ));
135
140
checkExtractedFile (p , null );
136
141
Path extractedRoot = p .getParent ().getParent ().getParent ();
142
+
137
143
checkExtractedFile (extractedRoot .resolve ("site-packages/testpkg.libs/file1.tso" ), null );
138
144
checkExtractedFile (extractedRoot .resolve ("site-packages/testpkg.libs/file2.tso" ), null );
139
145
checkExtractedFile (extractedRoot .resolve ("site-packages/testpkg.libs/dir/file1.tso" ), null );
@@ -142,7 +148,7 @@ public void libsExtract() throws Exception {
142
148
checkExtractedFile (extractedRoot .resolve ("site-packages/testpkg.libs/dir/dir/file1.tso" ), null );
143
149
checkExtractedFile (extractedRoot .resolve ("site-packages/testpkg.libs/dir/dir/file2.tso" ), null );
144
150
145
- p = vfs .toAbsolutePath (Path .of ("site-packages/testpkg-nolibs/file.tso" ));
151
+ p = fs .toAbsolutePath (Path .of ("site-packages/testpkg-nolibs/file.tso" ));
146
152
checkExtractedFile (p , null );
147
153
}
148
154
@@ -433,24 +439,25 @@ public Context getContext(Function<VirtualFileSystem.Builder, VirtualFileSystem.
433
439
VirtualFileSystem .Builder builder = VirtualFileSystem .newBuilder ().//
434
440
unixMountPoint (VFS_MOUNT_POINT ).//
435
441
windowsMountPoint (VFS_WIN_MOUNT_POINT ).//
442
+ extractFilter (p -> p .getFileName ().toString ().endsWith (".tso" )).//
436
443
resourceLoadingClass (VirtualFileSystemTest .class );
437
444
if (builderFunction != null ) {
438
445
builder = builderFunction .apply (builder );
439
446
}
440
- VirtualFileSystem vfs = builder .build ();
441
- Context context = VirtualFileSystem .contextBuilder (vfs ).build ();
447
+ VirtualFileSystem fs = builder .build ();
448
+ Context context = GraalPyResources .contextBuilder (fs ).build ();
442
449
if (builderFunction == null ) {
443
450
cachedContext = context ;
444
451
}
445
452
return context ;
446
453
}
447
454
448
455
@ Test
449
- public void builderTest () {
450
- Context context = VirtualFileSystem .contextBuilder ().allowAllAccess (true ).allowHostAccess (HostAccess .ALL ).build ();
456
+ public void vfsBuilderTest () {
457
+ Context context = GraalPyResources .contextBuilder ().allowAllAccess (true ).allowHostAccess (HostAccess .ALL ).build ();
451
458
context .eval (PYTHON , "import java; java.type('java.lang.String')" );
452
459
453
- context = VirtualFileSystem .contextBuilder ().allowAllAccess (false ).allowHostAccess (HostAccess .NONE ).build ();
460
+ context = GraalPyResources .contextBuilder ().allowAllAccess (false ).allowHostAccess (HostAccess .NONE ).build ();
454
461
context .eval (PYTHON , """
455
462
import java
456
463
try:
@@ -461,17 +468,17 @@ public void builderTest() {
461
468
assert False, 'expected NotImplementedError'
462
469
""" );
463
470
464
- VirtualFileSystem vfs = VirtualFileSystem .newBuilder ().//
471
+ VirtualFileSystem fs = VirtualFileSystem .newBuilder ().//
465
472
unixMountPoint (VFS_MOUNT_POINT ).//
466
473
windowsMountPoint (VFS_WIN_MOUNT_POINT ).//
467
474
resourceLoadingClass (VirtualFileSystemTest .class ).build ();
468
- context = VirtualFileSystem .contextBuilder (vfs ).build ();
475
+ context = GraalPyResources .contextBuilder (fs ).build ();
469
476
context .eval (PYTHON , patchMountPoint ("from os import listdir; listdir('/test_mount_point')" ));
470
477
471
- context = VirtualFileSystem . contextBuilder (). build ();
478
+ context = GraalPyResources . createContext ();
472
479
context .eval (PYTHON , "from os import listdir; listdir('.')" );
473
480
474
- context = VirtualFileSystem .contextBuilder ().allowIO (IOAccess .NONE ).build ();
481
+ context = GraalPyResources .contextBuilder ().allowIO (IOAccess .NONE ).build ();
475
482
boolean gotPE = false ;
476
483
try {
477
484
context .eval (PYTHON , "from os import listdir; listdir('.')" );
@@ -480,4 +487,36 @@ public void builderTest() {
480
487
}
481
488
assert gotPE : "expected PolyglotException" ;
482
489
}
490
+
491
+ @ Test
492
+ public void externalResourcesBuilderTest () throws IOException {
493
+ VirtualFileSystem fs = VirtualFileSystem .newBuilder ().resourceLoadingClass (VirtualFileSystemTest .class ).build ();
494
+ Path resourcesDir = Files .createTempDirectory ("vfs-test-resources" );
495
+
496
+ // extract VFS
497
+ GraalPyResources .extractVirtualFileSystemResources (fs , resourcesDir );
498
+
499
+ // check extracted contents
500
+ InputStream stream = VirtualFileSystemTest .class .getResourceAsStream ("/org.graalvm.python.vfs/fileslist.txt" );
501
+ BufferedReader br = new BufferedReader (new InputStreamReader (stream ));
502
+ String line ;
503
+ while ((line = br .readLine ()) != null ) {
504
+ line = line .substring ("/org.graalvm.python.vfs/" .length ());
505
+ if (line .length () == 0 ) {
506
+ continue ;
507
+ }
508
+ Path extractedFile = resourcesDir .resolve (line );
509
+ assert Files .exists (extractedFile );
510
+ if (line .endsWith ("/" )) {
511
+ assert Files .isDirectory (extractedFile );
512
+ }
513
+ }
514
+ checkExtractedFile (resourcesDir .resolve (Path .of ("file1" )), new String []{"text1" , "text2" });
515
+
516
+ // create context with extracted resource dir and check if we can see the extracted file
517
+ try (Context context = GraalPyResources .contextBuilder (resourcesDir ).build ()) {
518
+ context .eval ("python" , "import os; assert os.path.exists('" + resourcesDir .resolve ("file1" ).toString ().replace ("\\ " , "\\ \\ " ) + "')" );
519
+ }
520
+ }
521
+
483
522
}
0 commit comments