Remove fs-extra and klaw (#882)

These packages are obsoleted by the promises API to the built-in Node fs
module.  The fs-extra package is also causing problems with broken APIs
that don't behave the same as Node's fs module, and throwing errors
without code properties on them.
This commit is contained in:
Hornwitser authored and GitHub committed 2026-04-09 22:53:13 +02:00
1 parent 130dca4261
commit 2e48b728dd
74 files changed
+570 -529

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const http = require("http");
const https = require("https");
const util = require("util");
+9 -8
View File
@@ -1,6 +1,6 @@
"use strict";
const path = require("path");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const assert = require("assert").strict;
const jwt = require("jsonwebtoken");
const { Role, SubscribableDatastore, ControllerConfig } = require("@clusterio/lib");
@@ -26,7 +26,8 @@ describe("controller/UserManager", function () {
"controller.auth_secret": Buffer.from("mysecret").toString("base64"),
});
userManager = new UserManager(records, roles, config);
await fs.emptyDir(TEMP_DIR);
await fs.rm(TEMP_DIR, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(TEMP_DIR, { recursive: true });
});
describe(".has() / .get() / .getMutable() / .hasName() / .getByName() / .getByNameMutable()", function () {
@@ -142,14 +143,14 @@ describe("controller/UserManager", function () {
});
it("should do nothing if roles key missing", async function () {
await fs.writeJson(usersFile, { users: [] });
await fs.writeFile(usersFile, JSON.stringify({ users: [] }));
await UserManager.attemptMigrateLegacyUsersFile(usersFile, rolesFile);
const content = await fs.readJson(usersFile);
const content = JSON.parse(await fs.readFile(usersFile));
assert.deepEqual(content, { users: [] });
});
it("should throw if users key missing in legacy file", async function () {
await fs.writeJson(usersFile, { roles: [] });
await fs.writeFile(usersFile, JSON.stringify({ roles: [] }));
await assert.rejects(
() => UserManager.attemptMigrateLegacyUsersFile(usersFile, rolesFile),
/Error: Legacy users json does not contain users property/
@@ -158,12 +159,12 @@ describe("controller/UserManager", function () {
it("should migrate valid legacy file", async function () {
const legacy = { roles: [{ id: 1 }, { id: 2 }], users: [{ name: "Karl" }] };
await fs.writeJson(usersFile, legacy);
await fs.writeFile(usersFile, JSON.stringify(legacy));
await UserManager.attemptMigrateLegacyUsersFile(usersFile, rolesFile);
const writtenRoles = await fs.readJson(rolesFile);
const writtenUsers = await fs.readJson(usersFile);
const writtenRoles = JSON.parse(await fs.readFile(rolesFile));
const writtenUsers = JSON.parse(await fs.readFile(usersFile));
assert.deepEqual(writtenRoles, [{ id: 1 }, { id: 2 }]);
assert.deepEqual(writtenUsers, [{ name: "Karl" }]);
+2 -2
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const JSZip = require("jszip");
const path = require("path");
@@ -231,7 +231,7 @@ describe("host/patch", function() {
let zip = new JSZip();
zip.file("world/control.lua", "-- unknown\n");
let zipPath = path.join("temp", "test", "patch.zip");
await fs.outputFile(zipPath, await zip.generateAsync({ type: "nodebuffer" }));
await fs.writeFile(zipPath, await zip.generateAsync({ type: "nodebuffer" }));
await assert.rejects(
patch.patch(zipPath, []),
+12 -8
View File
@@ -1,7 +1,7 @@
"use strict";
const assert = require("assert").strict;
const events = require("events");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const hostServer = require("@clusterio/host/dist/node/src/server");
@@ -135,9 +135,10 @@ describe("host/server", function() {
slowTest(this);
const url = "https://github.com/clusterio/clusterio/archive/refs/tags/v2.0.0-alpha.22.zip";
const downloads = path.join("temp", "test", "downloads");
await fs.emptyDir(downloads);
await fs.rm(downloads, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(downloads, { recursive: true });
await hostServer._downloadAndExtractZip(url, path.join(downloads, "zip"));
assert.ok(await fs.exists(path.join(downloads, "zip", "packages", "controller", "package.json")));
await fs.access(path.join(downloads, "zip", "packages", "controller", "package.json"));
});
it("errors and bad status", async function() {
global.fetch = () => ({ ok: false, status: -1, statusText: "Fetch called" });
@@ -157,9 +158,10 @@ describe("host/server", function() {
it("works", async function() {
const url = "https://github.com/clusterio/clusterio/archive/refs/tags/v2.0.0-alpha.22.tar.gz";
const downloads = path.join("temp", "test", "downloads");
await fs.emptyDir(downloads);
await fs.rm(downloads, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(downloads, { recursive: true });
await hostServer._downloadAndExtractTar(url, path.join(downloads, "tar"));
assert.ok(await fs.exists(path.join(downloads, "tar", "packages", "controller", "package.json")));
await fs.access(path.join(downloads, "tar", "packages", "controller", "package.json"));
});
it("errors and bad status", async function() {
global.fetch = () => ({ ok: false, status: -1, statusText: "Fetch called" });
@@ -281,13 +283,14 @@ describe("host/server", function() {
);
});
it("should load and delete json file", async function() {
await fs.mkdir(server.writePath("script-output"), { recursive: true });
let filePath = server.writePath("script-output", "data.json");
await fs.outputFile(filePath, '{"data":"spam"}');
await fs.writeFile(filePath, '{"data":"spam"}');
let waiter = events.once(server, "ipc-channel");
await server._handleIpc(Buffer.from("\f$ipc:channel?fdata.json"));
let result = await waiter;
assert.deepEqual(result[0], { "data": "spam" });
assert(!await fs.pathExists(filePath), "File was not deleted");
await assert.rejects(fs.access(filePath), "File was not deleted");
});
});
@@ -469,7 +472,8 @@ describe("host/server", function() {
server._factorioDir = path.join("test", "file", "factorioDownload");
server._targetVersion = "latest";
global.fetch = _fetch;
await fs.emptyDir(server._factorioDir);
await fs.rm(server._factorioDir, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(server._factorioDir, { recursive: true });
await server.checkForUpdates([{
stable: true,
version: "2.0.73",
+91 -86
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const jwt = require("jsonwebtoken");
const path = require("path");
const events = require("events");
@@ -36,9 +36,9 @@ async function spawnAltHost(config = "alt-host-config.json") {
async function startAltHost() {
let config = "alt-host-config.json";
let configPath = path.join("temp", "test", config);
await fs.remove(configPath);
await fs.remove(path.join("temp", "test", "alt-instances"));
await fs.remove(path.join("temp", "test", "alt-mods"));
await fs.rm(configPath, { force: true });
await fs.rm(path.join("temp", "test", "alt-instances"), { force: true, recursive: true, maxRetries: 10 });
await fs.rm(path.join("temp", "test", "alt-mods"), { force: true, recursive: true, maxRetries: 10 });
await execCtl(`host create-config --id 5 --name alt-host --generate-token --output ${config}`);
await execHost(`--config ${config} config set host.instances_directory alt-instances`);
const dir = path.isAbsolute(factorioDir) ? factorioDir : path.join("..", "..", factorioDir);
@@ -131,10 +131,10 @@ describe("Integration of Clusterio", function() {
try {
await execController("bootstrap create-admin BootstrapAdminTest2 --bypass-lock-file");
} finally {
await fs.move("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
await fs.rename("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
}
const json = await fs.readJSON(path.join(databaseDir, "users.json"));
const json = JSON.parse(await fs.readFile(path.join(databaseDir, "users.json"), "utf8"));
assert.equal(Object.values(json).some(user => user.name === "BootstrapAdminTest2"), true);
});
});
@@ -154,10 +154,10 @@ describe("Integration of Clusterio", function() {
try {
await execController("config set controller.name ConfigEditTest2 --bypass-lock-file");
} finally {
await fs.move("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
await fs.rename("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
}
const json = await fs.readJSON(path.join(controllerConfigPath));
const json = JSON.parse(await fs.readFile(path.join(controllerConfigPath), "utf8"));
assert.equal(json["controller.name"], "ConfigEditTest2");
});
});
@@ -197,17 +197,19 @@ describe("Integration of Clusterio", function() {
} catch (err) {
assert.equal(/Error: Server listening failed/.test(err.stderr), true);
}
await fs.move("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
await fs.rename("temp.lock", lockFilePath); // Replace the lockfile after it is deleted
});
it("should refuse to start when no users are loaded", async function() {
const dir = path.join("temp", "test", "empty_controller");
await fs.emptyDir(dir);
await fs.rm(dir, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(dir, { recursive: true });
await assert.rejects(execController("run", { cwd: dir }));
});
it("should start when no users are loaded if bypass option given", async function() {
slowTest(this);
const dir = path.join("temp", "test", "empty_controller");
await fs.emptyDir(dir);
await fs.rm(dir, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(dir, { recursive: true });
const child = await spawnNode(
"altController",
"../../../packages/controller run --no-check-user-count",
@@ -349,7 +351,7 @@ describe("Integration of Clusterio", function() {
await stopAltHost(hostProcess);
}
const json = await fs.readJSON(path.join("temp", "test", config));
const json = JSON.parse(await fs.readFile(path.join("temp", "test", config), "utf8"));
assert.equal(json["host.name"], "ConfigEditTest2");
});
});
@@ -615,7 +617,10 @@ describe("Integration of Clusterio", function() {
describe("instance assign", function() {
it("creates the instance files", async function() {
await execCtl("instance assign test 4");
assert(await fs.exists(path.join(instancesDir, "test")), "Instance directory was not created");
await assert.doesNotReject(
fs.access(path.join(instancesDir, "test")),
"Instance directory was not created"
);
await checkInstanceStatus(44, "stopped");
});
});
@@ -652,15 +657,15 @@ describe("Integration of Clusterio", function() {
slowTest(this);
requiresFactorio(this);
let exportPath = path.join("temp", "test", "static");
await fs.remove(exportPath);
await fs.rm(exportPath, { force: true, recursive: true, maxRetries: 10 });
await execCtl("instance export-data test");
let modPack = await getControl().send(new lib.ModPackGetDefaultRequest());
let assets = modPack.exportManifest.assets;
assert(Object.keys(assets).length > 1, "Export assets is empty");
for (let key of ["settings", "prototypes", "metadata", "spritesheet", "locale"]) {
assert(assets[key], `Missing ${key} from assets`);
assert(
await fs.exists(path.join(exportPath, assets[key])),
await assert.doesNotReject(
fs.access(path.join(exportPath, assets[key])),
`Manifest entry for ${key} was not written to filesystem`
);
}
@@ -733,7 +738,7 @@ describe("Integration of Clusterio", function() {
slowTest(this);
await execCtl("instance stop 44");
let savesDir = path.join("temp", "test", "instances", "test", "saves");
await fs.copy(path.join(savesDir, "world.zip"), path.join(savesDir, "_autosave1.zip"));
await fs.copyFile(path.join(savesDir, "world.zip"), path.join(savesDir, "_autosave1.zip"));
await prepareToStart();
await execCtl(`instance ${cmd} test`);
let saves = await getControl().sendTo({ instanceId: 44 }, new lib.InstanceSaveDetailsListRequest());
@@ -1156,28 +1161,28 @@ describe("Integration of Clusterio", function() {
describe("instance save upload", function() {
it("should upload a zip file", async function() {
await fs.outputFile(path.join("temp", "test", "upload.zip"), "a test");
await fs.writeFile(path.join("temp", "test", "upload.zip"), "a test");
await execCtl("instance save upload 44 upload.zip");
assert(
await fs.pathExists(path.join("temp", "test", "instances", "test", "saves", "upload.zip")),
"file not uploaded to saves directory"
await assert.doesNotReject(
fs.access(path.join("temp", "test", "instances", "test", "saves", "upload.zip")),
"file not uploaded to saves directory",
);
});
it("should reject non-zip files", async function() {
await fs.outputFile(path.join("temp", "test", "invalid"), "a test");
await fs.writeFile(path.join("temp", "test", "invalid"), "a test");
await assert.rejects(execCtl("instance save upload 44 invalid"));
});
it("should reject path traversal attacks", async function() {
await fs.outputFile(path.join("temp", "test", "upload.zip"), "a test");
await fs.writeFile(path.join("temp", "test", "upload.zip"), "a test");
await assert.rejects(execCtl("instance save upload 44 upload.zip --name ../traversal.zip"));
});
});
describe("instance save download", function() {
it("should download a save", async function() {
await fs.remove(path.join("temp", "test", "upload.zip"));
await fs.rm(path.join("temp", "test", "upload.zip"), { force: true });
await execCtl("instance save download 44 upload.zip");
assert(await fs.pathExists(path.join("temp", "test", "upload.zip")));
await fs.access(path.join("temp", "test", "upload.zip"));
});
it("should error if save does not exist", async function() {
await assert.rejects(execCtl("instance save download 44 invalid"));
@@ -1190,7 +1195,7 @@ describe("Integration of Clusterio", function() {
describe("instance save copy", function() {
it("copy a save file", async function() {
await execCtl("instance save copy 44 upload.zip copy.zip");
assert(await fs.pathExists(path.join("temp", "test", "instances", "test", "saves", "copy.zip")));
await fs.access(path.join("temp", "test", "instances", "test", "saves", "copy.zip"));
});
it("should error if destination exist", async function() {
await assert.rejects(execCtl("instance save copy 44 upload.zip copy.zip"));
@@ -1208,8 +1213,8 @@ describe("Integration of Clusterio", function() {
describe("instance save rename", function() {
it("rename a save file", async function() {
await execCtl("instance save rename 44 copy.zip rename.zip");
assert(!await fs.pathExists(path.join("temp", "test", "instances", "test", "saves", "copy.zip")));
assert(await fs.pathExists(path.join("temp", "test", "instances", "test", "saves", "rename.zip")));
await assert.rejects(fs.access(path.join("temp", "test", "instances", "test", "saves", "copy.zip")));
await fs.access(path.join("temp", "test", "instances", "test", "saves", "rename.zip"));
});
it("should error if new name exist", async function() {
await assert.rejects(execCtl("instance save rename 44 upload.zip rename.zip"));
@@ -1264,31 +1269,31 @@ describe("Integration of Clusterio", function() {
it("should transfers a save", async function() {
await uploadSave(pri, "transfer.zip", "transfer.zip");
await execCtlProcess(`instance save transfer ${pri} transfer.zip ${sec}`);
assert(!await fs.pathExists(path.join(priSaves, "transfer.zip")), "save still at pri");
assert(await fs.pathExists(path.join(secSaves, "transfer.zip")), "save not at sec");
await assert.rejects(fs.access(path.join(priSaves, "transfer.zip")), "save still at pri");
await assert.doesNotReject(fs.access(path.join(secSaves, "transfer.zip")), "save not at sec");
await deleteSave(sec, "transfer.zip");
});
it("should support rename", async function() {
await uploadSave(pri, "transfer.zip", "transfer.zip");
await execCtlProcess(`instance save transfer ${pri} transfer.zip ${sec} rename.zip`);
assert(!await fs.pathExists(path.join(priSaves, "transfer.zip")), "save still at pri");
assert(await fs.pathExists(path.join(secSaves, "rename.zip")), "save not at sec");
await assert.rejects(fs.access(path.join(priSaves, "transfer.zip")), "save still at pri");
assert(await (path.join(secSaves, "rename.zip")), "save not at sec");
await deleteSave(sec, "rename.zip");
});
it("should auto-rename if target exists", async function() {
await uploadSave(pri, "transfer.zip", "transfer.zip");
await uploadSave(sec, "transfer.zip", "transfer.zip");
await execCtlProcess(`instance save transfer ${pri} transfer.zip ${sec}`);
assert(!await fs.pathExists(path.join(priSaves, "transfer.zip")), "save still at pri");
assert(await fs.pathExists(path.join(secSaves, "transfer-2.zip")), "save not at sec");
await assert.rejects(fs.access(path.join(priSaves, "transfer.zip")), "save still at pri");
await assert.doesNotReject(fs.access(path.join(secSaves, "transfer-2.zip")), "save not at sec");
await deleteSave(sec, "transfer.zip");
await deleteSave(sec, "transfer-2.zip");
});
it("should copy when using --copy", async function() {
await uploadSave(pri, "transfer.zip", "transfer.zip");
await execCtlProcess(`instance save transfer ${pri} transfer.zip ${sec} --copy`);
assert(await fs.pathExists(path.join(priSaves, "transfer.zip")), "save not at pri");
assert(await fs.pathExists(path.join(secSaves, "transfer.zip")), "save not at sec");
await assert.doesNotReject(fs.access(path.join(priSaves, "transfer.zip")), "save not at pri");
await assert.doesNotReject(fs.access(path.join(secSaves, "transfer.zip")), "save not at sec");
await deleteSave(pri, "transfer.zip");
await deleteSave(sec, "transfer.zip");
});
@@ -1344,16 +1349,16 @@ describe("Integration of Clusterio", function() {
describe("instance save delete", function() {
it("should delete a save", async function() {
await execCtl("instance save delete 44 upload.zip");
assert(
!await fs.pathExists(path.join("temp", "test", "instances", "test", "saves", "upload.zip")),
"file not deleted"
await assert.rejects(
fs.access(path.join("temp", "test", "instances", "test", "saves", "upload.zip")),
"file not deleted",
);
});
it("should error if save does not exist", async function() {
await assert.rejects(execCtl("instance save delete 44 upload.zip"));
});
it("should error on path traversal attacks", async function() {
await fs.outputFile(path.join("temp", "test", "upload.zip"), "a test");
await fs.writeFile(path.join("temp", "test", "upload.zip"), "a test");
await assert.rejects(execCtl("instance save delete 44 ../../upload.zip"));
});
});
@@ -1363,7 +1368,7 @@ describe("Integration of Clusterio", function() {
slowTest(this);
getControl().saveUpdates = [];
await execCtl("instance delete test");
assert(!await fs.exists(path.join(instancesDir, "test")), "Instance files was not deleted");
await assert.rejects(fs.access(path.join(instancesDir, "test")), "Instance files was not deleted");
let instances = await getInstances();
assert(!instances.has(44), "instance was not deleted from controller");
@@ -1497,9 +1502,9 @@ describe("Integration of Clusterio", function() {
outputDir: path.join("temp", "test"),
});
await execCtl("mod upload empty_mod_1.0.0.zip");
assert(
await fs.pathExists(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip")),
"mod not present in mods directory"
await assert.doesNotReject(
fs.access(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip")),
"mod not present in mods directory",
);
});
it("rejects path traversing mods", async function() {
@@ -1510,11 +1515,11 @@ describe("Integration of Clusterio", function() {
outputDir: path.join("temp", "test"),
modName: "path_traversing_mod_1.0.0",
});
await fs.remove(path.join("temp", "test", "bad_path_mod_1.0.0.zip"));
await fs.rm(path.join("temp", "test", "bad_path_mod_1.0.0.zip"), { force: true });
await assert.rejects(execCtl("mod upload path_traversing_mod_1.0.0.zip"));
assert(
!await fs.pathExists(path.join("temp", "test", "bad_path_mod_1.0.0.zip")),
"mod executed a path traversal attack",
await assert.rejects(
fs.access(path.join("temp", "test", "bad_path_mod_1.0.0.zip")),
"mod executed a path traversal attack"
);
});
});
@@ -1562,9 +1567,9 @@ describe("Integration of Clusterio", function() {
it("downloads a mod", async function() {
await fs.unlink(path.join("temp", "test", "empty_mod_1.0.0.zip"));
await execCtl("mod download empty_mod 1.0.0");
assert(
await fs.pathExists(path.join("temp", "test", "empty_mod_1.0.0.zip")),
"mod not downloaded to cwd"
await assert.doesNotReject(
fs.access(path.join("temp", "test", "empty_mod_1.0.0.zip")),
"mod not downloaded to cwd",
);
});
});
@@ -1572,9 +1577,9 @@ describe("Integration of Clusterio", function() {
describe("mod delete", function() {
it("deletes a mod", async function() {
await execCtl("mod delete empty_mod 1.0.0");
assert(
!await fs.pathExists(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip")),
"mod still present in mods dir"
await assert.rejects(
fs.access(path.join("temp", "test", "mods", "empty_mod_1.0.0.zip")),
"mod still present in mods dir",
);
});
});
@@ -1761,7 +1766,7 @@ describe("Integration of Clusterio", function() {
await execCtl("user set-whitelisted import_control --remove");
});
it("should import users", async function() {
await fs.writeJSON(path.join("temp", "test", "import-users.json"), {
await fs.writeFile(path.join("temp", "test", "import-users.json"), JSON.stringify({
export_version: "2.0.0.alpha20",
users: [
{ username: "import_user_admin", is_admin: true },
@@ -1771,7 +1776,7 @@ describe("Integration of Clusterio", function() {
{ username: "import_user_none" },
{ username: "import_control", is_admin: true },
],
});
}));
await execCtl("user import --users import-users.json");
@@ -1794,10 +1799,10 @@ describe("Integration of Clusterio", function() {
assert.equal(userBanReason.banReason, "banned", "import_user_ban_reason had incorrect ban reason");
});
it("should import users by json format", async function() {
await fs.writeJSON(path.join("temp", "test", "import-users.json"), {
await fs.writeFile(path.join("temp", "test", "import-users.json"), JSON.stringify({
export_version: "2.0.0.alpha20",
users: [{ username: "import_user", is_admin: true, is_whitelisted: true }],
});
}));
await execCtl("user import import-users.json");
const userFoo = (await getUsers()).get("import_user");
@@ -1805,11 +1810,11 @@ describe("Integration of Clusterio", function() {
assert(userFoo.isWhitelisted, "import_user was not whitelisted");
});
it("should import bans", async function() {
await fs.writeJSON(path.join("temp", "test", "import-bans.json"), [
await fs.writeFile(path.join("temp", "test", "import-bans.json"), JSON.stringify([
{ username: "import_ban_reason", reason: "banned" },
"import_ban",
"import_control",
]);
]));
await execCtl("user import --bans import-bans.json");
@@ -1826,17 +1831,17 @@ describe("Integration of Clusterio", function() {
assert(users.get("import_control").isBanned, "import_control was not banned");
});
it("should import bans by filename", async function() {
await fs.writeJSON(path.join("temp", "test", "import-bans.json"), [
await fs.writeFile(path.join("temp", "test", "import-bans.json"), JSON.stringify([
"import_ban_filename",
]);
]));
await execCtl("user import import-bans.json");
assert((await getUsers()).get("import_ban_filename").isBanned, "import_ban_filename was not banned");
});
it("should import admins", async function() {
await fs.writeJSON(path.join("temp", "test", "import-admins.json"), [
await fs.writeFile(path.join("temp", "test", "import-admins.json"), JSON.stringify([
"import_admin", "import_control",
]);
]));
await execCtl("user import --admins import-admins.json");
const users = await getUsers();
@@ -1844,17 +1849,17 @@ describe("Integration of Clusterio", function() {
assert(users.get("import_control").isAdmin, "import_control was not admin");
});
it("should import admins by filename", async function() {
await fs.writeJSON(path.join("temp", "test", "import-admins.json"), [
await fs.writeFile(path.join("temp", "test", "import-admins.json"), JSON.stringify([
"import_admin_filename",
]);
]));
await execCtl("user import import-admins.json");
assert((await getUsers()).get("import_admin_filename").isAdmin, "import_admin_filename was not admin");
});
it("should import whitelist", async function() {
await fs.writeJSON(path.join("temp", "test", "import-whitelist.json"), [
await fs.writeFile(path.join("temp", "test", "import-whitelist.json"), JSON.stringify([
"import_whitelist", "import_control",
]);
]));
await execCtl("user import --whitelist import-whitelist.json");
const users = await getUsers();
@@ -1862,9 +1867,9 @@ describe("Integration of Clusterio", function() {
assert(users.get("import_control").isWhitelisted, "import_control was not whitelisted");
});
it("should import whitelist by filename", async function() {
await fs.writeJSON(path.join("temp", "test", "import-whitelist.json"), [
await fs.writeFile(path.join("temp", "test", "import-whitelist.json"), JSON.stringify([
"import_whitelist_filename",
]);
]));
await execCtl("user import import-whitelist.json");
assert(
@@ -1885,7 +1890,7 @@ describe("Integration of Clusterio", function() {
await execCtl("user set-admin export_user --create");
await execCtl("user set-whitelisted export_user");
await execCtl("user export --users user-export.json");
const data = await fs.readJson(path.join("temp", "test", "user-export.json"));
const data = JSON.parse(await fs.readFile(path.join("temp", "test", "user-export.json")));
assert(data.users, "Users array does not exist");
assert(!data.users.find(u => u.username === "export_control"), "Control user is present");
assert.deepEqual(data.users.find(u => u.username === "export_user"), {
@@ -1896,7 +1901,7 @@ describe("Integration of Clusterio", function() {
await execCtl("user set-banned export_ban --create");
await execCtl("user set-banned export_ban_reason --create --reason banned");
await execCtl("user export --bans ban-export.json");
const data = await fs.readJson(path.join("temp", "test", "ban-export.json"));
const data = JSON.parse(await fs.readFile(path.join("temp", "test", "ban-export.json")));
assert.equal(data.indexOf("export_control"), -1, "Control user is present");
assert(data.indexOf("export_ban") >= 0, "export_ban is missing");
assert.deepEqual(data.find(u => typeof u === "object" && u.username === "export_ban_reason"), {
@@ -1906,14 +1911,14 @@ describe("Integration of Clusterio", function() {
it("should export admins", async function() {
await execCtl("user set-admin export_admin --create");
await execCtl("user export --admins admin-export.json");
const data = await fs.readJson(path.join("temp", "test", "admin-export.json"));
const data = JSON.parse(await fs.readFile(path.join("temp", "test", "admin-export.json")));
assert.equal(data.indexOf("export_control"), -1, "Control user is present");
assert(data.indexOf("export_admin") >= 0, "export_admin is missing");
});
it("should export whitelist", async function() {
await execCtl("user set-whitelisted export_whitelist --create");
await execCtl("user export --whitelist whitelist-export.json");
const data = await fs.readJson(path.join("temp", "test", "whitelist-export.json"));
const data = JSON.parse(await fs.readFile(path.join("temp", "test", "whitelist-export.json")));
assert.equal(data.indexOf("export_control"), -1, "Control user is present");
assert(data.indexOf("export_whitelist") >= 0, "export_whitelist is missing");
});
@@ -1927,13 +1932,13 @@ describe("Integration of Clusterio", function() {
await execCtl("user set-whitelisted restore_control");
});
it("should restore users", async function() {
await fs.writeJSON(path.join("temp", "test", "restore-users.json"), {
await fs.writeFile(path.join("temp", "test", "restore-users.json"), JSON.stringify({
export_version: "2.0.0.alpha20",
users: [
{ username: "restore_user", is_admin: true, is_whitelisted: true },
{ username: "restore_user_two", is_banned: true, ban_reason: "Test" },
],
});
}));
await execCtl("user restore restore-users.json");
const users = await getUsers();
@@ -1951,17 +1956,17 @@ describe("Integration of Clusterio", function() {
assert(!control.isBanned, "restore_control was banned");
assert(!control.isWhitelisted, "restore_control was whitelisted");
const data = await fs.readJson(path.join("temp", "test", "users-backup.json"));
const data = JSON.parse(await fs.readFile(path.join("temp", "test", "users-backup.json")));
assert(data.users, "Users array does not exist");
assert.deepEqual(data.users.find(u => u.username === "restore_control"), {
username: "restore_control", is_admin: true, is_banned: true, is_whitelisted: true,
});
});
it("should restore bans", async function() {
await fs.writeJSON(path.join("temp", "test", "restore-bans.json"), [
await fs.writeFile(path.join("temp", "test", "restore-bans.json"), JSON.stringify([
{ username: "restore_ban_reason", reason: "banned" },
"restore_ban",
]);
]));
await execCtl("user restore --bans restore-bans.json");
@@ -1977,13 +1982,13 @@ describe("Integration of Clusterio", function() {
assert(!users.get("restore_control").isBanned, "restore_control was banned");
const data = await fs.readJson(path.join("temp", "test", "bans-backup.json"));
const data = JSON.parse(await fs.readFile(path.join("temp", "test", "bans-backup.json")));
assert(data.indexOf("restore_control") >= 0, "restore_control is missing");
});
it("should restore admins", async function() {
await fs.writeJSON(path.join("temp", "test", "restore-admins.json"), [
await fs.writeFile(path.join("temp", "test", "restore-admins.json"), JSON.stringify([
"restore_admin",
]);
]));
await execCtl("user restore --admins restore-admins.json");
const users = await getUsers();
@@ -1991,13 +1996,13 @@ describe("Integration of Clusterio", function() {
assert(users.get("restore_admin").isAdmin, "restore_admin was not admin");
assert(!users.get("restore_control").isAdmin, "restore_control was admin");
const data = await fs.readJson(path.join("temp", "test", "admins-backup.json"));
const data = JSON.parse(await fs.readFile(path.join("temp", "test", "admins-backup.json")));
assert(data.indexOf("restore_control") >= 0, "restore_control is missing");
});
it("should restore whitelist", async function() {
await fs.writeJSON(path.join("temp", "test", "restore-whitelist.json"), [
await fs.writeFile(path.join("temp", "test", "restore-whitelist.json"), JSON.stringify([
"restore_whitelist",
]);
]));
await execCtl("user restore --whitelist restore-whitelist.json");
const users = await getUsers();
@@ -2005,7 +2010,7 @@ describe("Integration of Clusterio", function() {
assert(users.get("restore_whitelist").isWhitelisted, "restore_whitelist was not whitelisted");
assert(!users.get("restore_control").isWhitelisted, "restore_control was whitelisted");
const data = await fs.readJson(path.join("temp", "test", "whitelist-backup.json"));
const data = JSON.parse(await fs.readFile(path.join("temp", "test", "whitelist-backup.json")));
assert(data.indexOf("restore_control") >= 0, "restore_control is missing");
});
it("should reject multiple provided options", async function() {
+7 -7
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const jwt = require("jsonwebtoken");
const path = require("path");
const events = require("events");
@@ -18,10 +18,10 @@ async function exec(command, options = {}) {
describe("Integration of create tool", function () {
beforeEach(async function () {
await fs.ensureDir(cwd);
await fs.mkdir(cwd, { recursive: true });
});
afterEach(async function () {
await fs.remove(cwd);
await fs.rm(cwd, { recursive: true, maxRetries: 10 });
});
it("should create a new standalone installation", async function () {
this.skip();
@@ -37,10 +37,10 @@ describe("Integration of create tool", function () {
].join(" "));
// Check that the generated config is correct
const controllerConfig = await fs.readJson(path.join(cwd, "config-controller.json"));
const controllerConfig = JSON.parse(await fs.readFile(path.join(cwd, "config-controller.json")));
assert.equal(controllerConfig["controller.http_port"], 8099);
const hostConfig = await fs.readJson(path.join(cwd, "config-host.json"));
const hostConfig = JSON.parse(await fs.readFile(path.join(cwd, "config-host.json")));
assert.equal(hostConfig["host.name"], "local");
assert.equal(hostConfig["host.public_address"], "localhost");
assert.equal(hostConfig["host.factorio_directory"], "test/factorio");
@@ -58,9 +58,9 @@ describe("Integration of create tool", function () {
].join(" "));
// Check that the generated config is correct
const controllerConfig = await fs.readJson(path.join(cwd, "config-controller.json"));
const controllerConfig = JSON.parse(await fs.readFile(path.join(cwd, "config-controller.json")));
assert.equal(controllerConfig["controller.http_port"], 8099);
await assert.rejects(fs.readJson(path.join(cwd, "config-host.json")));
await assert.rejects(fs.access(path.join(cwd, "config-host.json")));
}).timeout(60000);
});
+12 -11
View File
@@ -1,7 +1,8 @@
/* eslint-disable no-console */
"use strict";
const path = require("path");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const { readFileSync } = require("node:fs");
const child_process = require("child_process");
const jwt = require("jsonwebtoken");
const util = require("util");
@@ -160,7 +161,7 @@ async function get(urlPath) {
function loadJSON(filePath) {
try {
// eslint-disable-next-line node/no-sync
return JSON.parse(fs.readFileSync(filePath, "utf8"));
return JSON.parse(readFileSync(filePath, "utf8"));
} catch (err) {
if (err.code === "ENOENT") {
return undefined;
@@ -319,19 +320,19 @@ before(async function() {
console.log("FAST_TEST is present in env, slow tests will be skipped.");
}
await fs.remove(instancesDir);
await fs.remove(modsDir);
await fs.remove(databaseDir);
await fs.rm(instancesDir, { force: true, recursive: true, maxRetries: 10 });
await fs.rm(modsDir, { force: true, recursive: true, maxRetries: 10 });
await fs.rm(databaseDir, { force: true, recursive: true, maxRetries: 10 });
await fs.remove(pluginListPath);
await fs.remove(controllerConfigPath);
await fs.remove(hostConfigPath);
await fs.remove(controlConfigPath);
await fs.rm(pluginListPath, { force: true });
await fs.rm(controllerConfigPath, { force: true });
await fs.rm(hostConfigPath, { force: true });
await fs.rm(controlConfigPath, { force: true });
await fs.ensureDir(path.join("temp", "test"));
await fs.mkdir(path.join("temp", "test"), { recursive: true });
console.log("Building Mods");
await fs.ensureDir(modsDir);
await fs.mkdir(modsDir, { recursive: true });
await lib.build({
build: true,
pack: true,
+5 -5
View File
@@ -2,7 +2,7 @@
const path = require("path");
const assert = require("assert").strict;
const lib = require("@clusterio/lib");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const { testMatrix } = require("../common");
const {
@@ -82,16 +82,16 @@ describe("Clusterio Instance", function() {
});
it("should always create a whitelist json even if unused", async function() {
slowTest(this);
await fs.remove(whitelistPath);
await fs.rm(whitelistPath, { force: true });
await execCtl(`instance start ${instName}`);
const whitelist = await fs.readJSON(whitelistPath);
const whitelist = JSON.parse(await fs.readFile(whitelistPath, "utf8"));
assert.deepEqual(whitelist, []);
});
it("should not overwrite the existing whitelist if sync is disabled", async function() {
slowTest(this);
await fs.writeJSON(whitelistPath, ["test_user"]);
await fs.writeFile(whitelistPath, JSON.stringify(["test_user"]));
await execCtl(`instance start ${instName}`);
const whitelist = await fs.readJSON(whitelistPath);
const whitelist = JSON.parse(await fs.readFile(whitelistPath, "utf8"));
assert.deepEqual(whitelist, ["test_user"]);
});
});
+1 -1
View File
@@ -1,7 +1,7 @@
"use strict";
const assert = require("assert").strict;
const events = require("events");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const jwt = require("jsonwebtoken");
const { TestControlConnector, TestHostConnector, TestControl, get, exec, url, slowTest } = require("./index");
+1 -1
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const JSZip = require("jszip");
const path = require("path");
+10 -11
View File
@@ -1,7 +1,7 @@
"use strict";
const assert = require("assert").strict;
const events = require("events");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const lib = require("@clusterio/lib");
@@ -44,12 +44,10 @@ describe("Integration of host/src/server", function() {
before(async function() {
// Delete result from previous run of these tests
if (await fs.pathExists(writePath)) {
await fs.remove(writePath);
}
await fs.rm(writePath, { force: true, recursive: true, maxRetries: 10 });
await fs.ensureDir(writePath);
logFile = fs.createWriteStream(path.join(writePath, "log.txt"), "utf8");
await fs.mkdir(writePath, { recursive: true });
logFile = (await fs.open(path.join(writePath, "log.txt"), "w")).createWriteStream({ encoding: "utf8" });
server.on("output", function(output) {
if (output.source === "stderr") {
// Factorio doesn't print on stderr but sometimes on
@@ -103,10 +101,10 @@ describe("Integration of host/src/server", function() {
// Make sure the test is not fooled by previous data
let mapPath = server.writePath("saves", "test.zip");
assert(!await fs.exists(mapPath), "save exist before test");
await assert.rejects(fs.access(mapPath), "save exist before test");
await server.create("test.zip", undefined, { width: 50, height: 50 });
assert(await fs.exists(mapPath), "test did not create save");
assert.doesNotReject(await fs.access(mapPath), "test did not create save");
});
});
@@ -119,11 +117,11 @@ describe("Integration of host/src/server", function() {
let settings = await server.exampleSettings();
settings.visibility = { public: false, lan: true };
settings.require_user_verification = false;
await fs.outputFile(server.writePath("server-settings.json"), JSON.stringify(settings, null, "\t"));
await fs.writeFile(server.writePath("server-settings.json"), JSON.stringify(settings, null, "\t"));
// Make sure the test does not fail due to create() failing.
let mapPath = server.writePath("saves", "test.zip");
assert(await fs.exists(mapPath), "save is missing");
await assert.doesNotReject(fs.access(mapPath), "save is missing");
await server.start("test.zip");
});
@@ -171,7 +169,8 @@ describe("Integration of host/src/server", function() {
describe(".startScenario()", function() {
before("Write test_scenario", async function() {
let content = "script.on_init(function() print('test_scenario init') end)\n";
await fs.outputFile(server.writePath("scenarios", "test_scenario", "control.lua"), content);
await fs.mkdir(server.writePath("scenarios", "test_scenario"), { recursive: true });
await fs.writeFile(server.writePath("scenarios", "test_scenario", "control.lua"), content);
});
it("runs the given scenario", async function() {
+5 -5
View File
@@ -1,7 +1,7 @@
/* eslint-disable node/no-sync */
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const { LockFile } = require("@clusterio/lib");
@@ -15,7 +15,7 @@ describe("class LockFile", function () {
let lock;
before(async function () {
await fs.ensureDir(baseDir);
await fs.mkdir(baseDir, { recursive: true });
});
beforeEach(function() {
@@ -30,7 +30,7 @@ describe("class LockFile", function () {
describe("acquire()", function () {
it("should create the lock file when acquiring", async function () {
await lock.acquire();
assert.ok(fs.existsSync(filePath));
await fs.access(filePath);
});
it("should throw LockFileBusyError if called when not idle", async function () {
@@ -41,7 +41,7 @@ describe("class LockFile", function () {
it("should remove stale lock file and acquire it", async function () {
await fs.writeFile(filePath, "999999\n"); // stale lock
await lock.acquire();
assert.ok(fs.existsSync(filePath));
await fs.access(filePath);
});
it("should throw if existing lock file is not stale", async function () {
@@ -54,7 +54,7 @@ describe("class LockFile", function () {
it("should remove the lock file when releasing", async function () {
await lock.acquire();
await lock.release();
assert.ok(!fs.existsSync(filePath));
await assert.rejects(fs.access(filePath));
});
it("should throw LockFileBusyError if called when not acquired", async function () {
+14 -11
View File
@@ -1,7 +1,7 @@
"use strict";
const assert = require("assert").strict;
const path = require("path");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const stream = require("node:stream");
const JSZip = require("jszip"); // Added for creating mock zips
@@ -44,7 +44,7 @@ async function waitBriefly() {
describe("lib/ModStore", function () {
before(async function () {
await fs.ensureDir(MODS_DIR);
await fs.mkdir(MODS_DIR, { recursive: true });
});
after(function() {
@@ -59,7 +59,8 @@ describe("lib/ModStore", function () {
});
afterEach(async function () {
await fs.emptyDir(MODS_DIR);
await fs.rm(MODS_DIR, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(MODS_DIR, { recursive: true });
});
describe("constructor", function () {
@@ -120,7 +121,7 @@ describe("lib/ModStore", function () {
// Add other file types and directories
await fs.writeFile(path.join(MODS_DIR, "some-text.txt"), "hello");
await fs.ensureDir(path.join(MODS_DIR, "a-directory"));
await fs.mkdir(path.join(MODS_DIR, "a-directory"), { recursive: true });
await fs.writeFile(path.join(MODS_DIR, "archive.tar.gz"), "not a zip");
await createMockModZip(path.join(MODS_DIR, "temp_file.tmp.zip"), "temp", "1.0.0"); // Should ignore .tmp.zip
@@ -139,7 +140,7 @@ describe("lib/ModStore", function () {
await createMockModZip(modPath, modName, modVersion);
let modStore = await ModStore.fromDirectory(MODS_DIR);
assert.equal(modStore.files.size, 1);
assert(await fs.pathExists(CACHE_FILE), "Cache file should exist");
await assert.doesNotReject(fs.access(CACHE_FILE), "Cache file should exist");
// Wait and recreate the file to update mtime
await waitBriefly();
@@ -197,7 +198,9 @@ describe("lib/ModStore", function () {
beforeEach(async function () {
// Start with an empty store for download tests
await fs.emptyDir(MODS_DIR); // Clear any existing files in the temp dir
// Clear any existing files in the temp dir
await fs.rm(MODS_DIR, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(MODS_DIR, { recursive: true });
modStore = await ModStore.fromDirectory(MODS_DIR); // Initialize using fromDirectory
// Restore fetch before each test in this suite to avoid pollution
@@ -269,7 +272,7 @@ describe("lib/ModStore", function () {
assert.equal(modStore.files.size, 1, "Should have 1 mod after download");
const filename = "download-mod-1_1.1.0.zip";
assert(modStore.files.has(filename), `Mod ${filename} should be in the store`);
assert(await fs.pathExists(path.join(MODS_DIR, filename)), `File ${filename} should exist`);
await assert.doesNotReject(fs.access(path.join(MODS_DIR, filename)), `File ${filename} should exist`);
const modInfo = modStore.files.get(filename);
assert(modInfo instanceof ModInfo);
assert.equal(modInfo.name, "download-mod-1");
@@ -290,8 +293,8 @@ describe("lib/ModStore", function () {
assert.equal(modStore.files.size, 2);
assert(modStore.files.has("multi-mod-a_1.0.0.zip"));
assert(modStore.files.has("multi-mod-b_2.0.0.zip"));
assert(await fs.pathExists(path.join(MODS_DIR, "multi-mod-a_1.0.0.zip")));
assert(await fs.pathExists(path.join(MODS_DIR, "multi-mod-b_2.0.0.zip")));
await fs.access(path.join(MODS_DIR, "multi-mod-a_1.0.0.zip"));
await fs.access(path.join(MODS_DIR, "multi-mod-b_2.0.0.zip"));
});
it("should throw when no releases available", async function () {
const modsToDownload = [{ name: "download-mod-1", version: ModVersionEquality.fromString("1.1.0") }];
@@ -803,12 +806,12 @@ describe("lib/ModStore", function () {
describe("deleteFile", function () {
it("should delete an existing file from store and filesystem", async function () {
assert(modStore.files.has(testMod1Filename), "Mod should exist before deletion");
assert(await fs.pathExists(testMod1Path), "Mod file should exist before deletion");
await assert.doesNotReject(fs.access(testMod1Path), "Mod file should exist before deletion");
await modStore.deleteFile(testMod1Filename);
assert(!modStore.files.has(testMod1Filename), "Mod should not exist in store after deletion");
assert(!(await fs.pathExists(testMod1Path)), "Mod file should not exist after deletion");
await assert.rejects(fs.access(testMod1Path), "Mod file should not exist after deletion");
});
it("should emit 'change' event with isDeleted flag when deleting", async function () {
+15 -16
View File
@@ -1,7 +1,6 @@
"use strict";
const lib = require("@clusterio/lib");
const { optional } = require("@clusterio/lib/dist/node/src/config/validators");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const assert = require("assert").strict;
const CA = lib.ConfigAccess;
@@ -289,10 +288,10 @@ describe("lib/config/classes", function() {
await assert.rejects(TestConfig.fromFile("local", filepath));
});
it("should load defaults for missing fields", async function() {
await fs.writeJSON(filepath, {
await fs.writeFile(filepath, JSON.stringify({
"alpha.foo": "a",
"test.enum": "a",
});
}));
const testInstance = await TestConfig.fromFile("local", filepath);
assert.equal(testInstance.get("alpha.foo"), "a");
assert.deepEqual(testInstance.get("beta.bar"), {});
@@ -302,14 +301,14 @@ describe("lib/config/classes", function() {
assert.equal(testInstance.filepath, filepath);
});
it("should load fields", async function() {
await fs.writeJSON(filepath, {
await fs.writeFile(filepath, JSON.stringify({
"test.enum": "c",
"test.test": "blah",
"test.func": 22,
"test.bool": null,
"test.json": { valid: true },
"test.priv": "bar",
});
}));
const testInstance = await TestConfig.fromFile("local", filepath);
assert.equal(testInstance.get("test.enum"), "c");
assert.equal(testInstance.get("test.test"), "blah");
@@ -335,13 +334,13 @@ describe("lib/config/classes", function() {
"test.beta": "decay",
"test.gamma": 99,
};
await fs.writeJson(filepath, testFields);
await fs.writeFile(filepath, JSON.stringify(testFields));
const testInstance = await TestConfig.fromFile("local", filepath);
assert.deepEqual(testInstance.toJSON(), testFields);
assert.equal(testInstance.filepath, filepath);
});
it("should ignore inaccessible fields", async function() {
await fs.writeJSON(filepath, { "test.priv": "bad" });
await fs.writeFile(filepath, JSON.stringify({ "test.priv": "bad" }));
const testInstance = await TestConfig.fromFile("remote", filepath);
assert.equal(testInstance.fields["test.priv"], undefined);
assert.equal(testInstance.fields["test.func"], 42);
@@ -352,8 +351,8 @@ describe("lib/config/classes", function() {
describe(".save()", function() {
const filepath = path.join("temp", "test", "config_test.json");
beforeEach(async function() {
await fs.remove(filepath);
assert(!await fs.exists(filepath), "File was not removed");
await fs.rm(filepath, { force: true });
await assert.rejects(fs.access(filepath), "File was not removed");
});
it("should throw if there is no filepath", async function() {
const testInstance = new TestConfig("local", { "alpha.foo": "a" });
@@ -366,24 +365,24 @@ describe("lib/config/classes", function() {
it("should do nothing when not dirty", async function() {
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
await testInstance.save();
assert(!await fs.exists(filepath), "File was created");
await assert.rejects(fs.access(filepath), "File was created");
});
it("should save data to file", async function() {
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
await testInstance.save();
const json = await fs.readJSON(filepath);
const json = JSON.parse(await fs.readFile(filepath, "utf8"));
assert.deepEqual(json, testInstance.toJSON());
});
it("should clear the dirty flag after saving", async function() {
const testInstance = new TestConfig("local", { "alpha.foo": "a" }, filepath);
testInstance.set("alpha.foo", "b"); // Sets the dirty flag
await testInstance.save();
assert(await fs.exists(filepath), "File was not created");
await fs.remove(filepath);
assert(!await fs.exists(filepath), "File was not removed");
await assert.doesNotReject(fs.access(filepath), "File was not created");
await fs.rm(filepath);
await assert.rejects(fs.access(filepath), "File was was not removed");
await testInstance.save();
assert(!await fs.exists(filepath), "File was created");
await assert.rejects(fs.access(filepath), "File was created");
});
it("should be round trip savable", async function() {
const testInstance = new TestConfig("local", {
+1 -1
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const lib = require("@clusterio/lib");
+1 -1
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const lib = require("@clusterio/lib");
+7 -6
View File
@@ -2,7 +2,7 @@
const events = require("events");
const assert = require("assert").strict;
const lib = require("@clusterio/lib");
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
class MockDatastoreProvider extends lib.DatastoreProvider {
@@ -37,7 +37,8 @@ describe("lib/datastore", function() {
const baseDir = path.join("temp", "test", "datastore");
const testFiles = path.join("test", "file");
before(async function() {
await fs.emptyDir(baseDir);
await fs.rm(baseDir, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(baseDir, { recursive: true });
});
describe("class DatastoreProvider", function() {
@@ -107,7 +108,7 @@ describe("lib/datastore", function() {
});
beforeEach(async function() {
await fs.copy(path.join(testFiles, "json", "load_map.json"), dataPath);
await fs.copyFile(path.join(testFiles, "json", "load_map.json"), dataPath);
datastoreProvider = new lib.JsonDatastoreProvider(dataPath);
});
@@ -170,7 +171,7 @@ describe("lib/datastore", function() {
assert.equal(loadedData.size, 0);
});
it("throws for invalid file contents", async function() {
await fs.copy(path.join(testFiles, "json", "invalid.json"), dataPath);
await fs.copyFile(path.join(testFiles, "json", "invalid.json"), dataPath);
await assert.rejects(() => datastoreProvider.load(dataPath));
});
it("writes a backup if size changes after load", async function() {
@@ -201,7 +202,7 @@ describe("lib/datastore", function() {
});
beforeEach(async function() {
await fs.copy(path.join(testFiles, "json", "load_array_map.json"), dataPath);
await fs.copyFile(path.join(testFiles, "json", "load_array_map.json"), dataPath);
datastoreProvider = new lib.JsonIdDatastoreProvider(dataPath);
});
@@ -265,7 +266,7 @@ describe("lib/datastore", function() {
assert.equal(loadedData.size, 0);
});
it("throws for invalid file contents", async function() {
await fs.copy(path.join(testFiles, "json", "invalid.json"), dataPath);
await fs.copyFile(path.join(testFiles, "json", "invalid.json"), dataPath);
await assert.rejects(() => datastoreProvider.load(dataPath));
});
it("writes a backup if size changes after load", async function() {
+1 -1
View File
@@ -1,5 +1,5 @@
const assert = require("assert").strict;
const { readFile } = require("fs-extra");
const { readFile } = require("node:fs/promises");
const { fetchFactorioVersions } = require("@clusterio/lib");
const { slowTest } = require("../../integration");
+27 -25
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const http = require("node:http");
const path = require("path");
const util = require("node:util");
@@ -10,20 +10,21 @@ const lib = require("@clusterio/lib");
describe("lib/file_ops", function() {
let baseDir = path.join("temp", "test", "file_ops");
async function setupTestingEnv() {
await fs.ensureDir(path.join(baseDir, "test", "folder"));
await fs.ensureDir(path.join(baseDir, "test", "another folder"));
await fs.remove(path.join(baseDir, "safe"));
await fs.ensureDir(path.join(baseDir, "safe"));
await fs.mkdir(path.join(baseDir, "test", "folder"), { recursive: true });
await fs.mkdir(path.join(baseDir, "test", "another folder"), { recursive: true });
await fs.rm(path.join(baseDir, "safe"), { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(path.join(baseDir, "safe"), { recursive: true });
await fs.outputFile(path.join(baseDir, "test", "file.txt"), "contents");
await fs.outputFile(path.join(baseDir, "test", "another file.txt"), "more contents");
await fs.writeFile(path.join(baseDir, "test", "file.txt"), "contents");
await fs.writeFile(path.join(baseDir, "test", "another file.txt"), "more contents");
await fs.outputFile(path.join(baseDir, "find", "file"), "contents");
await fs.outputFile(path.join(baseDir, "find", "file.txt"), "contents");
await fs.outputFile(path.join(baseDir, "find", "foo-1"), "contents");
await fs.outputFile(path.join(baseDir, "find", "foo-2"), "contents");
await fs.outputFile(path.join(baseDir, "find", "bar-1.txt"), "contents");
await fs.outputFile(path.join(baseDir, "find", "bar-2.txt"), "contents");
await fs.mkdir(path.join(baseDir, "find"), { recursive: true });
await fs.writeFile(path.join(baseDir, "find", "file"), "contents");
await fs.writeFile(path.join(baseDir, "find", "file.txt"), "contents");
await fs.writeFile(path.join(baseDir, "find", "foo-1"), "contents");
await fs.writeFile(path.join(baseDir, "find", "foo-2"), "contents");
await fs.writeFile(path.join(baseDir, "find", "bar-1.txt"), "contents");
await fs.writeFile(path.join(baseDir, "find", "bar-2.txt"), "contents");
}
before(setupTestingEnv);
@@ -90,14 +91,14 @@ describe("lib/file_ops", function() {
it("should write new target file", async function() {
let target = path.join(baseDir, "safe", "simple.txt");
await lib.safeOutputFile(target, "a text file", "utf8");
assert(!await fs.pathExists(target.replace(".txt", ".tmp.txt")), "temporary was left behind");
await assert.rejects(fs.access(target.replace(".txt", ".tmp.txt")), "temporary was left behind");
assert.equal(await fs.readFile(target, "utf8"), "a text file");
});
it("should overwrite existing target file", async function() {
let target = path.join(baseDir, "safe", "exists.txt");
await fs.outputFile(target, "previous", "utf8");
await fs.writeFile(target, "previous", "utf8");
await lib.safeOutputFile(target, "current", "utf8");
assert(!await fs.pathExists(target.replace(".txt", ".tmp.txt")), "temporary was left behind");
await assert.rejects(fs.access(target.replace(".txt", ".tmp.txt")), "temporary was left behind");
assert.equal(await fs.readFile(target, "utf8"), "current");
});
it("should handle creating file in current working directory", async function() {
@@ -228,7 +229,8 @@ describe("lib/file_ops", function() {
let server;
before(async function () {
await fs.emptyDir(downloadDir);
await fs.rm(downloadDir, { force: true, recursive: true, maxRetries: 10 });
await fs.mkdir(downloadDir, { recursive: true });
server = http.createServer();
server.unref();
server.on("request", (req, res) => {
@@ -303,7 +305,7 @@ describe("lib/file_ops", function() {
it("should throw if file exists and overwriteMode is error", async function () {
const downloadPath = path.join(downloadDir, "exists.txt");
await fs.outputFile(downloadPath, "");
await fs.writeFile(downloadPath, "");
await assert.rejects(
lib.downloadFile(new URL("/simple-file", baseUrl), downloadPath, "error"),
{ code: "EEXIST" },
@@ -322,7 +324,7 @@ describe("lib/file_ops", function() {
it("should write a new file if it exists and overwriteMode is rename", async function () {
const downloadPath = path.join(downloadDir, "exists.txt");
await fs.outputFile(downloadPath, "");
await fs.writeFile(downloadPath, "");
const newDownloadPath = await lib.downloadFile(new URL("/simple-file", baseUrl), downloadPath, "rename");
assert.equal(newDownloadPath, path.join(downloadDir, "exists-2.txt"));
const writtenContent = await fs.readFile(newDownloadPath, "utf8");
@@ -336,9 +338,9 @@ describe("lib/file_ops", function() {
path.join(downloadDir, "contended-3.txt"),
path.join(downloadDir, "contended-4.txt"),
];
await fs.outputFile(downloadPath, "");
await fs.writeFile(downloadPath, "");
for (const additionalPath of additionalPaths) {
await fs.outputFile(additionalPath, "");
await fs.writeFile(additionalPath, "");
}
const newDownloadPath = await lib.downloadFile(new URL("/simple-file", baseUrl), downloadPath, "rename");
assert.equal(newDownloadPath, path.join(downloadDir, "contended-5.txt"));
@@ -348,7 +350,7 @@ describe("lib/file_ops", function() {
it("should overwrite existing file if overwriteMode is overwrite", async function () {
const downloadPath = path.join(downloadDir, "overwrite.txt");
await fs.outputFile(downloadPath, "original content");
await fs.writeFile(downloadPath, "original content");
const newDownloadPath = await lib.downloadFile(new URL("/simple-file", baseUrl), downloadPath, "overwrite");
assert.equal(newDownloadPath, downloadPath);
const writtenContent = await fs.readFile(newDownloadPath, "utf8");
@@ -357,7 +359,7 @@ describe("lib/file_ops", function() {
it("should not overwrite existing temp files", async function () {
const tempPath = path.join(downloadDir, "temp-exists.tmp.txt");
await fs.outputFile(tempPath, "original content");
await fs.writeFile(tempPath, "original content");
const downloadPath = path.join(downloadDir, "temp-exists.txt");
await lib.downloadFile(new URL("/simple-file", baseUrl), downloadPath, "overwrite");
const writtenContent = await fs.readFile(tempPath, "utf8");
@@ -377,7 +379,7 @@ describe("lib/file_ops", function() {
const downloadSubdir = path.join(downloadDir, "subdir");
const downloadPath = path.join(downloadSubdir, "test.txt");
await fs.ensureDir(downloadSubdir);
await fs.mkdir(downloadSubdir, { recursive: true });
const [stage1, waitForStage1] = stageWaiter();
const [stage2, waitForStage2] = stageWaiter();
@@ -391,7 +393,7 @@ describe("lib/file_ops", function() {
download.catch(() => {});
await waitForStage1;
await fs.remove(downloadSubdir);
await fs.rm(downloadSubdir, { recursive: true, maxRetries: 10 });
stage2();
await assert.rejects(
+2 -2
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs");
const fs = require("node:fs/promises");
const path = require("path");
const lib = require("@clusterio/lib");
@@ -9,7 +9,7 @@ const lib = require("@clusterio/lib");
describe("lib/hash", function() {
describe("hashStream()", function() {
it("should hash a stream of bytes", async function() {
let stream = fs.createReadStream(path.join("test", "file", "hash.txt"));
let stream = (await fs.open(path.join("test", "file", "hash.txt"))).createReadStream();
let result = await lib.hashStream(stream);
assert.equal(result, "be417768b5c3c5c1d9bcb2e7c119196dd76b5570");
});
+1 -1
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const lib = require("@clusterio/lib");
describe("lib/ini", function() {
+3 -3
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const { testLines } = require("./factorio/lines");
@@ -47,7 +47,7 @@ describe("lib/logging_utils.js", function() {
});
it("should handle loading broken index", async function() {
let tempDir = path.join("temp", "test", "log");
await fs.outputFile(path.join(tempDir, "index.json"), "Broken JSON");
await fs.writeFile(path.join(tempDir, "index.json"), "Broken JSON");
let logIndex = await lib.LogIndex.load(tempDir);
assert.deepEqual(logIndex.index, new Map());
});
@@ -60,7 +60,7 @@ describe("lib/logging_utils.js", function() {
if (file === "excluded.txt") {
continue;
}
let fileStream = fs.createReadStream(path.join(logDir, file));
let fileStream = (await fs.open(path.join(logDir, file))).createReadStream();
let lineStream = new lib.LineSplitter({ readableObjectMode: true });
fileStream.pipe(lineStream);
for await (let line of lineStream) {
+19 -14
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const lib = require("@clusterio/lib");
@@ -16,11 +16,12 @@ describe("lib/plugin_loader", function() {
let invalidPlugin = path.join(baseDir, "invalid_plugin");
before(async function() {
async function writePlugin(pluginPath, infoName) {
await fs.outputFile(
await fs.mkdir(pluginPath, { recursive: true });
await fs.writeFile(
path.join(pluginPath, "index.js"),
`module.exports.plugin = { name: "${infoName}" };`
);
await fs.outputFile(
await fs.writeFile(
path.join(pluginPath, "package.json"),
'{ "version": "0.0.1" }'
);
@@ -28,7 +29,7 @@ describe("lib/plugin_loader", function() {
await writePlugin(testPlugin, "test");
await writePlugin(brokenPlugin, "broken");
await fs.outputFile(path.join(brokenPlugin, "index.js"), "Syntax Error");
await fs.writeFile(path.join(brokenPlugin, "index.js"), "Syntax Error");
await writePlugin(invalidPlugin, "wrong");
});
@@ -70,7 +71,8 @@ describe("lib/plugin_loader", function() {
let wrongParentClass = path.join(baseDir, "wrong_parent_class_plugin");
before(async function() {
async function writeEntrypoint(pluginPath, content) {
await fs.outputFile(path.join(pluginPath, "controller.js"), content);
await fs.mkdir(pluginPath, { recursive: true });
await fs.writeFile(path.join(pluginPath, "controller.js"), content);
}
await writeEntrypoint(missingClass, "");
@@ -122,16 +124,18 @@ describe("lib/plugin_loader", function() {
const npmPluginPath = path.join(baseDir, "node_modules", "npm-plugin");
const monorepoPluginPath = path.join(baseDir, "external_plugins", "monorepo", "monorepo-plugin");
const monorepoPluginPathAbs = path.resolve(monorepoPluginPath);
const notAPluginPath = path.join(baseDir, "node_modules", "not-a-plugin");
let pluginList;
before(async function() {
// Setup test plugins
async function writePlugin(pluginPath, name) {
await fs.outputFile(
await fs.mkdir(pluginPath, { recursive: true });
await fs.writeFile(
path.join(pluginPath, "index.js"),
`module.exports.plugin = { name: "${name}" };`
);
await fs.outputFile(
await fs.writeFile(
path.join(pluginPath, "package.json"),
JSON.stringify({
name: path.basename(pluginPath),
@@ -150,7 +154,7 @@ describe("lib/plugin_loader", function() {
// Write a monorepo plugin
await writePlugin(monorepoPluginPath, "monorepo-plugin");
// Create root package.json
await fs.outputFile(
await fs.writeFile(
path.join(baseDir, "package.json"),
JSON.stringify({
dependencies: {
@@ -160,8 +164,9 @@ describe("lib/plugin_loader", function() {
})
);
// Create an npm module that is not a plugin
await fs.outputFile(
path.join(baseDir, "node_modules", "not-a-plugin", "package.json"),
await fs.mkdir(notAPluginPath, { recursive: true });
await fs.writeFile(
path.join(notAPluginPath, "package.json"),
JSON.stringify({ name: "not-a-plugin", version: "1.0.0" })
);
@@ -169,7 +174,7 @@ describe("lib/plugin_loader", function() {
pluginList = await lib.loadPluginList(pluginListPath, true);
});
beforeEach(async function() {
await fs.remove(pluginListPath);
await fs.rm(pluginListPath, { force: true });
});
it("should discover local plugins", async function() {
@@ -188,7 +193,7 @@ describe("lib/plugin_loader", function() {
it("should load existing plugin list", async function() {
const existingList = new Map([["test", "/test/path"]]);
await fs.outputFile(pluginListPath, JSON.stringify([...existingList]));
await fs.writeFile(pluginListPath, JSON.stringify([...existingList]));
const loadedPlugins = await lib.loadPluginList(pluginListPath, false);
assert.ok(loadedPlugins.has("test"));
assert.strictEqual(loadedPlugins.get("test"), "/test/path");
@@ -201,7 +206,7 @@ describe("lib/plugin_loader", function() {
it("should not throw when package.json has no dependencies field", async function() {
// Create a package.json without dependencies
await fs.outputFile(
await fs.writeFile(
path.join("package.json"),
JSON.stringify({
name: "test-package",
@@ -217,7 +222,7 @@ describe("lib/plugin_loader", function() {
after(async function() {
process.chdir(old_cwd);
await fs.remove(baseDir);
await fs.rm(baseDir, { force: true, recursive: true, maxRetries: 10 });
});
});
});
+3 -3
View File
@@ -1,6 +1,6 @@
"use strict";
const assert = require("assert").strict;
const fs = require("fs-extra");
const fs = require("node:fs/promises");
const path = require("path");
const stream = require("stream");
const util = require("util");
@@ -109,7 +109,7 @@ describe("lib/stream", function() {
it("should read chunks in a file in reverse", async function() {
let content = "";
for (let i = 0; i < 10; i++) { content += String(i).repeat(10); }
await fs.outputFile(path.join("temp", "test", "reverse.txt"), content);
await fs.writeFile(path.join("temp", "test", "reverse.txt"), content);
let reverseStream = await lib.createReverseReadStream(
path.join("temp", "test", "reverse.txt"),
{ encoding: "utf8", highWaterMark: 10 }
@@ -123,7 +123,7 @@ describe("lib/stream", function() {
});
it("should reverse the lines of a file", async function() {
let ws = fs.createWriteStream(path.join("temp", "test", "reverse.txt"));
let ws = (await fs.open(path.join("temp", "test", "reverse.txt"), "w")).createWriteStream();
for (let i = 1; i < 100000; i++) {
ws.write(Buffer.from(`${i}\n`));
}