cc-libs/tests/install-ccpm.lua

121 lines
3.5 KiB
Lua

local createLibTest = require('/apis/libtest');
local testlib = createLibTest({ ... });
local function parentDir(path)
return path:match('^(.*)/[^/]+$');
end
local function sandboxPath(root, path)
if path:sub(1, 1) == '/' then
return root .. path;
end
return root .. '/' .. path;
end
local function fakeFs(root)
return {
exists = function(path)
return fs.exists(sandboxPath(root, path));
end,
makeDir = function(path)
fs.makeDir(sandboxPath(root, path));
end,
open = function(path, mode)
return fs.open(sandboxPath(root, path), mode);
end,
delete = function(path)
fs.delete(sandboxPath(root, path));
end,
};
end
local function fakeHttp(routes)
return {
get = function(url)
local body = routes[url];
if not body then return nil; end
return {
readAll = function() return body; end,
close = function() end,
};
end,
};
end
testlib.test('programs path resolves the ccpm command', function()
local previous = shell.path();
shell.setPath('/rom/programs:/programs');
local resolved = shell.resolveProgram('ccpm');
shell.setPath(previous);
testlib.assertEquals(resolved, 'programs/ccpm.lua');
end);
testlib.test('fresh beta bootstrap leaves ccpm runnable without tos-boot', function()
local root = '/install-ccpm-test/fresh-beta';
fs.delete(root);
fs.makeDir(root);
local base = 'https://raw.githubusercontent.com/guillaumearm/cc-libs/next/';
local routes = {
[base .. 'manifest.json'] = textutils.serializeJSON({ name = 'TrapOS', version = 'test' }),
[base .. 'packages/tos-core/ccpm.json'] = textutils.serializeJSON({
name = 'tos-core',
version = '1',
dependencies = {},
files = { 'apis/libccpm.lua', 'programs/ccpm.lua' },
}),
[base .. 'apis/libccpm.lua'] = 'libccpm-body',
[base .. 'programs/ccpm.lua'] = 'ccpm-body',
};
local calls = {
executes = {},
path = '/rom/programs',
dir = '/',
};
local fsApi = fakeFs(root);
local env = setmetatable({
fs = fsApi,
http = fakeHttp(routes),
print = function() end,
read = function() return 'yes'; end,
write = function() end,
shell = {
dir = function() return calls.dir; end,
setDir = function(path) calls.dir = path; end,
path = function() return calls.path; end,
setPath = function(path) calls.path = path; end,
execute = function(program, url, target)
calls.executes[#calls.executes + 1] = { program = program, url = url, target = target };
if program ~= 'wget' then return true; end
local body = routes[url];
if not body then return false; end
local dir = parentDir(target);
if dir then fsApi.makeDir(dir); end
local f = fsApi.open(target, 'w');
f.write(body);
f.close();
return true;
end,
},
}, { __index = _G });
local chunk, loadErr = loadfile('/trapos/install-ccpm.lua', 't', env);
if not chunk then error(loadErr, 0); end
local ok, err = pcall(chunk, '--beta');
if not ok then error(err, 0); end
testlib.assertTrue(fs.exists(root .. '/programs/ccpm.lua'));
testlib.assertTrue(string.find(calls.path, '/programs', 1, true));
for _, call in ipairs(calls.executes) do
testlib.assertTrue(call.program ~= '/startup/servers.lua');
end
local f = fs.open(root .. '/trapos/ccpm.json', 'r');
local config = textutils.unserializeJSON(f.readAll());
f.close();
testlib.assertEquals(config.registries[1].branch, 'next');
end);
testlib.run();