Skip to content

Commit c9e7e1b

Browse files
committed
improve unit tests to use single validation call and to output a better test failures info
1 parent 0d8d1c7 commit c9e7e1b

File tree

1 file changed

+102
-168
lines changed

1 file changed

+102
-168
lines changed

src/vs/workbench/contrib/chat/test/common/promptSyntax/testUtils/mockFilesystem.test.ts

Lines changed: 102 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,38 @@ import { MockFilesystem } from './mockFilesystem.js';
88
import { URI } from '../../../../../../../base/common/uri.js';
99
import { Schemas } from '../../../../../../../base/common/network.js';
1010
import { assertDefined } from '../../../../../../../base/common/types.js';
11-
import { IFileService } from '../../../../../../../platform/files/common/files.js';
1211
import { FileService } from '../../../../../../../platform/files/common/fileService.js';
1312
import { ILogService, NullLogService } from '../../../../../../../platform/log/common/log.js';
13+
import { IFileService, IFileStat } from '../../../../../../../platform/files/common/files.js';
1414
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../../../base/test/common/utils.js';
1515
import { InMemoryFileSystemProvider } from '../../../../../../../platform/files/common/inMemoryFilesystemProvider.js';
1616
import { TestInstantiationService } from '../../../../../../../platform/instantiation/test/common/instantiationServiceMock.js';
1717

1818
/**
19-
* TODO: @legomushroom
19+
* Base attribute for an expected filesystem node (a file or a folder).
2020
*/
21-
interface IBase {
22-
resource: URI;
23-
name: string;
24-
isFile: boolean;
25-
isDirectory: boolean;
26-
isSymbolicLink: boolean;
27-
}
21+
interface IExpectedFilesystemNode extends Pick<
22+
IFileStat,
23+
'resource' | 'name' | 'isFile' | 'isDirectory' | 'isSymbolicLink'
24+
> { }
2825

