diff --git a/package.json b/package.json index 8d33925a..541a855a 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "root", "private": true, "scripts": { - "test": "mocha --exit --check-leaks -R spec --recursive test plugins/*/test", + "test": "mocha --exit --check-leaks -R spec --recursive test plugins/global_chat/test plugins/research_sync/test plugins/statistics_exporter/test plugins/subspace_storage/test", "fast-test": "FAST_TEST=y npm test", "cover": "nyc npm test", "fast-cover": "FAST_TEST=y nyc npm test", diff --git a/packages/lib/factorio/server.js b/packages/lib/factorio/server.js index d2c764a6..896860b1 100644 --- a/packages/lib/factorio/server.js +++ b/packages/lib/factorio/server.js @@ -11,24 +11,9 @@ const ini = require("ini"); const rconClient = require("rcon-client"); const libErrors = require("@clusterio/lib/errors"); const { logger } = require("@clusterio/lib/logging"); +const { escapeRegExp } = require("@clusterio/lib/helpers"); -/** - * Escapes text for inclusion in a RegExp - * - * Adds \ character in front of special meta characters in the passsed in - * text so that it can be embedded into a RegExp and only match the text. - * - * See https://stackoverflow.com/a/9310752 - * - * @param {string} text - Text to escape RegExp meta chars in. - * @returns {string} escaped text. - * @inner - */ -function escapeRegExp(text) { - return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); -} - /** * Determines the version of Factorio the datadir is pointing to by * reading the changelog.txt in it. diff --git a/packages/lib/helpers.js b/packages/lib/helpers.js index cbbed42e..17464ec5 100644 --- a/packages/lib/helpers.js +++ b/packages/lib/helpers.js @@ -75,9 +75,26 @@ async function readStream(stream) { } +/** + * Escapes text for inclusion in a RegExp + * + * Adds \ character in front of special meta characters in the passsed in + * text so that it can be embedded into a RegExp and only match the text. + * + * See https://stackoverflow.com/a/9310752 + * + * @param {string} text - Text to escape RegExp meta chars in. + * @returns {string} escaped text. + */ +function escapeRegExp(text) { + return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); +} + + module.exports = { basicType, wait, timeout, readStream, + escapeRegExp, }; diff --git a/test/lib/helpers.js b/test/lib/helpers.js index b9671a93..5b5159f7 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -23,7 +23,8 @@ describe("lib/helpers", function() { await libHelpers.wait(100); let duration = Number(hrtime() - startNs) / 1e6; - assert(duration > 90 && duration < 110, `duration waited (${duration}ms) 10% of expected (100ms)`); + // On windows the time waited is normally up to 15 ms late with outliers in the 20ms range. + assert(duration > 90 && duration < 130, `duration (${duration}ms) not within (90-130)`); }); }); diff --git a/test/lib/plugin_loader.js b/test/lib/plugin_loader.js index c5ce7332..b5098955 100644 --- a/test/lib/plugin_loader.js +++ b/test/lib/plugin_loader.js @@ -4,6 +4,7 @@ const fs = require("fs-extra"); const path = require("path"); const libPluginLoader = require("@clusterio/lib/plugin_loader"); +const { escapeRegExp } = require("@clusterio/lib/helpers"); describe("lib/plugin_loader", function() { @@ -34,7 +35,7 @@ describe("lib/plugin_loader", function() { it("should throw on missing plugin", async function() { await assert.rejects( libPluginLoader.loadPluginInfos(new Map([["missing", missingPlugin]]), []), - new RegExp(`^Error: PluginError: Cannot find module '${missingPlugin}/info'`) + new RegExp(`^Error: PluginError: Cannot find module '${escapeRegExp(missingPlugin)}/info'`) ); }); it("should load test plugin", async function() {