Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Int-Tests: use memfs instead of fs #78199

Merged
merged 4 commits into from
Jul 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion build/azure-pipelines/linux/continuous-build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ steps:
- script: |
DISPLAY=:10 ./scripts/test.sh --tfs "Unit Tests"
displayName: Run Unit Tests
- script: |
DISPLAY=:10 ./scripts/test-integration.sh --tfs "Integration Tests"
displayName: Run Integration Tests
- task: PublishTestResults@2
displayName: Publish Tests Results
inputs:
testResultsFiles: '*-results.xml'
searchFolder: '$(Build.ArtifactStagingDirectory)/test-results'
condition: succeededOrFailed()
condition: succeededOrFailed()
15 changes: 11 additions & 4 deletions build/azure-pipelines/linux/product-build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,23 @@ steps:

- script: |
set -e
yarn gulp "electron-x64"

# xvfb seems to be crashing often, let's make sure it's always up
service xvfb start
displayName: Start xvfb
condition: and(succeeded(), eq(variables['VSCODE_STEP_ON_IT'], 'false'))

- script: |
set -e
DISPLAY=:10 ./scripts/test.sh --build --tfs "Unit Tests"
# yarn smoketest -- --build "$(agent.builddirectory)/VSCode-linux-x64"
displayName: Run unit tests
condition: and(succeeded(), eq(variables['VSCODE_STEP_ON_IT'], 'false'))

- script: |
set -e
DISPLAY=:10 ./scripts/test-integration.sh --build --tfs "Integration Tests"
# yarn smoketest -- --build "$(agent.builddirectory)/VSCode-linux-x64"
displayName: Run integration tests
condition: and(succeeded(), eq(variables['VSCODE_STEP_ON_IT'], 'false'))

- script: |
set -e
AZURE_DOCUMENTDB_MASTERKEY="$(builds-docdb-key-readwrite)" \
Expand Down
229 changes: 229 additions & 0 deletions extensions/vscode-api-tests/src/memfs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/


import * as path from 'path';
import * as vscode from 'vscode';

class File implements vscode.FileStat {

type: vscode.FileType;
ctime: number;
mtime: number;
size: number;

name: string;
data?: Uint8Array;

constructor(name: string) {
this.type = vscode.FileType.File;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
}
}

class Directory implements vscode.FileStat {

type: vscode.FileType;
ctime: number;
mtime: number;
size: number;

name: string;
entries: Map<string, File | Directory>;

constructor(name: string) {
this.type = vscode.FileType.Directory;
this.ctime = Date.now();
this.mtime = Date.now();
this.size = 0;
this.name = name;
this.entries = new Map();
}
}

export type Entry = File | Directory;

export class MemFS implements vscode.FileSystemProvider {

readonly scheme = 'fake-fs';

readonly root = new Directory('');

// --- manage file metadata

stat(uri: vscode.Uri): vscode.FileStat {
return this._lookup(uri, false);
}

readDirectory(uri: vscode.Uri): [string, vscode.FileType][] {
const entry = this._lookupAsDirectory(uri, false);
let result: [string, vscode.FileType][] = [];
for (const [name, child] of entry.entries) {
result.push([name, child.type]);
}
return result;
}

// --- manage file contents

readFile(uri: vscode.Uri): Uint8Array {
const data = this._lookupAsFile(uri, false).data;
if (data) {
return data;
}
throw vscode.FileSystemError.FileNotFound();
}

writeFile(uri: vscode.Uri, content: Uint8Array, options: { create: boolean, overwrite: boolean }): void {
let basename = path.posix.basename(uri.path);
let parent = this._lookupParentDirectory(uri);
let entry = parent.entries.get(basename);
if (entry instanceof Directory) {
throw vscode.FileSystemError.FileIsADirectory(uri);
}
if (!entry && !options.create) {
throw vscode.FileSystemError.FileNotFound(uri);
}
if (entry && options.create && !options.overwrite) {
throw vscode.FileSystemError.FileExists(uri);
}
if (!entry) {
entry = new File(basename);
parent.entries.set(basename, entry);
this._fireSoon({ type: vscode.FileChangeType.Created, uri });
}
entry.mtime = Date.now();
entry.size = content.byteLength;
entry.data = content;

this._fireSoon({ type: vscode.FileChangeType.Changed, uri });
}

// --- manage files/folders

rename(oldUri: vscode.Uri, newUri: vscode.Uri, options: { overwrite: boolean }): void {

if (!options.overwrite && this._lookup(newUri, true)) {
throw vscode.FileSystemError.FileExists(newUri);
}

let entry = this._lookup(oldUri, false);
let oldParent = this._lookupParentDirectory(oldUri);

let newParent = this._lookupParentDirectory(newUri);
let newName = path.posix.basename(newUri.path);

oldParent.entries.delete(entry.name);
entry.name = newName;
newParent.entries.set(newName, entry);

this._fireSoon(
{ type: vscode.FileChangeType.Deleted, uri: oldUri },
{ type: vscode.FileChangeType.Created, uri: newUri }
);
}

delete(uri: vscode.Uri): void {
let dirname = uri.with({ path: path.posix.dirname(uri.path) });
let basename = path.posix.basename(uri.path);
let parent = this._lookupAsDirectory(dirname, false);
if (!parent.entries.has(basename)) {
throw vscode.FileSystemError.FileNotFound(uri);
}
parent.entries.delete(basename);
parent.mtime = Date.now();
parent.size -= 1;
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: dirname }, { uri, type: vscode.FileChangeType.Deleted });
}

