65
65
import java .nio .file .attribute .FileAttribute ;
66
66
import java .nio .file .attribute .FileTime ;
67
67
import java .util .ArrayList ;
68
+ import java .util .Collections ;
68
69
import java .util .HashSet ;
69
70
import java .util .Iterator ;
70
71
import java .util .List ;
@@ -142,7 +143,8 @@ public void toRealPath() throws Exception {
142
143
}
143
144
144
145
// from real FS
145
- final Path realFSPath = Files .createTempDirectory ("graalpy.vfs.test" ).resolve ("extractme" );
146
+ final Path realFSDir = Files .createTempDirectory ("graalpy.vfs.test" );
147
+ final Path realFSPath = realFSDir .resolve ("extractme" );
146
148
realFSPath .toFile ().createNewFile ();
147
149
for (FileSystem fs : new FileSystem []{rwHostIOVFS , rHostIOVFS }) {
148
150
assertTrue (Files .isSameFile (realFSPath , fs .toRealPath (Path .of (realFSPath .getParent ().toString () + "/../" + realFSPath .getParent ().getFileName ().toString () + "/extractme" ))));
@@ -151,6 +153,7 @@ public void toRealPath() throws Exception {
151
153
withCWD (fs , VFS_ROOT_PATH , (fst ) -> assertTrue (Files .isSameFile (realFSPath , fst .toRealPath (Path .of ("../" + realFSPath .toString ())))));
152
154
}
153
155
checkException (SecurityException .class , () -> noHostIOVFS .toRealPath (realFSPath ), "expected error for no host io fs" );
156
+ assertEquals (VFS_ROOT_PATH .resolve ("dir1" ), rwHostIOVFS .toRealPath (Path .of (realFSDir .toString () + "/.." .repeat (realFSDir .getNameCount ()) + VFS_ROOT ).resolve ("dir1" )));
154
157
}
155
158
156
159
private void toRealPathVFS (FileSystem fs , String pathPrefix ) throws IOException {
@@ -165,7 +168,7 @@ private void toRealPathVFS(FileSystem fs, String pathPrefix) throws IOException
165
168
166
169
@ Test
167
170
public void toAbsolutePath () throws Exception {
168
- // from VFS
171
+ // VFS
169
172
for (FileSystem fs : new FileSystem []{rwHostIOVFS , rHostIOVFS , noHostIOVFS }) {
170
173
checkException (NullPointerException .class , () -> fs .toAbsolutePath (null ));
171
174
@@ -193,15 +196,59 @@ public void toAbsolutePath() throws Exception {
193
196
});
194
197
}
195
198
196
- // from real FS
197
- Path realFSPath = Files .createTempDirectory ("graalpy.vfs.test" ).resolve ("extractme" );
199
+ // real FS
200
+ Path realFSDir = Files .createTempDirectory ("graalpy.vfs.test" );
201
+ Path realFSDir2 = realFSDir .resolve ("dir" );
202
+ Files .createDirectories (realFSDir2 );
203
+ Path realFSPath = realFSDir .resolve ("extractme" );
198
204
realFSPath .toFile ().createNewFile ();
199
205
for (FileSystem fs : new FileSystem []{rwHostIOVFS , rHostIOVFS }) {
200
206
assertEquals (Path .of ("extractme" ).toAbsolutePath (), fs .toAbsolutePath (Path .of ("extractme" )));
207
+
208
+ // absolute path starting with VFS, pointing to real FS
209
+ // /VFS_ROOT/../real/fs/path/
210
+ assertEquals (realFSPath , fs .toAbsolutePath (Path .of (VFS_ROOT + ".." + realFSPath .toString ())));
211
+ // absolute path starting with real FS, pointing to VFS
212
+ // /real/fs/path/../../../VFS_ROOT
213
+ assertEquals (VFS_ROOT_PATH , fs .toAbsolutePath (Path .of (realFSDir .toString () + "/.." .repeat (realFSDir .getNameCount ()) + VFS_ROOT )));
214
+
215
+ // no CWD set, so relative path starting in real FS, pointing to VFS
216
+ // ../../../VFS_ROOT
217
+ Path defaultCWD = Path .of ("." ).toAbsolutePath ();
218
+ assertEquals (VFS_ROOT_PATH , fs .toAbsolutePath (Path .of ("../" .repeat (defaultCWD .getNameCount ()) + VFS_ROOT )));
219
+ // ../../../VFS_ROOT/../real/fs/path
220
+ assertEquals (realFSPath , fs .toAbsolutePath (Path .of ("../" .repeat (defaultCWD .getNameCount ()) + VFS_ROOT + ".." + realFSPath .toString ())));
221
+
222
+ // CWD is VFS_ROOT, relative path pointing to real FS
223
+ // ../real/fs/path
224
+ withCWD (fs , VFS_ROOT_PATH , (fst ) -> assertEquals (realFSPath , fst .toAbsolutePath (Path .of ("../" + realFSPath .toString ()))));
225
+ // CWD is VFS_ROOT, relative path pointing through real FS back to VFS
226
+ // ../real/fs/path/../../../VFS_ROOT_PATH
227
+ withCWD (fs , VFS_ROOT_PATH , (fst ) -> assertEquals (VFS_ROOT_PATH , fst .toAbsolutePath (Path .of ("../some/path/../../" + VFS_ROOT ))));
228
+ // CWD is real FS, relative path pointing to VFS
229
+ // real/fs/path/../../
230
+ withCWD (fs , realFSPath .getParent (), (fst ) -> assertEquals (VFS_ROOT_PATH , fst .toAbsolutePath (Path .of ("dir/" + "../" .repeat (realFSDir2 .getNameCount ()) + VFS_ROOT ))));
231
+ // CWD is real FS, relative path pointing through VFS to real FS
232
+ // real/fs/path/../../../VFS
233
+ withCWD (fs , realFSPath .getParent (),
234
+ (fst ) -> assertEquals (realFSPath , fst .toAbsolutePath (Path .of ("dir/" + "../" .repeat (realFSDir2 .getNameCount ()) + VFS_ROOT + "../" + realFSPath .toString ()))));
235
+ assertEquals (Path .of ("extractme" ).toAbsolutePath (), fs .toAbsolutePath (Path .of ("extractme" )));
236
+
201
237
withCWD (fs , realFSPath .getParent (), (fst ) -> assertEquals (realFSPath , fst .toAbsolutePath (realFSPath .getFileName ())));
238
+
202
239
}
240
+
203
241
checkException (SecurityException .class , () -> noHostIOVFS .toAbsolutePath (realFSPath ));
204
242
assertEquals (Path .of (VFS_SRC + "extractme" ), noHostIOVFS .toAbsolutePath (realFSPath .getFileName ()));
243
+
244
+ // absolute path starting with real FS, pointing to VFS
245
+ // /real/fs/path/../../../VFS_ROOT
246
+ assertEquals (VFS_ROOT_PATH , noHostIOVFS .toAbsolutePath (Path .of (realFSDir .toString () + "/.." .repeat (realFSDir .getNameCount ()) + VFS_ROOT )));
247
+
248
+ // no CWD set, relative path starting in real FS, pointing to VFS
249
+ // ../../../VFS_ROOT
250
+ Path defaultCWD = Path .of ("." ).toAbsolutePath ();
251
+ assertEquals (VFS_ROOT_PATH , noHostIOVFS .toAbsolutePath (Path .of ("../" .repeat (defaultCWD .getNameCount ()) + VFS_ROOT )));
205
252
}
206
253
207
254
@ Test
@@ -608,10 +655,7 @@ private static void checkException(Class<?> exType, ExceptionCall c, String msg)
608
655
}
609
656
610
657
@ Test
611
- public void setCurrentWorkingDirectory () throws Exception {
612
-
613
- // XXX test with not normalized paths
614
-
658
+ public void currentWorkingDirectory () throws Exception {
615
659
Path realFSDir = Files .createTempDirectory ("graalpy.vfs.test" );
616
660
Path realFSFile = realFSDir .resolve ("extractme" );
617
661
Files .createFile (realFSFile );
@@ -648,6 +692,12 @@ public void setCurrentWorkingDirectory() throws Exception {
648
692
649
693
fs .setCurrentWorkingDirectory (realFSDir );
650
694
assertEquals (realFSDir , fs .toAbsolutePath (Path .of ("dir" )).getParent ());
695
+
696
+ fs .setCurrentWorkingDirectory (Path .of (VFS_ROOT + "../" + realFSDir .toString ()));
697
+ assertEquals (realFSDir , fs .toAbsolutePath (Path .of ("dir" )).getParent ());
698
+
699
+ fs .setCurrentWorkingDirectory (Path .of (realFSDir .toString () + "/.." .repeat (realFSDir .getNameCount ()) + VFS_ROOT ));
700
+ assertEquals (VFS_ROOT_PATH .resolve ("dir1" ), fs .toAbsolutePath (Path .of ("dir1" )));
651
701
} finally {
652
702
resetCWD (fs );
653
703
}
@@ -721,7 +771,7 @@ public void createLink() throws Exception {
721
771
for (FileSystem fs : new FileSystem []{rwHostIOVFS , rHostIOVFS , noHostIOVFS }) {
722
772
checkException (NullPointerException .class , () -> fs .createLink (null , null ));
723
773
checkException (NullPointerException .class , () -> fs .createLink (VFS_ROOT_PATH , null ));
724
- checkException (SecurityException .class , () -> fs .createLink (VFS_ROOT_PATH .resolve ("/ link1" ), realFSPath ));
774
+ checkException (SecurityException .class , () -> fs .createLink (VFS_ROOT_PATH .resolve ("link1" ), realFSPath ));
725
775
checkException (SecurityException .class , () -> fs .createLink (realFSPath .getParent ().resolve ("link2" ), VFS_ROOT_PATH ));
726
776
checkException (SecurityException .class , () -> fs .createLink (VFS_ROOT_PATH , VFS_ROOT_PATH .resolve ("link" )));
727
777
}
@@ -746,8 +796,8 @@ public void createAndReadSymbolicLink() throws Exception {
746
796
checkException (NullPointerException .class , () -> fs .createSymbolicLink (null , null ));
747
797
checkException (NullPointerException .class , () -> fs .readSymbolicLink (null ));
748
798
749
- checkException (SecurityException .class , () -> fs .createSymbolicLink (VFS_ROOT_PATH .resolve ("/ link1" ), realFSPath ));
750
- checkException (SecurityException .class , () -> fs .readSymbolicLink (VFS_ROOT_PATH .resolve ("/ link1" )));
799
+ checkException (SecurityException .class , () -> fs .createSymbolicLink (VFS_ROOT_PATH .resolve ("link1" ), realFSPath ));
800
+ checkException (SecurityException .class , () -> fs .readSymbolicLink (VFS_ROOT_PATH .resolve ("link1" )));
751
801
checkException (SecurityException .class , () -> fs .createSymbolicLink (realFSPath .getParent ().resolve ("link2" ), VFS_ROOT_PATH ));
752
802
checkException (SecurityException .class , () -> fs .createSymbolicLink (VFS_ROOT_PATH , VFS_ROOT_PATH .resolve ("link" )));
753
803
}
0 commit comments