Skip to content

Commit bc1d0f7

Browse files
Move listdir() to the new API.
1 parent 9ef20cc commit bc1d0f7

File tree

5 files changed

+137
-211
lines changed

5 files changed

+137
-211
lines changed

src/client/common/platform/fileSystem.ts

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,12 @@ import {
2323

2424
const ENCODING: string = 'utf8';
2525

26-
function getFileType(stat: FileStat): FileType {
27-
if (stat.isFile()) {
28-
return FileType.File;
29-
} else if (stat.isDirectory()) {
30-
return FileType.Directory;
31-
} else if (stat.isSymbolicLink()) {
32-
return FileType.SymbolicLink;
33-
} else {
34-
return FileType.Unknown;
35-
}
36-
}
37-
3826
// This is the parts of the vscode.workspace.fs API that we use here.
3927
interface INewAPI {
4028
//copy(source: vscode.Uri, target: vscode.Uri, options?: {overwrite: boolean}): Thenable<void>;
4129
//createDirectory(uri: vscode.Uri): Thenable<void>;
4230
delete(uri: vscode.Uri, options?: {recursive: boolean; useTrash: boolean}): Thenable<void>;
43-
//readDirectory(uri: vscode.Uri): Thenable<[string, FileType][]>;
31+
readDirectory(uri: vscode.Uri): Thenable<[string, FileType][]>;
4432
//readFile(uri: vscode.Uri): Thenable<Uint8Array>;
4533
//rename(source: vscode.Uri, target: vscode.Uri, options?: {overwrite: boolean}): Thenable<void>;
4634
//stat(uri: vscode.Uri): Thenable<vscode.FileStat>;
@@ -65,7 +53,6 @@ interface IRawFSExtra {
6553
stat(filename: string): Promise<fsextra.Stats>;
6654
lstat(filename: string): Promise<fsextra.Stats>;
6755
mkdirp(dirname: string): Promise<void>;
68-
readdir(dirname: string): Promise<string[]>;
6956

7057
// non-async
7158
statSync(filename: string): fsextra.Stats;
@@ -103,6 +90,11 @@ export class RawFileSystem implements IRawFileSystem {
10390
});
10491
}
10592

93+
public async listdir(dirname: string): Promise<[string, FileType][]> {
94+
const uri = vscode.Uri.file(dirname);
95+
return this.newapi.readDirectory(uri);
96+
}
97+
10698
//****************************
10799
// fs-extra
108100

@@ -133,20 +125,6 @@ export class RawFileSystem implements IRawFileSystem {
133125
return this.fsExtra.lstat(filename);
134126
}
135127

136-
// Once we move to the VS Code API, this method becomes a trivial
137-
// wrapper and The "path" parameter can go away.
138-
public async listdir(dirname: string, path: IFileSystemPath): Promise<[string, FileType][]> {
139-
const names: string[] = await this.fsExtra.readdir(dirname);
140-
const promises = names
141-
.map(name => {
142-
const filename = path.join(dirname, name);
143-
return this.lstat(filename)
144-
.then(stat => [name, getFileType(stat)] as [string, FileType])
145-
.catch(() => [name, FileType.Unknown] as [string, FileType]);
146-
});
147-
return Promise.all(promises);
148-
}
149-
150128
public async copyFile(src: string, dest: string): Promise<void> {
151129
const deferred = createDeferred<void>();
152130
const rs = this.fsExtra.createReadStream(src)
@@ -309,7 +287,7 @@ export class FileSystemUtils implements IFileSystemUtils {
309287

310288
public async listdir(dirname: string): Promise<[string, FileType][]> {
311289
try {
312-
return await this.raw.listdir(dirname, this.path);
290+
return await this.raw.listdir(dirname);
313291
} catch {
314292
return [];
315293
}

src/client/common/platform/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export interface IRawFileSystem {
6161
// directories
6262
mkdirp(dirname: string): Promise<void>;
6363
rmtree(dirname: string): Promise<void>;
64-
listdir(dirname: string, path: IFileSystemPath): Promise<[string, FileType][]>;
64+
listdir(dirname: string): Promise<[string, FileType][]>;
6565
// not async
6666
statSync(filename: string): FileStat;
6767
readTextSync(filename: string): string;

src/test/common/platform/filesystem.functional.test.ts

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -307,69 +307,6 @@ suite('Raw FileSystem', () => {
307307
});
308308
});
309309

310-
suite('listdir', () => {
311-
let fspath: IFileSystemPath;
312-
setup(() => {
313-
fspath = new FileSystemPath();
314-
});
315-
316-
test('mixed', async () => {
317-
// Create the target directory and its contents.
318-
const dirname = await fix.createDirectory('x/y/z');
319-
await fix.createFile('x/y/z/__init__.py', '');
320-
const script = await fix.createFile('x/y/z/__main__.py', '<script here>');
321-
await fix.createFile('x/y/z/spam.py', '...');
322-
await fix.createSocket('x/y/z/ipc.sock');
323-
await fix.createFile('x/y/z/eggs.py', '"""..."""');
324-
await fix.createSymlink(
325-
'x/y/z/info.py',
326-
// Link to an ignored file.
327-
await fix.createFile('x/_info.py', '<info here>') // source
328-
);
329-
await fix.createDirectory('x/y/z/w');
330-
// Create other files and directories (should be ignored).
331-
await fix.createSymlink(
332-
'my-script.py',
333-
// Link to a listed file.
334-
script // source (__main__.py)
335-
);
336-
const ignored1 = await fix.createFile('x/__init__.py', '');
337-
await fix.createFile('x/y/__init__.py', '');
338-
await fix.createSymlink(
339-
'x/y/z/w/__init__.py',
340-
ignored1 // source (x/__init__.py)
341-
);
342-
await fix.createDirectory('x/y/z/w/data');
343-
await fix.createFile('x/y/z/w/data/v1.json');
344-
345-
const entries = await filesystem.listdir(dirname, fspath);
346-
347-
expect(entries.sort()).to.deep.equal([
348-
['__init__.py', FileType.File],
349-
['__main__.py', FileType.File],
350-
['eggs.py', FileType.File],
351-
['info.py', FileType.SymbolicLink],
352-
['ipc.sock', FileType.Unknown],
353-
['spam.py', FileType.File],
354-
['w', FileType.Directory]
355-
]);
356-
});
357-
358-
test('empty', async () => {
359-
const dirname = await fix.createDirectory('x/y/z/eggs');
360-
361-
const entries = await filesystem.listdir(dirname, fspath);
362-
363-
expect(entries).to.deep.equal([]);
364-
});
365-
366-
test('fails if the directory does not exist', async () => {
367-
const promise = filesystem.listdir(DOES_NOT_EXIST, fspath);
368-
369-
await expect(promise).to.eventually.be.rejected;
370-
});
371-
});
372-
373310
suite('copyFile', () => {
374311
test('the source file gets copied (same directory)', async () => {
375312
const data = '<content>';
@@ -831,61 +768,6 @@ suite('FileSystem Utils', () => {
831768
});
832769
});
833770

834-
suite('getSubDirectories', () => {
835-
test('mixed types', async () => {
836-
const symlinkSource = await fix.createFile('x/info.py');
837-
const dirname = await fix.createDirectory('x/y/z/scripts');
838-
const subdir1 = await fix.createDirectory('x/y/z/scripts/w');
839-
await fix.createFile('x/y/z/scripts/spam.py');
840-
const subdir2 = await fix.createDirectory('x/y/z/scripts/v');
841-
await fix.createFile('x/y/z/scripts/eggs.py');
842-
await fix.createSocket('x/y/z/scripts/spam.sock');
843-
await fix.createSymlink('x/y/z/scripts/other', symlinkSource);
844-
await fix.createFile('x/y/z/scripts/data.json');
845-
846-
const results = await utils.getSubDirectories(dirname);
847-
848-
expect(results.sort()).to.deep.equal([
849-
subdir2,
850-
subdir1
851-
]);
852-
});
853-
854-
test('empty if the directory does not exist', async () => {
855-
const entries = await utils.getSubDirectories(DOES_NOT_EXIST);
856-
857-
expect(entries).to.deep.equal([]);
858-
});
859-
});
860-
861-
suite('getFiles', () => {
862-
test('mixed types', async () => {
863-
const symlinkSource = await fix.createFile('x/info.py');
864-
const dirname = await fix.createDirectory('x/y/z/scripts');
865-
await fix.createDirectory('x/y/z/scripts/w');
866-
const file1 = await fix.createFile('x/y/z/scripts/spam.py');
867-
await fix.createDirectory('x/y/z/scripts/v');
868-
const file2 = await fix.createFile('x/y/z/scripts/eggs.py');
869-
await fix.createSocket('x/y/z/scripts/spam.sock');
870-
await fix.createSymlink('x/y/z/scripts/other', symlinkSource);
871-
const file3 = await fix.createFile('x/y/z/scripts/data.json');
872-
873-
const results = await utils.getFiles(dirname);
874-
875-
expect(results.sort()).to.deep.equal([
876-
file3,
877-
file2,
878-
file1
879-
]);
880-
});
881-
882-
test('empty if the directory does not exist', async () => {
883-
const entries = await utils.getFiles(DOES_NOT_EXIST);
884-
885-
expect(entries).to.deep.equal([]);
886-
});
887-
});
888-
889771
suite('getFileHash', () => {
890772
test('Getting hash for a file should return non-empty string', async () => {
891773
const filename = await fix.createFile('x/y/z/spam.py');

src/test/common/platform/filesystem.test.ts

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
FileSystemUtils, RawFileSystem
88
} from '../../../client/common/platform/fileSystem';
99
import {
10+
FileType,
1011
IFileSystemUtils, IRawFileSystem
1112
} from '../../../client/common/platform/types';
1213
import {
@@ -63,6 +64,64 @@ suite('Raw FileSystem', () => {
6364
await expect(promise).to.eventually.be.rejected;
6465
});
6566
});
67+
68+
suite('listdir', () => {
69+
test('mixed', async () => {
70+
// Create the target directory and its contents.
71+
const dirname = await fix.createDirectory('x/y/z');
72+
await fix.createFile('x/y/z/__init__.py', '');
73+
const script = await fix.createFile('x/y/z/__main__.py', '<script here>');
74+
await fix.createFile('x/y/z/spam.py', '...');
75+
await fix.createSocket('x/y/z/ipc.sock');
76+
await fix.createFile('x/y/z/eggs.py', '"""..."""');
77+
await fix.createSymlink(
78+
'x/y/z/info.py',
79+
// Link to an ignored file.
80+
await fix.createFile('x/_info.py', '<info here>') // source
81+
);
82+
await fix.createDirectory('x/y/z/w');
83+
// Create other files and directories (should be ignored).
84+
await fix.createSymlink(
85+
'my-script.py',
86+
// Link to a listed file.
87+
script // source (__main__.py)
88+
);
89+
const ignored1 = await fix.createFile('x/__init__.py', '');
90+
await fix.createFile('x/y/__init__.py', '');
91+
await fix.createSymlink(
92+
'x/y/z/w/__init__.py',
93+
ignored1 // source (x/__init__.py)
94+
);
95+
await fix.createDirectory('x/y/z/w/data');
96+
await fix.createFile('x/y/z/w/data/v1.json');
97+
98+
const entries = await filesystem.listdir(dirname);
99+
100+
expect(entries.sort()).to.deep.equal([
101+
['__init__.py', FileType.File],
102+
['__main__.py', FileType.File],
103+
['eggs.py', FileType.File],
104+
['info.py', FileType.SymbolicLink],
105+
['ipc.sock', FileType.Unknown],
106+
['spam.py', FileType.File],
107+
['w', FileType.Directory]
108+
]);
109+
});
110+
111+
test('empty', async () => {
112+
const dirname = await fix.createDirectory('x/y/z/eggs');
113+
114+
const entries = await filesystem.listdir(dirname);
115+
116+
expect(entries).to.deep.equal([]);
117+
});
118+
119+
test('fails if the directory does not exist', async () => {
120+
const promise = filesystem.listdir(DOES_NOT_EXIST);
121+
122+
await expect(promise).to.eventually.be.rejected;
123+
});
124+
});
66125
});
67126

68127
suite('FileSystem Utils', () => {
@@ -76,6 +135,61 @@ suite('FileSystem Utils', () => {
76135
await fix.cleanUp();
77136
});
78137

138+
suite('getSubDirectories', () => {
139+
test('mixed types', async () => {
140+
const symlinkSource = await fix.createFile('x/info.py');
141+
const dirname = await fix.createDirectory('x/y/z/scripts');
142+
const subdir1 = await fix.createDirectory('x/y/z/scripts/w');
143+
await fix.createFile('x/y/z/scripts/spam.py');
144+
const subdir2 = await fix.createDirectory('x/y/z/scripts/v');
145+
await fix.createFile('x/y/z/scripts/eggs.py');
146+
await fix.createSocket('x/y/z/scripts/spam.sock');
147+
await fix.createSymlink('x/y/z/scripts/other', symlinkSource);
148+
await fix.createFile('x/y/z/scripts/data.json');
149+
150+
const results = await utils.getSubDirectories(dirname);
151+
152+
expect(results.sort()).to.deep.equal([
153+
subdir2,
154+
subdir1
155+
]);
156+
});
157+
158+
test('empty if the directory does not exist', async () => {
159+
const entries = await utils.getSubDirectories(DOES_NOT_EXIST);
160+
161+
expect(entries).to.deep.equal([]);
162+
});
163+
});
164+
165+
suite('getFiles', () => {
166+
test('mixed types', async () => {
167+
const symlinkSource = await fix.createFile('x/info.py');
168+
const dirname = await fix.createDirectory('x/y/z/scripts');
169+
await fix.createDirectory('x/y/z/scripts/w');
170+
const file1 = await fix.createFile('x/y/z/scripts/spam.py');
171+
await fix.createDirectory('x/y/z/scripts/v');
172+
const file2 = await fix.createFile('x/y/z/scripts/eggs.py');
173+
await fix.createSocket('x/y/z/scripts/spam.sock');
174+
await fix.createSymlink('x/y/z/scripts/other', symlinkSource);
175+
const file3 = await fix.createFile('x/y/z/scripts/data.json');
176+
177+
const results = await utils.getFiles(dirname);
178+
179+
expect(results.sort()).to.deep.equal([
180+
file3,
181+
file2,
182+
file1
183+
]);
184+
});
185+
186+
test('empty if the directory does not exist', async () => {
187+
const entries = await utils.getFiles(DOES_NOT_EXIST);
188+
189+
expect(entries).to.deep.equal([]);
190+
});
191+
});
192+
79193
suite('isDirReadonly', () => {
80194
if (!/^win/.test(process.platform)) {
81195
// On Windows, chmod won't have any effect on the file itself.

0 commit comments

Comments
 (0)