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

Add a few Checks (#248)

- Add some basic tests for several built-in programs.
 - Preserve the state of the shell between tests
This commit is contained in:
JakobDev 2019-06-14 09:15:12 +02:00 committed by SquidDev
parent 138a2cf08f
commit 717ab69093
12 changed files with 234 additions and 0 deletions

View File

@ -49,6 +49,8 @@ local function push_state()
term = term.current(),
input = io.input(),
output = io.output(),
dir = shell.dir(),
path = shell.path(),
stubs = stubs,
}
end
@ -65,6 +67,8 @@ local function pop_state(state)
term.redirect(state.term)
io.input(state.input)
io.output(state.output)
shell.setDir(state.dir)
shell.setPath(state.path)
end
local error_mt = { __tostring = function(self) return self.message end }

View File

@ -0,0 +1,20 @@
local capture = require "test_helpers".capture_program
describe("The cd program", function()
it("cd into a directory", function()
shell.run("cd /rom/programs")
expect(shell.dir()):eq("rom/programs")
end)
it("cd into a not existing directory", function()
expect(capture(stub, "cd /rom/nothing"))
:matches { ok = true, output = "Not a directory\n", error = "" }
end)
it("displays the usage with no arguments", function()
expect(capture(stub, "cd"))
:matches { ok = true, output = "Usage: cd <path>\n", error = "" }
end)
end)

View File

@ -0,0 +1,11 @@
local capture = require "test_helpers".capture_program
describe("The edit program", function()
it("displays its usage when given no argument", function()
multishell = nil
expect(capture(stub, "edit"))
:matches { ok = true, output = "Usage: edit <path>\n", error = "" }
end)
end)

View File

@ -0,0 +1,77 @@
local capture = require "test_helpers".capture_program
describe("The pastebin program", function()
local function setup_request()
stub(_G, "http", {
checkURL = function()
return true
end,
get = function()
return {
readAll = function()
return [[print("Hello", ...)]]
end,
close = function()
end,
getResponseHeaders = function()
local tHeader = {}
tHeader["Content-Type"] = "text/plain; charset=utf-8"
return tHeader
end
}
end,
post = function()
return {
readAll = function()
return "https://pastebin.com/abcde"
end,
close = function()
end,
}
end
})
end
it("downloads one file", function()
setup_request()
capture(stub, "pastebin", "get", "abcde", "testdown")
expect(fs.exists("/testdown")):eq(true)
end)
it("runs a program from the internet", function()
setup_request()
expect(capture(stub, "pastebin", "run", "abcde", "a", "b", "c"))
:matches { ok = true, output = "Connecting to pastebin.com... Success.\nHello a b c\n", error = "" }
end)
it("upload a program to pastebin", function()
setup_request()
local file = fs.open( "testup", "w" )
file.close()
expect(capture(stub, "pastebin", "put", "testup" ))
:matches { ok = true, output = "Connecting to pastebin.com... Success.\nUploaded as https://pastebin.com/abcde\nRun \"pastebin get abcde\" to download anywhere\n", error = "" }
end)
it("upload a not existing program to pastebin", function()
setup_request()
expect(capture(stub, "pastebin", "put", "nothing" ))
:matches { ok = true, output = "No such file\n", error = "" }
end)
it("displays its usage when given no arguments", function()
setup_request()
expect(capture(stub, "pastebin"))
:matches { ok = true, output = "Usages:\npastebin put <filename>\npastebin get <code> <filename>\npastebin run <code> <arguments>\n", error = "" }
end)
it("can be completed", function()
local complete = shell.getCompletionInfo()["rom/programs/http/pastebin.lua"].fnComplete
expect(complete(shell, 1, "", {})):same { "put ", "get ", "run " }
end)
end)

View File

@ -0,0 +1,11 @@
local capture = require "test_helpers".capture_program
describe("The id program", function()
it("displays computer id", function()
local id = os.getComputerID()
expect(capture(stub, "id"))
:matches { ok = true, output = "This is computer #"..id.."\n", error = "" }
end)
end)

View File

@ -0,0 +1,14 @@
local capture = require "test_helpers".capture_program
describe("The motd program", function()
it("displays MODT", function()
local file = fs.open("/modt_check.txt","w")
file.write("Hello World!")
file.close()
settings.set("motd.path","/modt_check.txt")
expect(capture(stub, "motd"))
:matches { ok = true, output = "Hello World!\n", error = "" }
end)
end)

View File

@ -0,0 +1,26 @@
local capture = require "test_helpers".capture_program
describe("The move program", function()
local function touch(file)
io.open(file, "w"):close()
end
it("move a file", function()
touch("/test-files/move/a.txt")
shell.run("move /test-files/move/a.txt /test-files/move/b.txt")
expect(fs.exists("/test-files/move/a.txt")):eq(false)
expect(fs.exists("/test-files/move/b.txt")):eq(true)
end)
it("try to move a not existing file", function()
expect(capture(stub, "move nothing destination"))
:matches { ok = true, output = "", error = "No matching files\n" }
end)
it("displays the usage with no arguments", function()
expect(capture(stub, "move"))
:matches { ok = true, output = "Usage: mv <source> <destination>\n", error = "" }
end)
end)

View File

@ -0,0 +1,29 @@
local capture = require "test_helpers".capture_program
describe("The set program", function()
it("displays all settings", function()
settings.clear()
settings.set("Test","Hello World!")
settings.set("123",456)
expect(capture(stub, "set"))
:matches { ok = true, output = '"123" is 456\n"Test" is "Hello World!"\n', error = "" }
end)
it("displays a single settings", function()
settings.clear()
settings.set("Test","Hello World!")
settings.set("123",456)
expect(capture(stub, "set Test"))
:matches { ok = true, output = '"Test" is "Hello World!"\n', error = "" }
end)
it("set a setting", function()
expect(capture(stub, "set Test Hello"))
:matches { ok = true, output = '"Test" set to "Hello"\n', error = "" }
expect(settings.get("Test")):eq("Hello")
end)
end)

View File

@ -27,6 +27,10 @@ describe("The shell", function()
shell.setDir(shell.dir())
expect.error(shell.setDir, nil):eq("bad argument #1 (expected string, got nil)")
end)
it("not existing directory", function()
expect.error(shell.setDir, "/rom/nothing"):eq("Not a directory")
end)
end)
describe("shell.setPath", function()

View File

@ -0,0 +1,12 @@
local capture = require "test_helpers".capture_program
describe("The time program", function()
it("displays time", function()
local time = textutils.formatTime(os.time())
local day = os.day()
expect(capture(stub, "time"))
:matches { ok = true, output = "The time is "..time.." on Day "..day.."\n", error = "" }
end)
end)

View File

@ -0,0 +1,26 @@
local capture = require "test_helpers".capture_program
describe("The type program", function()
it("displays the usage with no arguments", function()
expect(capture(stub, "type"))
:matches { ok = true, output = "Usage: type <path>\n", error = "" }
end)
it("displays the output for a file", function()
expect(capture(stub, "type /rom/startup.lua"))
:matches { ok = true, output = "file\n", error = "" }
end)
it("displays the output for a directory", function()
expect(capture(stub, "type /rom"))
:matches { ok = true, output = "directory\n", error = "" }
end)
it("displays the output for a not existing path", function()
expect(capture(stub, "type /rom/nothing"))
:matches { ok = true, output = "No such path\n", error = "" }
end)
end)