diff --git a/src/test/resources/test-rom/mcfly.lua b/src/test/resources/test-rom/mcfly.lua index 56a4430e8..02b4b26ed 100644 --- a/src/test/resources/test-rom/mcfly.lua +++ b/src/test/resources/test-rom/mcfly.lua @@ -34,10 +34,12 @@ local active_stubs = {} -- @tparam string var The variable to stub -- @param value The value to stub it with local function stub(tbl, var, value) - table.insert(active_stubs, { tbl = tbl, var = var, value = tbl[var] }) - _G[var] = value -end + check('stub', 1, 'table', tbl) + check('stub', 2, 'string', var) + table.insert(active_stubs, { tbl = tbl, var = var, value = tbl[var] }) + rawset(tbl, var, value) +end --- Capture the current global state of the computer local function push_state() @@ -55,7 +57,7 @@ end local function pop_state(state) for i = #active_stubs, 1, -1 do local stub = active_stubs[i] - stub.tbl[stub.var] = stub.value + rawset(stub.tbl, stub.var, stub.value) end active_stubs = state.stubs @@ -353,6 +355,9 @@ if not fs.isDir(root_dir) then error() end +-- Ensure the test folder is also on the package path +package.path = ("/%s/?.lua;/%s/?/init.lua;%s"):format(root_dir, root_dir, package.path) + do -- Load in the tests from all our files local env = setmetatable({}, { __index = _ENV }) diff --git a/src/test/resources/test-rom/spec/programs/delete_spec.lua b/src/test/resources/test-rom/spec/programs/delete_spec.lua index bf2433467..d7434e550 100644 --- a/src/test/resources/test-rom/spec/programs/delete_spec.lua +++ b/src/test/resources/test-rom/spec/programs/delete_spec.lua @@ -1,3 +1,5 @@ +local capture = require "test_helpers".capture_program + describe("The rm program", function() local function touch(file) io.open(file, "w"):close() @@ -32,4 +34,14 @@ describe("The rm program", function() expect(fs.exists("/test-files/a.txt")):eq(false) expect(fs.exists("/test-files/b.txt")):eq(false) end) + + it("displays the usage with no arguments", function() + expect(capture(stub, "rm")) + :matches { ok = true, output = "Usage: rm \n", error = "" } + end) + + it("errors when a glob fails to match", function() + expect(capture(stub, "rm", "never-existed")) + :matches { ok = true, output = "", error = "never-existed: No matching files\n" } + end) end) diff --git a/src/test/resources/test-rom/spec/test_helpers.lua b/src/test/resources/test-rom/spec/test_helpers.lua new file mode 100644 index 000000000..6ffe3942d --- /dev/null +++ b/src/test/resources/test-rom/spec/test_helpers.lua @@ -0,0 +1,46 @@ +--- Run a program and capture its output +-- +-- @tparam function(tbl:table, var:string, value:string) stub The active stub function. +-- @tparam string program The program name. +-- @tparam string ... Arguments to this program. +-- @treturn { ok = boolean, output = string, error = string, combined = string } +-- Whether this program terminated successfully, and the various output streams. +local function capture_program(stub, program, ...) + local output, error, combined = {}, {}, {} + + local function out(stream, msg) + table.insert(stream, msg) + table.insert(combined, msg) + end + + stub(_G, "print", function(...) + for i = 1, select('#', ...) do + if i > 1 then out(output, " ") end + out(output, tostring(select(i, ...))) + end + out(output, "\n") + end) + + stub(_G, "printError", function(...) + for i = 1, select('#', ...) do + if i > 1 then out(error, " ") end + out(error, tostring(select(i, ...))) + end + out(error, "\n") + end) + + stub(_G, "write", function(msg) out(output, tostring(msg)) end) + + local ok = shell.run(program, ...) + + return { + output = table.concat(output), + error = table.concat(error), + combined = table.concat(combined), + ok = ok + } +end + +return { + capture_program = capture_program, +}