encoding.test.ts 8.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

8
import * as assert from 'assert';
9
import * as fs from 'fs';
10
import * as encoding from 'vs/base/node/encoding';
11
import { readExactlyByFile } from 'vs/base/node/stream';
12
import { Readable } from 'stream';
E
Erich Gamma 已提交
13 14

suite('Encoding', () => {
15
	test('detectBOM UTF-8', () => {
16
		const file = require.toUrl('./fixtures/some_utf8.css');
E
Erich Gamma 已提交
17

18
		return encoding.detectEncodingByBOM(file).then((encoding: string) => {
E
Erich Gamma 已提交
19
			assert.equal(encoding, 'utf8');
20
		});
E
Erich Gamma 已提交
21 22
	});

23
	test('detectBOM UTF-16 LE', () => {
24
		const file = require.toUrl('./fixtures/some_utf16le.css');
E
Erich Gamma 已提交
25

26
		return encoding.detectEncodingByBOM(file).then((encoding: string) => {
E
Erich Gamma 已提交
27
			assert.equal(encoding, 'utf16le');
28
		});
E
Erich Gamma 已提交
29 30
	});

31
	test('detectBOM UTF-16 BE', () => {
32
		const file = require.toUrl('./fixtures/some_utf16be.css');
E
Erich Gamma 已提交
33

34
		return encoding.detectEncodingByBOM(file).then((encoding: string) => {
E
Erich Gamma 已提交
35
			assert.equal(encoding, 'utf16be');
36
		});
E
Erich Gamma 已提交
37 38
	});

39
	test('detectBOM ANSI', function () {
40
		const file = require.toUrl('./fixtures/some_ansi.css');
E
Erich Gamma 已提交
41

42
		return encoding.detectEncodingByBOM(file).then((encoding: string) => {
E
Erich Gamma 已提交
43
			assert.equal(encoding, null);
44
		});
E
Erich Gamma 已提交
45 46
	});

47
	test('detectBOM ANSI', function () {
48
		const file = require.toUrl('./fixtures/empty.txt');
E
Erich Gamma 已提交
49

50
		return encoding.detectEncodingByBOM(file).then((encoding: string) => {
E
Erich Gamma 已提交
51
			assert.equal(encoding, null);
52
		});
E
Erich Gamma 已提交
53
	});
54

55
	test('resolve terminal encoding (detect)', function () {
56 57
		return encoding.resolveTerminalEncoding().then(enc => {
			assert.ok(encoding.encodingExists(enc));
58
		});
59 60
	});

61
	test('resolve terminal encoding (environment)', function () {
62 63
		process.env['VSCODE_CLI_ENCODING'] = 'utf16le';

64 65 66
		return encoding.resolveTerminalEncoding().then(enc => {
			assert.ok(encoding.encodingExists(enc));
			assert.equal(enc, 'utf16le');
67
		});
68
	});
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

	test('detectEncodingFromBuffer (JSON saved as PNG)', function () {
		const file = require.toUrl('./fixtures/some.json.png');

		return readExactlyByFile(file, 512).then(buffer => {
			const mimes = encoding.detectEncodingFromBuffer(buffer);
			assert.equal(mimes.seemsBinary, false);
		});
	});

	test('detectEncodingFromBuffer (PNG saved as TXT)', function () {
		const file = require.toUrl('./fixtures/some.png.txt');
		return readExactlyByFile(file, 512).then(buffer => {
			const mimes = encoding.detectEncodingFromBuffer(buffer);
			assert.equal(mimes.seemsBinary, true);
		});
	});

	test('detectEncodingFromBuffer (XML saved as PNG)', function () {
		const file = require.toUrl('./fixtures/some.xml.png');
		return readExactlyByFile(file, 512).then(buffer => {
			const mimes = encoding.detectEncodingFromBuffer(buffer);
			assert.equal(mimes.seemsBinary, false);
		});
	});

	test('detectEncodingFromBuffer (QWOFF saved as TXT)', function () {
		const file = require.toUrl('./fixtures/some.qwoff.txt');
		return readExactlyByFile(file, 512).then(buffer => {
			const mimes = encoding.detectEncodingFromBuffer(buffer);
			assert.equal(mimes.seemsBinary, true);
		});
	});

	test('detectEncodingFromBuffer (CSS saved as QWOFF)', function () {
		const file = require.toUrl('./fixtures/some.css.qwoff');
		return readExactlyByFile(file, 512).then(buffer => {
			const mimes = encoding.detectEncodingFromBuffer(buffer);
			assert.equal(mimes.seemsBinary, false);
		});
	});

	test('detectEncodingFromBuffer (PDF)', function () {
		const file = require.toUrl('./fixtures/some.pdf');
		return readExactlyByFile(file, 512).then(buffer => {
			const mimes = encoding.detectEncodingFromBuffer(buffer);
			assert.equal(mimes.seemsBinary, true);
		});
	});

	test('detectEncodingFromBuffer (guess UTF-16 LE from content without BOM)', function () {
		const file = require.toUrl('./fixtures/utf16_le_nobom.txt');
		return readExactlyByFile(file, 512).then(buffer => {
			const mimes = encoding.detectEncodingFromBuffer(buffer);
			assert.equal(mimes.encoding, encoding.UTF16le);
			assert.equal(mimes.seemsBinary, false);
		});
	});

	test('detectEncodingFromBuffer (guess UTF-16 BE from content without BOM)', function () {
		const file = require.toUrl('./fixtures/utf16_be_nobom.txt');
		return readExactlyByFile(file, 512).then(buffer => {
			const mimes = encoding.detectEncodingFromBuffer(buffer);
			assert.equal(mimes.encoding, encoding.UTF16be);
			assert.equal(mimes.seemsBinary, false);
		});
	});

	test('autoGuessEncoding (ShiftJIS)', function () {
		const file = require.toUrl('./fixtures/some.shiftjis.txt');
		return readExactlyByFile(file, 512 * 8).then(buffer => {
			return encoding.detectEncodingFromBuffer(buffer, true).then(mimes => {
				assert.equal(mimes.encoding, 'shiftjis');
			});
		});
	});

	test('autoGuessEncoding (CP1252)', function () {
		const file = require.toUrl('./fixtures/some.cp1252.txt');
		return readExactlyByFile(file, 512 * 8).then(buffer => {
			return encoding.detectEncodingFromBuffer(buffer, true).then(mimes => {
				assert.equal(mimes.encoding, 'windows1252');
			});
		});
	});
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

	async function readAndDecodeFromDisk(path, _encoding) {
		return new Promise<string>((resolve, reject) => {
			fs.readFile(path, (err, data) => {
				if (err) {
					reject(err);
				} else {
					resolve(encoding.decode(data, _encoding));
				}
			});
		});
	}

	async function readAllAsString(stream: NodeJS.ReadableStream) {
		return new Promise<string>((resolve, reject) => {
			let all = '';
			stream.on('data', chunk => {
				all += chunk;
				assert.equal(typeof chunk, 'string');
			});
			stream.on('end', () => {
				resolve(all);
			});
			stream.on('error', reject);
		});
	}

	test('toDecodeStream - some stream', async function () {

		let source = new Readable({
			read(size) {
				this.push(Buffer.from([65, 66, 67]));
				this.push(Buffer.from([65, 66, 67]));
				this.push(Buffer.from([65, 66, 67]));
				this.push(null);
			}
		});

		let { detected, stream } = await encoding.toDecodeStream(source, { minBytesRequiredForDetection: 4, guessEncoding: true, overwriteEncoding() { return encoding.UTF8; } });

		assert.ok(detected);
		assert.ok(stream);

		const content = await readAllAsString(stream);
		assert.equal(content, 'ABCABCABC');
	});

	test('toDecodeStream - some stream, expect too much data', async function () {

		let source = new Readable({
			read(size) {
				this.push(Buffer.from([65, 66, 67]));
				this.push(Buffer.from([65, 66, 67]));
				this.push(Buffer.from([65, 66, 67]));
				this.push(null);
			}
		});

		let { detected, stream } = await encoding.toDecodeStream(source, { minBytesRequiredForDetection: 64, guessEncoding: true, overwriteEncoding() { return encoding.UTF8; } });

		assert.ok(detected);
		assert.ok(stream);

		const content = await readAllAsString(stream);
		assert.equal(content, 'ABCABCABC');
	});

	test('toDecodeStream - some stream, no data', async function () {

		let source = new Readable({
			read(size) {
				this.push(null); // empty
			}
		});

		let { detected, stream } = await encoding.toDecodeStream(source, { minBytesRequiredForDetection: 512, guessEncoding: true, overwriteEncoding() { return encoding.UTF8; } });

		assert.ok(detected);
		assert.ok(stream);

		const content = await readAllAsString(stream);
		assert.equal(content, '');
	});


	test('toDecodeStream - encoding, utf16be', async function () {

		let path = require.toUrl('./fixtures/some_utf16be.css');
		let source = fs.createReadStream(path);

		let { detected, stream } = await encoding.toDecodeStream(source, { minBytesRequiredForDetection: 64, guessEncoding: true, overwriteEncoding(detected) { return detected; } });

		assert.equal(detected.encoding, 'utf16be');
		assert.equal(detected.seemsBinary, false);

		let expected = await readAndDecodeFromDisk(path, detected.encoding);
		let actual = await readAllAsString(stream);
		assert.equal(actual, expected);
	});


	test('toDecodeStream - empty file', async function () {

		let path = require.toUrl('./fixtures/empty.txt');
		let source = fs.createReadStream(path);
		let { detected, stream } = await encoding.toDecodeStream(source, { minBytesRequiredForDetection: 64, guessEncoding: true, overwriteEncoding() { return encoding.UTF8; } });

		let expected = await readAndDecodeFromDisk(path, detected.encoding);
		let actual = await readAllAsString(stream);
		assert.equal(actual, expected);
	});
E
Erich Gamma 已提交
265
});