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

tests - use a common toResource() method #68959

Merged
merged 1 commit into from Feb 19, 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
7 changes: 6 additions & 1 deletion src/vs/base/test/common/utils.ts
Expand Up @@ -6,6 +6,7 @@
import { join } from 'vs/base/common/path';
import { URI } from 'vs/base/common/uri';
import { canceled } from 'vs/base/common/errors';
import { isWindows } from 'vs/base/common/platform';

export type ValueCallback<T = any> = (value: T | Promise<T>) => void;

Expand Down Expand Up @@ -49,7 +50,11 @@ export class DeferredPromise<T> {
}

export function toResource(this: any, path: string) {
return URI.file(join('C:\\', Buffer.from(this.test.fullTitle()).toString('base64'), path));
if (isWindows) {
return URI.file(join('C:\\', Buffer.from(this.test.fullTitle()).toString('base64'), path));
}

return URI.file(join('/', Buffer.from(this.test.fullTitle()).toString('base64'), path));
}

export function suiteRepeat(n: number, description: string, callback: (this: any) => void): void {
Expand Down
34 changes: 15 additions & 19 deletions src/vs/platform/files/test/files.test.ts
Expand Up @@ -6,36 +6,32 @@
import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { isEqual, isEqualOrParent } from 'vs/base/common/extpath';
import { join } from 'vs/base/common/path';
import { FileChangeType, FileChangesEvent, isParent } from 'vs/platform/files/common/files';
import { isLinux, isMacintosh, isWindows } from 'vs/base/common/platform';
import { toResource } from 'vs/base/test/common/utils';

suite('Files', () => {

function toResource(path) {
return URI.file(join('C:\\', path));
}

test('FileChangesEvent', () => {
test('FileChangesEvent', function () {
let changes = [
{ resource: toResource('/foo/updated.txt'), type: FileChangeType.UPDATED },
{ resource: toResource('/foo/otherupdated.txt'), type: FileChangeType.UPDATED },
{ resource: toResource('/added.txt'), type: FileChangeType.ADDED },
{ resource: toResource('/bar/deleted.txt'), type: FileChangeType.DELETED },
{ resource: toResource('/bar/folder'), type: FileChangeType.DELETED }
{ resource: toResource.call(this, '/foo/updated.txt'), type: FileChangeType.UPDATED },
{ resource: toResource.call(this, '/foo/otherupdated.txt'), type: FileChangeType.UPDATED },
{ resource: toResource.call(this, '/added.txt'), type: FileChangeType.ADDED },
{ resource: toResource.call(this, '/bar/deleted.txt'), type: FileChangeType.DELETED },
{ resource: toResource.call(this, '/bar/folder'), type: FileChangeType.DELETED }
];

let r1 = new FileChangesEvent(changes);

assert(!r1.contains(toResource('/foo'), FileChangeType.UPDATED));
assert(r1.contains(toResource('/foo/updated.txt'), FileChangeType.UPDATED));
assert(!r1.contains(toResource('/foo/updated.txt'), FileChangeType.ADDED));
assert(!r1.contains(toResource('/foo/updated.txt'), FileChangeType.DELETED));
assert(!r1.contains(toResource.call(this, '/foo'), FileChangeType.UPDATED));
assert(r1.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.UPDATED));
assert(!r1.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.ADDED));
assert(!r1.contains(toResource.call(this, '/foo/updated.txt'), FileChangeType.DELETED));

assert(r1.contains(toResource('/bar/folder'), FileChangeType.DELETED));
assert(r1.contains(toResource('/bar/folder/somefile'), FileChangeType.DELETED));
assert(r1.contains(toResource('/bar/folder/somefile/test.txt'), FileChangeType.DELETED));
assert(!r1.contains(toResource('/bar/folder2/somefile'), FileChangeType.DELETED));
assert(r1.contains(toResource.call(this, '/bar/folder'), FileChangeType.DELETED));
assert(r1.contains(toResource.call(this, '/bar/folder/somefile'), FileChangeType.DELETED));
assert(r1.contains(toResource.call(this, '/bar/folder/somefile/test.txt'), FileChangeType.DELETED));
assert(!r1.contains(toResource.call(this, '/bar/folder2/somefile'), FileChangeType.DELETED));

assert.strictEqual(5, r1.changes.length);
assert.strictEqual(1, r1.getAdded().length);
Expand Down
38 changes: 17 additions & 21 deletions src/vs/workbench/contrib/files/test/browser/fileEditorInput.test.ts
Expand Up @@ -5,7 +5,7 @@

import * as assert from 'assert';
import { URI } from 'vs/base/common/uri';
import { join } from 'vs/base/common/path';
import { toResource } from 'vs/base/test/common/utils';
import { FileEditorInput } from 'vs/workbench/contrib/files/common/editors/fileEditorInput';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { workbenchInstantiationService, TestTextFileService } from 'vs/workbench/test/workbenchTestServices';
Expand All @@ -17,10 +17,6 @@ import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textF
import { IModelService } from 'vs/editor/common/services/modelService';
import { timeout } from 'vs/base/common/async';

function toResource(self, path) {
return URI.file(join('C:\\', Buffer.from(self.test.fullTitle()).toString('base64'), path));
}

class ServiceAccessor {
constructor(
@IEditorService public editorService: IEditorService,
Expand All @@ -41,9 +37,9 @@ suite('Files - FileEditorInput', () => {
});

test('Basics', function () {
let input = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/file.js'), undefined);
const otherInput = instantiationService.createInstance(FileEditorInput, toResource(this, 'foo/bar/otherfile.js'), undefined);
const otherInputSame = instantiationService.createInstance(FileEditorInput, toResource(this, 'foo/bar/file.js'), undefined);
let input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/file.js'), undefined);
const otherInput = instantiationService.createInstance(FileEditorInput, toResource.call(this, 'foo/bar/otherfile.js'), undefined);
const otherInputSame = instantiationService.createInstance(FileEditorInput, toResource.call(this, 'foo/bar/file.js'), undefined);

assert(input.matches(input));
assert(input.matches(otherInputSame));
Expand All @@ -55,13 +51,13 @@ suite('Files - FileEditorInput', () => {

assert.strictEqual('file.js', input.getName());

assert.strictEqual(toResource(this, '/foo/bar/file.js').fsPath, input.getResource().fsPath);
assert.strictEqual(toResource.call(this, '/foo/bar/file.js').fsPath, input.getResource().fsPath);
assert(input.getResource() instanceof URI);

input = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar.html'), undefined);
input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar.html'), undefined);

const inputToResolve: FileEditorInput = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/file.js'), undefined);
const sameOtherInput: FileEditorInput = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/file.js'), undefined);
const inputToResolve: FileEditorInput = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/file.js'), undefined);
const sameOtherInput: FileEditorInput = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/file.js'), undefined);

return inputToResolve.resolve().then(resolved => {
assert.ok(inputToResolve.isResolved());
Expand Down Expand Up @@ -100,10 +96,10 @@ suite('Files - FileEditorInput', () => {
});

test('matches', function () {
const input1 = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/updatefile.js'), undefined);
const input2 = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/updatefile.js'), undefined);
const input3 = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/other.js'), undefined);
const input2Upper = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/UPDATEFILE.js'), undefined);
const input1 = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
const input2 = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);
const input3 = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/other.js'), undefined);
const input2Upper = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/UPDATEFILE.js'), undefined);

assert.strictEqual(input1.matches(null), false);
assert.strictEqual(input1.matches(input1), true);
Expand All @@ -114,7 +110,7 @@ suite('Files - FileEditorInput', () => {
});

test('getEncoding/setEncoding', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/updatefile.js'), undefined);
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);

input.setEncoding('utf16', EncodingMode.Encode);
assert.equal(input.getEncoding(), 'utf16');
Expand All @@ -127,7 +123,7 @@ suite('Files - FileEditorInput', () => {
});

test('save', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/updatefile.js'), undefined);
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);

return input.resolve().then((resolved: TextFileEditorModel) => {
resolved.textEditorModel.setValue('changed');
Expand All @@ -142,7 +138,7 @@ suite('Files - FileEditorInput', () => {
});

test('revert', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/updatefile.js'), undefined);
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);

return input.resolve().then((resolved: TextFileEditorModel) => {
resolved.textEditorModel.setValue('changed');
Expand All @@ -157,7 +153,7 @@ suite('Files - FileEditorInput', () => {
});

test('resolve handles binary files', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/updatefile.js'), undefined);
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);

accessor.textFileService.setResolveTextContentErrorOnce(new FileOperationError('error', FileOperationResult.FILE_IS_BINARY));

Expand All @@ -169,7 +165,7 @@ suite('Files - FileEditorInput', () => {
});

test('resolve handles too large files', function () {
const input = instantiationService.createInstance(FileEditorInput, toResource(this, '/foo/bar/updatefile.js'), undefined);
const input = instantiationService.createInstance(FileEditorInput, toResource.call(this, '/foo/bar/updatefile.js'), undefined);

accessor.textFileService.setResolveTextContentErrorOnce(new FileOperationError('error', FileOperationResult.FILE_TOO_LARGE));

Expand Down
Expand Up @@ -5,8 +5,7 @@

import * as assert from 'assert';
import { FileEditorTracker } from 'vs/workbench/contrib/files/browser/editors/fileEditorTracker';
import { URI } from 'vs/base/common/uri';
import { join } from 'vs/base/common/path';
import { toResource } from 'vs/base/test/common/utils';
import { IEditorService } from 'vs/workbench/services/editor/common/editorService';
import { workbenchInstantiationService, TestTextFileService, TestFileService } from 'vs/workbench/test/workbenchTestServices';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
Expand All @@ -16,10 +15,6 @@ import { TextFileEditorModel } from 'vs/workbench/services/textfile/common/textF
import { IEditorGroupsService } from 'vs/workbench/services/editor/common/editorGroupsService';
import { timeout } from 'vs/base/common/async';

function toResource(self: any, path: string) {
return URI.file(join('C:\\', Buffer.from(self.test.fullTitle()).toString('base64'), path));
}

class ServiceAccessor {
constructor(
@IEditorService public editorService: IEditorService,
Expand All @@ -43,7 +38,7 @@ suite('Files - FileEditorTracker', () => {
test('file change event updates model', function () {
const tracker = instantiationService.createInstance(FileEditorTracker);

const resource = toResource(this, '/path/index.txt');
const resource = toResource.call(this, '/path/index.txt');

return accessor.textFileService.models.loadOrCreate(resource).then((model: TextFileEditorModel) => {
model.textEditorModel.setValue('Super Good');
Expand Down