-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathProjectIndexer.test.ts
More file actions
94 lines (86 loc) · 2.93 KB
/
Copy pathProjectIndexer.test.ts
File metadata and controls
94 lines (86 loc) · 2.93 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
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import * as ts from 'typescript'
import { test } from 'uvu'
import * as assert from 'uvu/assert'
import { GlobalCache, ProjectOptions } from './CommandLineOptions'
import {
languageForFileName,
prettyMilliseconds,
ProjectIndexer,
} from './ProjectIndexer'
function minute(x: number): number {
return x * 60 * 1000
}
function second(x: number): number {
return x * 1000
}
test('prettyMilliseconds', () => {
assert.is(prettyMilliseconds(0), '0ms')
assert.is(prettyMilliseconds(1), '1ms')
assert.is(prettyMilliseconds(second(1)), '1s 0ms')
assert.is(prettyMilliseconds(second(1) + 300), '1s 300ms')
assert.is(prettyMilliseconds(second(2)), '2s 0ms')
assert.is(prettyMilliseconds(second(5)), '5s 0ms')
assert.is(prettyMilliseconds(minute(1)), '1m 0s 0ms')
assert.is(prettyMilliseconds(minute(2)), '2m 0s 0ms')
assert.is(prettyMilliseconds(minute(5)), '5m 0s 0ms')
assert.is(prettyMilliseconds(minute(60)), '60m 0s 0ms')
assert.is(prettyMilliseconds(minute(5) + second(8) + 999), '5m 8s 999ms')
})
test('languageForFileName', () => {
assert.is(languageForFileName('index.ts'), 'TypeScript')
assert.is(languageForFileName('index.mts'), 'TypeScript')
assert.is(languageForFileName('index.cts'), 'TypeScript')
assert.is(languageForFileName('index.tsx'), 'TypeScriptReact')
assert.is(languageForFileName('index.js'), 'JavaScript')
assert.is(languageForFileName('index.mjs'), 'JavaScript')
assert.is(languageForFileName('index.cjs'), 'JavaScript')
assert.is(languageForFileName('index.jsx'), 'JavaScriptReact')
assert.is(languageForFileName('package.json'), 'JSON')
assert.is(languageForFileName('Component.svelte'), '')
})
test('only deduplicates documents after successful emission', () => {
const projectRoot = fs.mkdtempSync(
path.join(os.tmpdir(), 'scip-typescript-project-')
)
try {
const fileName = path.join(projectRoot, 'index.ts')
fs.writeFileSync(fileName, 'export const value = 1\n')
const cache: GlobalCache = {
sources: new Map(),
parsedCommandLines: new Map(),
indexedFiles: new Set(),
}
const options: ProjectOptions = {
cwd: projectRoot,
projectRoot,
projectDisplayName: projectRoot,
output: path.join(projectRoot, 'index.scip'),
inferTsconfig: false,
progressBar: false,
yarnWorkspaces: false,
yarnBerryWorkspaces: false,
pnpmWorkspaces: false,
globalCaches: false,
indexedProjects: new Set(),
writeIndex: () => {
throw new Error('emission failed')
},
}
const config: ts.ParsedCommandLine = {
options: {},
fileNames: [fileName],
errors: [],
}
assert.throws(
() => new ProjectIndexer(config, options, cache).index(),
/emission failed/
)
assert.not.ok(cache.indexedFiles.has(fileName))
} finally {
fs.rmSync(projectRoot, { recursive: true })
}
})
test.run()