return service.createFolder(folderResource).then(f => { const resource = uri.file(path.join(testDir, 'deep', 'conway.js')); return service.importFile(resource, uri.file(testDir)).then(res => { assert.equal(fs.existsSync(res.stat.resource.fsPath), true); assert.ok(fs.readdirSync(testDir).some(f => f === 'conway.js')); assert.ok(fs.statSync(res.stat.resource.fsPath).isFile); assert.ok(createEvent); assert.ok(deleteEvent); assert.ok(importEvent); assert.equal(importEvent.resource.fsPath, resource.fsPath); assert.equal(importEvent.target.resource.fsPath, res.stat.resource.fsPath); assert.equal(deleteEvent.resource.fsPath, folderResource.fsPath); toDispose.dispose(); done(); }); });
test('watchFileChanges - support atomic save', function (done: () => void) { let toWatch = uri.file(path.join(testDir, 'index.html')); service.watchFileChanges(toWatch); events.addListener2(EventType.FILE_CHANGES, (e: FileChangesEvent) => { assert.ok(e); service.unwatchFileChanges(toWatch); done(); }); setTimeout(() => { // Simulate atomic save by deleting the file, creating it under different name // and then replacing the previously deleted file with those contents const renamed = `${toWatch.fsPath}.bak`; fs.unlinkSync(toWatch.fsPath); fs.writeFileSync(renamed, 'Changes'); fs.renameSync(renamed, toWatch.fsPath); }, 100); });
fs.readFile(resource.fsPath, (error, data) => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), null); const model = TextModel.createFromString('Hello Bom'); // Update content: UTF_8 => UTF_8_BOM _service.updateContent(resource, model.createSnapshot(), { encoding: encodingLib.UTF8_with_bom }).done(() => { fs.readFile(resource.fsPath, (error, data) => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), encodingLib.UTF8); // Update content: PRESERVE BOM when using UTF-8 model.setValue('Please stay Bom'); _service.updateContent(resource, model.createSnapshot(), { encoding: encodingLib.UTF8 }).done(() => { fs.readFile(resource.fsPath, (error, data) => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), encodingLib.UTF8); // Update content: REMOVE BOM model.setValue('Go away Bom'); _service.updateContent(resource, model.createSnapshot(), { encoding: encodingLib.UTF8, overwriteEncoding: true }).done(() => { fs.readFile(resource.fsPath, (error, data) => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), null); // Update content: BOM comes not back model.setValue('Do not come back Bom'); _service.updateContent(resource, model.createSnapshot(), { encoding: encodingLib.UTF8 }).done(() => { fs.readFile(resource.fsPath, (error, data) => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), null); model.dispose(); _service.dispose(); done(); }); }); }); }); }); }); }); }); });
test('updateContent - encoding preserved (UTF 16 LE, ITextSnapShot)', function () { const encoding = 'utf16le'; const resource = uri.file(path.join(testDir, 'some_utf16le.css')); return service.resolveContent(resource).then(c => { assert.equal(c.encoding, encoding); const model = TextModel.createFromString('Some updates'); return service.updateContent(c.resource, model.createSnapshot(), { encoding: encoding }).then(c => { return encodingLib.detectEncodingByBOM(c.resource.fsPath).then((enc) => { assert.equal(enc, encodingLib.UTF16le); return service.resolveContent(resource).then(c => { assert.equal(c.encoding, encoding); model.dispose(); }); }); }); }); });
test('updateContent - encoding preserved (UTF 16 LE)', function(done: () => void) { let charset = 'utf16le'; let resource = uri.file(path.join(testDir, 'some_utf16le.css')); service.resolveContent(resource).done(c => { assert.equal(c.charset, charset); c.value = 'Some updates'; return service.updateContent(c.resource, c.value, { charset: charset }).then(c => { return nfcall(encoding.detectEncodingByBOM, c.resource.fsPath).then((enc) => { assert.equal(enc, encoding.UTF16le); return service.resolveContent(resource).then(c => { assert.equal(c.charset, charset); done(); }); }); }); }); });
return pfs.readFile(resource.fsPath).then(data => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), encodingLib.UTF8); // Update content: REMOVE BOM model.setValue('Go away Bom'); return _service.updateContent(resource, model.createSnapshot(), { encoding: encodingLib.UTF8, overwriteEncoding: true }).then(() => { return pfs.readFile(resource.fsPath).then(data => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), null); // Update content: BOM comes not back model.setValue('Do not come back Bom'); return _service.updateContent(resource, model.createSnapshot(), { encoding: encodingLib.UTF8 }).then(() => { return pfs.readFile(resource.fsPath).then(data => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), null); model.dispose(); _service.dispose(); }); }); }); }); });
test('updateContent - encoding preserved (UTF 16 LE)', function (done: () => void) { let encoding = 'utf16le'; let resource = uri.file(path.join(testDir, 'some_utf16le.css')); service.resolveContent(resource).done(c => { assert.equal(c.encoding, encoding); c.value = 'Some updates'; return service.updateContent(c.resource, c.value, { encoding: encoding }).then(c => { return nfcall(encodingLib.detectEncodingByBOM, c.resource.fsPath).then((enc) => { assert.equal(enc, encodingLib.UTF16le); return service.resolveContent(resource).then(c => { assert.equal(c.encoding, encoding); done(); }); }); }); }, error => onError(error, done)); });
test('updateContent - use encoding (UTF 16 BE, ITextSnapShot)', function () { const resource = uri.file(path.join(testDir, 'small.txt')); const encoding = 'utf16be'; return service.resolveContent(resource).then(c => { c.encoding = encoding; const model = TextModel.createFromString(c.value); return service.updateContent(c.resource, model.createSnapshot(), { encoding: encoding }).then(c => { return encodingLib.detectEncodingByBOM(c.resource.fsPath).then((enc) => { assert.equal(enc, encodingLib.UTF16be); return service.resolveContent(resource).then(c => { assert.equal(c.encoding, encoding); model.dispose(); }); }); }); }); });
service.resolveFile(uri.file(testDir)).done(parent => { const folderResource = uri.file(path.join(parent.resource.fsPath, 'conway.js')); return service.createFolder(folderResource).then(f => { const resource = uri.file(path.join(testDir, 'deep', 'conway.js')); return service.copyFile(resource, f.resource, true).then(copied => { assert.equal(fs.existsSync(copied.resource.fsPath), true); assert.ok(fs.statSync(copied.resource.fsPath).isFile); assert.ok(createEvent); assert.ok(deleteEvent); assert.ok(copyEvent); assert.equal(copyEvent.resource.fsPath, resource.fsPath); assert.equal(copyEvent.target.resource.fsPath, copied.resource.fsPath); assert.equal(deleteEvent.resource.fsPath, folderResource.fsPath); toDispose.dispose(); done(); }); }); }, error => onError(error, done));
return pfs.readFile(resource.fsPath).then(data => { assert.equal(encodingLib.detectEncodingByBOMFromBuffer(data, 512), null); model.dispose(); _service.dispose(); });