textFileService.io.test.ts 22.0 KB
Newer Older
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.
 *--------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { ILifecycleService } from 'vs/platform/lifecycle/common/lifecycle';
8
import { workbenchInstantiationService, TestLifecycleService, TestTextFileService, TestWindowsService, TestContextService, TestFileService } from 'vs/workbench/test/workbenchTestServices';
9
import { IWindowsService } from 'vs/platform/windows/common/windows';
B
Benjamin Pasero 已提交
10
import { ITextFileService, snapshotToString, TextFileOperationResult, TextFileOperationError } from 'vs/workbench/services/textfile/common/textfiles';
11
import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService';
B
Benjamin Pasero 已提交
12
import { IFileService } from 'vs/platform/files/common/files';
13 14 15 16 17 18 19 20
import { TextFileEditorModelManager } from 'vs/workbench/services/textfile/common/textFileEditorModelManager';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { IModelService } from 'vs/editor/common/services/modelService';
import { ModelServiceImpl } from 'vs/editor/common/services/modelServiceImpl';
import { Schemas } from 'vs/base/common/network';
import { ServiceCollection } from 'vs/platform/instantiation/common/serviceCollection';
import { rimraf, RimRafMode, copy, readFile, exists } from 'vs/base/node/pfs';
import { dispose, IDisposable } from 'vs/base/common/lifecycle';
B
Benjamin Pasero 已提交
21
import { FileService2 } from 'vs/workbench/services/files/common/fileService2';
22 23 24
import { NullLogService } from 'vs/platform/log/common/log';
import { getRandomTestPath } from 'vs/base/test/node/testUtils';
import { tmpdir } from 'os';
B
Benjamin Pasero 已提交
25
import { DiskFileSystemProvider } from 'vs/workbench/services/files/node/diskFileSystemProvider';
26
import { generateUuid } from 'vs/base/common/uuid';
27
import { join, basename } from 'vs/base/common/path';
28 29
import { getPathFromAmdModule } from 'vs/base/common/amd';
import { detectEncodingByBOM, UTF16be, UTF16le, UTF8_with_bom, UTF8 } from 'vs/base/node/encoding';
B
Benjamin Pasero 已提交
30
import { NodeTextFileService, EncodingOracle, IEncodingOverride } from 'vs/workbench/services/textfile/node/textFileService';
31
import { DefaultEndOfLine, ITextSnapshot } from 'vs/editor/common/model';
32
import { TextModel } from 'vs/editor/common/model/textModel';
B
Benjamin Pasero 已提交
33
import { isWindows } from 'vs/base/common/platform';
34
import { readFileSync, statSync } from 'fs';
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

class ServiceAccessor {
	constructor(
		@ILifecycleService public lifecycleService: TestLifecycleService,
		@ITextFileService public textFileService: TestTextFileService,
		@IUntitledEditorService public untitledEditorService: IUntitledEditorService,
		@IWindowsService public windowsService: TestWindowsService,
		@IWorkspaceContextService public contextService: TestContextService,
		@IModelService public modelService: ModelServiceImpl,
		@IFileService public fileService: TestFileService
	) {
	}
}

class TestNodeTextFileService extends NodeTextFileService {

B
Benjamin Pasero 已提交
51
	private _testEncoding: TestEncodingOracle;
52
	get encoding(): TestEncodingOracle {
53
		if (!this._testEncoding) {
B
Benjamin Pasero 已提交
54
			this._testEncoding = this._register(this.instantiationService.createInstance(TestEncodingOracle));
55 56 57 58 59 60
		}

		return this._testEncoding;
	}
}

B
Benjamin Pasero 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73
class TestEncodingOracle extends EncodingOracle {

	protected get encodingOverrides(): IEncodingOverride[] {
		return [
			{ extension: 'utf16le', encoding: UTF16le },
			{ extension: 'utf16be', encoding: UTF16be },
			{ extension: 'utf8bom', encoding: UTF8_with_bom }
		];
	}

