183 lines
7.3 KiB
Lua
183 lines
7.3 KiB
Lua
local createLibTest = require('/apis/libtest');
|
|
local createCcpm = require('/apis/libccpm');
|
|
|
|
local testlib = createLibTest({ ... });
|
|
|
|
local counter = 0;
|
|
|
|
-- Fresh, isolated state + install sandbox per case (never touches real /trapos).
|
|
local function freshDirs()
|
|
counter = counter + 1;
|
|
local stateDir = '/ccpm-test/state-' .. counter;
|
|
local installRoot = '/ccpm-test/root-' .. counter;
|
|
fs.delete(stateDir);
|
|
fs.delete(installRoot);
|
|
return stateDir, installRoot;
|
|
end
|
|
|
|
-- Minimal stub of the CC `http` API backed by a url -> body map.
|
|
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
|
|
|
|
local function ghBase(name, branch)
|
|
return 'https://raw.githubusercontent.com/' .. name .. '/' .. branch .. '/';
|
|
end
|
|
|
|
testlib.test('registryBaseUrl resolves a github branch', function()
|
|
local ccpm = createCcpm({ stateDir = freshDirs() });
|
|
testlib.assertEquals(
|
|
ccpm.registryBaseUrl({ name = 'guillaumearm/cc-libs', type = 'github', branch = 'next' }),
|
|
'https://raw.githubusercontent.com/guillaumearm/cc-libs/next/'
|
|
);
|
|
end);
|
|
|
|
testlib.test('registry urls resolve an http base', function()
|
|
local ccpm = createCcpm({ stateDir = freshDirs() });
|
|
testlib.assertEquals(
|
|
ccpm.registryBaseUrl({ name = 'http://example.com/repo', type = 'http' }),
|
|
'http://example.com/repo/'
|
|
);
|
|
testlib.assertEquals(
|
|
ccpm.descriptorUrl({ name = 'http://example.com/repo/', type = 'http' }, 'tos-net'),
|
|
'http://example.com/repo/packages/tos-net/ccpm.json'
|
|
);
|
|
end);
|
|
|
|
testlib.test('resolve orders dependencies before dependents', function()
|
|
local base = ghBase('me/repo', 'master');
|
|
local routes = {
|
|
[base .. 'packages/a/ccpm.json'] = textutils.serializeJSON({ name = 'a', version = '1', dependencies = { 'b', 'c' }, files = {} }),
|
|
[base .. 'packages/b/ccpm.json'] = textutils.serializeJSON({ name = 'b', version = '1', dependencies = { 'c' }, files = {} }),
|
|
[base .. 'packages/c/ccpm.json'] = textutils.serializeJSON({ name = 'c', version = '1', dependencies = {}, files = {} }),
|
|
};
|
|
local ccpm = createCcpm({ stateDir = freshDirs(), http = fakeHttp(routes) });
|
|
ccpm.writeConfig({ registries = { { name = 'me/repo', type = 'github', branch = 'master' } } });
|
|
local ordered, err = ccpm.resolve('a');
|
|
testlib.assertTrue(ordered, tostring(err));
|
|
testlib.assertEquals(#ordered, 3);
|
|
testlib.assertEquals(ordered[1].name, 'c');
|
|
testlib.assertEquals(ordered[2].name, 'b');
|
|
testlib.assertEquals(ordered[3].name, 'a');
|
|
end);
|
|
|
|
testlib.test('resolve reports a missing package', function()
|
|
local ccpm = createCcpm({ stateDir = freshDirs(), http = fakeHttp({}) });
|
|
ccpm.writeConfig({ registries = { { name = 'me/repo', type = 'github', branch = 'master' } } });
|
|
local ordered, err = ccpm.resolve('ghost');
|
|
testlib.assertTrue(not ordered);
|
|
testlib.assertTrue(string.find(err, 'not found', 1, true));
|
|
end);
|
|
|
|
testlib.test('resolve detects a dependency cycle', function()
|
|
local base = ghBase('me/repo', 'master');
|
|
local routes = {
|
|
[base .. 'packages/a/ccpm.json'] = textutils.serializeJSON({ name = 'a', version = '1', dependencies = { 'b' } }),
|
|
[base .. 'packages/b/ccpm.json'] = textutils.serializeJSON({ name = 'b', version = '1', dependencies = { 'a' } }),
|
|
};
|
|
local ccpm = createCcpm({ stateDir = freshDirs(), http = fakeHttp(routes) });
|
|
ccpm.writeConfig({ registries = { { name = 'me/repo', type = 'github', branch = 'master' } } });
|
|
local ordered, err = ccpm.resolve('a');
|
|
testlib.assertTrue(not ordered);
|
|
testlib.assertTrue(string.find(err, 'cycle', 1, true));
|
|
end);
|
|
|
|
testlib.test('install rejects an already-installed package', function()
|
|
local ccpm = createCcpm({ stateDir = freshDirs() });
|
|
ccpm.writeLock({ packages = { foo = { version = '1', files = {} } } });
|
|
local ok, msg = ccpm.install('foo', {});
|
|
testlib.assertTrue(not ok);
|
|
testlib.assertTrue(string.find(msg, 'already installed', 1, true));
|
|
testlib.assertTrue(string.find(msg, 'reinstall foo', 1, true));
|
|
end);
|
|
|
|
testlib.test('install downloads files and records the lock', function()
|
|
local base = ghBase('me/repo', 'master');
|
|
local routes = {
|
|
[base .. 'packages/tos-core/ccpm.json'] = textutils.serializeJSON({ name = 'tos-core', version = '1', dependencies = {}, files = { 'apis/eventloop.lua' } }),
|
|
[base .. 'packages/tos-net/ccpm.json'] = textutils.serializeJSON({ name = 'tos-net', version = '1', dependencies = { 'tos-core' }, files = { 'apis/net.lua' } }),
|
|
[base .. 'apis/eventloop.lua'] = 'eventloop-body',
|
|
[base .. 'apis/net.lua'] = 'net-body',
|
|
};
|
|
local sd, root = freshDirs();
|
|
local ccpm = createCcpm({ stateDir = sd, installRoot = root, http = fakeHttp(routes) });
|
|
ccpm.writeConfig({ registries = { { name = 'me/repo', type = 'github', branch = 'master' } } });
|
|
|
|
local ok = ccpm.install('tos-net', {});
|
|
testlib.assertTrue(ok);
|
|
testlib.assertTrue(fs.exists(root .. '/apis/net.lua'));
|
|
testlib.assertTrue(fs.exists(root .. '/apis/eventloop.lua'));
|
|
|
|
local f = fs.open(root .. '/apis/net.lua', 'r');
|
|
local body = f.readAll();
|
|
f.close();
|
|
testlib.assertEquals(body, 'net-body');
|
|
|
|
local lock = ccpm.readLock();
|
|
testlib.assertTrue(lock.packages['tos-net']);
|
|
testlib.assertTrue(lock.packages['tos-core']);
|
|
testlib.assertEquals(lock.packages['tos-net'].registry, 'me/repo');
|
|
end);
|
|
|
|
testlib.test('registry add and remove round-trip', function()
|
|
local ccpm = createCcpm({ stateDir = freshDirs() });
|
|
ccpm.writeConfig({ registries = {} });
|
|
testlib.assertTrue(ccpm.addRegistry('foo/bar', { branch = 'next' }));
|
|
|
|
local regs = ccpm.listRegistries();
|
|
testlib.assertEquals(#regs, 1);
|
|
testlib.assertEquals(regs[1].name, 'foo/bar');
|
|
testlib.assertEquals(regs[1].branch, 'next');
|
|
|
|
local dupOk = ccpm.addRegistry('foo/bar', {});
|
|
testlib.assertTrue(not dupOk);
|
|
|
|
testlib.assertTrue(ccpm.removeRegistry('foo/bar'));
|
|
testlib.assertEquals(#ccpm.listRegistries(), 0);
|
|
|
|
local rmOk = ccpm.removeRegistry('nope');
|
|
testlib.assertTrue(not rmOk);
|
|
end);
|
|
|
|
testlib.test('uninstall refuses a package with dependents', function()
|
|
local ccpm = createCcpm({ stateDir = freshDirs() });
|
|
ccpm.writeLock({ packages = {
|
|
['tos-core'] = { version = '1', files = {}, dependencies = {} },
|
|
['tos-net'] = { version = '1', files = {}, dependencies = { 'tos-core' } },
|
|
} });
|
|
local ok, err = ccpm.uninstall('tos-core', {});
|
|
testlib.assertTrue(not ok);
|
|
testlib.assertTrue(string.find(err, 'required by', 1, true));
|
|
testlib.assertTrue(string.find(err, 'tos-net', 1, true));
|
|
end);
|
|
|
|
testlib.test('uninstall removes a leaf package and its files', function()
|
|
local sd, root = freshDirs();
|
|
fs.makeDir(root .. '/apis');
|
|
local f = fs.open(root .. '/apis/net.lua', 'w');
|
|
f.write('x');
|
|
f.close();
|
|
|
|
local ccpm = createCcpm({ stateDir = sd, installRoot = root });
|
|
ccpm.writeLock({ packages = {
|
|
['tos-core'] = { version = '1', files = {}, dependencies = {} },
|
|
['tos-net'] = { version = '1', files = { 'apis/net.lua' }, dependencies = { 'tos-core' } },
|
|
} });
|
|
|
|
testlib.assertTrue(ccpm.uninstall('tos-net', {}));
|
|
testlib.assertTrue(not fs.exists(root .. '/apis/net.lua'));
|
|
testlib.assertTrue(ccpm.readLock().packages['tos-net'] == nil);
|
|
testlib.assertTrue(ccpm.readLock().packages['tos-core'] ~= nil);
|
|
end);
|
|
|
|
testlib.run();
|