2926
/**
30-
* TODO: @legomushroom
27+
* Represents an expected `file` info.
3128
*/
32-
interface IExpectedFile extends IBase {
29+
interface IExpectedFile extends IExpectedFilesystemNode {
30+
/**
31+
* Expected file contents.
32+
*/
3333
contents: string;
3434
}
3535

3636
/**
37-
* TODO: @legomushroom
37+
* Represents an expected `folder` info.
3838
*/
39-
interface IExpectedFolder extends IBase {
39+
interface IExpectedFolder extends IExpectedFilesystemNode {
40+
/**
41+
* Expected folder children.
42+
*/
4043
children: (IExpectedFolder | IExpectedFile)[];
4144
}
4245

@@ -48,7 +51,12 @@ const validateFile = async (
4851
expectedFile: IExpectedFile,
4952
fileService: IFileService,
5053
) => {
51-
const readFile = await fileService.resolve(URI.file(filePath));
54+
let readFile: IFileStat | undefined;
55+
try {
56+
readFile = await fileService.resolve(URI.file(filePath));
57+
} catch (error) {
58+
throw new Error(`Failed to read file '${filePath}': ${error}.`);
59+
}
5260

5361
assert.strictEqual(
5462
readFile.name,
@@ -59,34 +67,33 @@ const validateFile = async (
5967
assert.deepStrictEqual(
6068
readFile.resource,
6169
expectedFile.resource,
62-
'File must have correct `URI`.',
70+
`File '${filePath}' must have correct 'URI'.`,
6371
);
6472

6573
assert.strictEqual(
6674
readFile.isFile,
6775
expectedFile.isFile,
68-
'File must have correct `isFile` value.',
76+
`File '${filePath}' must have correct 'isFile' value.`,
6977
);
7078

7179
assert.strictEqual(
7280
readFile.isDirectory,
7381
expectedFile.isDirectory,
74-
'File must have correct `isDirectory` value.',
82+
`File '${filePath}' must have correct 'isDirectory' value.`,
7583
);
7684

7785
assert.strictEqual(
7886
readFile.isSymbolicLink,
7987
expectedFile.isSymbolicLink,
80-
'File must have correct `isSymbolicLink` value.',
88+
`File '${filePath}' must have correct 'isSymbolicLink' value.`,
8189
);
8290

8391
assert.strictEqual(
8492
readFile.children,
8593
undefined,
86-
'File must not have children.',
94+
`File '${filePath}' must not have children.`,
8795
);
8896

89-
// TODO: @legomushroom - add folder/file path to all asserts
9097
const fileContents = await fileService.readFile(readFile.resource);
9198
assert.strictEqual(
9299
fileContents.value.toString(),
@@ -103,49 +110,73 @@ const validateFolder = async (
103110
expectedFolder: IExpectedFolder,
104111
fileService: IFileService,
105112
) => {
106-
const readFolder = await fileService.resolve(URI.file(folderPath));
113+
let readFolder: IFileStat | undefined;
114+
try {
115+
readFolder = await fileService.resolve(URI.file(folderPath));
116+
} catch (error) {
117+
throw new Error(`Failed to read folder '${folderPath}': ${error}.`);
118+
}
107119

108120
assert.strictEqual(
109121
readFolder.name,
110122
expectedFolder.name,
111-
'Folder must have correct `name`.',
123+
`Folder '${folderPath}' must have correct 'name'.`,
112124
);
113125

114126
assert.deepStrictEqual(
115127
readFolder.resource,
116128
expectedFolder.resource,
117-
'Folder must have correct `URI`.',
129+
`Folder '${folderPath}' must have correct 'URI'.`,
118130
);
119131

120132
assert.strictEqual(
121133
readFolder.isFile,
122134
expectedFolder.isFile,
123-
'Folder must have correct `isFile` value.',
135+
`Folder '${folderPath}' must have correct 'isFile' value.`,
124136
);
125137

126138
assert.strictEqual(
127139
readFolder.isDirectory,
128140
expectedFolder.isDirectory,
129-
'Folder must have correct `isDirectory` value.',
141+
`Folder '${folderPath}' must have correct 'isDirectory' value.`,
130142
);
131143

132144
assert.strictEqual(
133145
readFolder.isSymbolicLink,
134146
expectedFolder.isSymbolicLink,
135-
'Folder must have correct `isSymbolicLink` value.',
147+
`Folder '${folderPath}' must have correct 'isSymbolicLink' value.`,
136148
);
137149

138-
139150
assertDefined(
140151
readFolder.children,
141-
'Folder must have children.',
152+
`Folder '${folderPath}' must have children.`,
142153
);
143154

144155
assert.strictEqual(
145156
readFolder.children.length,
146157
expectedFolder.children.length,
147-
'Folder must have correct number of children.',
158+
`Folder '${folderPath}' must have correct number of children.`,
148159
);
160+
161+
for (const expectedChild of expectedFolder.children) {
162+
const childPath = URI.joinPath(expectedFolder.resource, expectedChild.name).fsPath;
163+
164+
if ('children' in expectedChild) {
165+
await validateFolder(
166+
childPath,
167+
expectedChild,
168+
fileService,
169+
);
170+
171+
continue;
172+
}
173+
174+
await validateFile(
175+
childPath,
176+
expectedChild,
177+
fileService,
178+
);
179+
}
149180
};
150181

151182
suite('MockFilesystem', () => {
@@ -164,7 +195,7 @@ suite('MockFilesystem', () => {
164195
initService.stub(IFileService, fileService);
165196
});
166197

167-
test('mocks file structure', async () => {
198+
test('mocks file structure', async () => {
168199
const mockFilesystem = initService.createInstance(MockFilesystem, [
169200
{
170201
name: '/root/folder',
@@ -209,147 +240,50 @@ suite('MockFilesystem', () => {
209240
isDirectory: true,
210241
isSymbolicLink: false,
211242
children: [
212-
// TODO: @legomushroom - add real children
213-
{} as any,
214-
{} as any,
215-
],
216-
},
217-
fileService,
218-
);
219-
220-
const rootFolder = await fileService.resolve(URI.file('/root/folder'));
221-
222-
assertDefined(
223-
rootFolder.children,
224-
'Root folder must have children.',
225-
);
226-
227-
const file = rootFolder.children[0];
228-
229-
await validateFile(
230-
'/root/folder/file.txt',
231-
{
232-
resource: URI.file('/root/folder/file.txt'),
233-
name: 'file.txt',
234-
isFile: true,
235-
isDirectory: false,
236-
isSymbolicLink: false,
237-
contents: 'contents',
238-
},
239-
fileService,
240-
);
241-
242-
await validateFile(
243-
file.resource.fsPath,
244-
{
245-
resource: URI.file('/root/folder/file.txt'),
246-
name: 'file.txt',
247-
isFile: true,
248-
isDirectory: false,
249-
isSymbolicLink: false,
250-
contents: 'contents',
251-
},
252-
fileService,
253-
);
254-
255-
const subfolder = await fileService.resolve(URI.file('/root/folder/Subfolder'));
256-
257-
await validateFolder(
258-
'/root/folder/Subfolder',
259-
{
260-
resource: URI.file('/root/folder/Subfolder'),
261-
name: 'Subfolder',
262-
isFile: false,
263-
isDirectory: true,
264-
isSymbolicLink: false,
265-
children: [
266-
// TODO: @legomushroom - add real children
267-
{} as any,
268-
{} as any,
269-
{} as any,
243+
{
244+
resource: URI.file('/root/folder/file.txt'),
245+
name: 'file.txt',
246+
isFile: true,
247+
isDirectory: false,
248+
isSymbolicLink: false,
249+
contents: 'contents',
250+
},
251+
{
252+
resource: URI.file('/root/folder/Subfolder'),
253+
name: 'Subfolder',
254+
isFile: false,
255+
isDirectory: true,
256+
isSymbolicLink: false,
257+
children: [
258+
{
259+
resource: URI.file('/root/folder/Subfolder/test.ts'),
260+
name: 'test.ts',
261+
isFile: true,
262+
isDirectory: false,
263+
isSymbolicLink: false,
264+
contents: 'other contents',
265+
},
266+
{
267+
resource: URI.file('/root/folder/Subfolder/file.test.ts'),
268+
name: 'file.test.ts',
269+
isFile: true,
270+
isDirectory: false,
271+
isSymbolicLink: false,
272+
contents: 'hello test',
273+
},
274+
{
275+
resource: URI.file('/root/folder/Subfolder/.file-2.TEST.ts'),
276+
name: '.file-2.TEST.ts',
277+
isFile: true,
278+
isDirectory: false,
279+
isSymbolicLink: false,
280+
contents: 'test hello',
281+
},
282+
],
283+
}
270284
],
271285
},
272286
fileService,
273287
);
274-
275-
assertDefined(
276-
subfolder.children,
277-
'Subfolder must have children.',
278-
);
279-
280-
await validateFile(
281-
'/root/folder/Subfolder/test.ts',
282-
{
283-
resource: URI.file('/root/folder/Subfolder/test.ts'),
284-
name: 'test.ts',
285-
isFile: true,
286-
isDirectory: false,
287-
isSymbolicLink: false,
288-
contents: 'other contents',
289-
},
290-
fileService,
291-
);
292-
await validateFile(
293-
subfolder.children[0].resource.fsPath,
294-
{
295-
resource: URI.file('/root/folder/Subfolder/test.ts'),
296-
name: 'test.ts',
297-
isFile: true,
298-
isDirectory: false,
299-
isSymbolicLink: false,
300-
contents: 'other contents',
301-
},
302-
fileService,
303-
);
304-
305-
await validateFile(
306-
'/root/folder/Subfolder/file.test.ts',
307-
{
308-
resource: URI.file('/root/folder/Subfolder/file.test.ts'),
309-
name: 'file.test.ts',
310-
isFile: true,
311-
isDirectory: false,
312-
isSymbolicLink: false,
313-
contents: 'hello test',
314-
},
315-
fileService,
316-
);
317-
await validateFile(
318-
subfolder.children[1].resource.fsPath,
319-
{
320-
resource: URI.file('/root/folder/Subfolder/file.test.ts'),
321-
name: 'file.test.ts',
322-
isFile: true,
323-
isDirectory: false,
324-
isSymbolicLink: false,
325-
contents: 'hello test',
326-
},
327-
fileService,
328-
);
329-
330-
await validateFile(
331-
'/root/folder/Subfolder/.file-2.TEST.ts',
332-
{
333-
resource: URI.file('/root/folder/Subfolder/.file-2.TEST.ts'),
334-
name: '.file-2.TEST.ts',
335-
isFile: true,
336-
isDirectory: false,
337-
isSymbolicLink: false,
338-
contents: 'test hello',
339-
},
340-
fileService,
341-
);
342-
await validateFile(
343-
subfolder.children[2].resource.fsPath,
344-
{
345-
resource: URI.file('/root/folder/Subfolder/.file-2.TEST.ts'),
346-
name: '.file-2.TEST.ts',
347-
isFile: true,
348-
isDirectory: false,
349-
isSymbolicLink: false,
350-
contents: 'test hello',
351-
},
352-
fileService,
353-
);
354288
});
355289
});

0 commit comments

Comments
 (0)