	protected set encodingOverrides(overrides: IEncodingOverride[]) { }
}

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
suite('Files - TextFileService i/o', () => {
	const parentDir = getRandomTestPath(tmpdir(), 'vsctests', 'textfileservice');

	let accessor: ServiceAccessor;
	let disposables: IDisposable[] = [];
	let service: ITextFileService;
	let testDir: string;

	setup(async () => {
		const instantiationService = workbenchInstantiationService();
		accessor = instantiationService.createInstance(ServiceAccessor);

		const logService = new NullLogService();
		const fileService = new FileService2(logService);

		const fileProvider = new DiskFileSystemProvider(logService);
		disposables.push(fileService.registerProvider(Schemas.file, fileProvider));
		disposables.push(fileProvider);

		const collection = new ServiceCollection();
		collection.set(IFileService, fileService);

		service = instantiationService.createChild(collection).createInstance(TestNodeTextFileService);

		const id = generateUuid();
		testDir = join(parentDir, id);
		const sourceDir = getPathFromAmdModule(require, './fixtures');

		await copy(sourceDir, testDir);
	});

	teardown(async () => {
		(<TextFileEditorModelManager>accessor.textFileService.models).clear();
		(<TextFileEditorModelManager>accessor.textFileService.models).dispose();
		accessor.untitledEditorService.revertAll();

		disposables = dispose(disposables);

		await rimraf(parentDir, RimRafMode.MOVE);
	});

	test('create - no encoding - content empty', async () => {
		const resource = URI.file(join(testDir, 'small_new.txt'));

		await service.create(resource);

		assert.equal(await exists(resource.fsPath), true);
	});

	test('create - no encoding - content provided', async () => {
		const resource = URI.file(join(testDir, 'small_new.txt'));

		await service.create(resource, 'Hello World');

		assert.equal(await exists(resource.fsPath), true);
		assert.equal((await readFile(resource.fsPath)).toString(), 'Hello World');
	});

132 133
	test('create - UTF 16 LE - no content', async () => {
		const resource = URI.file(join(testDir, 'small_new.utf16le'));
134

135
		await service.create(resource);
136

137
		assert.equal(await exists(resource.fsPath), true);
138

139 140 141
		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF16le);
	});
142 143 144 145 146 147 148 149 150 151 152 153

	test('create - UTF 16 LE - content provided', async () => {
		const resource = URI.file(join(testDir, 'small_new.utf16le'));

		await service.create(resource, 'Hello World');

		assert.equal(await exists(resource.fsPath), true);

		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF16le);
	});

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
	test('create - UTF 16 BE - no content', async () => {
		const resource = URI.file(join(testDir, 'small_new.utf16be'));

		await service.create(resource);

		assert.equal(await exists(resource.fsPath), true);

		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF16be);
	});

	test('create - UTF 16 BE - content provided', async () => {
		const resource = URI.file(join(testDir, 'small_new.utf16be'));

		await service.create(resource, 'Hello World');

		assert.equal(await exists(resource.fsPath), true);

		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF16be);
	});

	test('create - UTF 8 BOM - no content', async () => {
		const resource = URI.file(join(testDir, 'small_new.utf8bom'));
178

179
		await service.create(resource);
180

181
		assert.equal(await exists(resource.fsPath), true);
182

183 184 185
		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);
	});
186 187 188 189 190 191 192 193

	test('create - UTF 8 BOM - content provided', async () => {
		const resource = URI.file(join(testDir, 'small_new.utf8bom'));

		await service.create(resource, 'Hello World');

		assert.equal(await exists(resource.fsPath), true);

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);
	});

	test('create - UTF 8 BOM - empty content - snapshot', async () => {
		const resource = URI.file(join(testDir, 'small_new.utf8bom'));

		await service.create(resource, TextModel.createFromString('').createSnapshot());

		assert.equal(await exists(resource.fsPath), true);

		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);
	});

	test('create - UTF 8 BOM - content provided - snapshot', async () => {
		const resource = URI.file(join(testDir, 'small_new.utf8bom'));

		await service.create(resource, TextModel.createFromString('Hello World').createSnapshot());

		assert.equal(await exists(resource.fsPath), true);

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
		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);
	});

	test('write - use encoding (UTF 16 BE) - small content as string', async () => {
		await testEncoding(URI.file(join(testDir, 'small.txt')), UTF16be, 'Hello\nWorld', 'Hello\nWorld');
	});

	test('write - use encoding (UTF 16 BE) - small content as snapshot', async () => {
		await testEncoding(URI.file(join(testDir, 'small.txt')), UTF16be, TextModel.createFromString('Hello\nWorld').createSnapshot(), 'Hello\nWorld');
	});

	test('write - use encoding (UTF 16 BE) - large content as string', async () => {
		await testEncoding(URI.file(join(testDir, 'lorem.txt')), UTF16be, 'Hello\nWorld', 'Hello\nWorld');
	});

	test('write - use encoding (UTF 16 BE) - large content as snapshot', async () => {
		await testEncoding(URI.file(join(testDir, 'lorem.txt')), UTF16be, TextModel.createFromString('Hello\nWorld').createSnapshot(), 'Hello\nWorld');
	});

	async function testEncoding(resource: URI, encoding: string, content: string | ITextSnapshot, expectedContent: string) {
		await service.write(resource, content, { encoding });

		const detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, encoding);

