mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 21:52:59 +00:00 
			
		
		
		
	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.
This commit is contained in:
		| @@ -8,7 +8,6 @@ package dan200.computercraft; | ||||
| import com.google.auto.service.AutoService; | ||||
| import com.mojang.authlib.GameProfile; | ||||
| import com.mojang.brigadier.arguments.ArgumentType; | ||||
| import dan200.computercraft.api.filesystem.Mount; | ||||
| import dan200.computercraft.api.network.wired.WiredElement; | ||||
| import dan200.computercraft.api.peripheral.IPeripheral; | ||||
| import dan200.computercraft.impl.AbstractComputerCraftAPI; | ||||
| @@ -221,12 +220,6 @@ public class TestPlatformHelper extends AbstractComputerCraftAPI implements Plat | ||||
|         return "1.0"; | ||||
|     } | ||||
| 
 | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public Mount createResourceMount(MinecraftServer server, String domain, String subPath) { | ||||
|         throw new UnsupportedOperationException("Cannot create resource mount"); | ||||
|     } | ||||
| 
 | ||||
|     @Nullable | ||||
|     @Override | ||||
|     public <T> T tryGetRegistryObject(ResourceKey<Registry<T>> registry, ResourceLocation id) { | ||||
|   | ||||
| @@ -5,73 +5,59 @@ | ||||
|  */ | ||||
| package dan200.computercraft.shared.computer.core; | ||||
| 
 | ||||
| import com.google.common.io.MoreFiles; | ||||
| import com.google.common.io.RecursiveDeleteOption; | ||||
| import dan200.computercraft.api.filesystem.Mount; | ||||
| import dan200.computercraft.test.core.CloseScope; | ||||
| import dan200.computercraft.test.core.filesystem.MountContract; | ||||
| import net.minecraft.Util; | ||||
| import net.minecraft.server.packs.PackType; | ||||
| import net.minecraft.server.packs.PathPackResources; | ||||
| import net.minecraft.server.packs.resources.ReloadableResourceManager; | ||||
| import net.minecraft.util.Unit; | ||||
| import org.junit.jupiter.api.BeforeEach; | ||||
| import org.junit.jupiter.api.Test; | ||||
| import org.junit.jupiter.api.AfterEach; | ||||
| 
 | ||||
| import java.io.IOException; | ||||
| import java.nio.file.Path; | ||||
| import java.util.ArrayList; | ||||
| import java.util.Arrays; | ||||
| import java.util.Comparator; | ||||
| import java.nio.file.Files; | ||||
| import java.util.List; | ||||
| import java.util.concurrent.CompletableFuture; | ||||
| import java.util.concurrent.ExecutionException; | ||||
| 
 | ||||
| import static org.junit.jupiter.api.Assertions.*; | ||||
| public class ResourceMountTest implements MountContract { | ||||
|     private final CloseScope toClose = new CloseScope(); | ||||
| 
 | ||||
| public class ResourceMountTest { | ||||
|     private Mount mount; | ||||
|     @Override | ||||
|     public Mount createSkeleton() throws IOException { | ||||
|         var path = Files.createTempDirectory("cctweaked-test"); | ||||
|         toClose.add(() -> MoreFiles.deleteRecursively(path, RecursiveDeleteOption.ALLOW_INSECURE)); | ||||
| 
 | ||||
|         Files.createDirectories(path.resolve("data/computercraft/rom/dir")); | ||||
|         try (var writer = Files.newBufferedWriter(path.resolve("data/computercraft/rom/dir/file.lua"))) { | ||||
|             writer.write("print('testing')"); | ||||
|         } | ||||
|         Files.newBufferedWriter(path.resolve("data/computercraft/rom/f.lua")).close(); | ||||
| 
 | ||||
|     @BeforeEach | ||||
|     public void before() { | ||||
|         var manager = new ReloadableResourceManager(PackType.SERVER_DATA); | ||||
|         var done = new CompletableFuture<Unit>(); | ||||
|         manager.createReload(Util.backgroundExecutor(), Util.backgroundExecutor(), done, List.of( | ||||
|             new PathPackResources("resources", Path.of("../core/src/main/resources"), false) | ||||
|         var reload = manager.createReload(Util.backgroundExecutor(), Util.backgroundExecutor(), CompletableFuture.completedFuture(Unit.INSTANCE), List.of( | ||||
|             new PathPackResources("resources", path, false) | ||||
|         )); | ||||
| 
 | ||||
|         mount = ResourceMount.get("computercraft", "lua/rom", manager); | ||||
|         try { | ||||
|             reload.done().get(); | ||||
|         } catch (InterruptedException | ExecutionException e) { | ||||
|             throw new RuntimeException("Failed to load resources", e); | ||||
|         } | ||||
| 
 | ||||
|         return new ResourceMount("computercraft", "rom", manager); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testList() throws IOException { | ||||
|         List<String> files = new ArrayList<>(); | ||||
|         mount.list("", files); | ||||
|         files.sort(Comparator.naturalOrder()); | ||||
| 
 | ||||
|         assertEquals( | ||||
|             Arrays.asList("apis", "autorun", "help", "modules", "motd.txt", "programs", "startup.lua"), | ||||
|             files | ||||
|         ); | ||||
|     @Override | ||||
|     public boolean hasFileTimes() { | ||||
|         return false; | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testExists() throws IOException { | ||||
|         assertTrue(mount.exists("")); | ||||
|         assertTrue(mount.exists("startup.lua")); | ||||
|         assertTrue(mount.exists("programs/fun/advanced/paint.lua")); | ||||
| 
 | ||||
|         assertFalse(mount.exists("programs/fun/advance/paint.lua")); | ||||
|         assertFalse(mount.exists("programs/fun/advanced/paint.lu")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testIsDir() throws IOException { | ||||
|         assertTrue(mount.isDirectory("")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testIsFile() throws IOException { | ||||
|         assertFalse(mount.isDirectory("startup.lua")); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testSize() throws IOException { | ||||
|         assertNotEquals(mount.getSize("startup.lua"), 0); | ||||
|     @AfterEach | ||||
|     public void after() throws Exception { | ||||
|         toClose.close(); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates