From 752ba64c84cdd98e130d5db280e05b584074c566 Mon Sep 17 00:00:00 2001 From: Andrei Bintintan Date: Wed, 1 May 2024 08:49:10 +0200 Subject: [PATCH] Use any config file: backstop.json, backstop.js --- capture/config.default.js | 54 ++++++++++++++++++++++++++++ capture/config.default.json | 54 ---------------------------- cli/index.js | 2 +- core/command/init.js | 25 +++++++++++-- core/util/extendConfig.js | 2 +- test/core/util/makeConfig_it_spec.js | 2 +- 6 files changed, 80 insertions(+), 59 deletions(-) create mode 100644 capture/config.default.js delete mode 100644 capture/config.default.json diff --git a/capture/config.default.js b/capture/config.default.js new file mode 100644 index 000000000..79c95efc8 --- /dev/null +++ b/capture/config.default.js @@ -0,0 +1,54 @@ +module.exports = { + id: 'backstop_default', + viewports: [ + { + label: 'phone', + width: 320, + height: 480 + }, + { + label: 'tablet', + width: 1024, + height: 768 + } + ], + onBeforeScript: 'puppet/onBefore.js', + onReadyScript: 'puppet/onReady.js', + scenarios: [ + { + label: 'BackstopJS Homepage', + cookiePath: 'backstop_data/engine_scripts/cookies.json', + url: 'https://garris.github.io/BackstopJS/', + referenceUrl: '', + readyEvent: '', + readySelector: '', + delay: 0, + hideSelectors: [], + removeSelectors: [], + hoverSelector: '', + clickSelector: '', + postInteractionWait: 0, + selectors: [], + selectorExpansion: true, + expect: 0, + misMatchThreshold: 0.1, + requireSameDimensions: true + } + ], + paths: { + bitmaps_reference: 'backstop_data/bitmaps_reference', + bitmaps_test: 'backstop_data/bitmaps_test', + engine_scripts: 'backstop_data/engine_scripts', + html_report: 'backstop_data/html_report', + ci_report: 'backstop_data/ci_report' + }, + report: ['browser'], + engine: 'puppeteer', + engineOptions: { + args: ['--no-sandbox'] + }, + asyncCaptureLimit: 5, + asyncCompareLimit: 50, + debug: false, + debugWindow: false +}; diff --git a/capture/config.default.json b/capture/config.default.json deleted file mode 100644 index 967585f45..000000000 --- a/capture/config.default.json +++ /dev/null @@ -1,54 +0,0 @@ -{ - "id": "backstop_default", - "viewports": [ - { - "label": "phone", - "width": 320, - "height": 480 - }, - { - "label": "tablet", - "width": 1024, - "height": 768 - } - ], - "onBeforeScript": "puppet/onBefore.js", - "onReadyScript": "puppet/onReady.js", - "scenarios": [ - { - "label": "BackstopJS Homepage", - "cookiePath": "backstop_data/engine_scripts/cookies.json", - "url": "https://garris.github.io/BackstopJS/", - "referenceUrl": "", - "readyEvent": "", - "readySelector": "", - "delay": 0, - "hideSelectors": [], - "removeSelectors": [], - "hoverSelector": "", - "clickSelector": "", - "postInteractionWait": 0, - "selectors": [], - "selectorExpansion": true, - "expect": 0, - "misMatchThreshold" : 0.1, - "requireSameDimensions": true - } - ], - "paths": { - "bitmaps_reference": "backstop_data/bitmaps_reference", - "bitmaps_test": "backstop_data/bitmaps_test", - "engine_scripts": "backstop_data/engine_scripts", - "html_report": "backstop_data/html_report", - "ci_report": "backstop_data/ci_report" - }, - "report": ["browser"], - "engine": "puppeteer", - "engineOptions": { - "args": ["--no-sandbox"] - }, - "asyncCaptureLimit": 5, - "asyncCompareLimit": 50, - "debug": false, - "debugWindow": false -} diff --git a/cli/index.js b/cli/index.js index 893ab7f21..ca5ceee4d 100755 --- a/cli/index.js +++ b/cli/index.js @@ -12,7 +12,7 @@ function main () { boolean: ['h', 'help', 'v', 'version', 'i', 'docker'], string: ['config'], default: { - config: 'backstop.json' + config: 'backstop' } }); diff --git a/core/command/init.js b/core/command/init.js index 3b8fc775a..d7913bace 100644 --- a/core/command/init.js +++ b/core/command/init.js @@ -14,9 +14,30 @@ module.exports = { logger.error('ERROR: Can\'t generate a scripts directory. No \'engine_scripts\' path property was found in backstop.json.'); } + const resolveAndWriteConfigFile = (fileName, useJs) => { + const extension = useJs ? '.js' : '.json'; + if (!fileName.endsWith(extension)) { + fileName += extension; + } + + const writeFile = () => { + if (useJs) { + return fs.copy(config.captureConfigFileNameDefault, fileName); + } else { + const configContent = require(config.captureConfigFileNameDefault); + const jsonContent = JSON.stringify(configContent, null, 2); + return fs.writeFile(fileName, `module.exports = ${jsonContent};`); + } + }; + + return writeFile().then(() => fileName); // Return fileName after the operation + }; + + console.log(config); + // Copies a boilerplate config file to the current config file location. - promises.push(fs.copy(config.captureConfigFileNameDefault, config.backstopConfigFileName).then(function () { - logger.log("Configuration file written at '" + config.backstopConfigFileName + "'"); + promises.push(resolveAndWriteConfigFile(config.backstopConfigFileName, config.args.js).then(configFile => { + logger.log("Configuration file written at '" + configFile + "'"); }, function (err) { throw err; })); diff --git a/core/util/extendConfig.js b/core/util/extendConfig.js index 4c5ac9d33..eb856c81d 100644 --- a/core/util/extendConfig.js +++ b/core/util/extendConfig.js @@ -92,7 +92,7 @@ function captureConfigPaths (config) { } const configHash = hash(config); config.captureConfigFileName = path.join(tmpdir, 'capture', configHash + '.json'); - config.captureConfigFileNameDefault = path.join(config.backstop, 'capture', 'config.default.json'); + config.captureConfigFileNameDefault = path.join(config.backstop, 'capture', 'config.default.js'); } function engine (config, userConfig) { diff --git a/test/core/util/makeConfig_it_spec.js b/test/core/util/makeConfig_it_spec.js index dea9de5bd..19b2425a4 100644 --- a/test/core/util/makeConfig_it_spec.js +++ b/test/core/util/makeConfig_it_spec.js @@ -23,7 +23,7 @@ const expectedConfig = { comparePath: path.resolve(backstopDir, 'compare/output'), captureConfigFileNameDefault: path.resolve( backstopDir, - 'capture/config.default.json' + 'capture/config.default.js' ), engine: null, engine_scripts: path.resolve('backstop_data/engine_scripts'),