From f06d3509e6c3b7bd7dcdd1ef5306917157408a3b Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Wed, 21 Oct 2020 21:26:44 +0200 Subject: [PATCH] Move lua code to slave package and fix paths Move the export and clusterio mod code to the slave package and fix the path used for them. Refactor tests to use temp/tests/ as the cwd when running integration tests in order to ensure paths to lua code are correctly handled. --- .travis.yml | 4 +- packages/lib/build_mod.js | 5 +- packages/lib/factorio/export.js | 3 +- .../slave/lua}/clusterio_lib/api.lua | 0 .../slave/lua}/clusterio_lib/changelog.txt | 0 .../slave/lua}/clusterio_lib/info.json | 2 +- .../slave/lua}/export/control.lua | 0 .../slave/lua}/export/data-final-fixes.lua | 0 {lua => packages/slave/lua}/export/info.json | 0 .../slave/modules}/clusterio/api.lua | 0 .../slave/modules}/clusterio/impl.lua | 0 .../slave/modules}/clusterio/module.json | 0 .../slave/modules}/clusterio/serialize.lua | 0 packages/slave/slave.js | 8 +-- test/integration/index.js | 50 ++++++++++--------- 15 files changed, 37 insertions(+), 35 deletions(-) rename {lua => packages/slave/lua}/clusterio_lib/api.lua (100%) rename {lua => packages/slave/lua}/clusterio_lib/changelog.txt (100%) rename {lua => packages/slave/lua}/clusterio_lib/info.json (85%) rename {lua => packages/slave/lua}/export/control.lua (100%) rename {lua => packages/slave/lua}/export/data-final-fixes.lua (100%) rename {lua => packages/slave/lua}/export/info.json (100%) rename {modules => packages/slave/modules}/clusterio/api.lua (100%) rename {modules => packages/slave/modules}/clusterio/impl.lua (100%) rename {modules => packages/slave/modules}/clusterio/module.json (100%) rename {modules => packages/slave/modules}/clusterio/serialize.lua (100%) diff --git a/.travis.yml b/.travis.yml index d46fbc66..52d21e37 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,8 @@ before_install: - npm install -g codecov script: - npx lerna bootstrap --no-ci - - node packages/lib/build_mod --bump-patch --factorio-version 0.18 --output-dir sharedMods - - curl -o sharedMods/subspace_storage_1.99.1.zip -L https://github.com/clusterio/factorioClusterioMod/releases/download/1.99.1/subspace_storage_1.99.1.zip + - node packages/lib/build_mod --source-dir packages/slave/lua/clusterio_lib --bump-patch --factorio-version 0.18 --output-dir temp/test/sharedMods + - curl -o temp/test/sharedMods/subspace_storage_1.99.1.zip -L https://github.com/clusterio/factorioClusterioMod/releases/download/1.99.1/subspace_storage_1.99.1.zip - npm run-script ci-cover - cat temp/test/integration/log.txt - codecov -f coverage/lcov.info diff --git a/packages/lib/build_mod.js b/packages/lib/build_mod.js index aef57612..4be597a1 100644 --- a/packages/lib/build_mod.js +++ b/packages/lib/build_mod.js @@ -8,14 +8,13 @@ const yargs = require("yargs"); async function main() { - const libPath = path.join("lua", "clusterio_lib"); const args = yargs .scriptName("build") .options({ "clean": { describe: "Remove previous builds", type: "boolean", default: false }, "build": { describe: "Build mod", type: "boolean", default: true }, "pack": { describe: "Pack into zip file", type: "boolean", default: true }, - "source-dir": { describe: "Path to mod source files", nargs: 1, type: "string", default: libPath }, + "source-dir": { describe: "Path to mod source files", nargs: 1, type: "string", demandOption: true }, "output-dir": { describe: "Path to output built mod", nargs: 1, type: "string", default: "dist" }, "bump-patch": { describe: "Increment patch number of build", type: "boolean", default: false }, "factorio-version": { describe: "Override factorio_version", type: "string" }, @@ -79,7 +78,7 @@ async function build(args) { await events.once(walker, "end"); for (let pathParts of info.additional_files || []) { - let filePath = path.join(...pathParts); + let filePath = path.join(args.sourceDir, ...pathParts); zip.file(path.posix.join(modName, path.basename(filePath)), fs.createReadStream(filePath)); } delete info.additional_files; diff --git a/packages/lib/factorio/export.js b/packages/lib/factorio/export.js index 9a19617b..132ecada 100644 --- a/packages/lib/factorio/export.js +++ b/packages/lib/factorio/export.js @@ -33,7 +33,8 @@ async function generateExportMod(server) { clean: false, build: true, pack: true, - sourceDir: path.join("lua", "export"), + // XXX fix path when moving lib/factorio to slave + sourceDir: path.join(__dirname, "..", "..", "slave", "lua", "export"), outputDir: server.writePath("mods"), bumpPatch: false, factorioVersion: server.version.replace(/\.\d+$/, ""), diff --git a/lua/clusterio_lib/api.lua b/packages/slave/lua/clusterio_lib/api.lua similarity index 100% rename from lua/clusterio_lib/api.lua rename to packages/slave/lua/clusterio_lib/api.lua diff --git a/lua/clusterio_lib/changelog.txt b/packages/slave/lua/clusterio_lib/changelog.txt similarity index 100% rename from lua/clusterio_lib/changelog.txt rename to packages/slave/lua/clusterio_lib/changelog.txt diff --git a/lua/clusterio_lib/info.json b/packages/slave/lua/clusterio_lib/info.json similarity index 85% rename from lua/clusterio_lib/info.json rename to packages/slave/lua/clusterio_lib/info.json index 43177950..b66e2a2e 100644 --- a/lua/clusterio_lib/info.json +++ b/packages/slave/lua/clusterio_lib/info.json @@ -7,6 +7,6 @@ "description": "Library for interfacing with Clusterio. Warning: work in progress alpha.", "factorio_version": "0.17", "additional_files": [ - ["modules", "clusterio", "serialize.lua"] + ["..", "..", "modules", "clusterio", "serialize.lua"] ] } diff --git a/lua/export/control.lua b/packages/slave/lua/export/control.lua similarity index 100% rename from lua/export/control.lua rename to packages/slave/lua/export/control.lua diff --git a/lua/export/data-final-fixes.lua b/packages/slave/lua/export/data-final-fixes.lua similarity index 100% rename from lua/export/data-final-fixes.lua rename to packages/slave/lua/export/data-final-fixes.lua diff --git a/lua/export/info.json b/packages/slave/lua/export/info.json similarity index 100% rename from lua/export/info.json rename to packages/slave/lua/export/info.json diff --git a/modules/clusterio/api.lua b/packages/slave/modules/clusterio/api.lua similarity index 100% rename from modules/clusterio/api.lua rename to packages/slave/modules/clusterio/api.lua diff --git a/modules/clusterio/impl.lua b/packages/slave/modules/clusterio/impl.lua similarity index 100% rename from modules/clusterio/impl.lua rename to packages/slave/modules/clusterio/impl.lua diff --git a/modules/clusterio/module.json b/packages/slave/modules/clusterio/module.json similarity index 100% rename from modules/clusterio/module.json rename to packages/slave/modules/clusterio/module.json diff --git a/modules/clusterio/serialize.lua b/packages/slave/modules/clusterio/serialize.lua similarity index 100% rename from modules/clusterio/serialize.lua rename to packages/slave/modules/clusterio/serialize.lua diff --git a/packages/slave/slave.js b/packages/slave/slave.js index 026785ed..053258db 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -411,14 +411,14 @@ class Instance extends link.Link{ } // Find stand alone modules to load - // XXX for now it's assumed all available modules should be loaded. - for (let entry of await fs.readdir("modules", { withFileTypes: true })) { + // XXX for now only the included clusterio module is loaded + for (let entry of await fs.readdir(path.join(__dirname, "modules"), { withFileTypes: true })) { if (entry.isDirectory()) { if (modules.has(entry.name)) { throw new Error(`Module with name ${entry.name} already exists in a plugin`); } - let moduleJsonPath = path.join("modules", entry.name, "module.json"); + let moduleJsonPath = path.join(__dirname, "modules", entry.name, "module.json"); if (!await fs.pathExists(moduleJsonPath)) { throw new Error(`Module ${entry.name} is missing module.json`); } @@ -429,7 +429,7 @@ class Instance extends link.Link{ } module = { - path: path.join("modules", entry.name), + path: path.join(__dirname, "modules", entry.name), dependencies: { "clusterio": "*" }, load: [], require: [], diff --git a/test/integration/index.js b/test/integration/index.js index ed05ce5c..7c3fd2c2 100644 --- a/test/integration/index.js +++ b/test/integration/index.js @@ -63,18 +63,20 @@ let control; let url = "https://localhost:4443/"; let controlToken = jwt.sign({ aud: "user", user: "test" }, "TestSecretDoNotUse"); let instancesDir = path.join("temp", "test", "instances"); -let databaseDir = path.join("temp", "test", "databse"); -let masterConfigPath = path.join("temp", "test", "master-integration.json"); -let slaveConfigPath = path.join("temp", "test", "slave-integration.json"); -let controlConfigPath = path.join("temp", "test", "control-integration.json"); +let databaseDir = path.join("temp", "test", "database"); +let pluginListPath = path.join("temp", "test", "plugin-list.json"); +let masterConfigPath = path.join("temp", "test", "config-master.json"); +let slaveConfigPath = path.join("temp", "test", "config-slave.json"); +let controlConfigPath = path.join("temp", "test", "config-control.json"); -async function exec(...args) { - console.log(args[0]); - return await util.promisify(child_process.exec)(...args); +async function exec(command, options = {}) { + console.log(command); + options = { cwd: path.join("temp", "test"), ...options }; + return await util.promisify(child_process.exec)(command, options); } async function execCtl(...args) { - args[0] = `node packages/ctl --config ${controlConfigPath} ${args[0]}`; + args[0] = `node ../../packages/ctl ${args[0]}`; return await exec(...args); } @@ -91,7 +93,7 @@ function spawn(name, cmd, waitFor) { return new Promise((resolve, reject) => { console.log(cmd); let parts = cmd.split(" "); - let process = child_process.spawn(parts[0], parts.slice(1)); + let process = child_process.spawn(parts[0], parts.slice(1), { cwd: path.join("temp", "test") }); let stdout = new server._LineSplitter((line) => { line = line.toString("utf8"); if (line.startsWith(waitFor)) { @@ -113,30 +115,30 @@ before(async function() { await fs.remove(databaseDir); await fs.remove(instancesDir); + await fs.remove(pluginListPath); await fs.remove(masterConfigPath); await fs.remove(slaveConfigPath); await fs.remove(controlConfigPath); - await exec(`node packages/master --config ${masterConfigPath} config set master.database_directory ${databaseDir}`); - await exec(`node packages/master --config ${masterConfigPath} config set master.auth_secret TestSecretDoNotUse`); - await exec(`node packages/master --config ${masterConfigPath} config set master.http_port 8880`); - await exec(`node packages/master --config ${masterConfigPath} config set master.https_port 4443`); - await exec(`node packages/master --config ${masterConfigPath} config set master.tls_certificate ${path.join(databaseDir, "cert.crt")}`); - await exec(`node packages/master --config ${masterConfigPath} config set master.tls_private_key ${path.join(databaseDir, "cert.key")}`); - await exec(`node packages/master --config ${masterConfigPath} config set master.tls_bits 1024`); - await exec(`node packages/master --config ${masterConfigPath} config set master.heartbeat_interval 0.25`); - await exec(`node packages/master --config ${masterConfigPath} config set master.connector_shutdown_timeout 2`); + await fs.ensureDir(path.join("temp", "test")); - await exec(`node packages/master --config ${masterConfigPath} bootstrap create-admin test`); - await exec(`node packages/master --config ${masterConfigPath} bootstrap create-ctl-config test --output ${controlConfigPath}`); + await exec("node ../../packages/master config set master.auth_secret TestSecretDoNotUse"); + await exec("node ../../packages/master config set master.http_port 8880"); + await exec("node ../../packages/master config set master.https_port 4443"); + await exec("node ../../packages/master config set master.tls_bits 1024"); + await exec("node ../../packages/master config set master.heartbeat_interval 0.25"); + await exec("node ../../packages/master config set master.connector_shutdown_timeout 2"); - masterProcess = await spawn("master:", `node packages/master --config ${masterConfigPath} run`, "All plugins loaded"); + await exec("node ../../packages/master bootstrap create-admin test"); + await exec("node ../../packages/master bootstrap create-ctl-config test"); - let createArgs = `--id 4 --name slave --generate-token --output ${slaveConfigPath}`; + masterProcess = await spawn("master:", "node ../../packages/master run", "All plugins loaded"); + + let createArgs = "--id 4 --name slave --generate-token"; await execCtl(`slave create-config ${createArgs}`); - await exec(`node packages/slave --config ${slaveConfigPath} config set slave.instances_directory ${instancesDir}`); - slaveProcess = await spawn("slave:", `node packages/slave --config ${slaveConfigPath} run`, "SOCKET | received ready from master"); + await exec(`node ../../packages/slave config set slave.factorio_directory ${path.join("..", "..", "factorio")}`); + slaveProcess = await spawn("slave:", "node ../../packages/slave run", "SOCKET | received ready from master"); let controlConnector = new TestControlConnector(url, 2); controlConnector.token = controlToken;