-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
171 lines (160 loc) · 5.01 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
const path = require("path");
const child_process = require("child_process");
const { performance } = require("perf_hooks");
qx.Class.define("qxl.testnode.LibraryApi", {
extend: qx.tool.cli.api.LibraryApi,
members: {
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",
};
args.builder.terse = {
describe: "show only summary and errors",
type: "boolean",
};
return args;
};
},
async load() {
let command = this.getCompilerApi().getCommand();
if (command instanceof qx.tool.cli.commands.Test) {
command.addListener("runTests", this.__onRunTests, this);
}
},
__onRunTests(data) {
let result = data.getData();
let app = this.getTestApp("qxl.testnode.Application");
if (!app) {
qx.tool.compiler.Console.log("Please install qxl.testnode package!");
return qx.Promise.resolve(false);
}
this.require("minimist");
qx.tool.compiler.Console.log("TAP version 13");
qx.tool.compiler.Console.log("# run unit tests via qxl.testnode");
let target = app.maker.getTarget();
let outputDir = target.getOutputDir();
let boot = path.join(outputDir, app.name, "index.js");
let args = [];
args.push(boot);
for (const arg of ["colorize", "verbose", "method", "class"]) {
if (app.argv[arg]) {
args.push(` --${arg}=${app.argv[arg]}`);
}
}
return new qx.Promise((resolve, reject) => {
let notOk = 0;
let Ok = 0;
let skipped = 0;
if (app.argv.diag) {
qx.tool.compiler.Console.log(`run node ${args}`);
}
let startTime = performance.now();
let proc = child_process.spawn("node", args, {
cwd: ".",
shell: true,
});
proc.stdout.on("data", (data) => {
let arr = data.toString().trim().split("\n");
// value is serializable
arr.forEach((val) => {
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]`
);
result[app.name] = {
notOk: notOk,
ok: Ok,
};
result.setExitCode(notOk);
} else if (val.match(/^not ok /)) {
notOk++;
qx.tool.compiler.Console.log(val);
result.setExitCode(notOk);
} 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);
}
});
});
proc.stderr.on("data", (data) => {
let val = data.toString().trim();
qx.tool.compiler.Console.error(val);
});
proc.on("close", (code) => {
resolve(code);
});
proc.on("error", () => {
reject();
});
});
},
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;
}
return {
name: app.getName(),
argv: command.argv,
environment: app.getEnvironment(),
maker: maker,
};
},
_cnt: null,
_failed: null,
},
});
module.exports = {
LibraryApi: qxl.testnode.LibraryApi,
};