1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Test = void 0;
|
4 | const child_process_1 = require("child_process");
|
5 | const globby = require("globby");
|
6 | const path_1 = require("path");
|
7 | const fs_extra_1 = require("fs-extra");
|
8 | class Test {
|
9 | async run(config) {
|
10 | this.config = config;
|
11 | this.argv = config.argv;
|
12 | const execArgv = this.argv.execArgv || [];
|
13 |
|
14 | if (this.argv.cov) {
|
15 | const coverageDir = path_1.join(this.config.cwd, 'coverage');
|
16 | if (fs_extra_1.existsSync(coverageDir)) {
|
17 | await fs_extra_1.remove(coverageDir);
|
18 | }
|
19 | const outputDir = path_1.join(this.config.cwd, 'node_modules/.nyc_output');
|
20 | if (fs_extra_1.existsSync(outputDir)) {
|
21 | await fs_extra_1.remove(outputDir);
|
22 | }
|
23 | }
|
24 | const opt = {
|
25 | cwd: this.config.cwd,
|
26 | env: Object.assign({
|
27 | NODE_ENV: 'test',
|
28 | }, this.config.env),
|
29 | execArgv,
|
30 | };
|
31 |
|
32 | let binFile;
|
33 | if (this.argv.cov) {
|
34 | binFile = require.resolve('nyc/bin/nyc.js');
|
35 | }
|
36 | else {
|
37 | binFile = require.resolve('mocha/bin/_mocha');
|
38 | }
|
39 | const args = await this.formatTestArgs();
|
40 | return this.forkNode(binFile, args, opt);
|
41 | }
|
42 | async formatTestArgs() {
|
43 | const argsPre = [];
|
44 | const args = [];
|
45 | if (this.argv.typescript) {
|
46 | argsPre.push('--require', require.resolve('ts-node/register'));
|
47 | }
|
48 | if (this.argv.cov) {
|
49 | if (this.argv.nyc) {
|
50 | argsPre.push(...this.argv.nyc.split(' '));
|
51 | argsPre.push('--temp-directory', './node_modules/.nyc_output');
|
52 | }
|
53 | if (this.argv.typescript) {
|
54 | argsPre.push(`--extension`);
|
55 | argsPre.push(`.ts`);
|
56 | }
|
57 | argsPre.push(require.resolve('mocha/bin/_mocha'));
|
58 | }
|
59 | else if (this.argv.extension) {
|
60 | args.push(`--extension=${this.argv.extension}`);
|
61 | }
|
62 | let timeout = this.argv.timeout || process.env.TEST_TIMEOUT || 60000;
|
63 | if (process.env.JB_DEBUG_FILE) {
|
64 |
|
65 | timeout = false;
|
66 | }
|
67 | args.push(timeout ? `--timeout=${timeout}` : '--no-timeout');
|
68 | if (this.argv.reporter || process.env.TEST_REPORTER) {
|
69 | args.push('--reporter=true');
|
70 | }
|
71 | args.push('--exit=true');
|
72 | const requireArr = [].concat(this.argv.require || this.argv.r || []);
|
73 | if (!this.argv.fullTrace) {
|
74 | requireArr.unshift(require.resolve('./mocha-clean'));
|
75 | }
|
76 | requireArr.forEach(requireItem => {
|
77 | args.push(`--require=${requireItem}`);
|
78 | });
|
79 | let pattern;
|
80 | if (!pattern) {
|
81 |
|
82 | pattern = this.argv._.slice();
|
83 | }
|
84 | if (!pattern.length && process.env.TESTS) {
|
85 | pattern = process.env.TESTS.split(',');
|
86 | }
|
87 | if (!pattern.length) {
|
88 | pattern = [`test/**/*.test.${this.argv.typescript ? 'ts' : 'js'}`];
|
89 | }
|
90 | pattern = pattern.concat(['!test/fixtures', '!test/node_modules']);
|
91 | const files = globby.sync(pattern);
|
92 | if (files.length === 0) {
|
93 | console.log(`No test files found with ${pattern}`);
|
94 | return;
|
95 | }
|
96 | args.push(...files);
|
97 |
|
98 | const setupFile = path_1.join(process.cwd(), `test/.setup.${this.argv.typescript ? 'ts' : 'js'}`);
|
99 | if (fs_extra_1.existsSync(setupFile)) {
|
100 | args.unshift(setupFile);
|
101 | }
|
102 | return argsPre.concat(args);
|
103 | }
|
104 | forkNode(modulePath, args = [], options = {}) {
|
105 | options.stdio = options.stdio || 'inherit';
|
106 | const proc = child_process_1.fork(modulePath, args, options);
|
107 | gracefull(proc);
|
108 | return new Promise((resolve, reject) => {
|
109 | proc.once('exit', (code) => {
|
110 | childs.delete(proc);
|
111 | if (code !== 0) {
|
112 | const err = new Error(modulePath + ' ' + args + ' exit with code ' + code);
|
113 | err.code = code;
|
114 | reject(err);
|
115 | }
|
116 | else {
|
117 | resolve();
|
118 | }
|
119 | });
|
120 | });
|
121 | }
|
122 | }
|
123 | exports.Test = Test;
|
124 | const childs = new Set();
|
125 | let hadHook = false;
|
126 | function gracefull(proc) {
|
127 |
|
128 | childs.add(proc);
|
129 |
|
130 |
|
131 | if (!hadHook) {
|
132 | hadHook = true;
|
133 | let signal;
|
134 | ['SIGINT', 'SIGQUIT', 'SIGTERM'].forEach((event) => {
|
135 | process.once(event, () => {
|
136 | signal = event;
|
137 | process.exit(0);
|
138 | });
|
139 | });
|
140 | process.once('exit', () => {
|
141 |
|
142 | for (const child of childs) {
|
143 | child.kill(signal);
|
144 | }
|
145 | });
|
146 | }
|
147 | }
|
148 |
|
\ | No newline at end of file |