forked from adopted-ember-addons/ember-electron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-runner.js
107 lines (97 loc) · 3.97 KB
/
test-runner.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
/**
* THIS FILE IS DEPRECATED. It has been moved to lib/test-support/ and
* modified, but is left here so apps that haven't rerun the blueprint to
* update the test-runner.js/test-main.js interactions will still function.
**/
//
// This script does double-duty. It can be included from testem-electron.js
// to define an Electron test runner like so:
//
// // testem.js
// module.exports = {
// "launchers": {
// "Electron": require("ember-electron/test-runner")
// },
// "launch_in_ci": [
// "Electron"
// ],
// "launch_in_dev": [
// "Electron"
// ]
// }
//
// The runner is configured to invoke this script as a command-line executable
// with the proper arguments to run electron and communicate back to testem.
//
module.exports = {
'exe': 'node',
'args': [__filename, '<cwd>', '<baseUrl>', '<testPage>', '<id>'],
'protocol': 'browser',
};
if (require.main === module) {
let path = require('path');
let fs = require('fs');
let url = require('url');
let fileUrl = require('file-url');
let treeKill = require('tree-kill');
let { start: efStart } = require('electron-forge');
let [, , buildDir, baseUrl, testPageUrl, id] = process.argv;
let emberAppDir = path.join(buildDir, 'ember');
let baseObj = url.parse(baseUrl);
let testPageObj = url.parse(testPageUrl, true);
// Build testem.js URL
baseObj.pathname = '/testem.js';
let testemJsUrl = url.format(baseObj);
// Process the HTML to:
// * inject our getTestemId() script so the testem client can extract the ID
// from the query params and be able to communicate with the testem server
// * rewrite the testem.js script to use an absolute URL pointing to the
// testem server
let testPagePath = path.join(emberAppDir, path.join.apply(null, testPageObj.pathname.split('/')));
let htmlContent = fs.readFileSync(testPagePath, 'utf8').toString();
htmlContent = htmlContent.replace(/^(\s*)<head>/m, [
'$1<head>',
'$1 <script>',
'$1 window.getTestemId = function() {',
// FIXME: It should be safe to replace "\?" with "?" below due to context -- need to check though
// eslint-disable-next-line no-useless-escape
'$1 var match = window.location.search.match(/[\?&]testemId=([^\?&]+)/);',
'$1 return match ? match[1] : null;',
'$1 }',
'$1 </script>',
].join('\n'));
htmlContent = htmlContent.replace(/src="[^"]*testem\.js"/, `src="${ testemJsUrl }"`);
let htmlPath = path.join(emberAppDir, 'tests', 'index-electron.html');
fs.writeFileSync(htmlPath, htmlContent, 'utf8');
// Build a file: URL to our temp file, preserving query params from the test
// page and adding the testem id
let htmlFileObj = url.parse(fileUrl(htmlPath));
htmlFileObj.query = testPageObj.query;
htmlFileObj.query.testemId = id;
let testUrl = url.format(htmlFileObj);
// On windows the testUrl argv is truncated before the first '&' by the time
// it reaches our main.js. This appears to have something to do with how
// electron-compile (I think) uses a batch script to invoke its cli.js, and
// the fact that '&' is a special shell character. So we do our own cheesy
// workaround.
testUrl = testUrl.replace(/&/g, '__amp__');
// Start electron
efStart({ appPath: buildDir, dir: buildDir, args: [testUrl] }).then(({ pid }) => {
// Clean up when we're killed
process.on('SIGTERM', () => {
treeKill(pid);
});
});
} else {
// We put this here because when this script is invoked as an executable,
// testem squashes the output so the warning isn't visible to the user. But
// when testem-electron.js requires this script, the output is going to
// stdout and is visible to the user.
const UI = require('console-ui');
const ui = new UI({
inputStream: process.stdin,
outputStream: process.stdout,
errorStream: process.stderr,
});
ui.writeDeprecateLine('This test-runner.js is deprecated. Please read https://github.com/felixrieseberg/ember-electron/blob/master/docs/faq/test-runner-deprecation.md for more information.');
}