-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathmain.test.ts
More file actions
238 lines (231 loc) · 7.92 KB
/
Copy pathmain.test.ts
File metadata and controls
238 lines (231 loc) · 7.92 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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')
}
const documentPaths = new Set<string>()
const duplicateDocuments: string[] = []
for (const document of index.documents) {
if (documentPaths.has(document.relative_path)) {
duplicateDocuments.push(document.relative_path)
}
documentPaths.add(document.relative_path)
}
assert.equal(duplicateDocuments, [], 'SCIP document paths should be unique')
const symbolInformation = new Set(
index.documents.flatMap(document =>
document.symbols.map(symbol => symbol.symbol)
)
)
const externalSymbolInformation = new Set(
index.external_symbols.map(symbol => symbol.symbol)
)
assert.equal(
externalSymbolInformation.size,
index.external_symbols.length,
'external SymbolInformation should be unique'
)
assert.equal(
index.external_symbols
.map(symbol => symbol.symbol)
.filter(symbol => symbolInformation.has(symbol)),
[],
'symbols should not be both internal and external'
)
const availableSymbols = new Set([
...symbolInformation,
...externalSymbolInformation,
])
const indexedPackages = new Set(
[...symbolInformation]
.filter(symbol => !symbol.startsWith('local '))
.map(symbolPackage)
)
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`
)
const occurrences = new Set<string>()
const duplicateOccurrences: string[] = []
for (const occurrence of document.occurrences) {
const key = `${occurrence.range.join(':')} ${occurrence.symbol_roles} ${occurrence.symbol}`
if (occurrences.has(key)) {
duplicateOccurrences.push(key)
}
occurrences.add(key)
}
assert.equal(
duplicateOccurrences,
[],
`${document.relative_path} should not contain duplicate occurrences`
)
const missingOccurrenceSymbols = document.occurrences
.map(occurrence => occurrence.symbol)
.filter(
symbol =>
symbol &&
!symbol.startsWith('local ') &&
!availableSymbols.has(symbol)
)
assert.equal(
missingOccurrenceSymbols,
[],
`${document.relative_path} global occurrences should have SymbolInformation`
)
const missingRelationshipSymbols = document.symbols
.flatMap(symbol => symbol.relationships)
.map(relationship => relationship.symbol)
.filter(
symbol =>
symbol &&
!symbol.startsWith('local ') &&
!availableSymbols.has(symbol)
)
assert.equal(
missingRelationshipSymbols,
[],
`${document.relative_path} relationship targets should have SymbolInformation`
)
const missingInternalSymbols = document.occurrences
.map(occurrence => occurrence.symbol)
.filter(
symbol =>
symbol &&
indexedPackages.has(symbolPackage(symbol)) &&
!symbolInformation.has(symbol)
)
assert.equal(
missingInternalSymbols,
[],
`${document.relative_path} occurrences in indexed packages should have 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, index.external_symbols)
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)
}
}
})
}
function symbolPackage(symbol: string): string {
return symbol.split(' ', 4).join(' ')
}
test.run()