-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (90 loc) · 3.58 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"use strict";
// libs
const globby = require('globby');
const _ = require('lodash');
const backstop = require('backstopjs');
const path = require('path');
const jsonfile = require('jsonfile');
// settings
const defaultSettings = require('./conf/commonSettings');
const defaultScenario = require('./conf/defaultScenario');
const defaultConfig = require('./conf/defaultConfig');
const bracco = {
init(settings) {
//setup vars (testhost, refhost, settings..)
this.conf = Object.assign({}, defaultSettings, settings);
},
buildConfig(options) {
let scenarios = [];
let conf = this.conf;
let tags = (options.tags) ? options.tags : false;
tags = (tags && !Array.isArray(tags)) ? [tags] : tags;
let labels = (options.labels) ? options.labels : false;
labels = (labels && !Array.isArray(labels)) ? [labels] : labels;
let scenarioNames = (options.scenario) ? options.scenario : false;
scenarioNames = (scenarioNames && !Array.isArray(scenarioNames)) ? [scenarioNames] : scenarioNames;
// build path for globby
let root = path.normalize(process.cwd());
let scenariosPaths = [];
if (scenarioNames.length > 0) {
scenariosPaths = scenarioNames.map(e => {
return root + '/scenarios/' + e + '/*.js'
})
} else {
scenariosPaths.push(root + '/scenarios/**/*.js');
}
// get scenarios from js files
globby.sync(scenariosPaths).map(e => {
let getScenario = require(e);
let newScenarios = [];
if (tags && tags.length > 0) {
getScenario(conf)
.filter(scenario => scenario.hasOwnProperty('tags'))
.map(scenario => {
scenario.tags.map(cTag => {
let match = _.findIndex(tags, o => o === cTag );
if (match !== -1) {
newScenarios.push(Object.assign({}, defaultScenario, scenario));
}
});
});
} else {
newScenarios = getScenario(conf).map(scenario => Object.assign({}, defaultScenario, scenario));
}
if(labels && labels.length > 0) {
newScenarios = newScenarios
.filter(scenario => scenario.hasOwnProperty('label'))
.filter(scenario => {
return labels.indexOf(scenario.label) === 0
});
}
scenarios = scenarios.concat(newScenarios);
});
// merge with default config of backstop
let config = Object.assign({}, defaultConfig);
// add scenario to config
config.scenarios = config.scenarios.concat(scenarios);
// write config in dafault backstop json
let jsonFile = root + '/backstop.json';
jsonfile.writeFileSync(jsonFile, config);
},
reference(options, test) {
this.buildConfig(options);
return backstop('reference')
.then(() => {
if(test) {
backstop('test');
}
}).catch((err) => {
if(err) {
console.log("Error: ", err);
}
return Promise.reject(err);
});
},
test(options) {
this.buildConfig(options);
return backstop('test');
}
};
module.exports = bracco;