From 5b0ce7410d72d30a639cce90b017b9b1e6ceebcb Mon Sep 17 00:00:00 2001 From: hydraz Date: Sat, 25 May 2019 05:02:42 -0300 Subject: [PATCH] Make delete delete many files (#210) Actually, many *globs*. It additionally prints the glob if no files matched it, since that's clearer. Also move the ComputerTestDelegate's filesystem to be disk-based. This is what actual computers use, and the MemoryMount is a little broken. --- .../computercraft/lua/rom/programs/delete.lua | 21 +++++------ .../assets/computercraft/lua/rom/startup.lua | 5 ++- .../core/ComputerTestDelegate.java | 29 +++++++++++---- .../core/filesystem/MemoryMount.java | 2 +- .../test-rom/spec/programs/delete_spec.lua | 35 +++++++++++++++++++ 5 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 src/test/resources/test-rom/spec/programs/delete_spec.lua diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/delete.lua b/src/main/resources/assets/computercraft/lua/rom/programs/delete.lua index 388491e00..51e8d288c 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/delete.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/delete.lua @@ -1,16 +1,17 @@ +local args = table.pack(...) -local tArgs = { ... } -if #tArgs < 1 then - print( "Usage: rm " ) +if args.n < 1 then + print("Usage: rm ") return end -local sPath = shell.resolve( tArgs[1] ) -local tFiles = fs.find( sPath ) -if #tFiles > 0 then - for n,sFile in ipairs( tFiles ) do - fs.delete( sFile ) +for i = 1, args.n do + local files = fs.find(shell.resolve(args[i])) + if #files > 0 then + for n, file in ipairs(files) do + fs.delete(file) + end + else + printError(args[i] .. ": No matching files") end -else - printError( "No matching files" ) end diff --git a/src/main/resources/assets/computercraft/lua/rom/startup.lua b/src/main/resources/assets/computercraft/lua/rom/startup.lua index 9e508a952..37e7f0e10 100644 --- a/src/main/resources/assets/computercraft/lua/rom/startup.lua +++ b/src/main/resources/assets/computercraft/lua/rom/startup.lua @@ -76,6 +76,9 @@ local function completeEither( shell, nIndex, sText, tPreviousText ) return fs.complete( sText, shell.dir(), true, true ) end end +local function completeEitherMany( shell, nIndex, sText, tPreviousText ) + return fs.complete( sText, shell.dir(), true, true ) +end local function completeEitherEither( shell, nIndex, sText, tPreviousText ) if nIndex == 1 then local tResults = fs.complete( sText, shell.dir(), true, true ) @@ -180,7 +183,7 @@ end shell.setCompletionFunction( "rom/programs/alias.lua", completeAlias ) shell.setCompletionFunction( "rom/programs/cd.lua", completeDir ) shell.setCompletionFunction( "rom/programs/copy.lua", completeEitherEither ) -shell.setCompletionFunction( "rom/programs/delete.lua", completeEither ) +shell.setCompletionFunction( "rom/programs/delete.lua", completeEitherMany ) shell.setCompletionFunction( "rom/programs/drive.lua", completeDir ) shell.setCompletionFunction( "rom/programs/edit.lua", completeFile ) shell.setCompletionFunction( "rom/programs/eject.lua", completePeripheral ) diff --git a/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java b/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java index 5b6828a3e..73d34df3e 100644 --- a/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java +++ b/src/test/java/dan200/computercraft/core/ComputerTestDelegate.java @@ -14,8 +14,8 @@ import dan200.computercraft.core.computer.BasicEnvironment; import dan200.computercraft.core.computer.Computer; import dan200.computercraft.core.computer.MainThread; +import dan200.computercraft.core.filesystem.FileMount; import dan200.computercraft.core.filesystem.FileSystemException; -import dan200.computercraft.core.filesystem.MemoryMount; import dan200.computercraft.core.terminal.Terminal; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -25,9 +25,13 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; +import java.io.File; +import java.io.IOException; +import java.io.Writer; +import java.nio.channels.Channels; +import java.nio.channels.WritableByteChannel; +import java.nio.charset.StandardCharsets; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; @@ -70,14 +74,25 @@ public class ComputerTestDelegate private boolean finished = false; @BeforeEach - public void before() + public void before() throws IOException { ComputerCraft.logPeripheralErrors = true; ComputerCraft.log = LogManager.getLogger( ComputerCraft.MOD_ID ); Terminal term = new Terminal( 78, 20 ); - IWritableMount mount = new MemoryMount() - .addFile( "startup.lua", "loadfile('test/mcfly.lua', _ENV)('test/spec') cct_test.finish()" ); + IWritableMount mount = new FileMount( new File( "test-files/mount" ), Long.MAX_VALUE ); + + // Remove any existing files + List children = new ArrayList<>(); + mount.list( "", children ); + for( String child : children ) mount.delete( child ); + + // And add our startup file + try( WritableByteChannel channel = mount.openChannelForWrite( "startup.lua" ); + Writer writer = Channels.newWriter( channel, StandardCharsets.UTF_8.newEncoder(), -1 ) ) + { + writer.write( "loadfile('test/mcfly.lua', _ENV)('test/spec') cct_test.finish()" ); + } computer = new Computer( new BasicEnvironment( mount ), term, 0 ); computer.addApi( new ILuaAPI() diff --git a/src/test/java/dan200/computercraft/core/filesystem/MemoryMount.java b/src/test/java/dan200/computercraft/core/filesystem/MemoryMount.java index da4e733c3..60070a4d0 100644 --- a/src/test/java/dan200/computercraft/core/filesystem/MemoryMount.java +++ b/src/test/java/dan200/computercraft/core/filesystem/MemoryMount.java @@ -120,7 +120,7 @@ public void list( @Nonnull String path, @Nonnull List files ) { for( String file : this.files.keySet() ) { - if( file.startsWith( path ) ) files.add( file ); + if( file.startsWith( path ) ) files.add( file.substring( path.length() + 1 ) ); } } diff --git a/src/test/resources/test-rom/spec/programs/delete_spec.lua b/src/test/resources/test-rom/spec/programs/delete_spec.lua new file mode 100644 index 000000000..bf2433467 --- /dev/null +++ b/src/test/resources/test-rom/spec/programs/delete_spec.lua @@ -0,0 +1,35 @@ +describe("The rm program", function() + local function touch(file) + io.open(file, "w"):close() + end + + it("deletes one file", function() + touch("/test-files/a.txt") + + shell.run("rm /test-files/a.txt") + + expect(fs.exists("/test-files/a.txt")):eq(false) + end) + + it("deletes many files", function() + touch("/test-files/a.txt") + touch("/test-files/b.txt") + touch("/test-files/c.txt") + + shell.run("rm /test-files/a.txt /test-files/b.txt") + + expect(fs.exists("/test-files/a.txt")):eq(false) + expect(fs.exists("/test-files/b.txt")):eq(false) + expect(fs.exists("/test-files/c.txt")):eq(true) + end) + + it("deletes a glob", function() + touch("/test-files/a.txt") + touch("/test-files/b.txt") + + shell.run("rm /test-files/*.txt") + + expect(fs.exists("/test-files/a.txt")):eq(false) + expect(fs.exists("/test-files/b.txt")):eq(false) + end) +end)