1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 07:03:22 +00:00
CC-Tweaked/projects/core/src/test/java/dan200/computercraft/core/filesystem/WritableFileMountTest.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
2.2 KiB
Java
Raw Normal View History

/*
* 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();
}
}
}