1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-12 03:00:30 +00:00

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.
This commit is contained in:
hydraz 2019-05-25 05:02:42 -03:00 committed by SquidDev
parent d5ea22d1a0
commit 5b0ce7410d
5 changed files with 73 additions and 19 deletions

View File

@ -1,16 +1,17 @@
local args = table.pack(...)
local tArgs = { ... }
if #tArgs < 1 then
print( "Usage: rm <path>" )
if args.n < 1 then
print("Usage: rm <paths>")
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

View File

@ -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 )

View File

@ -14,8 +14,8 @@ import dan200.computercraft.api.lua.LuaException;
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 org.opentest4j.AssertionFailedError;
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<String> 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()

View File

@ -120,7 +120,7 @@ public class MemoryMount implements IWritableMount
{
for( String file : this.files.keySet() )
{
if( file.startsWith( path ) ) files.add( file );
if( file.startsWith( path ) ) files.add( file.substring( path.length() + 1 ) );
}
}

View File

@ -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)