1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-16 18:19:55 +00:00
CC-Tweaked/projects/core/src/test/java/dan200/computercraft/core/filesystem/WritableFileMountTest.java
Jonathan Coates 367773e173
Some refactoring of mounts
- Separate FileMount into separate FileMount and WritableFileMount
   classes. This separates the (relatively simple) read-only code from
   the (soon to be even more complex) read/write code.

   It also allows you to create read-only mounts which don't bother with
   filesystem accounting, which is nice.

 - Make openForWrite/openForAppend always return a SeekableFileHandle.
   Appendable files still cannot be seeked within, but that check is now
   done on the FS side.

 - Refactor the various mount tests to live in test contract interfaces,
   allowing us to reuse them between mounts.

 - Clean up our error handling a little better. (Most) file-specific code
   has been moved to FileMount, and ArchiveMount-derived classes now
   throw correct path-localised exceptions.
2022-12-09 22:02:31 +00:00

68 lines
2.2 KiB
Java

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.filesystem;
import com.google.common.io.MoreFiles;
import com.google.common.io.RecursiveDeleteOption;
import dan200.computercraft.api.filesystem.WritableMount;
import dan200.computercraft.test.core.filesystem.WritableMountContract;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class WritableFileMountTest implements WritableMountContract {
private final List<Path> cleanup = new ArrayList<>();
@Override
public MountAccess createMount(long capacity) throws IOException {
var path = Files.createTempDirectory("cctweaked-test");
cleanup.add(path);
return new MountAccessImpl(path.resolve("mount"), capacity);
}
@AfterEach
public void cleanup() throws IOException {
for (var mount : cleanup) MoreFiles.deleteRecursively(mount, RecursiveDeleteOption.ALLOW_INSECURE);
}
private static final class MountAccessImpl implements MountAccess {
private final Path root;
private final long capacity;
private final WritableMount mount;
private MountAccessImpl(Path root, long capacity) {
this.root = root;
this.capacity = capacity;
mount = new WritableFileMount(root.toFile(), capacity);
}
@Override
public WritableMount mount() {
return mount;
}
@Override
public void makeReadOnly(String path) {
Assumptions.assumeTrue(root.resolve(path).toFile().setReadOnly(), "Change file to read-only");
}
@Override
public void ensuresExist() throws IOException {
Files.createDirectories(root);
}
@Override
public long computeRemainingSpace() {
return new WritableFileMount(root.toFile(), capacity).getRemainingSpace();
}
}
}