createDirectory(uri: vscode.Uri): void {
let basename = path.posix.basename(uri.path);
let dirname = uri.with({ path: path.posix.dirname(uri.path) });
let parent = this._lookupAsDirectory(dirname, false);

let entry = new Directory(basename);
parent.entries.set(entry.name, entry);
parent.mtime = Date.now();
parent.size += 1;
this._fireSoon({ type: vscode.FileChangeType.Changed, uri: dirname }, { type: vscode.FileChangeType.Created, uri });
}

// --- lookup

private _lookup(uri: vscode.Uri, silent: false): Entry;
private _lookup(uri: vscode.Uri, silent: boolean): Entry | undefined;
private _lookup(uri: vscode.Uri, silent: boolean): Entry | undefined {
let parts = uri.path.split('/');
let entry: Entry = this.root;
for (const part of parts) {
if (!part) {
continue;
}
let child: Entry | undefined;
if (entry instanceof Directory) {
child = entry.entries.get(part);
}
if (!child) {
if (!silent) {
throw vscode.FileSystemError.FileNotFound(uri);
} else {
return undefined;
}
}
entry = child;
}
return entry;
}

private _lookupAsDirectory(uri: vscode.Uri, silent: boolean): Directory {
let entry = this._lookup(uri, silent);
if (entry instanceof Directory) {
return entry;
}
throw vscode.FileSystemError.FileNotADirectory(uri);
}

private _lookupAsFile(uri: vscode.Uri, silent: boolean): File {
let entry = this._lookup(uri, silent);
if (entry instanceof File) {
return entry;
}
throw vscode.FileSystemError.FileIsADirectory(uri);
}

private _lookupParentDirectory(uri: vscode.Uri): Directory {
const dirname = uri.with({ path: path.posix.dirname(uri.path) });
return this._lookupAsDirectory(dirname, false);
}

// --- manage file events

private _emitter = new vscode.EventEmitter<vscode.FileChangeEvent[]>();
private _bufferedEvents: vscode.FileChangeEvent[] = [];
private _fireSoonHandle?: NodeJS.Timer;

readonly onDidChangeFile: vscode.Event<vscode.FileChangeEvent[]> = this._emitter.event;

watch(_resource: vscode.Uri): vscode.Disposable {
// ignore, fires for all changes...
return new vscode.Disposable(() => { });
}

private _fireSoon(...events: vscode.FileChangeEvent[]): void {
this._bufferedEvents.push(...events);

if (this._fireSoonHandle) {
clearTimeout(this._fireSoonHandle);
}

this._fireSoonHandle = setTimeout(() => {
this._emitter.fire(this._bufferedEvents);
this._bufferedEvents.length = 0;
}, 5);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import * as assert from 'assert';
import { join } from 'path';
import * as vscode from 'vscode';
import { createRandomFile } from '../utils';
import { createRandomFile, testFs } from '../utils';

suite('languages namespace tests', () => {

Expand Down Expand Up @@ -103,7 +103,7 @@ suite('languages namespace tests', () => {
return [new vscode.DocumentLink(range, target)];
}
};
vscode.languages.registerDocumentLinkProvider({ language: 'java', scheme: 'file' }, linkProvider);
vscode.languages.registerDocumentLinkProvider({ language: 'java', scheme: testFs.scheme }, linkProvider);

const links = await vscode.commands.executeCommand<vscode.DocumentLink[]>('vscode.executeLinkProvider', doc.uri);
assert.equal(2, links && links.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

import * as assert from 'assert';
import * as vscode from 'vscode';
import { createRandomFile, deleteFile, closeAllEditors, pathEquals, rndName, disposeAll } from '../utils';
import { createRandomFile, deleteFile, closeAllEditors, pathEquals, rndName, disposeAll, testFs } from '../utils';
import { join, posix, basename } from 'path';
import * as fs from 'fs';
import * as os from 'os';

suite('workspace-namespace', () => {

Expand Down Expand Up @@ -131,8 +130,7 @@ suite('workspace-namespace', () => {
assert.ok(fs.existsSync(path));

d0.dispose();

return deleteFile(vscode.Uri.file(join(vscode.workspace.rootPath || '', './newfile.txt')));
fs.unlinkSync(join(vscode.workspace.rootPath || '', './newfile.txt'));
});
});

Expand Down Expand Up @@ -702,10 +700,8 @@ suite('workspace-namespace', () => {

test('WorkspaceEdit: edit and rename parent folder duplicates resource #42641', async function () {

let dir = join(os.tmpdir(), 'before-' + rndName());
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
let dir = vscode.Uri.parse(`${testFs.scheme}:/before-${rndName()}`);
await testFs.createDirectory(dir);

let docUri = await createRandomFile('', dir);
let docParent = docUri.with({ path: posix.dirname(docUri.path) });
Expand Down