242
		const resolved = await service.readStream(resource);
243 244
		assert.equal(resolved.encoding, encoding);

B
Benjamin Pasero 已提交
245
		assert.equal(snapshotToString(resolved.value.create(isWindows ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF).createSnapshot(false)), expectedContent);
246 247
	}

B
Benjamin Pasero 已提交
248
	test('write - use encoding (cp1252)', async () => {
B
Benjamin Pasero 已提交
249
		await testEncodingKeepsData(URI.file(join(testDir, 'some_cp1252.txt')), 'cp1252', ['ObjectCount = LoadObjects("Öffentlicher Ordner");', '', 'Private = "Persönliche Information"', ''].join(isWindows ? '\r\n' : '\n'));
B
Benjamin Pasero 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
	});

	test('write - use encoding (shiftjis)', async () => {
		await testEncodingKeepsData(URI.file(join(testDir, 'some_shiftjs.txt')), 'shiftjis', '中文abc');
	});

	test('write - use encoding (gbk)', async () => {
		await testEncodingKeepsData(URI.file(join(testDir, 'some_gbk.txt')), 'gbk', '中国abc');
	});

	test('write - use encoding (cyrillic)', async () => {
		await testEncodingKeepsData(URI.file(join(testDir, 'some_cyrillic.txt')), 'cp866', 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя');
	});

	test('write - use encoding (big5)', async () => {
		await testEncodingKeepsData(URI.file(join(testDir, 'some_big5.txt')), 'cp950', '中文abc');
	});

	async function testEncodingKeepsData(resource: URI, encoding: string, expected: string) {
269
		let resolved = await service.readStream(resource, { encoding });
B
Benjamin Pasero 已提交
270
		const content = snapshotToString(resolved.value.create(isWindows ? DefaultEndOfLine.CRLF : DefaultEndOfLine.LF).createSnapshot(false));
B
Benjamin Pasero 已提交
271 272 273 274
		assert.equal(content, expected);

		await service.write(resource, content, { encoding });

275
		resolved = await service.readStream(resource, { encoding });
B
Benjamin Pasero 已提交
276
		assert.equal(snapshotToString(resolved.value.create(DefaultEndOfLine.CRLF).createSnapshot(false)), content);
B
Benjamin Pasero 已提交
277 278 279

		await service.write(resource, TextModel.createFromString(content).createSnapshot(), { encoding });

280
		resolved = await service.readStream(resource, { encoding });
B
Benjamin Pasero 已提交
281
		assert.equal(snapshotToString(resolved.value.create(DefaultEndOfLine.CRLF).createSnapshot(false)), content);
B
Benjamin Pasero 已提交
282 283
	}

284 285 286 287 288 289 290
	test('write - no encoding - content as string', async () => {
		const resource = URI.file(join(testDir, 'small.txt'));

		const content = (await readFile(resource.fsPath)).toString();

		await service.write(resource, content);

291
		const resolved = await service.readStream(resource);
B
Benjamin Pasero 已提交
292
		assert.equal(resolved.value.getFirstLineText(999999), content);
293 294 295 296 297 298 299 300 301
	});

	test('write - no encoding - content as snapshot', async () => {
		const resource = URI.file(join(testDir, 'small.txt'));

		const content = (await readFile(resource.fsPath)).toString();

		await service.write(resource, TextModel.createFromString(content).createSnapshot());

302
		const resolved = await service.readStream(resource);
B
Benjamin Pasero 已提交
303
		assert.equal(resolved.value.getFirstLineText(999999), content);
304 305 306 307 308
	});

	test('write - encoding preserved (UTF 16 LE) - content as string', async () => {
		const resource = URI.file(join(testDir, 'some_utf16le.css'));

309
		const resolved = await service.readStream(resource);
310 311 312 313 314 315 316 317
		assert.equal(resolved.encoding, UTF16le);

		await testEncoding(URI.file(join(testDir, 'some_utf16le.css')), UTF16le, 'Hello\nWorld', 'Hello\nWorld');
	});

	test('write - encoding preserved (UTF 16 LE) - content as snapshot', async () => {
		const resource = URI.file(join(testDir, 'some_utf16le.css'));

318
		const resolved = await service.readStream(resource);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		assert.equal(resolved.encoding, UTF16le);

		await testEncoding(URI.file(join(testDir, 'some_utf16le.css')), UTF16le, TextModel.createFromString('Hello\nWorld').createSnapshot(), 'Hello\nWorld');
	});

	test('write - UTF8 variations - content as string', async () => {
		const resource = URI.file(join(testDir, 'index.html'));

		let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, null);

		const content = (await readFile(resource.fsPath)).toString() + 'updates';
		await service.write(resource, content, { encoding: UTF8_with_bom });

		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);

		// ensure BOM preserved
		await service.write(resource, content, { encoding: UTF8 });
		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);

		// allow to remove BOM
		await service.write(resource, content, { encoding: UTF8, overwriteEncoding: true });
		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, null);

		// BOM does not come back
		await service.write(resource, content, { encoding: UTF8 });
		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, null);
	});

	test('write - UTF8 variations - content as snapshot', async () => {
		const resource = URI.file(join(testDir, 'index.html'));

		let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, null);

		const model = TextModel.createFromString((await readFile(resource.fsPath)).toString() + 'updates');
		await service.write(resource, model.createSnapshot(), { encoding: UTF8_with_bom });

		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);

		// ensure BOM preserved
		await service.write(resource, model.createSnapshot(), { encoding: UTF8 });
		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);

		// allow to remove BOM
		await service.write(resource, model.createSnapshot(), { encoding: UTF8, overwriteEncoding: true });
		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, null);

		// BOM does not come back
		await service.write(resource, model.createSnapshot(), { encoding: UTF8 });
		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, null);
	});

	test('write - preserve UTF8 BOM - content as string', async () => {
		const resource = URI.file(join(testDir, 'some_utf8_bom.txt'));

		let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);

		await service.write(resource, 'Hello World');
		detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);
	});

