Skip to content

Commit

Permalink
Use any config file: backstop.json, backstop.js
Browse files Browse the repository at this point in the history
  • Loading branch information
klodoma committed May 1, 2024
1 parent 6d0e5fe commit 752ba64
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 59 deletions.
54 changes: 54 additions & 0 deletions capture/config.default.js
Original file line number Diff line number Diff line change
@@ -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
};
54 changes: 0 additions & 54 deletions capture/config.default.json

This file was deleted.

2 changes: 1 addition & 1 deletion cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function main () {
boolean: ['h', 'help', 'v', 'version', 'i', 'docker'],
string: ['config'],
default: {
config: 'backstop.json'
config: 'backstop'
}
});

Expand Down
25 changes: 23 additions & 2 deletions core/command/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}));
Expand Down
2 changes: 1 addition & 1 deletion core/util/extendConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion test/core/util/makeConfig_it_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down

0 comments on commit 752ba64

Please sign in to comment.