Skip to content

Commit 45717d3

Browse files
Use the new API for writeText().
1 parent b7b5bd7 commit 45717d3

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

src/client/common/platform/fileSystem.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ interface INewAPI {
4949
readFile(uri: vscode.Uri): Thenable<Uint8Array>;
5050
//rename(source: vscode.Uri, target: vscode.Uri, options?: {overwrite: boolean}): Thenable<void>;
5151
stat(uri: vscode.Uri): Thenable<FileStat>;
52-
//writeFile(uri: vscode.Uri, content: Uint8Array): Thenable<void>;
52+
writeFile(uri: vscode.Uri, content: Uint8Array): Thenable<void>;
5353
}
5454

5555
interface IRawFS {
@@ -64,8 +64,6 @@ interface IRawFS {
6464

6565
interface IRawFSExtra {
6666
chmod(filePath: string, mode: string | number): Promise<void>;
67-
//tslint:disable-next-line:no-any
68-
writeFile(path: string, data: any, options: any): Promise<void>;
6967
lstat(filename: string): Promise<fsextra.Stats>;
7068
mkdirp(dirname: string): Promise<void>;
7169

@@ -96,6 +94,12 @@ export class RawFileSystem implements IRawFileSystem {
9694
return data.toString(ENCODING);
9795
}
9896

97+
public async writeText(filename: string, text: string): Promise<void> {
98+
const uri = vscode.Uri.file(filename);
99+
const data = Buffer.from(text);
100+
await this.newapi.writeFile(uri, data);
101+
}
102+
99103
public async rmtree(dirname: string): Promise<void> {
100104
const uri = vscode.Uri.file(dirname);
101105
return this.newapi.delete(uri, {
@@ -125,13 +129,6 @@ export class RawFileSystem implements IRawFileSystem {
125129
//****************************
126130
// fs-extra
127131

128-
public async writeText(filename: string, data: {}): Promise<void> {
129-
const options: fsextra.WriteFileOptions = {
130-
encoding: ENCODING
131-
};
132-
await this.fsExtra.writeFile(filename, data, options);
133-
}
134-
135132
public async mkdirp(dirname: string): Promise<void> {
136133
return this.fsExtra.mkdirp(dirname);
137134
}

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

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

110-
suite('writeText', () => {
111-
test('creates the file if missing', async () => {
112-
const filename = await fix.resolve('x/y/z/spam.py');
113-
await ensureDoesNotExist(filename);
114-
const data = 'line1\nline2\n';
115-
116-
await filesystem.writeText(filename, data);
117-
118-
const actual = await fsextra.readFile(filename)
119-
.then(buffer => buffer.toString());
120-
expect(actual).to.equal(data);
121-
});
122-
123-
test('always UTF-8', async () => {
124-
const filename = await fix.resolve('x/y/z/spam.py');
125-
const data = '... 😁 ...';
126-
127-
await filesystem.writeText(filename, data);
128-
129-
const actual = await fsextra.readFile(filename)
130-
.then(buffer => buffer.toString());
131-
expect(actual).to.equal(data);
132-
});
133-
134-
test('overwrites existing file', async () => {
135-
const filename = await fix.createFile('x/y/z/spam.py', '...');
136-
const data = 'line1\nline2\n';
137-
138-
await filesystem.writeText(filename, data);
139-
140-
const actual = await fsextra.readFile(filename)
141-
.then(buffer => buffer.toString());
142-
expect(actual).to.equal(data);
143-
});
144-
});
145-
146110
suite('mkdirp', () => {
147111
test('creates the directory and all missing parents', async () => {
148112
await fix.createDirectory('x');

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,42 @@ suite('Raw FileSystem', () => {
5757
});
5858
});
5959

60+
suite('writeText', () => {
61+
test('creates the file if missing', async () => {
62+
const filename = await fix.resolve('x/y/z/spam.py');
63+
await ensureDoesNotExist(filename);
64+
const data = 'line1\nline2\n';
65+
66+
await filesystem.writeText(filename, data);
67+
68+
const actual = await fsextra.readFile(filename)
69+
.then(buffer => buffer.toString());
70+
expect(actual).to.equal(data);
71+
});
72+
73+
test('always UTF-8', async () => {
74+
const filename = await fix.resolve('x/y/z/spam.py');
75+
const data = '... 😁 ...';
76+
77+
await filesystem.writeText(filename, data);
78+
79+
const actual = await fsextra.readFile(filename)
80+
.then(buffer => buffer.toString());
81+
expect(actual).to.equal(data);
82+
});
83+
84+
test('overwrites existing file', async () => {
85+
const filename = await fix.createFile('x/y/z/spam.py', '...');
86+
const data = 'line1\nline2\n';
87+
88+
await filesystem.writeText(filename, data);
89+
90+
const actual = await fsextra.readFile(filename)
91+
.then(buffer => buffer.toString());
92+
expect(actual).to.equal(data);
93+
});
94+
});
95+
6096
suite('rmtree', () => {
6197
test('deletes the directory and everything in it', async () => {
6298
const dirname = await fix.createDirectory('x');

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ interface IRawFS {
2323
readDirectory(uri: vscode.Uri): Thenable<[string, FileType][]>;
2424
readFile(uri: vscode.Uri): Thenable<Uint8Array>;
2525
stat(uri: vscode.Uri): Thenable<FileStat>;
26+
writeFile(uri: vscode.Uri, content: Uint8Array): Thenable<void>;
2627

2728
// node "fs"
2829
//tslint:disable-next-line:no-any
@@ -32,8 +33,6 @@ interface IRawFS {
3233

3334
// "fs-extra"
3435
chmod(filePath: string, mode: string): Promise<void>;
35-
//tslint:disable-next-line:no-any
36-
writeFile(path: string, data: any, options: any): Promise<void>;
3736
lstat(filename: string): Promise<fsextra.Stats>;
3837
mkdirp(dirname: string): Promise<void>;
3938
statSync(filename: string): fsextra.Stats;
@@ -150,11 +149,24 @@ suite('Raw FileSystem', () => {
150149
suite('writeText', () => {
151150
test('wraps the low-level function', async () => {
152151
const filename = 'x/y/z/spam.py';
153-
const data = '<data>';
154-
raw.setup(r => r.writeFile(filename, data, { encoding: 'utf8' }))
152+
const text = '<data>';
153+
const data = Buffer.from(text);
154+
raw.setup(r => r.writeFile(vscode.Uri.file(filename), data))
155+
.returns(() => Promise.resolve());
156+
157+
await filesystem.writeText(filename, text);
158+
159+
verifyAll();
160+
});
161+
162+
test('always UTF-8', async () => {
163+
const filename = 'x/y/z/spam.py';
164+
const text = '... 😁 ...';
165+
const data = Buffer.from(text);
166+
raw.setup(r => r.writeFile(vscode.Uri.file(filename), data))
155167
.returns(() => Promise.resolve());
156168

157-
await filesystem.writeText(filename, data);
169+
await filesystem.writeText(filename, text);
158170

159171
verifyAll();
160172
});

0 commit comments

Comments
 (0)