mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-16 15:30:41 +09:00
Refactor module patching logic
Break out the module info and patch info into data classes and pack all the files for a module into one more easily modified structure.
This commit is contained in:
1 parent
b1a6628bfa
commit
a1148c7ec9
6 files changed
+339
-206
No files matched your search
+48
-35
@@ -4,6 +4,7 @@ const fs = require("fs-extra");
|
||||
const JSZip = require("jszip");
|
||||
const path = require("path");
|
||||
|
||||
const lib = require("@clusterio/lib");
|
||||
const patch = require("@clusterio/host/dist/src/patch");
|
||||
|
||||
|
||||
@@ -24,36 +25,48 @@ describe("host/patch", function() {
|
||||
'require("modules/spam/spam")',
|
||||
"",
|
||||
].join("\n");
|
||||
let patchInfo = {
|
||||
"patch_number": 1,
|
||||
"scenario": { "modules": ["foo"] },
|
||||
"modules": [{
|
||||
"name": "spam",
|
||||
"files": [
|
||||
{ "path": "bar.lua", "load": true, "require": false },
|
||||
{ "path": "spam.lua", "load": false, "require": true },
|
||||
{ "path": "excluded.lua", "load": false, "require": false },
|
||||
],
|
||||
}],
|
||||
};
|
||||
let patchInfo = new patch.PatchInfo(
|
||||
1,
|
||||
new lib.ModuleInfo("foo", "0.0.0", ["foo"]),
|
||||
[
|
||||
new patch.SaveModule(
|
||||
new lib.ModuleInfo(
|
||||
"spam",
|
||||
"0.0.0",
|
||||
["bar.lua"],
|
||||
["spam.lua"],
|
||||
),
|
||||
new Map([
|
||||
["bar.lua", Buffer.from("")],
|
||||
["spam.lua", Buffer.from("")],
|
||||
["excluded.lua", Buffer.from("")],
|
||||
]),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
it("should produce output matching the reference", function() {
|
||||
assert.equal(patch._generateLoader(patchInfo), reference);
|
||||
});
|
||||
});
|
||||
|
||||
function module({ name, version, dependencies = {} }) {
|
||||
const dependencyMap = new Map(Object.entries(dependencies));
|
||||
return new patch.SaveModule(new lib.ModuleInfo(name, version, [], [], dependencyMap));
|
||||
}
|
||||
|
||||
describe("reorderDependencies()", function() {
|
||||
it("should reorder to satisfy simple dependency", function() {
|
||||
let modules = [
|
||||
{ name: "a", version: "1.0.0", dependencies: {"b": "*"} },
|
||||
{ name: "b", version: "1.0.0", dependencies: {} },
|
||||
module({ name: "a", version: "1.0.0", dependencies: {"b": "*"} }),
|
||||
module({ name: "b", version: "1.0.0", dependencies: {} }),
|
||||
];
|
||||
patch._reorderDependencies(modules);
|
||||
assert(modules[0].name === "b", "Dependency was not reorederd");
|
||||
assert(modules[0].info.name === "b", "Dependency was not reorederd");
|
||||
});
|
||||
it("should throw on invalid version", function() {
|
||||
let modules = [
|
||||
{ name: "a", version: "foo", dependencies: {} },
|
||||
module({ name: "a", version: "foo", dependencies: {} }),
|
||||
];
|
||||
assert.throws(
|
||||
() => patch._reorderDependencies(modules),
|
||||
@@ -62,7 +75,7 @@ describe("host/patch", function() {
|
||||
});
|
||||
it("should throw on invalid version range", function() {
|
||||
let modules = [
|
||||
{ name: "a", version: "1.0.0", dependencies: {"b": "invalid"} },
|
||||
module({ name: "a", version: "1.0.0", dependencies: {"b": "invalid"} }),
|
||||
];
|
||||
assert.throws(
|
||||
() => patch._reorderDependencies(modules),
|
||||
@@ -71,16 +84,16 @@ describe("host/patch", function() {
|
||||
});
|
||||
it("should throw on missing dependency", function() {
|
||||
let modules = [
|
||||
{ name: "a", version: "1.0.0", dependencies: {"b": "*"} },
|
||||
module({ name: "a", version: "1.0.0", dependencies: {"b": "*"} }),
|
||||
];
|
||||
assert.throws(
|
||||
() => patch._reorderDependencies(modules),
|
||||
new Error("Missing dependency b for module a")
|
||||
);
|
||||
modules = [
|
||||
{ name: "a", version: "1.0.0", dependencies: {"b": "*"} },
|
||||
{ name: "b", version: "1.0.0", dependencies: {"c": "*"} },
|
||||
{ name: "c", version: "1.0.0", dependencies: {"d": "*"} },
|
||||
module({ name: "a", version: "1.0.0", dependencies: {"b": "*"} }),
|
||||
module({ name: "b", version: "1.0.0", dependencies: {"c": "*"} }),
|
||||
module({ name: "c", version: "1.0.0", dependencies: {"d": "*"} }),
|
||||
];
|
||||
assert.throws(
|
||||
() => patch._reorderDependencies(modules),
|
||||
@@ -89,8 +102,8 @@ describe("host/patch", function() {
|
||||
});
|
||||
it("should throw on outdated dependency", function() {
|
||||
let modules = [
|
||||
{ name: "a", version: "1.0.0", dependencies: {} },
|
||||
{ name: "b", version: "1.0.0", dependencies: {"a": ">=2"} },
|
||||
module({ name: "a", version: "1.0.0", dependencies: {} }),
|
||||
module({ name: "b", version: "1.0.0", dependencies: {"a": ">=2"} }),
|
||||
];
|
||||
assert.throws(
|
||||
() => patch._reorderDependencies(modules),
|
||||
@@ -99,18 +112,18 @@ describe("host/patch", function() {
|
||||
});
|
||||
it("should throw on dependency loops", function() {
|
||||
let modules = [
|
||||
{ name: "a", version: "1.0.0", dependencies: {"b": "*"} },
|
||||
{ name: "b", version: "1.0.0", dependencies: {"a": "*"} },
|
||||
module({ name: "a", version: "1.0.0", dependencies: {"b": "*"} }),
|
||||
module({ name: "b", version: "1.0.0", dependencies: {"a": "*"} }),
|
||||
];
|
||||
assert.throws(
|
||||
() => patch._reorderDependencies(modules),
|
||||
new Error("Module dependency loop detected: a -> b -> a")
|
||||
);
|
||||
modules = [
|
||||
{ name: "d", version: "1.0.0", dependencies: {"b": "*"} },
|
||||
{ name: "a", version: "1.0.0", dependencies: {"b": "*"} },
|
||||
{ name: "b", version: "1.0.0", dependencies: {"c": "*"} },
|
||||
{ name: "c", version: "1.0.0", dependencies: {"a": "*"} },
|
||||
module({ name: "d", version: "1.0.0", dependencies: {"b": "*"} }),
|
||||
module({ name: "a", version: "1.0.0", dependencies: {"b": "*"} }),
|
||||
module({ name: "b", version: "1.0.0", dependencies: {"c": "*"} }),
|
||||
module({ name: "c", version: "1.0.0", dependencies: {"a": "*"} }),
|
||||
];
|
||||
assert.throws(
|
||||
() => patch._reorderDependencies(modules),
|
||||
@@ -120,13 +133,13 @@ describe("host/patch", function() {
|
||||
it("should reorder correctly for multiple dependencies", function() {
|
||||
function validate(modules) {
|
||||
let present = new Set();
|
||||
for (let module of modules) {
|
||||
for (let dependency of Object.keys(module.dependencies)) {
|
||||
for (let { info } of modules) {
|
||||
for (let dependency of info.dependencies.keys()) {
|
||||
if (!present.has(dependency)) {
|
||||
assert.fail(`${module.name} depends on ${dependency}, but was ordered before it`);
|
||||
assert.fail(`${info.name} depends on ${dependency}, but was ordered before it`);
|
||||
}
|
||||
}
|
||||
present.add(module.name);
|
||||
present.add(info.name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,7 +162,7 @@ describe("host/patch", function() {
|
||||
dependencies[names[k + (k >= j)]] = "*";
|
||||
}
|
||||
}
|
||||
modules.push({ name, version: "1.0.0", dependencies });
|
||||
modules.push(module({ name, version: "1.0.0", dependencies }));
|
||||
}
|
||||
let success = false;
|
||||
try {
|
||||
@@ -164,7 +177,7 @@ describe("host/patch", function() {
|
||||
|
||||
// Deriving the formula for the number of graphs without
|
||||
// dependencies loops is left as an excercise for the reader.
|
||||
assert(successCount === 25, "Incorrect number of successful orderings");
|
||||
assert.equal(successCount, 25, "Incorrect number of successful orderings");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user