forked from garris/BackstopJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgulpfile.js
307 lines (198 loc) · 8.63 KB
/
gulpfile.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
var gulp = require('gulp');
var del = require('del');
var open = require("gulp-open");
var rename = require("gulp-rename");
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var fs = require('fs');
var fse = require('fs-extra')
var path = require("path");
var serverPidFile = __dirname+'/server.pid';
var bitmaps_reference = __dirname+'/bitmaps_reference';
var bitmaps_test = 'bitmaps_test';
var backstopConfigFileName = path.join(__dirname, '../..', 'backstop.json')
var captureConfigFileName = __dirname+'/capture/config.json';
var captureConfigFileNameCache = __dirname+'/capture/.config.json.cache';
var captureConfigFileNameDefault = __dirname+'/capture/config.default.json';
var comparePath = __dirname+'/compare';
var compareConfigFileName = comparePath+'/config.json';
var compareReportURL = 'http://localhost:3001/compare/';
var activeCaptureConfigPath = '';
var isWin = /^win/.test(process.platform);
if(!fs.existsSync(backstopConfigFileName)){
// console.log('\nCould not find a valid config file.');
// console.log('\nUsing demo configuration.');
console.log('\nTo run your own tests create a config here...\n ==> '+backstopConfigFileName);
console.log('\nRun `$ gulp genConfig` to generate a config template file in this location.\n')
activeCaptureConfigPath = captureConfigFileNameDefault;
}else{
// console.log('\nBackstopJS Config loaded.\n')
activeCaptureConfigPath = backstopConfigFileName;
}
//this is for compare/genBitmaps.js until we can pass the active location via env
fse.copySync(activeCaptureConfigPath,captureConfigFileName);
//Default config for report (compare) app
var configDefault = {
"testPairs": []
};
var genDefaultCompareConfig = function genDefaultCompareConfig(){
fs.writeFileSync(compareConfigFileName, JSON.stringify(configDefault,null,2));
}
if(!fs.existsSync(compareConfigFileName)){
console.log('No compare/config.json file exists. Creating default file.')
genDefaultCompareConfig();
}
var config = JSON.parse(fs.readFileSync(compareConfigFileName, 'utf8'));
if(!config.testPairs||config.testPairs.length==0){
//console.log('No config data found.');
config=configDefault;
}
var watcher = null;
//MANAGE DEPENDENCIES
gulp.task('init',function(cb){
//load missing bower components
if(!fs.existsSync(comparePath+'/bower_components')){
console.log('\nBackstopJS needs to update bower_components, please hang on...\n');
var bowerProcess = (process.platform === "win32" ? "bower.cmd" : "bower");
spawn(bowerProcess,['install'],{cwd:comparePath}).on('error', function(){console.log('\nBower process fail. :( Please report this bug on github.\n');});
}
cb();
});
//GENERATE CAPTURE CONFIG
gulp.task('genConfig',function(){
return gulp.src(captureConfigFileNameDefault)
.pipe(rename(backstopConfigFileName))
.pipe(gulp.dest('/'));
});
//FIRST CLEAN REFERENCE DIR. THEN TEST
gulp.task('reference', ['clean','bless'], function() {
gulp.run('test');
console.log('reference has run.')
});
//CLEAN THE bitmaps_reference DIRECTORY
gulp.task('clean', function (cb) {
del([
bitmaps_reference + '/**'
], cb);
genDefaultCompareConfig();
console.log('bitmaps_reference was cleaned.');
});
//BLESS THE CURRENT CAPTURE CONFIG
gulp.task('bless',function(){
return gulp.src(activeCaptureConfigPath)
.pipe(rename(captureConfigFileNameCache))
.pipe(gulp.dest('/'));
});
gulp.task('echo',function(){
var genReferenceMode = false;
var tests = ['capture/echoFiles.js'];
// var args = ['test'].concat(tests); //this is required if using casperjs test option
var casperChild = spawn('casperjs', tests);//use args here to add test option to casperjs execute stmt
casperChild.stdout.on('data', function (data) {
console.log('CasperJS:', data.toString().slice(0, -1)); // Remove \n
});
casperChild.on('close', function (code) {
var success = code === 0; // Will be 1 in the event of failure
var result = (success)?'Echo files completed.':'Echo files failed with code: '+code;
console.log('\n'+result);
//exit if there was some kind of failure in the casperChild process
if(code!=0)return false;
});
})
//This task will generate a date-named directory with DOM screenshot files as specified in `./capture/config.json` followed by running a report.
//NOTE: If there is no bitmaps_reference directory or if the bitmaps_reference directory is empty then a new batch of reference files will be generated in the bitmaps_reference directory. Reporting will be skipped in this case.
gulp.task('test',['init'], function () {
// genReferenceMode contains the state which switches test or reference file generation modes
var genReferenceMode = false;
// THIS IS THE BLOCK WHICH SWITCHES US INTO "GENERATE REFERENCE" MODE. I'D RATHER SOMETHING MORE EXPLICIT THO. LIKE AN ENV PARAMETER...
if(!fs.existsSync(bitmaps_reference)){
console.log('\nGenerating reference files.\n');
genReferenceMode = true;
}
//IF WE ARE IN TEST GENERATION MODE -- LOOK FOR CHANGES IN THE 'CAPTURE CONFIG'.
if(!genReferenceMode){
// TEST FOR CAPTURE CONFIG CACHE -- CREATE IF ONE DOESN'T EXIST (If a .cache file does not exist it is likely a scenario where the user is testing shared reference files in a new context. e.g different dev env.).
if(fs.existsSync(captureConfigFileNameCache)){
//COMPARE CAPTURE CONFIG AGAINST THE CACHED VERSION. PROMPT IF DIFFERENT.
var config = fs.readFileSync(activeCaptureConfigPath, 'utf8');
var cache = fs.readFileSync(captureConfigFileNameCache, 'utf8');
if(config !== cache){
console.log('\nIt looks like the reference configuration has been changed since last reference batch.');
console.log('Please run `$ gulp reference` to generate a fresh set of reference files')
console.log('or run `$ gulp bless` then `$ gulp test` to enable testing with this configuration.\n\n')
return;
}
}else{
gulp.run('bless');
}
}
// AT THIS POINT WE ARE EITHER RUNNING IN "TEST" OR "REFERENCE" MODE
var tests = ['capture/genBitmaps.js'];
// var args = ['test'].concat(tests); //this is required if using casperjs test option
// var casperChild = spawn('casperjs', tests);//use args here to add test option to casperjs execute stmt
var casperProcess = (process.platform === "win32" ? "casperjs.cmd" : "casperjs");
var casperChild = spawn(casperProcess, tests);
casperChild.stdout.on('data', function (data) {
console.log('CasperJS:', data.toString().slice(0, -1)); // Remove \n
});
casperChild.on('close', function (code) {
var success = code === 0; // Will be 1 in the event of failure
var result = (success)?'Bitmap file generation completed.':'Testing script failed with code: '+code;
console.log('\n'+result);
//exit if there was some kind of failure in the casperChild process
if(code!=0){
console.log('\nLooks like an error occured. You may want to try running `$ gulp echo`. This will echo the requested test URL output to the console. You can check this output to verify that the file requested is indeed being received in the expected format.');
return false;
};
var resultConfig = JSON.parse(fs.readFileSync(compareConfigFileName, 'utf8'));
if(genReferenceMode || !resultConfig.testPairs||resultConfig.testPairs.length==0){
console.log('\nRun `$ gulp test` to generate diff report.\n')
}else{
gulp.run('report');
}
});
});
gulp.task('report',['start'],function(){
setTimeout(function(){gulp.run('openReport')},100);
})
gulp.task("openReport", function(){
console.log('\nOpening report -> ',compareReportURL);
var options = {
url: compareReportURL
,app: isWin ? "chrome" : "Google Chrome"
};
gulp.src(compareConfigFileName)
.pipe(open("",options));
});
//THIS WILL START THE LOCAL WEBSERVER
//IF ALREADY STARTED IT WILL NOT TRY TO START AGAIN
gulp.task("start",function(){
fs.readFile(serverPidFile, function(err,data){
if(data){
exec('kill -0 '+data,function(error, stdout, stderr){
if(/no such process/i.test(stderr))
start();
});
}else{
start();
}
});
function start(){
var serverHook = spawn('node', ['server.js'], {detached: true, stdio:'ignore'});
serverHook.unref();
fs.writeFileSync(serverPidFile,serverHook.pid);
console.log('\nServer launched in background with PID: '+serverHook.pid)
console.log('NOTE: Sever will auto-shutdown (default time 15 mins). See documentation for more info.\n')
}
});
gulp.task("stop",function(){
fs.readFile(serverPidFile, function(err,pid){
if(pid){
exec('kill '+pid,function(error, stdout, stderr){
console.log('Stopped PID:'+pid)
fs.unlinkSync(serverPidFile);
});
}
});
});
gulp.task('default',function(){});