-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathfindNvim.test.ts
More file actions
155 lines (136 loc) · 5.4 KB
/
Copy pathfindNvim.test.ts
File metadata and controls
155 lines (136 loc) · 5.4 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
import { join } from 'node:path';
import { mkdirSync, writeFileSync, rmSync } from 'node:fs';
import expect from 'expect';
import { findNvim, exportsForTesting, FindNvimResult } from './findNvim';
const parseVersion = exportsForTesting.parseVersion;
const compareVersions = exportsForTesting.compareVersions;
const normalizePath = exportsForTesting.normalizePath as (p: string) => string;
describe('findNvim', () => {
const testDir = join(process.cwd(), 'test-dir');
const nvimExecutablePath = normalizePath(
join(testDir, process.platform === 'win32' ? 'nvim.exe' : 'nvim')
);
before(() => {
mkdirSync(testDir, { recursive: true });
writeFileSync(nvimExecutablePath, 'fake-nvim-executable');
});
after(() => {
rmSync(testDir, { recursive: true, force: true });
});
it('parseVersion()', () => {
expect(parseVersion('0.5.0-dev+1357-g192f89ea1')).toEqual([0, 5, 0, 'dev+1357-g192f89ea1']);
expect(parseVersion('0.5.0-dev+1357-g192f89ea1-Homebrew')).toEqual([
0,
5,
0,
'dev+1357-g192f89ea1-Homebrew',
]);
expect(parseVersion('0.9.1')).toEqual([0, 9, 1, 'zzz']);
// Failure modes:
expect(() => parseVersion(42 as any)).toThrow(TypeError);
expect(parseVersion('x.y.z')).toEqual(undefined);
expect(parseVersion('1.y.z')).toEqual(undefined);
expect(parseVersion('1.2.z')).toEqual(undefined);
expect(parseVersion('x.2.3')).toEqual(undefined);
expect(parseVersion('1.y.3')).toEqual(undefined);
});
it('compareVersions()', () => {
expect(compareVersions('0.3.0', '0.3.0')).toBe(0);
expect(compareVersions('0.3.0', '0.3.1')).toBe(-1);
expect(compareVersions('0.3.1', '0.3.0')).toBe(1);
expect(compareVersions('0.3.0-abc', '0.3.0-dev-420')).toBe(-1);
expect(compareVersions('0.3.0', '0.3.0-dev-658+g06694203e-Homebrew')).toBe(1);
expect(compareVersions('0.3.0-dev-658+g06694203e-Homebrew', '0.3.0')).toBe(-1);
expect(
compareVersions('0.3.0-dev-658+g06694203e-Homebrew', '0.3.0-dev-658+g06694203e-Homebrew')
).toBe(0);
expect(
compareVersions('0.3.0-dev-658+g06694203e-Homebrew', '0.3.0-dev-659+g06694203e-Homebrew')
).toBe(-1);
expect(
compareVersions('0.3.0-dev-659+g06694203e-Homebrew', '0.3.0-dev-658+g06694203e-Homebrew')
).toBe(1);
// Failure modes:
expect(compareVersions('0.3.0', 'nonsense')).toBe(1);
expect(() => compareVersions('nonsense', '0.3.0')).toThrow('Invalid version: "nonsense"');
expect(() => compareVersions('nonsense', 'nonsense')).toThrow('Invalid version: "nonsense"');
expect(() => compareVersions(undefined, undefined)).toThrow(
'Invalid version format: not a string'
);
});
/** Asserts that >=1 nvim binaries were found. */
function assertOneOrMore(nvimRes: Readonly<FindNvimResult>) {
expect(nvimRes).toEqual({
matches: expect.any(Array),
invalid: expect.any(Array),
});
expect(nvimRes.matches.length).toBeGreaterThan(0);
expect(nvimRes.matches[0]).toEqual({
nvimVersion: expect.any(String),
path: expect.any(String),
buildType: expect.any(String),
luaJitVersion: expect.any(String),
error: undefined,
});
expect(nvimRes.invalid.length).toEqual(0);
}
it('gets Nvim satisfying given min version', () => {
const nvimRes = findNvim({ minVersion: '0.3.0', orderBy: 'desc' });
assertOneOrMore(nvimRes);
});
it('gets Nvim without specified min version', () => {
const nvimRes = findNvim();
assertOneOrMore(nvimRes);
});
it('collects invalid matches separately', () => {
const nvimRes = findNvim({ minVersion: '9999.0.0' });
expect(nvimRes).toEqual({
matches: [],
invalid: expect.any(Array),
});
expect(nvimRes.matches.length).toEqual(0);
expect(nvimRes.invalid.length).toBeGreaterThan(0);
expect(nvimRes.invalid[0]).toEqual({
nvimVersion: expect.any(String),
path: expect.any(String),
buildType: expect.any(String),
luaJitVersion: expect.any(String),
error: undefined,
});
});
it('stops searching on first match when firstMatch is True', () => {
const nvimRes = findNvim({ minVersion: '0.3.0', firstMatch: true });
expect(nvimRes).toEqual({
matches: expect.any(Array),
invalid: expect.any(Array),
});
expect(nvimRes.matches.length).toEqual(1);
expect(nvimRes.matches[0]).toEqual({
nvimVersion: expect.any(String),
path: expect.any(String),
buildType: expect.any(String),
luaJitVersion: expect.any(String),
error: undefined,
});
});
it('searches in additional custom paths', () => {
const customPaths = [
join(process.cwd(), 'package.json'),
'/custom/path/to/nvim',
'/another/custom/path',
].map(normalizePath);
const nvimRes = findNvim({ paths: customPaths });
expect(nvimRes.matches.length).toBeGreaterThanOrEqual(1);
expect(nvimRes.invalid.length).toBe(3);
const invalidPaths = nvimRes.invalid.map(i => i.path);
expect(invalidPaths).toEqual(customPaths);
});
it('searches in additional custom dirs', () => {
const customDirs = [testDir, '/non/existent/dir'].map(normalizePath);
const nvimRes = findNvim({ dirs: customDirs, firstMatch: true });
expect(nvimRes.matches.length).toBeGreaterThanOrEqual(1);
expect(nvimRes.invalid.length).toBeGreaterThanOrEqual(1);
const invalidPaths = nvimRes.invalid.map(i => i.path);
expect(invalidPaths).toContain(nvimExecutablePath);
});
});