forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.ts
89 lines (76 loc) · 2.19 KB
/
index.spec.ts
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
import * as path from 'path';
import * as fs from 'fs';
import { describe, it } from 'vitest';
import { fork } from 'child_process';
const binPath = require.resolve('../bin/vue-tsc.js');
const workspaceVue3 = path.resolve(__dirname, '../../vue-test-workspace/vue-tsc');
const workspaceVue2 = path.resolve(__dirname, '../../vue-test-workspace-vue-2/vue-tsc');
function prettyPath(path: string, isRoot: boolean) {
const segments = path.split('/');
const slicePath = (seg: number) => segments
.slice(segments.length - seg, segments.length)
.join('/')
.replace('/vue-tsc', '');
return !isRoot ? slicePath(4) : slicePath(3);
}
function collectTests(dir: string, depth = 2, isRoot: boolean = true): [filePath: string, isRoot: boolean][] {
const tests: [filePath: string, isRoot: boolean][] = [];
if (depth <= 0) {
return tests;
}
const files = fs.readdirSync(dir);
for (const file of files) {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
const tsconfigPath = path.join(filePath, 'tsconfig.json');
if (fs.existsSync(tsconfigPath)) {
tests.push([
filePath.replace(/\\/g, '/'),
isRoot,
]);
}
tests.push(...collectTests(filePath, depth - 1, false));
}
}
return tests;
}
const testsVue3 = collectTests(workspaceVue3);
const testsVue2 = collectTests(workspaceVue2);
function runVueTsc(cwd: string) {
return new Promise((resolve, reject) => {
const cp = fork(
binPath,
['--noEmit'],
{
silent: true,
cwd
},
);
cp.stdout?.setEncoding('utf8');
cp.stdout?.on('data', (data) => {
console.log(data);
});
cp.stderr?.setEncoding('utf8');
cp.stderr?.on('data', (data) => {
console.error(data);
});
cp.on('exit', (code) => {
if (code === 0) {
resolve(undefined);
} else {
reject(new Error(`Exited with code ${code}`));
}
});
});
}
describe(`vue-tsc`, () => {
for (const [path, isRoot] of testsVue3) {
it(`vue-tsc no errors (${prettyPath(path, isRoot)})`, () => runVueTsc(path), 40_000);
}
});
describe(`vue-tsc (vue 2)`, () => {
for (const [path, isRoot] of testsVue2) {
it(`vue-tsc no errors (${prettyPath(path, isRoot)})`, () => runVueTsc(path), 40_000);
}
});