-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfile-system.unit.test.ts
More file actions
306 lines (279 loc) · 7.72 KB
/
file-system.unit.test.ts
File metadata and controls
306 lines (279 loc) · 7.72 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { vol } from 'memfs';
import { stat } from 'node:fs/promises';
import path from 'node:path';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { MEMFS_VOLUME } from '@code-pushup/test-utils';
import {
type FileResult,
crawlFileSystem,
ensureDirectoryExists,
filePathToCliArg,
findInFile,
findLineNumberInText,
findNearestFile,
logMultipleFileResults,
projectToFilename,
} from './file-system.js';
import * as logResults from './log-results.js';
describe('ensureDirectoryExists', () => {
it('should create a nested folder', async () => {
vol.fromJSON({}, MEMFS_VOLUME);
const dir = path.join(MEMFS_VOLUME, 'sub', 'dir');
await ensureDirectoryExists(dir);
await expect(
stat(dir).then(stats => stats.isDirectory()),
).resolves.toBeTruthy();
});
});
describe('logMultipleFileResults', () => {
it('should call logMultipleResults with the correct arguments', () => {
const logMultipleResultsSpy = vi.spyOn(
logResults,
'logMultipleResults' as never,
);
const persistResult = [
{
status: 'fulfilled',
value: ['out.json', 10_000],
} as PromiseFulfilledResult<FileResult>,
];
const messagePrefix = 'Generated reports';
logMultipleFileResults(persistResult, messagePrefix);
expect(logMultipleResultsSpy).toHaveBeenCalled();
expect(logMultipleResultsSpy).toHaveBeenCalledWith(
persistResult,
messagePrefix,
expect.any(Function),
expect.any(Function),
);
});
});
describe('crawlFileSystem', () => {
beforeEach(() => {
vol.fromJSON(
{
'README.md': '# Markdown',
'src/README.md': '# Markdown',
'src/index.ts': 'const var = "markdown";',
},
MEMFS_VOLUME,
);
});
it('should list all files in file system', async () => {
await expect(
crawlFileSystem({
directory: MEMFS_VOLUME,
}),
).resolves.toEqual([
expect.stringContaining('README.md'),
expect.stringContaining(path.join('src', 'README.md')),
expect.stringContaining(path.join('src', 'index.ts')),
]);
});
it('should list files matching a pattern', async () => {
await expect(
crawlFileSystem({
directory: MEMFS_VOLUME,
pattern: /\.md$/,
}),
).resolves.toEqual([
expect.stringContaining('README.md'),
expect.stringContaining(path.join('src', 'README.md')),
]);
});
it('should apply sync fileTransform function if given', async () => {
await expect(
crawlFileSystem({
directory: MEMFS_VOLUME,
pattern: /\.md$/,
fileTransform: () => '42',
}),
).resolves.toEqual(['42', '42']);
});
it('should apply async fileTransform function if given', async () => {
await expect(
crawlFileSystem({
directory: MEMFS_VOLUME,
pattern: /\.md$/,
fileTransform: () => Promise.resolve('42'),
}),
).resolves.toEqual(['42', '42']);
});
});
describe('findNearestFile', () => {
it('should find file in current working directory', async () => {
vol.fromJSON(
{
'eslint.config.js': '',
},
MEMFS_VOLUME,
);
await expect(findNearestFile(['eslint.config.js'])).resolves.toBe(
path.join(MEMFS_VOLUME, 'eslint.config.js'),
);
});
it('should find first matching file in array', async () => {
vol.fromJSON(
{
'eslint.config.cjs': '',
'eslint.config.mjs': '',
},
MEMFS_VOLUME,
);
await expect(
findNearestFile([
'eslint.config.js',
'eslint.config.cjs',
'eslint.config.mjs',
]),
).resolves.toBe(path.join(MEMFS_VOLUME, 'eslint.config.cjs'));
});
it('should resolve to undefined if file not found', async () => {
vol.fromJSON({ '.eslintrc.json': '' }, MEMFS_VOLUME);
await expect(
findNearestFile([
'eslint.config.js',
'eslint.config.cjs',
'eslint.config.mjs',
]),
).resolves.toBeUndefined();
});
it('should find file in parent directory', async () => {
vol.fromJSON(
{
'eslint.config.js': '',
'e2e/main.spec.js': '',
},
MEMFS_VOLUME,
);
await expect(
findNearestFile(
['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs'],
path.join(MEMFS_VOLUME, 'e2e'),
),
).resolves.toBe(path.join(MEMFS_VOLUME, 'eslint.config.js'));
});
it('should find file in directory multiple levels up', async () => {
vol.fromJSON(
{
'eslint.config.cjs': '',
'packages/core/package.json': '',
},
MEMFS_VOLUME,
);
await expect(
findNearestFile(
['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs'],
path.join(MEMFS_VOLUME, 'packages/core'),
),
).resolves.toBe(path.join(MEMFS_VOLUME, 'eslint.config.cjs'));
});
it("should find file that's nearest to current folder", async () => {
vol.fromJSON(
{
'eslint.config.js': '',
'packages/core/eslint.config.js': '',
'packages/core/package.json': '',
},
MEMFS_VOLUME,
);
await expect(
findNearestFile(
['eslint.config.js', 'eslint.config.cjs', 'eslint.config.mjs'],
path.join(MEMFS_VOLUME, 'packages/core'),
),
).resolves.toBe(path.join(MEMFS_VOLUME, 'packages/core/eslint.config.js'));
});
it('should not find file in sub-folders of current folder', async () => {
vol.fromJSON({ 'packages/core/eslint.config.js': '' }, MEMFS_VOLUME);
await expect(
findNearestFile([
'eslint.config.js',
'eslint.config.cjs',
'eslint.config.mjs',
]),
).resolves.toBeUndefined();
});
});
describe('findLineNumberInText', () => {
it('should return correct line number', () => {
expect(
findLineNumberInText(
`
1
2 xxx
3
`,
'x',
),
).toBe(3);
});
it('should return line number of the first pattern occurrence', () => {
expect(
findLineNumberInText(
`
1 xxx
2
3 xxx
`,
'x',
),
).toBe(2);
});
it('should return null if pattern not in content', () => {
expect(findLineNumberInText(``, 'x')).toBeNull();
});
});
describe('filePathToCliArg', () => {
it('should wrap path in quotes', () => {
expect(filePathToCliArg('My Project/index.js')).toBe(
'"My Project/index.js"',
);
});
});
describe('projectToFilename', () => {
it.each([
['frontend', 'frontend'],
['@code-pushup/utils', 'code-pushup-utils'],
['Web API', 'Web-API'],
['backend/shared/auth', 'backend-shared-auth'],
])('should convert project name %p to file name %p', (project, file) => {
expect(projectToFilename(project)).toBe(file);
});
});
describe('findInFile', () => {
const file = 'file.txt';
const content =
'line 1 - even:false\nline 2 - even:true\nline 3 - even:false\nline 4 - even:true\nline 5 - even:false\n';
const filePath = path.join(MEMFS_VOLUME, file);
beforeEach(() => {
vol.reset();
vol.fromJSON({ [file]: content }, MEMFS_VOLUME);
});
it('should find pattern in a file if a string is given', async () => {
const result = await findInFile(filePath, 'line 3');
expect(result).toStrictEqual([
{
file: filePath,
endColumn: 6,
endLine: 3,
startColumn: 0,
startLine: 3,
},
]);
});
// @TODO any second test will fail
// Error: EBADF: bad file descriptor, close
it.todo('should find pattern in a file if a RegEx is given', async () => {
const result = await findInFile('file.txt', new RegExp('line 3', 'g'));
expect(result).toStrictEqual([
{
file: 'file.txt',
endColumn: 6,
endLine: 3,
startColumn: 0,
startLine: 3,
},
]);
});
});