-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcheck-tests.ts
More file actions
executable file
·95 lines (86 loc) · 3.01 KB
/
Copy pathcheck-tests.ts
File metadata and controls
executable file
·95 lines (86 loc) · 3.01 KB
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
#!/usr/bin/env node
// Script to check test files in the codebase.
// - Avoid focused tests (test.only()) that developer could forget to unfocus
// - Avoid skipped tests (test.skip())
// - Ensure tests have describe() blocks
// - Ensure workflows run existing scripts
// - Ensure every project excluded from main.yml is tested by own workflow
// - Ensure every test is marked as offline:/online: and that own: tests,
// which main.yml does not run, have own workflow
import { globSync, readFileSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { join, relative } from 'node:path'
import { styleText } from 'node:util'
const ROOT = join(import.meta.dirname, '..')
function check(
all: Buffer,
part: string,
filename: string,
message: string
): void {
if (all.includes(part)) {
let lines = all.toString().split('\n')
let line = lines.findIndex(i => i.includes(part)) + 1
let path = relative(ROOT, filename)
process.stderr.write(styleText('red', `${path}:${line} ${message}\n`))
process.exit(1)
}
}
function checkMissing(
all: Buffer,
part: string,
filename: string,
message: string
): void {
if (!all.includes(part)) {
let path = relative(ROOT, filename)
process.stderr.write(styleText('red', `${path} ${message}\n`))
process.exit(1)
}
}
async function checkFile(filename: string): Promise<void> {
let code = await readFile(filename)
check(code, 'test.only(', filename, 'has focused test')
check(code, 'test.skip(', filename, 'has skipped test')
checkMissing(code, 'describe(', filename, 'missing describe() block')
}
function error(message: string): void {
process.stderr.write(styleText('red', `${message}\n`))
process.exit(1)
}
function checkWorkflows(): void {
let workflows = globSync('.github/workflows/*.yml').map(file => ({
content: readFileSync(join(ROOT, file)).toString(),
file
}))
let all = workflows.map(i => i.content).join('\n')
for (let { content, file } of workflows) {
if (file.endsWith('main.yml')) {
for (let [, project] of content.matchAll(/-F '!([\w-]+)'/g)) {
if (!all.includes(`pnpm -F ${project} test`)) {
error(`main.yml excludes ${project}, but no workflow tests it`)
}
}
}
}
for (let pkg of globSync(['package.json', '*/package.json'])) {
let project = pkg.replace('/package.json', '')
let content = readFileSync(join(ROOT, pkg)).toString()
for (let [, script] of content.matchAll(/"((?:test|own):[\w:-]+)":/g)) {
let [family, network] = script!.split(':')
if (network !== 'offline' && network !== 'online') {
error(`${pkg} has ${script}, mark it as offline: or online:`)
} else if (
family === 'own' &&
!all.includes(script!) &&
!all.includes(`pnpm -F ${project} test`)
) {
error(`${pkg} has ${script}, but no workflow runs it`)
}
}
}
}
let files =
process.argv.length > 2 ? process.argv.slice(2) : globSync('**/*.test.ts')
await Promise.all(files.map(file => checkFile(file)))
checkWorkflows()