Skip to content

Commit 84e1d84

Browse files
committed
properly reset cwd in VFS tests
1 parent c22707f commit 84e1d84

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

graalpython/com.oracle.graal.python.test/src/org/graalvm/python/embedding/utils/test/VirtualFileSystemTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ public void currentWorkingDirectory() throws Exception {
681681

682682
checkException(IllegalArgumentException.class, () -> fs.setCurrentWorkingDirectory(VFS_ROOT_PATH.resolve("file1")));
683683

684+
Object oldCwd = getCwd(fs);
684685
try {
685686
Path nonExistingDir = VFS_ROOT_PATH.resolve("does-not-exist");
686687
fs.setCurrentWorkingDirectory(nonExistingDir);
@@ -710,7 +711,7 @@ public void currentWorkingDirectory() throws Exception {
710711
assertEquals(realFSDir, fs.toAbsolutePath(Path.of("dir")).getParent());
711712
}
712713
} finally {
713-
resetCWD(fs);
714+
resetCWD(fs, oldCwd);
714715
}
715716
}
716717
}
@@ -950,22 +951,25 @@ private interface FSCall {
950951
}
951952

952953
private void withCWD(FileSystem fs, Path cwd, FSCall c) throws Exception {
954+
Object prevCwd = getCwd(fs);
953955
fs.setCurrentWorkingDirectory(cwd);
954956
try {
955957
c.call(fs);
956958
} finally {
957-
resetCWD(fs);
959+
resetCWD(fs, prevCwd);
958960
}
959961
}
960962

961-
private void resetCWD(FileSystem fs) throws NoSuchFieldException, IllegalAccessException {
963+
private Object getCwd(FileSystem fs) throws IllegalAccessException, NoSuchFieldException {
962964
Field f = fs.getClass().getDeclaredField("cwd");
963965
f.setAccessible(true);
964-
if (fs == noHostIOVFS) {
965-
f.set(fs, Path.of(VFS_MOUNT_POINT + File.separator + "src"));
966-
} else {
967-
f.set(fs, null);
968-
}
966+
return f.get(fs);
967+
}
968+
969+
private void resetCWD(FileSystem fs, Object oldCwd) throws NoSuchFieldException, IllegalAccessException {
970+
Field f = fs.getClass().getDeclaredField("cwd");
971+
f.setAccessible(true);
972+
f.set(fs, oldCwd);
969973
}
970974

971975
}

0 commit comments

Comments
 (0)