diff --git a/extensions/vscode-api-tests/src/editor.test.ts b/extensions/vscode-api-tests/src/editor.test.ts new file mode 100644 index 00000000000..0e7bfaf05a0 --- /dev/null +++ b/extensions/vscode-api-tests/src/editor.test.ts @@ -0,0 +1,39 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import * as assert from 'assert'; +import * as fs from 'fs'; +import * as os from 'os'; +import {workspace, window, Position} from 'vscode'; +import {createRandomFile, deleteFile} from './utils'; +import {join} from 'path'; + +suite("editor tests", () => { + + test('make edit', (done) => { + createRandomFile().then(file => { + return workspace.openTextDocument(file).then(doc => { + return window.showTextDocument(doc).then((editor) => { + return editor.edit((builder) => { + builder.insert(new Position(0, 0), 'Hello World'); + }).then(applied => { + assert.ok(applied); + assert.equal(doc.getText(), 'Hello World'); + assert.ok(doc.isDirty); + + return doc.save().then(saved => { + assert.ok(saved); + assert.ok(!doc.isDirty); + + return deleteFile(file); + }); + }); + }); + }); + }).then(() => done(), (error) => done(error)); + }); +}); \ No newline at end of file diff --git a/extensions/vscode-api-tests/src/index.ts b/extensions/vscode-api-tests/src/index.ts index 84f3fc542f3..512c2794dd6 100644 --- a/extensions/vscode-api-tests/src/index.ts +++ b/extensions/vscode-api-tests/src/index.ts @@ -21,7 +21,8 @@ const testRunner = require('vscode/lib/testrunner'); // See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info testRunner.configure({ ui: 'tdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.) - useColors: true // colored output from test results + useColors: true, // colored output from test results + timeout: 10000 }); export= testRunner; \ No newline at end of file diff --git a/extensions/vscode-api-tests/src/utils.ts b/extensions/vscode-api-tests/src/utils.ts new file mode 100644 index 00000000000..23e36c59b6b --- /dev/null +++ b/extensions/vscode-api-tests/src/utils.ts @@ -0,0 +1,40 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import * as vscode from 'vscode'; +import * as fs from 'fs'; +import * as os from 'os'; +import {join} from 'path'; + +function rndName() { + return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10); +} + +export function createRandomFile(contents = ''): Thenable { + return new Promise((resolve, reject) => { + const tmpFile = join(os.tmpdir(), rndName()); + fs.writeFile(tmpFile, contents, (error) => { + if (error) { + return reject(error); + } + + resolve(vscode.Uri.file(tmpFile)); + }); + }); +} + +export function deleteFile(file: vscode.Uri): Thenable { + return new Promise((resolve, reject) => { + fs.unlink(file.fsPath, (err) => { + if (err) { + reject(err); + } else { + resolve(true); + } + }); + }); +} \ No newline at end of file diff --git a/extensions/vscode-api-tests/src/window.test.ts b/extensions/vscode-api-tests/src/window.test.ts index 7b0a5142702..2de42113ce3 100644 --- a/extensions/vscode-api-tests/src/window.test.ts +++ b/extensions/vscode-api-tests/src/window.test.ts @@ -6,12 +6,18 @@ 'use strict'; import * as assert from 'assert'; -import {window, workspace} from 'vscode'; +import {workspace, window} from 'vscode'; +import {join} from 'path'; -suite("window namespace texts", () => { - - // test('open document fires event', (done) => { - - // }); +suite("window namespace tests", () => { + test('active text editor', (done) => { + workspace.openTextDocument(join(workspace.rootPath, './far.js')).then(doc => { + return window.showTextDocument(doc).then((editor) => { + const active = window.activeTextEditor; + assert.ok(active); + assert.equal(active.document.uri.fsPath, doc.uri.fsPath); + }); + }).then(() => done(), (error) => done(error)); + }); }); \ No newline at end of file diff --git a/extensions/vscode-api-tests/src/workspace.test.ts b/extensions/vscode-api-tests/src/workspace.test.ts index 4a89e6b278a..8b2a17b2af4 100644 --- a/extensions/vscode-api-tests/src/workspace.test.ts +++ b/extensions/vscode-api-tests/src/workspace.test.ts @@ -6,8 +6,15 @@ 'use strict'; import * as assert from 'assert'; -import {workspace, TextDocument} from 'vscode'; +import {workspace, TextDocument, window, Position} from 'vscode'; +import {createRandomFile, deleteFile} from './utils'; import {join} from 'path'; +import * as fs from 'fs'; +import * as os from 'os'; + +function rndName() { + return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 10); +} suite('workspace-namespace', () => { @@ -38,17 +45,52 @@ suite('workspace-namespace', () => { }); }); - // test('createTextDocument', done => { + test('events: onDidOpenTextDocument, onDidChangeTextDocument, onDidSaveTextDocument', (done) => { + createRandomFile().then(file => { + let onDidOpenTextDocument = false; + workspace.onDidOpenTextDocument(e => { + assert.equal(e.uri.fsPath, file.fsPath); + onDidOpenTextDocument = true; + }); - // let text = 'Das Pferd isst keinen Reis.' + let onDidChangeTextDocument = false; + workspace.onDidChangeTextDocument(e => { + assert.equal(e.document.uri.fsPath, file.fsPath); + onDidChangeTextDocument = true; + }); - // workspace.createTextDocument(text).then(doc => { - // assert.equal(doc.getText(), text); - // assert.equal(doc.uri.scheme, 'untitled'); - // assert.equal(doc.languageId, 'plaintext'); - // done(); - // }, err => { - // done(err); - // }); - // }); + let onDidSaveTextDocument = false; + workspace.onDidSaveTextDocument(e => { + assert.equal(e.uri.fsPath, file.fsPath); + onDidSaveTextDocument = true; + }); + + return workspace.openTextDocument(file).then(doc => { + return window.showTextDocument(doc).then((editor) => { + return editor.edit((builder) => { + builder.insert(new Position(0, 0), 'Hello World'); + }).then(applied => { + return doc.save().then(saved => { + assert.ok(onDidOpenTextDocument); + assert.ok(onDidChangeTextDocument); + assert.ok(onDidSaveTextDocument); + + return deleteFile(file); + }); + }); + }); + }); + }).then(() => done(), (error) => done(error)); + }); + + test('findFiles', done => { + workspace.findFiles('*.js', null).then((res) => { + assert.equal(res.length, 1); + assert.equal(workspace.asRelativePath(res[0]), '/far.js'); + + done(); + }, err => { + done(err); + }); + }); }); \ No newline at end of file diff --git a/src/vs/workbench/electron-main/main.ts b/src/vs/workbench/electron-main/main.ts index e1f5ca381c5..cbea266a3ed 100644 --- a/src/vs/workbench/electron-main/main.ts +++ b/src/vs/workbench/electron-main/main.ts @@ -190,6 +190,15 @@ function setupIPC(): TPromise { // there's a running instance, let's connect to it return connect(env.mainIPCHandle).then( client => { + + // Tests from CLI require to be the only instance currently (TODO@Ben support multiple instances and output) + if (env.isTestingFromCli) { + const errorMsg = 'Running tests from the command line is currently only supported if no other instance of Code is running.'; + console.error(errorMsg); + + return Promise.wrapError(errorMsg); + } + env.log('Sending env to running instance...'); const service = client.getService('LaunchService', LaunchService);