/*---------------------------------------------------------------------------------------------
* 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 * as vscode from 'vscode';
import { assertNoRpc, createRandomFile, disposeAll, withLogDisabled } from '../utils';
suite('vscode API - workspace events', () => {
const disposables: vscode.Disposable[] = [];
teardown(() => {
assertNoRpc();
disposeAll(disposables);
disposables.length = 0;
});
test('onWillCreate/onDidCreate', withLogDisabled(async function () {
const base = await createRandomFile();
const newUri = base.with({ path: base.path + '-foo' });
let onWillCreate: vscode.FileWillCreateEvent | undefined;
let onDidCreate: vscode.FileCreateEvent | undefined;
disposables.push(vscode.workspace.onWillCreateFiles(e => onWillCreate = e));
disposables.push(vscode.workspace.onDidCreateFiles(e => onDidCreate = e));
const edit = new vscode.WorkspaceEdit();
edit.createFile(newUri);
const success = await vscode.workspace.applyEdit(edit);
assert.ok(success);
assert.ok(onWillCreate);
assert.equal(onWillCreate?.files.length, 1);
assert.equal(onWillCreate?.files[0].toString(), newUri.toString());
assert.ok(onDidCreate);
assert.equal(onDidCreate?.files.length, 1);
assert.equal(onDidCreate?.files[0].toString(), newUri.toString());
}));
test('onWillCreate/onDidCreate, make changes, edit another file', withLogDisabled(async function () {
const baseDoc = await vscode.workspace.openTextDocument(base);
disposables.push(vscode.workspace.onWillCreateFiles(e => {
const ws = new vscode.WorkspaceEdit();
ws.insert(base, new vscode.Position(0, 0), 'HALLO_NEW');
e.waitUntil(Promise.resolve(ws));
assert.equal(baseDoc.getText(), 'HALLO_NEW');
test('onWillCreate/onDidCreate, make changes, edit new file fails', withLogDisabled(async function () {
ws.insert(e.files[0], new vscode.Position(0, 0), 'nope');
assert.equal((await vscode.workspace.fs.readFile(newUri)).toString(), '');
assert.equal((await vscode.workspace.openTextDocument(newUri)).getText(), '');
test('onWillDelete/onDidDelete', withLogDisabled(async function () {
let onWilldelete: vscode.FileWillDeleteEvent | undefined;
let onDiddelete: vscode.FileDeleteEvent | undefined;
disposables.push(vscode.workspace.onWillDeleteFiles(e => onWilldelete = e));
disposables.push(vscode.workspace.onDidDeleteFiles(e => onDiddelete = e));
edit.deleteFile(base);
assert.ok(onWilldelete);
assert.equal(onWilldelete?.files.length, 1);
assert.equal(onWilldelete?.files[0].toString(), base.toString());
assert.ok(onDiddelete);
assert.equal(onDiddelete?.files.length, 1);
assert.equal(onDiddelete?.files[0].toString(), base.toString());
test('onWillDelete/onDidDelete, make changes', withLogDisabled(async function () {
const newUri = base.with({ path: base.path + '-NEW' });
disposables.push(vscode.workspace.onWillDeleteFiles(e => {
edit.insert(newUri, new vscode.Position(0, 0), 'hahah');
e.waitUntil(Promise.resolve(edit));
test('onWillDelete/onDidDelete, make changes, del another file', withLogDisabled(async function () {
const base2 = await createRandomFile();
if (e.files[0].toString() === base.toString()) {
edit.deleteFile(base2);
}
test('onWillDelete/onDidDelete, make changes, double delete', withLogDisabled(async function () {
let cnt = 0;
if (++cnt === 0) {
edit.deleteFile(e.files[0]);
test('onWillRename/onDidRename', withLogDisabled(async function () {
const oldUri = await createRandomFile();
const newUri = oldUri.with({ path: oldUri.path + '-NEW' });
let onWillRename: vscode.FileWillRenameEvent | undefined;
let onDidRename: vscode.FileRenameEvent | undefined;
disposables.push(vscode.workspace.onWillRenameFiles(e => onWillRename = e));
disposables.push(vscode.workspace.onDidRenameFiles(e => onDidRename = e));
edit.renameFile(oldUri, newUri);
assert.ok(onWillRename);
assert.equal(onWillRename?.files.length, 1);
assert.equal(onWillRename?.files[0].oldUri.toString(), oldUri.toString());
assert.equal(onWillRename?.files[0].newUri.toString(), newUri.toString());
assert.ok(onDidRename);
assert.equal(onDidRename?.files.length, 1);
assert.equal(onDidRename?.files[0].oldUri.toString(), oldUri.toString());
assert.equal(onDidRename?.files[0].newUri.toString(), newUri.toString());
test('onWillRename - make changes (saved file)', withLogDisabled(function () {
return testOnWillRename(false);
test('onWillRename - make changes (dirty file)', withLogDisabled(function () {
return testOnWillRename(true);
async function testOnWillRename(withDirtyFile: boolean): Promise<void> {
const oldUri = await createRandomFile('BAR');
if (withDirtyFile) {
edit.insert(oldUri, new vscode.Position(0, 0), 'BAR');
const oldDocument = await vscode.workspace.openTextDocument(oldUri);
assert.ok(oldDocument.isDirty);
const anotherFile = await createRandomFile('BAR');
disposables.push(vscode.workspace.onWillRenameFiles(e => {
onWillRename = e;
edit.insert(e.files[0].oldUri, new vscode.Position(0, 0), 'FOO');
edit.replace(anotherFile, new vscode.Range(0, 0, 0, 3), 'FARBOO');
const newDocument = await vscode.workspace.openTextDocument(newUri);
const anotherDocument = await vscode.workspace.openTextDocument(anotherFile);
assert.equal(newDocument.getText(), withDirtyFile ? 'FOOBARBAR' : 'FOOBAR');
assert.equal(anotherDocument.getText(), 'FARBOO');
assert.ok(newDocument.isDirty);
assert.ok(anotherDocument.isDirty);