cc-libs/tests/startup-servers.lua

92 lines
2.2 KiB
Lua

local createLibTest = require('/apis/libtest');
local testlib = createLibTest({ ... });
local function runStartupWithStubs(settingsValues)
settingsValues = settingsValues or {};
local calls = {
rebooted = false,
shutdown = false,
shellRuns = {},
prints = {},
};
local env = setmetatable({
require = require,
fs = {
exists = function(path)
return path ~= '/trapos/manifest.json' and fs.exists(path);
end,
},
os = {
reboot = function()
calls.rebooted = true;
end,
shutdown = function()
calls.shutdown = true;
end,
sleep = function() end,
},
settings = {
get = function(key)
return settingsValues[key];
end,
},
parallel = {
waitForAny = function(firstFn)
firstFn();
end,
},
print = function(message)
calls.prints[#calls.prints + 1] = tostring(message);
end,
shell = {
path = function()
return '/rom/programs';
end,
run = function(program)
calls.shellRuns[#calls.shellRuns + 1] = program;
return true;
end,
setPath = function() end,
},
}, { __index = _G });
env._G = env;
local chunk, loadErr = loadfile('/startup/servers.lua', 't', env);
if not chunk then
error(loadErr, 0);
end
local ok, err = pcall(chunk);
if not ok then
error(err, 0);
end
return calls;
end
testlib.test('shell exit shuts down by default', function()
local calls = runStartupWithStubs();
testlib.assertEquals(calls.shellRuns[1], 'shell');
testlib.assertEquals(calls.shutdown, true);
testlib.assertEquals(calls.rebooted, false);
for _, message in ipairs(calls.prints) do
testlib.assertTrue(
not string.find(message, 'Servers stopped', 1, true),
'startup should not print a forced reboot message'
);
end
end);
testlib.test('shell exit does not shut down when setting is disabled', function()
local calls = runStartupWithStubs({ ['trapos.shutdown_on_shell_exit'] = false });
testlib.assertEquals(calls.shellRuns[1], 'shell');
testlib.assertEquals(calls.shutdown, false);
testlib.assertEquals(calls.rebooted, false);
end);
testlib.run();