-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmain.test.ts
More file actions
132 lines (126 loc) · 4.52 KB
/
Copy pathmain.test.ts
File metadata and controls
132 lines (126 loc) · 4.52 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
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
import * as fs from 'fs'
import { join } from 'path'
import * as path from 'path'
import * as process from 'process'
import * as Diff from 'diff'
import { test } from 'uvu'
import { Input } from './Input'
import { indexCommand } from './main'
import * as scip from './scip'
import { formatSnapshot } from './SnapshotTesting'
function isUpdateSnapshot(): boolean {
return process.argv.includes('--update-snapshots')
}
const snapshotNodeModules = join(process.cwd(), 'snapshots', 'node_modules')
if (!fs.existsSync(snapshotNodeModules)) {
throw new Error(
`no such file: ${snapshotNodeModules} (to fix this problem, run 'yarn install' in the snapshots/ directory)`
)
}
const inputDirectory = join(process.cwd(), 'snapshots', 'input')
const outputDirectory = join(process.cwd(), 'snapshots', 'output')
const snapshotDirectories = fs.readdirSync(inputDirectory)
const isUpdate = isUpdateSnapshot()
if (isUpdate && fs.existsSync(outputDirectory)) {
fs.rmSync(outputDirectory, { recursive: true })
}
interface PackageJson {
workspaces: string[]
packageManager?: string
}
for (const snapshotDirectory of snapshotDirectories) {
// Uncomment below if you want to skip certain tests for local development.
// if (!snapshotDirectory.includes('syntax')) {
// continue
// }
const inputRoot = join(inputDirectory, snapshotDirectory)
const outputRoot = join(outputDirectory, snapshotDirectory)
if (!fs.statSync(inputRoot).isDirectory()) {
continue
}
test(snapshotDirectory, () => {
const packageJsonPath = path.join(inputRoot, 'package.json')
const packageJson = JSON.parse(
fs.readFileSync(packageJsonPath).toString()
) as PackageJson
const tsconfigJsonPath = path.join(inputRoot, 'tsconfig.json')
const inferTsconfig = !fs.existsSync(tsconfigJsonPath)
const output = path.join(inputRoot, 'index.scip')
const isPnpm = Boolean(packageJson.packageManager?.includes('pnpm'))
const isNpm =
!isPnpm && Boolean(packageJson.packageManager?.startsWith('npm@'))
indexCommand([], {
cwd: inputRoot,
inferTsconfig,
output,
yarnWorkspaces: !isNpm && !isPnpm && Boolean(packageJson.workspaces),
yarnBerryWorkspaces: false,
pnpmWorkspaces: isPnpm,
npmWorkspaces: isNpm,
progressBar: false,
indexedProjects: new Set(),
globalCaches: true,
})
if (inferTsconfig) {
fs.rmSync(tsconfigJsonPath)
}
const index = scip.scip.Index.deserializeBinary(
fs.readFileSync(path.join(inputRoot, 'index.scip'))
)
fs.mkdirSync(outputRoot, { recursive: true })
fs.renameSync(output, path.join(outputRoot, 'index.scip'))
if (index.documents.length === 0) {
throw new Error('empty LSIF index')
}
// Normalize backslashes so a Windows-emitted `packages\a\src\a.ts`
// and a forward-slash `packages/a/src/a.ts` are recognized as the
// same document (this was the npm-workspaces dedup bug).
const documentPaths = index.documents.map(d =>
d.relative_path.replaceAll('\\', '/')
)
const duplicatePaths = documentPaths.filter(
(p, i) => documentPaths.indexOf(p) !== i
)
if (duplicatePaths.length > 0) {
throw new Error(
`duplicate documents in index: ${JSON.stringify([
...new Set(duplicatePaths),
])}`
)
}
for (const document of index.documents) {
const inputPath = path.join(inputRoot, document.relative_path)
const relativeToInputDirectory = path.relative(inputDirectory, inputPath)
const outputPath = path.resolve(outputDirectory, relativeToInputDirectory)
const expected: string = fs.existsSync(outputPath)
? fs.readFileSync(outputPath).toString()
: ''
const input = Input.fromFile(inputPath)
const obtained = formatSnapshot(input, document)
if (obtained === expected) {
// Test passed
continue
}
if (isUpdate) {
// Update the snapshot test to reflect the new behavior
fs.mkdirSync(path.dirname(outputPath), {
recursive: true,
})
fs.writeFileSync(outputPath, obtained)
console.log(`updated snapshot: ${outputPath}`)
} else {
// Fail the test with a diff error message
const patch = Diff.createTwoFilesPatch(
outputPath,
outputPath,
expected,
obtained,
'(what the snapshot tests expect)',
"(what the current code produces). Run the command 'npm run update-snapshots' to accept the new behavior."
)
throw new Error(patch)
}
}
})
}
test.run()