-
Notifications
You must be signed in to change notification settings - Fork 1
/
compile.js
305 lines (287 loc) · 9.78 KB
/
compile.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
303
304
305
const { promisify } = require("util");
const fs = require("fs");
const writeFile = promisify(fs.writeFile);
const mkdir = promisify(fs.mkdir);
const path = require("path");
const { URL } = require("url");
const { performance } = require("perf_hooks");
qx.Class.define("qxl.testtapper.compile.LibraryApi", {
extend: qx.tool.cli.api.LibraryApi,
members: {
// @overridden
async initialize() {
let yargs = qx.tool.cli.commands.Test.getYargsCommand;
qx.tool.cli.commands.Test.getYargsCommand = () => {
let args = yargs();
args.builder.class = {
describe: "only run tests of this class",
type: "string",
};
args.builder.method = {
describe: "only run tests of this method",
type: "string",
};
args.builder.diag = {
describe: "show diagnostic output",
type: "boolean",
default: false,
};
args.builder.terse = {
describe: "show only summary and errors",
type: "boolean",
default: false,
};
/*
args.builder.coverage = {
describe: "writes coverage infos, only working for chromium yet",
type: "boolean",
default: false
};
*/
args.builder.headless = {
describe: "runs test headless",
type: "boolean",
default: false,
};
args.builder.browsers = {
describe:
"list of browsers to test against, currently supported chromium, firefox, webkit",
type: "string",
};
return args;
};
},
__enviroment: null,
// @overridden
async load() {
let command = this.getCompilerApi().getCommand();
if (command instanceof qx.tool.cli.commands.Test) {
command.addListener("runTests", this.__testIt, this);
if (command.setNeedsServer) {
command.setNeedsServer(true);
}
}
},
__testIt(data) {
let app = this.__getTestApp("qxl.testtapper.Application");
if (!app) {
qx.tool.compiler.Console.error(
"Please install testtapper application in compile.json"
);
return qx.Promise.resolve(false);
}
let result = data.getData();
return this.__runTests(app, result);
},
__runTestInBrowser(browserType, url, app, result) {
return new qx.Promise(async (resolve, reject) => {
try {
const playwright = this.require("playwright");
const { execSync } = require("child_process");
let s = `npx playwright install`;
qx.tool.compiler.Console.info(s);
execSync(s, {
stdio: "inherit"
});
const v8toIstanbul = this.require("v8-to-istanbul");
console.log("TAP version 13");
console.log("# TESTTAPPER: Running tests in " + browserType);
const launchArgs = {
args: ["--no-sandbox", "--disable-setuid-sandbox"],
headless:
app.argv.headless === null
? app.environment["qxl.testtapper.headless"] === null
? true
: app.environment["qxl.testtapper.headless"]
: app.argv.headless,
};
const browser = playwright[browserType];
if (!browser) {
reject(new Error(`unknown browser ${browserType}`));
}
const context = await browser.launch(launchArgs);
const page = await context.newPage();
let cov =
(app.argv.coverage === null
? app.environment["qxl.testtapper.coverage"] === null
? false
: app.environment["qxl.testtapper.coverage"]
: app.argv.coverage) && browserType === "chromium";
if (cov) {
await page.coverage.startJSCoverage();
}
let Ok = 0;
let notOk = 0;
let skipped = 0;
let startTime;
page.on("console", async (msg) => {
let val = msg.text();
// value is serializable
if (val.match(/^\d+\.\.\d+$/)) {
let endTime = performance.now();
let timeDiff = endTime - startTime;
qx.tool.compiler.Console.info(
`DONE testing ${Ok} ok, ${notOk} not ok, ${skipped} skipped - [${timeDiff.toFixed(
0
)} ms]`
);
if (cov) {
qx.tool.compiler.Console.info(
"writing coverage information ..."
);
const coverage = await page.coverage.stopJSCoverage();
const entries = {};
let target = app.maker.getTarget();
let outputDir = target.getOutputDir();
const sourceMapUrl = this.require("source-map-url");
for await (entry of coverage) {
let source;
let sm = sourceMapUrl.getFrom(entry.source);
if (sm) {
sm = sm.split("?")[0];
source = sourceMapUrl.removeFrom(entry.source);
source += `//# sourceMappingURL=${sm}`;
} else {
source = entry.source;
}
let url = new URL(entry.url);
filePath = path.join(process.cwd(), outputDir, url.pathname);
const converter = new v8toIstanbul(filePath, 0, {
source: source,
});
await converter.load();
converter.applyCoverage(entry.functions);
const instanbul = converter.toIstanbul();
entries[filePath] = instanbul[filePath];
}
await mkdir(path.join(process.cwd(), ".nyc_output"), {
recursive: true,
});
await writeFile(
path.join(process.cwd(), ".nyc_output", "out.json"),
JSON.stringify(entries)
);
}
await context.close();
result[app.name][browserType] = {
notOk: notOk,
ok: Ok,
};
result.setExitCode(result.getExitCode() + notOk);
resolve();
} else if (val.match(/^not ok /)) {
notOk++;
qx.tool.compiler.Console.log(val);
} else if (val.includes("# SKIP")) {
skipped++;
if (!app.argv.terse) {
qx.tool.compiler.Console.log(val);
}
} else if (val.match(/^ok\s/)) {
Ok++;
if (!app.argv.terse) {
qx.tool.compiler.Console.log(val);
}
} else if (val.match(/^#/) && app.argv.diag) {
qx.tool.compiler.Console.log(val);
} else if (app.argv.verbose) {
qx.tool.compiler.Console.log(val);
}
});
startTime = performance.now();
page.goto(url.href);
} catch (e) {
reject(e);
}
});
},
async __runTests(app, result) {
let outputDir = "";
if (this.getCompilerApi().getCommand().showStartpage()) {
let target = app.maker.getTarget();
outputDir = target.getOutputDir();
}
let href = `http://localhost:${app.port}/${outputDir}${app.name}/`;
let url = new URL(href);
let s = "";
if (app.argv.method) {
s += "method=" + app.argv.method;
}
if (app.argv.class) {
if (s.length > 0) {
s += "&";
}
s += "class=" + app.argv.class;
}
if (s.length > 0) {
url.search = s;
}
qx.tool.compiler.Console.log("CALL " + url.href);
result[app.name] = {};
let browsers = (app.argv.browsers || "")
.split(",")
.filter((s) => s.length > 0);
if (browsers.length === 0) {
browsers = app.environment["qxl.testtapper.browsers"];
}
if (!browsers || browsers.length === 0) {
browsers = ["chromium"];
}
for (const browserType of browsers) {
try {
await this.__runTestInBrowser(browserType, url, app, result);
} catch (e) {
qx.tool.compiler.Console.error(e);
}
}
},
__getTestApp(classname) {
let command = this.getCompilerApi().getCommand();
let maker = null;
let app = null;
command.getMakers().forEach((tmp) => {
let apps = tmp
.getApplications()
.filter(
(app) => app.getClassName() === classname && app.isBrowserApp()
);
if (apps.length) {
if (maker) {
qx.tool.compiler.Console.print("qx.tool.cli.test.tooManyMakers");
return null;
}
if (apps.length != 1) {
qx.tool.compiler.Console.print(
"qx.tool.cli.test.tooManyApplications"
);
return null;
}
maker = tmp;
app = apps[0];
}
});
if (!app) {
qx.tool.compiler.Console.print("qx.tool.cli.test.noAppName");
return null;
}
let env = app.getEnvironment();
if (env["testtapper.testNameSpace"]) {
qx.tool.compiler.Console.error(
'environment["testtapper.testNameSpace"] is deprecated, use environment["qxl.testtapper.testNameSpace"] instead'
);
env["qxl.testtapper.testNameSpace"] = env["testtapper.testNameSpace"];
}
var config = command.getCompilerApi().getConfiguration();
return {
name: app.getName(),
environment: env,
port: config.serve.listenPort,
argv: command.argv,
maker: maker,
};
},
},
});
module.exports = {
LibraryApi: qxl.testtapper.compile.LibraryApi,
};