B
Benjamin Pasero 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	test('write - ensure BOM in empty file - content as string', async () => {
		const resource = URI.file(join(testDir, 'small.txt'));

		await service.write(resource, '', { encoding: UTF8_with_bom });

		let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);
	});

	test('write - ensure BOM in empty file - content as snapshot', async () => {
		const resource = URI.file(join(testDir, 'small.txt'));

		await service.write(resource, TextModel.createFromString('').createSnapshot(), { encoding: UTF8_with_bom });

		let detectedEncoding = await detectEncodingByBOM(resource.fsPath);
		assert.equal(detectedEncoding, UTF8);
	});
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
	test('readStream - small text', async () => {
		const resource = URI.file(join(testDir, 'small.txt'));

		await testReadStream(resource);
	});

	test('readStream - large text', async () => {
		const resource = URI.file(join(testDir, 'lorem.txt'));

		await testReadStream(resource);
	});

	async function testReadStream(resource: URI): Promise<void> {
		const result = await service.readStream(resource);

		assert.equal(result.name, basename(resource.fsPath));
		assert.equal(result.size, statSync(resource.fsPath).size);
		assert.equal(snapshotToString(result.value.create(DefaultEndOfLine.LF).createSnapshot(false)), snapshotToString(TextModel.createFromString(readFileSync(resource.fsPath).toString()).createSnapshot(false)));
	}

429 430 431
	test('read - small text', async () => {
		const resource = URI.file(join(testDir, 'small.txt'));

432
		await testRead(resource);
433 434 435 436 437
	});

	test('read - large text', async () => {
		const resource = URI.file(join(testDir, 'lorem.txt'));

438
		await testRead(resource);
439 440
	});

441
	async function testRead(resource: URI): Promise<void> {
B
Benjamin Pasero 已提交
442 443 444 445
		const result = await service.read(resource);

		assert.equal(result.name, basename(resource.fsPath));
		assert.equal(result.size, statSync(resource.fsPath).size);
446
		assert.equal(result.value, readFileSync(resource.fsPath).toString());
B
Benjamin Pasero 已提交
447 448
	}

449 450 451 452 453 454 455 456 457
	test('readStream - encoding picked up (CP1252)', async () => {
		const resource = URI.file(join(testDir, 'some_small_cp1252.txt'));
		const encoding = 'windows1252';

		const result = await service.readStream(resource, { encoding });
		assert.equal(result.encoding, encoding);
		assert.equal(result.value.getFirstLineText(999999), 'Private = "Persönlicheß Information"');
	});

458 459 460 461 462 463
	test('read - encoding picked up (CP1252)', async () => {
		const resource = URI.file(join(testDir, 'some_small_cp1252.txt'));
		const encoding = 'windows1252';

		const result = await service.read(resource, { encoding });
		assert.equal(result.encoding, encoding);
464
		assert.equal(result.value, 'Private = "Persönlicheß Information"');
465 466
	});

