This commit is contained in:
Benjamin Pasero
2025-02-19 12:53:49 +01:00
parent 52d6a94d3d
commit da6fc30c98
9 changed files with 119 additions and 98 deletions

View File

@@ -12,6 +12,12 @@ import { assertNoRpc, closeAllEditors, createRandomFile, delay, deleteFile, disp
suite('vscode API - workspace', () => {
let root: vscode.Uri;
suiteSetup(function () {
root = vscode.workspace.workspaceFolders![0]!.uri;
});
teardown(async function () {
assertNoRpc();
await closeAllEditors();
@@ -1326,4 +1332,72 @@ suite('vscode API - workspace', () => {
const doc5 = await vscode.workspace.openTextDocument({ content: 'Hello World' });
assert.strictEqual(doc5.encoding, 'utf8');
});
test('fs.decode', async function () {
const uri = root.with({ path: posix.join(root.path, 'file.txt') });
// without setting
assert.strictEqual(await vscode.workspace.decode(uri, Buffer.from('Hello World')), 'Hello World');
assert.strictEqual(await vscode.workspace.decode(uri, Buffer.from('Hellö Wörld')), 'Hellö Wörld');
assert.strictEqual(await vscode.workspace.decode(uri, new Uint8Array([0xEF, 0xBB, 0xBF, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100])), 'Hello World'); // UTF-8 with BOM
assert.strictEqual(await vscode.workspace.decode(uri, new Uint8Array([0xFE, 0xFF, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100])), 'Hello World'); // UTF-16 BE with BOM
assert.strictEqual(await vscode.workspace.decode(uri, new Uint8Array([0xFF, 0xFE, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0])), 'Hello World'); // UTF-16 LE with BOM
assert.strictEqual(await vscode.workspace.decode(uri, new Uint8Array([0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100])), 'Hello World');
assert.strictEqual(await vscode.workspace.decode(uri, new Uint8Array([72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0])), 'Hello World');
// with auto-guess encoding
try {
await vscode.workspace.getConfiguration('files', uri).update('autoGuessEncoding', true, vscode.ConfigurationTarget.Global);
assert.strictEqual(await vscode.workspace.decode(uri, new Uint8Array([72, 101, 108, 108, 0xF6, 32, 87, 0xF6, 114, 108, 100])), 'Hellö Wörld');
} finally {
await vscode.workspace.getConfiguration('files', uri).update('autoGuessEncoding', false, vscode.ConfigurationTarget.Global);
}
// with encoding setting
try {
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'windows1252', vscode.ConfigurationTarget.Global);
assert.strictEqual(await vscode.workspace.decode(uri, new Uint8Array([72, 101, 108, 108, 0xF6, 32, 87, 0xF6, 114, 108, 100])), 'Hellö Wörld');
} finally {
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf8', vscode.ConfigurationTarget.Global);
}
});
test('fs.encode', async function () {
const uri = root.with({ path: posix.join(root.path, 'file.txt') });
// without setting
assert.strictEqual((await vscode.workspace.encode(uri, 'Hello World')).toString(), 'Hello World');
// with encoding setting
try {
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf8bom', vscode.ConfigurationTarget.Global);
assert.ok(equalsUint8Array(await vscode.workspace.encode(uri, 'Hello World'), new Uint8Array([0xEF, 0xBB, 0xBF, 72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100])));
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf16le', vscode.ConfigurationTarget.Global);
assert.ok(equalsUint8Array(await vscode.workspace.encode(uri, 'Hello World'), new Uint8Array([0xFF, 0xFE, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100, 0])));
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf16be', vscode.ConfigurationTarget.Global);
assert.ok(equalsUint8Array(await vscode.workspace.encode(uri, 'Hello World'), new Uint8Array([0xFE, 0xFF, 0, 72, 0, 101, 0, 108, 0, 108, 0, 111, 0, 32, 0, 87, 0, 111, 0, 114, 0, 108, 0, 100])));
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'cp1252', vscode.ConfigurationTarget.Global);
assert.ok(equalsUint8Array(await vscode.workspace.encode(uri, 'Hellö Wörld'), new Uint8Array([72, 101, 108, 108, 0xF6, 32, 87, 0xF6, 114, 108, 100])));
} finally {
await vscode.workspace.getConfiguration('files', uri).update('encoding', 'utf8', vscode.ConfigurationTarget.Global);
}
});
function equalsUint8Array(a: Uint8Array, b: Uint8Array): boolean {
if (a === b) {
return true;
}
if (a.byteLength !== b.byteLength) {
return false;
}
for (let i = 0; i < a.byteLength; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
});