From 95ec3096e0830af0724078d33d23ee4e2ddddbea Mon Sep 17 00:00:00 2001 From: Karsten Hassel Date: Fri, 22 Sep 2023 14:45:46 +0200 Subject: [PATCH] avoid overriding `config.js` when running tests (#3205) solves #3201 --- CHANGELOG.md | 1 + tests/e2e/helpers/weather-functions.js | 3 +-- tests/e2e/modules/weather_current_spec.js | 2 ++ tests/e2e/modules/weather_forecast_spec.js | 2 ++ tests/e2e/modules/weather_hourly_spec.js | 2 ++ tests/e2e/template_spec.js | 6 ++++++ tests/electron/helpers/weather-setup.js | 5 ++--- tests/electron/modules/weather_spec.js | 2 ++ tests/utils/weather_mocker.js | 15 ++++++++++++--- 9 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f06974f635..e59f84c60d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ _This release is scheduled to be released on 2023-10-01._ - Respect width/height (no fullscreen) if set in electronOptions (together with `fullscreen: false`) in `config.js` (#3174) - Fix: AnimateCSS merge hide() and show() animated css class when we do multiple call - Fix `Uncaught SyntaxError: Identifier 'getCorsUrl' has already been declared (at utils.js:1:1)` when using `clock` and `weather` module (#3204) +- Fix overriding `config.js` when running tests (#3201) ## [2.24.0] - 2023-07-01 diff --git a/tests/e2e/helpers/weather-functions.js b/tests/e2e/helpers/weather-functions.js index e28eb9de02..02713754cf 100644 --- a/tests/e2e/helpers/weather-functions.js +++ b/tests/e2e/helpers/weather-functions.js @@ -13,7 +13,6 @@ exports.getText = async (element, result) => { }; exports.startApp = async (configFileName, additionalMockData) => { - injectMockData(configFileName, additionalMockData); - await helpers.startApplication(""); + await helpers.startApplication(injectMockData(configFileName, additionalMockData)); await helpers.getDocument(); }; diff --git a/tests/e2e/modules/weather_current_spec.js b/tests/e2e/modules/weather_current_spec.js index 592b3735d0..d97f83094e 100644 --- a/tests/e2e/modules/weather_current_spec.js +++ b/tests/e2e/modules/weather_current_spec.js @@ -1,9 +1,11 @@ const helpers = require("../helpers/global-setup"); const weatherFunc = require("../helpers/weather-functions"); +const { cleanupMockData } = require("../../utils/weather_mocker"); describe("Weather module", () => { afterAll(async () => { await helpers.stopApplication(); + await cleanupMockData(); }); describe("Current weather", () => { diff --git a/tests/e2e/modules/weather_forecast_spec.js b/tests/e2e/modules/weather_forecast_spec.js index 2f10692276..a5ce70d20a 100644 --- a/tests/e2e/modules/weather_forecast_spec.js +++ b/tests/e2e/modules/weather_forecast_spec.js @@ -1,9 +1,11 @@ const helpers = require("../helpers/global-setup"); const weatherFunc = require("../helpers/weather-functions"); +const { cleanupMockData } = require("../../utils/weather_mocker"); describe("Weather module: Weather Forecast", () => { afterAll(async () => { await helpers.stopApplication(); + await cleanupMockData(); }); describe("Default configuration", () => { diff --git a/tests/e2e/modules/weather_hourly_spec.js b/tests/e2e/modules/weather_hourly_spec.js index 3a5f03f138..87fc4411cb 100644 --- a/tests/e2e/modules/weather_hourly_spec.js +++ b/tests/e2e/modules/weather_hourly_spec.js @@ -1,9 +1,11 @@ const helpers = require("../helpers/global-setup"); const weatherFunc = require("../helpers/weather-functions"); +const { cleanupMockData } = require("../../utils/weather_mocker"); describe("Weather module: Weather Hourly Forecast", () => { afterAll(async () => { await helpers.stopApplication(); + await cleanupMockData(); }); describe("Default configuration", () => { diff --git a/tests/e2e/template_spec.js b/tests/e2e/template_spec.js index 3bcc5e4427..46417aea79 100644 --- a/tests/e2e/template_spec.js +++ b/tests/e2e/template_spec.js @@ -1,3 +1,4 @@ +const fs = require("fs"); const helpers = require("./helpers/global-setup"); describe("templated config with port variable", () => { @@ -6,6 +7,11 @@ describe("templated config with port variable", () => { }); afterAll(async () => { await helpers.stopApplication(); + try { + fs.unlinkSync("tests/configs/port_variable.js"); + } catch (err) { + // do nothing + } }); it("should return 200", async () => { diff --git a/tests/electron/helpers/weather-setup.js b/tests/electron/helpers/weather-setup.js index 4dd3cdb2a6..e939af6763 100644 --- a/tests/electron/helpers/weather-setup.js +++ b/tests/electron/helpers/weather-setup.js @@ -13,7 +13,6 @@ exports.getText = async (element, result) => { ).toBe(result); }; -exports.startApp = async (configFileNameName, systemDate) => { - injectMockData(configFileNameName); - await helpers.startApplication("", systemDate); +exports.startApp = async (configFileName, systemDate) => { + await helpers.startApplication(injectMockData(configFileName), systemDate); }; diff --git a/tests/electron/modules/weather_spec.js b/tests/electron/modules/weather_spec.js index fe77743110..811a0e02da 100644 --- a/tests/electron/modules/weather_spec.js +++ b/tests/electron/modules/weather_spec.js @@ -1,9 +1,11 @@ const helpers = require("../helpers/global-setup"); const weatherHelper = require("../helpers/weather-setup"); +const { cleanupMockData } = require("../../utils/weather_mocker"); describe("Weather module", () => { afterEach(async () => { await helpers.stopApplication(); + await cleanupMockData(); }); describe("Current weather with sunrise", () => { diff --git a/tests/utils/weather_mocker.js b/tests/utils/weather_mocker.js index 81e912451e..83279001fa 100644 --- a/tests/utils/weather_mocker.js +++ b/tests/utils/weather_mocker.js @@ -1,5 +1,7 @@ const fs = require("fs"); const path = require("path"); +const util = require("util"); +const exec = util.promisify(require("child_process").exec); const _ = require("lodash"); /** @@ -35,9 +37,16 @@ const injectMockData = (configFileName, extendedData = {}) => { } else { mockWeather = readMockData("current", extendedData); } - let content = fs.readFileSync(path.resolve(`${__dirname}../../../${configFileName}`)).toString(); + let content = fs.readFileSync(configFileName).toString(); content = content.replace("#####WEATHERDATA#####", mockWeather); - fs.writeFileSync(path.resolve(`${__dirname}../../../config/config.js`), content); + const tempFile = configFileName.replace(".js", "_temp.js"); + fs.writeFileSync(tempFile, content); + return tempFile; }; -module.exports = { injectMockData }; +const cleanupMockData = async () => { + const tempDir = path.resolve(`${__dirname}/../configs`).toString(); + await exec(`find ${tempDir} -type f -name *_temp.js -delete`); +}; + +module.exports = { injectMockData, cleanupMockData };