Skip to content

Commit 90ba66f

Browse files
committed
[GR-66484] Add a file system test that exercises more exception types.
PullRequest: graal/21238
2 parents 9ac6c5e + b195652 commit 90ba66f

File tree

1 file changed

+65
-0
lines changed
  • truffle/src/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot

1 file changed

+65
-0
lines changed

truffle/src/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/polyglot/TruffleFileTest.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import java.nio.file.AccessMode;
6969
import java.nio.file.CopyOption;
7070
import java.nio.file.DirectoryStream;
71+
import java.nio.file.FileAlreadyExistsException;
7172
import java.nio.file.FileStore;
7273
import java.nio.file.FileSystems;
7374
import java.nio.file.FileVisitOption;
@@ -76,6 +77,8 @@
7677
import java.nio.file.Files;
7778
import java.nio.file.LinkOption;
7879
import java.nio.file.NoSuchFileException;
80+
import java.nio.file.NotDirectoryException;
81+
import java.nio.file.NotLinkException;
7982
import java.nio.file.OpenOption;
8083
import java.nio.file.Path;
8184
import java.nio.file.Paths;
@@ -1221,6 +1224,59 @@ private static void assertInTolerance(long expected, long actual) {
12211224
}
12221225
}
12231226

1227+
@Test
1228+
public void testCorrectExceptions() {
1229+
IOAccess ioAccess = IOAccess.newBuilder().fileSystem(FileSystem.newDefaultFileSystem()).build();
1230+
try (Context context = Context.newBuilder().allowIO(ioAccess).build()) {
1231+
AbstractExecutableTestLanguage.evalTestLanguage(context, TestCorrectExceptions.class, "");
1232+
}
1233+
}
1234+
1235+
@Registration
1236+
static final class TestCorrectExceptions extends AbstractExecutableTestLanguage {
1237+
1238+
@Override
1239+
@TruffleBoundary
1240+
protected Object execute(RootNode node, Env env, Object[] contextArguments, Object[] frameArguments) throws Exception {
1241+
TruffleFile tmp = env.createTempDirectory(null, "exceptions-test");
1242+
try {
1243+
TruffleFile folder = tmp.resolve("folder");
1244+
folder.createDirectories();
1245+
TruffleFile file = tmp.resolve("file");
1246+
file.createFile();
1247+
TruffleFile nonExistent = tmp.resolve("non-existent");
1248+
boolean linkSupported;
1249+
try {
1250+
tmp.resolve("link").createSymbolicLink(file);
1251+
linkSupported = true;
1252+
} catch (UnsupportedOperationException unsupported) {
1253+
linkSupported = false;
1254+
}
1255+
assertFails(() -> {
1256+
file.createFile();
1257+
return true;
1258+
}, FileAlreadyExistsException.class);
1259+
assertFails(() -> {
1260+
nonExistent.delete();
1261+
return true;
1262+
}, NoSuchFileException.class);
1263+
assertFails(() -> {
1264+
file.list();
1265+
return true;
1266+
}, NotDirectoryException.class);
1267+
if (linkSupported) {
1268+
assertFails(() -> {
1269+
file.readSymbolicLink();
1270+
return true;
1271+
}, NotLinkException.class);
1272+
}
1273+
} finally {
1274+
delete(tmp);
1275+
}
1276+
return null;
1277+
}
1278+
}
1279+
12241280
private static void delete(Path path) throws IOException {
12251281
if (Files.isDirectory(path)) {
12261282
try (DirectoryStream<Path> dir = Files.newDirectoryStream(path)) {
@@ -1232,6 +1288,15 @@ private static void delete(Path path) throws IOException {
12321288
Files.delete(path);
12331289
}
12341290

1291+
private static void delete(TruffleFile file) throws IOException {
1292+
if (file.isDirectory()) {
1293+
for (TruffleFile child : file.list()) {
1294+
delete(child);
1295+
}
1296+
}
1297+
file.delete();
1298+
}
1299+
12351300
private static Type erase(Type type) {
12361301
if (type instanceof ParameterizedType) {
12371302
return ((ParameterizedType) type).getRawType();

0 commit comments

Comments
 (0)