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

Merge branches 'copycheck' and 'renamefix'

This commit is contained in:
SquidDev 2019-06-13 08:03:35 +01:00
commit 138a2cf08f
4 changed files with 92 additions and 1 deletions

View File

@ -15,6 +15,10 @@ if #tFiles > 0 then
elseif #tFiles == 1 then
if fs.exists( sDest ) then
printError( "Destination exists" )
elseif fs.isReadOnly( sDest ) then
printError( "Destination is read-only" )
elseif fs.getFreeSpace( sDest ) < fs.getSize( sFile ) then
printError( "Not enough space" )
else
fs.copy( sFile, sDest )
end

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 copy program", function()
local function touch(file)
io.open(file, "w"):close()
end
it("copies a file", function()
touch("/test-files/copy/a.txt")
shell.run("copy /test-files/copy/a.txt /test-files/copy/b.txt")
expect(fs.exists("/test-files/copy/a.txt")):eq(true)
expect(fs.exists("/test-files/copy/b.txt")):eq(true)
end)
it("fails when copying a non-existent file", function()
expect(capture(stub, "copy nothing destination"))
:matches { ok = true, output = "", error = "No matching files\n" }
end)
it("fails when overwriting an existing file", function()
touch("/test-files/copy/c.txt")
expect(capture(stub, "copy /test-files/copy/c.txt /test-files/copy/c.txt"))
:matches { ok = true, output = "", error = "Destination exists\n" }
end)
it("fails when copying into read-only locations", function()
touch("/test-files/copy/d.txt")
expect(capture(stub, "copy /test-files/copy/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, "copy"))
:matches { ok = true, output = "Usage: cp <source> <destination>\n", error = "" }
end)
end)

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)