Newer
Older
diosmosis
a validé
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
/*!
* Piwik - Web Analytics
*
* UI screenshot test runner Application class
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
var fs = require('fs'),
path = require('./path'),
DiffViewerGenerator = require('./diff-viewer').DiffViewerGenerator
var walk = function (dir, pattern, result) {
result = result || [];
fs.list(dir).forEach(function (item) {
if (item == '.'
|| item == '..'
) {
return;
}
var wholePath = path.join(dir, item);
if (fs.isDirectory(wholePath)) {
walk(wholePath, pattern, result);
} else if (wholePath.match(pattern)) {
result.push(wholePath);
}
});
return result;
};
var Application = function () {
this.runner = null;
this.diffViewerGenerator = new DiffViewerGenerator(path.join(uiTestsDir, config.screenshotDiffDir));
};
Application.prototype.printHelpAndExit = function () {
console.log("Usage: phantomjs run-tests.js [options] [test-files]");
console.log();
console.log("Available options:");
console.log(" --help: Prints this message.");
console.log(" --persist-fixture-data: Persists test data in a database and does not execute tear down.");
console.log(" After the first run, the database setup will not be called, which");
console.log(" Makes running tests faster.");
console.log(" --keep-symlinks: If supplied, the recursive symlinks created in tests/PHPUnit/proxy");
console.log(" aren't deleted after tests are run. Specify this option if you'd like");
console.log(" to view pages phantomjs captures in a browser.");
console.log(" --print-logs: Prints webpage logs even if tests succeed.");
console.log(" --store-in-ui-tests-repo: Stores processed screenshots within the UI tests repository even if");
console.log(" the tests are in another plugin. For use with travis build.");
console.log(" --use-github-expected: Only show the github expected file links in diffviewer.html output.");
console.log(" For use with travis build.");
diosmosis
a validé
phantom.exit(0);
};
diosmosis
a validé
Application.prototype.init = function () {
var app = this;
// overwrite describe function so we can inject the base directory of a suite
var oldDescribe = describe;
describe = function () {
var suite = oldDescribe.apply(null, arguments);
suite.baseDirectory = !options['store-in-ui-tests-repo'] && app.currentModulePath.match(/\/plugins\//)
? path.dirname(app.currentModulePath)
: uiTestsDir
;
diosmosis
a validé
return suite;
};
};
diosmosis
a validé
Application.prototype.loadTestModules = function () {
diosmosis
a validé
var self = this,
pluginDir = path.join(PIWIK_INCLUDE_PATH, 'plugins');
diosmosis
a validé
// find all installed plugins
var plugins = fs.list(pluginDir).map(function (item) {
return path.join(pluginDir, item);
}).filter(function (path) {
return fs.isDirectory(path) && !path.match(/\/\.*$/);
});
// load all UI tests we can find
var modulePaths = walk(uiTestsDir, /_spec\.js$/);
plugins.forEach(function (pluginPath) {
walk(path.join(pluginPath, 'tests'), /_spec\.js$/, modulePaths);
});
modulePaths.forEach(function (path) {
diosmosis
a validé
self.currentModulePath = path;
diosmosis
a validé
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
require(path);
});
// filter suites to run
if (options.tests.length) {
mocha.suite.suites = mocha.suite.suites.filter(function (suite) {
return options.tests.indexOf(suite.title) != -1;
});
}
};
Application.prototype.runTests = function () {
var self = this;
// make sure all necessary directories exist (symlinks handled by PHP since phantomjs can't create any)
var dirsToCreate = [
path.join(PIWIK_INCLUDE_PATH, 'tmp/sessions')
];
dirsToCreate.forEach(function (path) {
if (!fs.isDirectory(path)) {
fs.makeTree(path);
}
});
// remove existing diffs
fs.list(config.screenshotDiffDir).forEach(function (item) {
var file = path.join(uiTestsDir, config.screenshotDiffDir, item);
if (fs.exists(file)
&& item.slice(-4) == '.png'
) {
fs.remove(file);
}
});
this.setupDatabase();
};
Application.prototype.setupDatabase = function () {
console.log("Setting up database...");
var self = this,
setupFile = path.join("./support", "setupDatabase.php"),
processArgs = [setupFile, "--server=" + JSON.stringify(config.phpServer)];
if (options['persist-fixture-data']) {
processArgs.push('--persist-fixture-data');
}
diosmosis
a validé
if (options['drop']) {
processArgs.push('--drop');
}
diosmosis
a validé
var child = require('child_process').spawn(config.php, processArgs);
child.stdout.on("data", function (data) {
fs.write("/dev/stdout", data, "w");
});
child.stderr.on("data", function (data) {
fs.write("/dev/stderr", data, "w");
});
child.on("exit", function (code) {
if (code) {
console.log("\nERROR: Failed to setup database!");
phantom.exit(-1);
} else {
self.doRunTests();
}
});
};
Application.prototype.doRunTests = function () {
var self = this;
diosmosis
a validé
// run tests
this.runner = mocha.run(function () {
// remove symlinks
if (!options['keep-symlinks']) {
var symlinks = ['libs', 'plugins', 'tests', 'piwik.js'];
diosmosis
a validé
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
symlinks.forEach(function (item) {
var file = path.join(uiTestsDir, '..', 'proxy', item);
if (fs.exists(file)) {
fs.remove(file);
}
});
}
// build diffviewer
self.diffViewerGenerator.checkImageMagickCompare(function () {
self.diffViewerGenerator.generate(function () {
if (options['persist-fixture-data']) {
self.finish();
} else {
// teardown database
self.tearDownDatabase();
}
});
});
});
};
Application.prototype.tearDownDatabase = function () {
console.log("Tearing down database...");
var self = this,
teardownFile = path.join("./support", "teardownDatabase.php"),
child = require('child_process').spawn(config.php, [teardownFile, "--server=" + JSON.stringify(config.phpServer)]);
child.stdout.on("data", function (data) {
fs.write("/dev/stdout", data, "w");
});
child.stderr.on("data", function (data) {
fs.write("/dev/stderr", data, "w");
});
child.on("exit", function (code) {
if (code) {
console.log("\nERROR: Failed to teardown database!");
phantom.exit(-2);
} else {
self.finish();
}
});
};
Application.prototype.finish = function () {
phantom.exit(this.runner.failures);
};
exports.Application = new Application();