-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmain.test.ts
More file actions
142 lines (136 loc) · 4.75 KB
/
Copy pathmain.test.ts
File metadata and controls
142 lines (136 loc) · 4.75 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
133
134
135
136
137
138
139
140
141
142
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 * as assert from 'uvu/assert'
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')
indexCommand([], {
cwd: inputRoot,
inferTsconfig,
output,
yarnWorkspaces: Boolean(packageJson.workspaces),
yarnBerryWorkspaces: false,
pnpmWorkspaces: Boolean(packageJson.packageManager?.includes('pnpm')),
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')
}
for (const document of index.documents) {
const symbols = new Set<string>()
const duplicateSymbols: string[] = []
for (const symbol of document.symbols) {
if (symbols.has(symbol.symbol)) {
duplicateSymbols.push(symbol.symbol)
}
symbols.add(symbol.symbol)
}
assert.equal(
duplicateSymbols,
[],
`${document.relative_path} should not contain duplicate SymbolInformation`
)
assert.ok(
document.language,
`${document.relative_path} should have a SCIP document language`
)
if (document.relative_path === 'src/symbol-kinds.ts') {
assert.equal(
document.symbols
.filter(
symbol =>
symbol.kind === scip.scip.SymbolInformation.Kind.UnspecifiedKind
)
.map(symbol => symbol.symbol),
[],
'all symbols in the symbol-kind fixture should have a SCIP kind'
)
}
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()