B
Benjamin Pasero 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	test('read - encoding picked up (binary)', async () => {
		const resource = URI.file(join(testDir, 'some_small_cp1252.txt'));
		const encoding = 'binary';

		const result = await service.read(resource, { encoding });
		assert.equal(result.encoding, encoding);
		assert.equal(result.value, 'Private = "Persönlicheß Information"');
	});

	test('read - encoding picked up (base64)', async () => {
		const resource = URI.file(join(testDir, 'some_small_cp1252.txt'));
		const encoding = 'base64';

		const result = await service.read(resource, { encoding });
		assert.equal(result.encoding, encoding);
		assert.equal(result.value, btoa('Private = "Persönlicheß Information"'));
	});

485
	test('readStream - user overrides BOM', async () => {
486 487
		const resource = URI.file(join(testDir, 'some_utf16le.css'));

488
		const result = await service.readStream(resource, { encoding: 'windows1252' });
489 490 491
		assert.equal(result.encoding, 'windows1252');
	});

492
	test('readStream - BOM removed', async () => {
493 494
		const resource = URI.file(join(testDir, 'some_utf8_bom.txt'));

495
		const result = await service.readStream(resource);
496 497 498
		assert.equal(result.value.getFirstLineText(999999), 'This is some UTF 8 with BOM file.');
	});

499
	test('readStream - invalid encoding', async () => {
500 501
		const resource = URI.file(join(testDir, 'index.html'));

502
		const result = await service.readStream(resource, { encoding: 'superduper' });
503 504 505
		assert.equal(result.encoding, 'utf8');
	});

506
	test('readStream - encoding override', async () => {
507 508
		const resource = URI.file(join(testDir, 'some.utf16le'));

509
		const result = await service.readStream(resource, { encoding: 'windows1252' });
510 511 512 513
		assert.equal(result.encoding, 'utf16le');
		assert.equal(result.value.getFirstLineText(999999), 'This is some UTF 16 with BOM file.');
	});

514
	test('readStream - large Big5', async () => {
B
Benjamin Pasero 已提交
515 516
		await testLargeEncoding('big5', '中文abc');
	});
517

518
	test('readStream - large CP1252', async () => {
B
Benjamin Pasero 已提交
519 520 521
		await testLargeEncoding('cp1252', 'öäüß');
	});

522
	test('readStream - large Cyrillic', async () => {
B
Benjamin Pasero 已提交
523 524 525
		await testLargeEncoding('cp866', 'АБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя');
	});

526
	test('readStream - large GBK', async () => {
B
Benjamin Pasero 已提交
527 528 529
		await testLargeEncoding('gbk', '中国abc');
	});

530
	test('readStream - large ShiftJS', async () => {
B
Benjamin Pasero 已提交
531 532 533
		await testLargeEncoding('shiftjis', '中文abc');
	});

534
	test('readStream - large UTF8 BOM', async () => {
B
Benjamin Pasero 已提交
535 536 537
		await testLargeEncoding('utf8bom', 'öäüß');
	});

538
	test('readStream - large UTF16 LE', async () => {
B
Benjamin Pasero 已提交
539 540 541
		await testLargeEncoding('utf16le', 'öäüß');
	});

542
	test('readStream - large UTF16 BE', async () => {
B
Benjamin Pasero 已提交
543 544 545 546 547 548
		await testLargeEncoding('utf16be', 'öäüß');
	});

	async function testLargeEncoding(encoding: string, needle: string): Promise<void> {
		const resource = URI.file(join(testDir, `lorem_${encoding}.txt`));

549
		const result = await service.readStream(resource, { encoding });
B
Benjamin Pasero 已提交
550 551 552 553 554 555
		assert.equal(result.encoding, encoding);

		const contents = snapshotToString(result.value.create(DefaultEndOfLine.LF).createSnapshot(false));

		assert.equal(contents.indexOf(needle), 0);
		assert.ok(contents.indexOf(needle, 10) > 0);
556 557
	}

558
	test('readStream - FILE_IS_BINARY', async () => {
559 560
		const resource = URI.file(join(testDir, 'binary.txt'));

B
Benjamin Pasero 已提交
561
		let error: TextFileOperationError | undefined = undefined;
562
		try {
563
			await service.readStream(resource, { acceptTextOnly: true });
564 565 566 567 568
		} catch (err) {
			error = err;
		}

		assert.ok(error);
B
Benjamin Pasero 已提交
569
		assert.equal(error!.textFileOperationResult, TextFileOperationResult.FILE_IS_BINARY);
570

571
		const result = await service.readStream(URI.file(join(testDir, 'small.txt')), { acceptTextOnly: true });
572 573
		assert.equal(result.name, 'small.txt');
	});
574
});