-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathCSpellIONode.test.ts
250 lines (225 loc) · 15.2 KB
/
CSpellIONode.test.ts
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
239
240
241
242
243
244
245
246
247
248
249
250
import { promises as fs } from 'node:fs';
import { basename } from 'node:path';
import { pathToFileURL } from 'node:url';
import { describe, expect, test } from 'vitest';
import { CSpellIONode } from './CSpellIONode.js';
import { toFileURL } from './node/file/url.js';
import { makePathToFile, pathToSample as ps, pathToTemp } from './test/test.helper.js';
const sc = (m: string) => expect.stringContaining(m);
const oc = <T>(obj: T) => expect.objectContaining(obj);
describe('CSpellIONode', () => {
test('constructor', () => {
const cspellIo = new CSpellIONode();
expect(cspellIo).toBeDefined();
});
test.each`
filename | baseFilename | content
${__filename} | ${basename(__filename)} | ${sc('This bit of text')}
${ps('cities.txt')} | ${'cities.txt'} | ${sc('San Francisco\n')}
${ps('cities.txt.gz')} | ${'cities.txt.gz'} | ${sc('San Francisco\n')}
${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'} | ${'hello.txt'} | ${sc('Hello, World!')}
${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'} | ${undefined} | ${sc('Hello, World!')}
`('readFile $filename', async ({ filename, content, baseFilename }) => {
const cspellIo = new CSpellIONode();
const url = toFileURL(filename);
const expected = { url, content, baseFilename };
const result = await cspellIo.readFile(filename);
const gz = filename.endsWith('.gz') || undefined;
expect(result.url).toEqual(expected.url);
expect(result.getText()).toEqual(expected.content);
expect(result.baseFilename).toEqual(expected.baseFilename);
expect(!!result.gz).toEqual(!!gz);
});
const urlCities =
'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/cities.txt';
const urlCitiesGz =
'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/cities.txt.gz';
test.runIf(process.env['TEST_FETCH']).each`
filename | baseFilename | content
${urlCities} | ${'cities.txt'} | ${sc('San Francisco\n')}
${urlCitiesGz} | ${'cities.txt.gz'} | ${sc('San Francisco\n')}
`('readFile https $filename', async ({ filename, content, baseFilename }) => {
const cspellIo = new CSpellIONode();
const url = toFileURL(filename);
const expected = { url, content, baseFilename };
const result = await cspellIo.readFile(filename);
const gz = filename.endsWith('.gz') || undefined;
expect(result.url).toEqual(expected.url);
expect(result.getText()).toEqual(expected.content);
expect(result.baseFilename).toEqual(expected.baseFilename);
expect(!!result.gz).toEqual(!!gz);
});
test.each`
baseFilename | content
${'hello.txt'} | ${'This bit of text'}
${'cities.md'} | ${'San Francisco\n'}
${'words.txt.gz'} | ${'one\ntwo\nthree\n'}
`('dataURL write/readFile $baseFilename', async ({ content, baseFilename }) => {
const cspellIo = new CSpellIONode();
const url = toFileURL(`data:text/plain,placeholder`);
const ref = await cspellIo.writeFile({ url, baseFilename }, content);
const result = await cspellIo.readFile(ref);
// console.error('dataURL %o', { ref, result });
expect(result.getText()).toEqual(content);
expect(result.baseFilename).toEqual(baseFilename);
expect(!!result.gz).toEqual(!!baseFilename.endsWith('.gz'));
});
test.each`
filename | expected
${ps('cities.not_found.txt')} | ${oc({ code: 'ENOENT' })}
${ps('cities.not_found.txt.gz')} | ${oc({ code: 'ENOENT' })}
`('readFile not found $filename', async ({ filename, expected }) => {
const cspellIo = new CSpellIONode();
await expect(cspellIo.readFile(filename)).rejects.toEqual(expected);
});
const urlCitiesNotFound =
'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/cities.not_found.txt';
const urlCitiesNotFoundGz =
'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/not_found/cities.txt.gz';
test.runIf(process.env['TEST_FETCH']).each`
filename | expected
${urlCitiesNotFound} | ${oc({ code: 'ENOENT' })}
${urlCitiesNotFoundGz} | ${oc({ code: 'ENOENT' })}
`('readFile not found https $filename', async ({ filename, expected }) => {
const cspellIo = new CSpellIONode();
await expect(cspellIo.readFile(filename)).rejects.toEqual(expected);
});
test.each`
filename | content
${__filename} | ${sc('This bit of text')}
${ps('cities.txt')} | ${sc('San Francisco\n')}
${ps('cities.txt.gz')} | ${sc('San Francisco\n')}
${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'} | ${sc('Hello, World!')}
${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'} | ${sc('Hello, World!')}
`('readFileSync $filename', ({ filename, content }) => {
const cspellIo = new CSpellIONode();
const result = cspellIo.readFileSync({ url: toFileURL(filename) });
expect(result.url).toEqual(toFileURL(filename));
expect(result.getText()).toEqual(content);
});
const stats = {
urlA: { eTag: 'W/"10c5e3c7c73159515d4334813d6ba0255230270d92ebfdbd37151db7a0db5918"', mtimeMs: 0, size: -1 },
urlB: { eTag: 'W/"10c5e3c7c73159515d4334813d6ba0255230270d92ebfdbd37151db7a0dbffff"', mtimeMs: 0, size: -1 },
file1: { mtimeMs: 1_658_757_408_444.0342, size: 1886 },
file2: { mtimeMs: 1_658_757_408_444.0342, size: 2886 },
file3: { mtimeMs: 1_758_757_408_444.0342, size: 1886 },
};
test.each`
left | right | expected
${stats.urlA} | ${stats.urlA} | ${0}
${stats.urlA} | ${stats.file1} | ${1}
${stats.file1} | ${stats.file3} | ${-1}
${stats.file2} | ${stats.file3} | ${1}
`('getStat $left <> $right', async ({ left, right, expected }) => {
const cspellIo = new CSpellIONode();
const r = cspellIo.compareStats(left, right);
expect(r).toEqual(expected);
});
test.each`
url | expected
${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/tsconfig.json'} | ${oc({ eTag: sc('W/') })}
${__filename} | ${oc({ mtimeMs: expect.any(Number) })}
`('getStat $url', { timeout: 30_000 }, async ({ url, expected }) => {
const cspellIo = new CSpellIONode();
const r = await cspellIo.getStat(url);
expect(r).toEqual(expected);
});
test.each`
url | expected
${'https://raw.gitubusrcotent.com/streetsidesoftware/cspell/main/tsconfig.json'} | ${oc({ code: 'ENOTFOUND' })}
${ps(__dirname, 'not-found.nf')} | ${oc({ code: 'ENOENT' })}
`('getStat with error $url', { timeout: 30_000 }, async ({ url, expected }) => {
const cspellIo = new CSpellIONode();
const r = cspellIo.getStat(url);
await expect(r).rejects.toEqual(expected);
});
test.each`
url | expected
${__filename} | ${oc({ mtimeMs: expect.any(Number) })}
`('getStatSync $url', ({ url, expected }) => {
const cspellIo = new CSpellIONode();
const r = cspellIo.getStatSync(url);
expect(r).toEqual(expected);
});
test.each`
url | expected
${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/tsconfig.json'} | ${'The URL must be of scheme file'}
${ps(__dirname, 'not-found.nf')} | ${oc({ code: 'ENOENT' })}
`('getStatSync with error $url', async ({ url, expected }) => {
const cspellIo = new CSpellIONode();
expect(() => cspellIo.getStatSync(url)).toThrow(expected);
});
test.each`
filename
${pathToTemp('cities.txt')}
${pathToTemp('cities.txt.gz')}
`('writeFile $filename', async ({ filename }) => {
const content = await fs.readFile(ps('cities.txt'), 'utf8');
const cspellIo = new CSpellIONode();
await makePathToFile(filename);
await cspellIo.writeFile(filename, content);
const result = await cspellIo.readFile(filename);
expect(result.getText()).toEqual(content);
});
test.each`
filename | expected
${'https://raw.guc.com/sss/cspell/main/packages/cspell-io/samples/cities.txt.gz'} | ${'https://raw.guc.com/sss/cspell/main/packages/cspell-io/samples/cities.txt.gz'}
${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'} | ${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'}
${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'} | ${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'}
`('toUrl $filename', ({ filename, expected }) => {
const cspellIo = new CSpellIONode();
expect(cspellIo.toURL(filename).toString()).toEqual(expected);
});
test.each`
filename | relativeTo | expected
${'samples/cities.txt'} | ${'file:///usr/local/'} | ${'file:///usr/local/samples/cities.txt'}
${'samples/cities.txt'} | ${'https://example.com'} | ${'https://example.com/samples/cities.txt'}
${'https://raw.guc.com/sss/cspell/main/packages/cspell-io/samples/cities.txt.gz'} | ${'file:///usr/local/'} | ${'https://raw.guc.com/sss/cspell/main/packages/cspell-io/samples/cities.txt.gz'}
${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'} | ${'file:///usr/local/'} | ${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'}
`('toUrl relativeTo $filename $relativeTo', ({ filename, relativeTo, expected }) => {
const cspellIo = new CSpellIONode();
expect(cspellIo.toURL(filename, relativeTo).toString()).toEqual(expected);
});
test.each`
filename | expected
${ps('samples/cities.txt')} | ${sc('samples/cities.txt')}
${'https://raw.guc.com/sss/cspell/main/packages/cspell-io/samples/cities.txt.gz'} | ${'https://raw.guc.com/sss/cspell/main/packages/cspell-io/samples/cities.txt.gz'}
${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'} | ${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'}
${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'} | ${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'}
`('toFileUrl $filename', ({ filename, expected }) => {
const cspellIo = new CSpellIONode();
expect(cspellIo.toFileURL(filename).toString()).toEqual(expected);
});
test.each`
filename | relativeTo | expected
${ps('samples/cities.txt')} | ${'file:///usr/local/'} | ${sc('samples/cities.txt')}
${'samples/cities.txt'} | ${'file:///usr/local/'} | ${'file:///usr/local/samples/cities.txt'}
${'samples/cities.txt'} | ${ps()} | ${pathToFileURL(ps('samples/cities.txt')).toString()}
${'https://raw.guc.com/sss/cspell/main/packages/cspell-io/samples/cities.txt.gz'} | ${'file:///usr/local/'} | ${'https://raw.guc.com/sss/cspell/main/packages/cspell-io/samples/cities.txt.gz'}
${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'} | ${'file:///usr/local/'} | ${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'}
`('toFileUrl relativeTo $filename $relativeTo', ({ filename, relativeTo, expected }) => {
const cspellIo = new CSpellIONode();
expect(cspellIo.toFileURL(filename, relativeTo).toString()).toEqual(expected);
});
test.each`
filename | expected
${ps('samples/cities.txt')} | ${'cities.txt'}
${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/cities.txt.gz'} | ${'cities.txt.gz'}
${'https://example.com/examples/code/'} | ${'code/'}
${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'} | ${'hello.txt'}
${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'} | ${'text.plain'}
`('uriBasename $filename', ({ filename, expected }) => {
const cspellIo = new CSpellIONode();
expect(cspellIo.urlBasename(toFileURL(filename))).toEqual(expected);
});
test.each`
filename | expected
${ps('samples/cities.txt')} | ${sc('samples/')}
${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/cities.txt.gz'} | ${'https://raw.githubusercontent.com/streetsidesoftware/cspell/main/packages/cspell-io/samples/'}
${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'} | ${'data:text/plain;charset=utf8;filename=hello.txt,Hello%2C%20World!'}
${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'} | ${'data:text/plain;charset=utf8;base64,SGVsbG8sIFdvcmxkISAlJSUlJCQkJCwsLCw'}
`('uriDirname $filename', ({ filename, expected }) => {
const cspellIo = new CSpellIONode();
expect(cspellIo.urlDirname(toFileURL(filename)).toString()).toEqual(expected);
});
});