Skip to content

Commit a152544

Browse files
Move readText() to the new API.
1 parent 5dc34f2 commit a152544

File tree

4 files changed

+75
-57
lines changed

4 files changed

+75
-57
lines changed

src/client/common/platform/fileSystem.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ interface INewAPI {
2929
//createDirectory(uri: vscode.Uri): Thenable<void>;
3030
delete(uri: vscode.Uri, options?: {recursive: boolean; useTrash: boolean}): Thenable<void>;
3131
readDirectory(uri: vscode.Uri): Thenable<[string, FileType][]>;
32-
//readFile(uri: vscode.Uri): Thenable<Uint8Array>;
32+
readFile(uri: vscode.Uri): Thenable<Uint8Array>;
3333
//rename(source: vscode.Uri, target: vscode.Uri, options?: {overwrite: boolean}): Thenable<void>;
3434
//stat(uri: vscode.Uri): Thenable<vscode.FileStat>;
3535
//writeFile(uri: vscode.Uri, content: Uint8Array): Thenable<void>;
@@ -47,7 +47,6 @@ interface IRawFS {
4747

4848
interface IRawFSExtra {
4949
chmod(filePath: string, mode: string | number): Promise<void>;
50-
readFile(path: string, encoding: string): Promise<string>;
5150
//tslint:disable-next-line:no-any
5251
writeFile(path: string, data: any, options: any): Promise<void>;
5352
stat(filename: string): Promise<fsextra.Stats>;
@@ -74,6 +73,13 @@ export class RawFileSystem implements IRawFileSystem {
7473
//****************************
7574
// VS Code API
7675

76+
public async readText(filename: string): Promise<string> {
77+
const uri = vscode.Uri.file(filename);
78+
const data = Buffer.from(
79+
await this.newapi.readFile(uri));
80+
return data.toString(ENCODING);
81+
}
82+
7783
public async rmtree(dirname: string): Promise<void> {
7884
const uri = vscode.Uri.file(dirname);
7985
return this.newapi.delete(uri, {
@@ -98,10 +104,6 @@ export class RawFileSystem implements IRawFileSystem {
98104
//****************************
99105
// fs-extra
100106

101-
public async readText(filename: string): Promise<string> {
102-
return this.fsExtra.readFile(filename, ENCODING);
103-
}
104-
105107
public async writeText(filename: string, data: {}): Promise<void> {
106108
const options: fsextra.WriteFileOptions = {
107109
encoding: ENCODING

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -107,32 +107,6 @@ suite('Raw FileSystem', () => {
107107
await fix.cleanUp();
108108
});
109109

110-
suite('readText', () => {
111-
test('returns contents of a file', async () => {
112-
const expected = '<some text>';
113-
const filename = await fix.createFile('x/y/z/spam.py', expected);
114-
115-
const content = await filesystem.readText(filename);
116-
117-
expect(content).to.be.equal(expected);
118-
});
119-
120-
test('always UTF-8', async () => {
121-
const expected = '... 😁 ...';
122-
const filename = await fix.createFile('x/y/z/spam.py', expected);
123-
124-
const text = await filesystem.readText(filename);
125-
126-
expect(text).to.equal(expected);
127-
});
128-
129-
test('throws an exception if file does not exist', async () => {
130-
const promise = filesystem.readText(DOES_NOT_EXIST);
131-
132-
await expect(promise).to.eventually.be.rejected;
133-
});
134-
});
135-
136110
suite('writeText', () => {
137111
test('creates the file if missing', async () => {
138112
const filename = await fix.resolve('x/y/z/spam.py');
@@ -902,24 +876,6 @@ suite('FileSystem - legacy aliases', () => {
902876
}
903877
}
904878

905-
test('ReadFile returns contents of a file', async () => {
906-
const file = __filename;
907-
const filesystem = new FileSystem();
908-
const expectedContents = await fsextra.readFile(file).then(buffer => buffer.toString());
909-
910-
const content = await filesystem.readFile(file);
911-
912-
expect(content).to.be.equal(expectedContents);
913-
});
914-
915-
test('ReadFile throws an exception if file does not exist', async () => {
916-
const filesystem = new FileSystem();
917-
918-
const readPromise = filesystem.readFile('xyz');
919-
920-
await expect(readPromise).to.be.rejectedWith();
921-
});
922-
923879
suite('Case sensitivity', () => {
924880
const path1 = 'c:\\users\\Peter Smith\\my documents\\test.txt';
925881
const path2 = 'c:\\USERS\\Peter Smith\\my documents\\test.TXT';

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
import { expect } from 'chai';
55
import * as fsextra from 'fs-extra';
6+
import * as path from 'path';
67
import {
7-
FileSystemUtils, RawFileSystem
8+
FileSystem, FileSystemUtils, RawFileSystem
89
} from '../../../client/common/platform/fileSystem';
910
import {
1011
FileType,
@@ -30,6 +31,32 @@ suite('Raw FileSystem', () => {
3031
await fix.cleanUp();
3132
});
3233

34+
suite('readText', () => {
35+
test('returns contents of a file', async () => {
36+
const expected = '<some text>';
37+
const filename = await fix.createFile('x/y/z/spam.py', expected);
38+
39+
const content = await filesystem.readText(filename);
40+
41+
expect(content).to.be.equal(expected);
42+
});
43+
44+
test('always UTF-8', async () => {
45+
const expected = '... 😁 ...';
46+
const filename = await fix.createFile('x/y/z/spam.py', expected);
47+
48+
const text = await filesystem.readText(filename);
49+
50+
expect(text).to.equal(expected);
51+
});
52+
53+
test('throws an exception if file does not exist', async () => {
54+
const promise = filesystem.readText(DOES_NOT_EXIST);
55+
56+
await expect(promise).to.eventually.be.rejected;
57+
});
58+
});
59+
3360
suite('rmtree', () => {
3461
test('deletes the directory and everything in it', async () => {
3562
const dirname = await fix.createDirectory('x');
@@ -218,3 +245,34 @@ suite('FileSystem Utils', () => {
218245
});
219246
});
220247
});
248+
249+
suite('FileSystem - legacy aliases', () => {
250+
const fileToAppendTo = path.join(__dirname, 'created_for_testing_dummy.txt');
251+
setup(() => {
252+
cleanTestFiles(); // This smells like functional testing...
253+
});
254+
teardown(cleanTestFiles);
255+
function cleanTestFiles() {
256+
if (fsextra.existsSync(fileToAppendTo)) {
257+
fsextra.unlinkSync(fileToAppendTo);
258+
}
259+
}
260+
261+
test('ReadFile returns contents of a file', async () => {
262+
const file = __filename;
263+
const filesystem = new FileSystem();
264+
const expectedContents = await fsextra.readFile(file).then(buffer => buffer.toString());
265+
266+
const content = await filesystem.readFile(file);
267+
268+
expect(content).to.be.equal(expectedContents);
269+
});
270+
271+
test('ReadFile throws an exception if file does not exist', async () => {
272+
const filesystem = new FileSystem();
273+
274+
const readPromise = filesystem.readFile('xyz');
275+
276+
await expect(readPromise).to.be.rejectedWith();
277+
});
278+
});

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface IRawFS {
2121
// VS Code
2222
delete(uri: vscode.Uri, options?: {recursive: boolean; useTrash: boolean}): Thenable<void>;
2323
readDirectory(uri: vscode.Uri): Thenable<[string, FileType][]>;
24+
readFile(uri: vscode.Uri): Thenable<Uint8Array>;
2425

2526
// node "fs"
2627
//tslint:disable-next-line:no-any
@@ -30,7 +31,6 @@ interface IRawFS {
3031

3132
// "fs-extra"
3233
chmod(filePath: string, mode: string): Promise<void>;
33-
readFile(path: string, encoding: string): Promise<string>;
3434
//tslint:disable-next-line:no-any
3535
writeFile(path: string, data: any, options: any): Promise<void>;
3636
stat(filename: string): Promise<fsextra.Stats>;
@@ -65,8 +65,9 @@ suite('Raw FileSystem', () => {
6565
test('wraps the low-level function', async () => {
6666
const filename = 'x/y/z/spam.py';
6767
const expected = '<text>';
68-
raw.setup(r => r.readFile(filename, TypeMoq.It.isAny()))
69-
.returns(() => Promise.resolve(expected));
68+
const data = Buffer.from(expected);
69+
raw.setup(r => r.readFile(vscode.Uri.file(filename)))
70+
.returns(() => Promise.resolve(data));
7071

7172
const text = await filesystem.readText(filename);
7273

@@ -76,9 +77,10 @@ suite('Raw FileSystem', () => {
7677

7778
test('always UTF-8', async () => {
7879
const filename = 'x/y/z/spam.py';
79-
const expected = '<text>';
80-
raw.setup(r => r.readFile(filename, 'utf8'))
81-
.returns(() => Promise.resolve(expected));
80+
const expected = '... 😁 ...';
81+
const data = Buffer.from(expected);
82+
raw.setup(r => r.readFile(vscode.Uri.file(filename)))
83+
.returns(() => Promise.resolve(data));
8284

8385
const text = await filesystem.readText(filename);
8486

0 commit comments

Comments
 (0)