1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-25 06:33:23 +00:00

Improve rename.lua's argument validation

This commit is contained in:
JakobDev 2019-06-10 15:31:38 +02:00 committed by SquidDev
parent 309cbdb8be
commit f3798bfb63
2 changed files with 48 additions and 1 deletions

View File

@ -7,8 +7,15 @@ end
local sSource = shell.resolve( tArgs[1] )
local sDest = shell.resolve( tArgs[2] )
if fs.exists( sDest ) then
if not fs.exists( sSource ) then
printError( "No matching files" )
return
elseif fs.exists( sDest ) then
printError( "Destination exists" )
return
elseif fs.isReadOnly( sDest ) then
printError( "Destination is read-only" )
return
end
fs.move( sSource, sDest )

View File

@ -0,0 +1,40 @@
local capture = require "test_helpers".capture_program
describe("The rename program", function()
local function touch(file)
io.open(file, "w"):close()
end
it("can rename a file", function()
touch("/test-files/rename/a.txt")
shell.run("rename /test-files/rename/a.txt /test-files/rename/b.txt")
expect(fs.exists("/test-files/rename/a.txt")):eq(false)
expect(fs.exists("/test-files/rename/b.txt")):eq(true)
end)
it("fails when renaming a file which doesn't exist", function()
expect(capture(stub, "rename nothing destination"))
:matches { ok = true, output = "", error = "No matching files\n" }
end)
it("fails when overwriting an existing file", function()
touch("/test-files/rename/c.txt")
expect(capture(stub, "rename /test-files/rename/c.txt /test-files/rename/c.txt"))
:matches { ok = true, output = "", error = "Destination exists\n" }
end)
it("fails when copying to read-only locations", function()
touch("/test-files/rename/d.txt")
expect(capture(stub, "rename /test-files/rename/d.txt /rom/test.txt"))
:matches { ok = true, output = "", error = "Destination is read-only\n" }
end)
it("displays the usage when given no arguments", function()
expect(capture(stub, "rename"))
:matches { ok = true, output = "Usage: rename <source> <destination>\n", error = "" }
end)
end)