mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 08:23:12 +09:00
Add missing tests
This commit is contained in:
2 files changed
+83
-13
No files matched your search
@@ -128,7 +128,13 @@ async function downloadAndExtractZip(url: string, targetDir: string) {
|
|||||||
await fs.ensureDir(targetDir);
|
await fs.ensureDir(targetDir);
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Object.values(zip.files).map(async (entry) => {
|
Object.values(zip.files).map(async (entry) => {
|
||||||
const entryPath = path.join(targetDir, entry.name);
|
const parts = entry.name.split("/").filter(Boolean);
|
||||||
|
const strippedPath = parts.slice(1).join("/");
|
||||||
|
if (!strippedPath) {
|
||||||
|
return; // This copies the behaviour of tar --strip-components 1
|
||||||
|
}
|
||||||
|
|
||||||
|
const entryPath = path.join(targetDir, strippedPath);
|
||||||
if (entry.dir) {
|
if (entry.dir) {
|
||||||
await fs.ensureDir(entryPath);
|
await fs.ensureDir(entryPath);
|
||||||
} else {
|
} else {
|
||||||
@@ -156,6 +162,7 @@ async function downloadAndExtractTar(url: string, targetDir: string) {
|
|||||||
const tmpFilePath = `${targetDir}.tmp`;
|
const tmpFilePath = `${targetDir}.tmp`;
|
||||||
const buffer = Buffer.from(await res.arrayBuffer());
|
const buffer = Buffer.from(await res.arrayBuffer());
|
||||||
await fs.writeFile(tmpFilePath, buffer);
|
await fs.writeFile(tmpFilePath, buffer);
|
||||||
|
await fs.ensureDir(targetDir);
|
||||||
|
|
||||||
// Execute tar CLI to extract
|
// Execute tar CLI to extract
|
||||||
try {
|
try {
|
||||||
@@ -1560,6 +1567,8 @@ export const _getFactorioVersion = getFactorioVersion;
|
|||||||
export const _versionOrder = versionOrder;
|
export const _versionOrder = versionOrder;
|
||||||
export const _findVersion = findVersion;
|
export const _findVersion = findVersion;
|
||||||
export const _listFactorioVersions = listFactorioVersions;
|
export const _listFactorioVersions = listFactorioVersions;
|
||||||
|
export const _downloadAndExtractZip = downloadAndExtractZip;
|
||||||
|
export const _downloadAndExtractTar = downloadAndExtractTar;
|
||||||
export const _randomDynamicPort = randomDynamicPort;
|
export const _randomDynamicPort = randomDynamicPort;
|
||||||
export const _generatePassword = generatePassword;
|
export const _generatePassword = generatePassword;
|
||||||
export const _parseOutput = parseOutput;
|
export const _parseOutput = parseOutput;
|
||||||
+73
-12
@@ -8,6 +8,7 @@ const hostServer = require("@clusterio/host/dist/node/src/server");
|
|||||||
const lib = require("@clusterio/lib");
|
const lib = require("@clusterio/lib");
|
||||||
const { wait } = lib;
|
const { wait } = lib;
|
||||||
const { testLines } = require("../lib/factorio/lines");
|
const { testLines } = require("../lib/factorio/lines");
|
||||||
|
const { slowTest } = require("../integration");
|
||||||
|
|
||||||
|
|
||||||
describe("host/server", function() {
|
describe("host/server", function() {
|
||||||
@@ -57,11 +58,12 @@ describe("host/server", function() {
|
|||||||
assert.equal(dir, path.join(installDir, "data"));
|
assert.equal(dir, path.join(installDir, "data"));
|
||||||
assert.equal(version, "0.1.2");
|
assert.equal(version, "0.1.2");
|
||||||
});
|
});
|
||||||
it("should search given directory for latest Factorio install", async function() {
|
it("should reject if the version does not match", async function() {
|
||||||
const installDir = path.join("test", "file", "factorio");
|
let installDir = path.join("test", "file", "0.1.1");
|
||||||
const [dir, version] = await hostServer._findVersion(installDir, "latest");
|
await assert.rejects(
|
||||||
assert.equal(dir, path.join(installDir, "0.1.2", "data"));
|
hostServer._findVersion(installDir, "0.1.2"),
|
||||||
assert.equal(version, "0.1.2");
|
new Error("Unable to find Factorio version 0.1.2")
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
describe("mutli install", function() {
|
describe("mutli install", function() {
|
||||||
@@ -91,13 +93,13 @@ describe("host/server", function() {
|
|||||||
new Error("Unable to find Factorio version 0.1.3")
|
new Error("Unable to find Factorio version 0.1.3")
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
it("should reject if no factorio install was found", async function() {
|
||||||
it("should reject if no factorio install was found", async function() {
|
let installDir = path.join("test", "file");
|
||||||
let installDir = path.join("test", "file");
|
await assert.rejects(
|
||||||
await assert.rejects(
|
hostServer._findVersion(installDir, "0.0.0"),
|
||||||
hostServer._findVersion(installDir, "latest"),
|
new Error(`Unable to find any Factorio install in ${installDir}`)
|
||||||
new Error(`Unable to find any Factorio install in ${installDir}`)
|
);
|
||||||
);
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -120,6 +122,50 @@ describe("host/server", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("downloadAndExtractZip", function() {
|
||||||
|
let _fetch;
|
||||||
|
beforeEach(function() {
|
||||||
|
_fetch = global.fetch;
|
||||||
|
});
|
||||||
|
afterEach(function() {
|
||||||
|
global.fetch = _fetch;
|
||||||
|
});
|
||||||
|
|
||||||
|
it("works", async function() {
|
||||||
|
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 hostServer._downloadAndExtractZip(url, path.join(downloads, "zip"));
|
||||||
|
assert.ok(await fs.exists(path.join(downloads, "zip", "packages", "controller", "package.json")));
|
||||||
|
});
|
||||||
|
it("errors and bad status", async function() {
|
||||||
|
global.fetch = () => ({ ok: false, status: -1, statusText: "Fetch called" });
|
||||||
|
await assert.rejects(hostServer._downloadAndExtractZip("url does not matter"), /-1 Fetch called/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("downloadAndExtractTar", function() {
|
||||||
|
let _fetch;
|
||||||
|
beforeEach(function() {
|
||||||
|
_fetch = global.fetch;
|
||||||
|
});
|
||||||
|
afterEach(function() {
|
||||||
|
global.fetch = _fetch;
|
||||||
|
});
|
||||||
|
|
||||||
|
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 hostServer._downloadAndExtractTar(url, path.join(downloads, "tar"));
|
||||||
|
assert.ok(await fs.exists(path.join(downloads, "tar", "packages", "controller", "package.json")));
|
||||||
|
});
|
||||||
|
it("errors and bad status", async function() {
|
||||||
|
global.fetch = () => ({ ok: false, status: -1, statusText: "Fetch called" });
|
||||||
|
await assert.rejects(hostServer._downloadAndExtractZip("url does not matter"), /-1 Fetch called/);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("randomDynamicPort()", function() {
|
describe("randomDynamicPort()", function() {
|
||||||
it("should return a port number", function() {
|
it("should return a port number", function() {
|
||||||
let port = hostServer._randomDynamicPort();
|
let port = hostServer._randomDynamicPort();
|
||||||
@@ -289,6 +335,7 @@ describe("host/server", function() {
|
|||||||
});
|
});
|
||||||
afterEach(function() {
|
afterEach(function() {
|
||||||
global.fetch = _fetch;
|
global.fetch = _fetch;
|
||||||
|
server._factorioDir = path.join("test", "file", "factorio");
|
||||||
Object.defineProperty(process, "platform", {
|
Object.defineProperty(process, "platform", {
|
||||||
value: _platform,
|
value: _platform,
|
||||||
});
|
});
|
||||||
@@ -552,6 +599,20 @@ describe("host/server", function() {
|
|||||||
assert.ok(logLine.endsWith("starting download..."));
|
assert.ok(logLine.endsWith("starting download..."));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
it("should download a version correctly (live api)", async function() {
|
||||||
|
slowTest(this);
|
||||||
|
if (_platform !== "linux") {
|
||||||
|
this.skip();
|
||||||
|
}
|
||||||
|
|
||||||
|
server._factorioDir = path.join("test", "file", "factorioDownload");
|
||||||
|
server._targetVersion = "latest";
|
||||||
|
await server.checkForUpdates([{
|
||||||
|
stable: true,
|
||||||
|
version: "2.0.73",
|
||||||
|
headlessUrl: "https://www.factorio.com/get-download/2.0.73/headless/linux64",
|
||||||
|
}]);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user