From a0d33217172f625879717963bef557bc542b244a Mon Sep 17 00:00:00 2001 From: Johannes Rieken Date: Fri, 12 Aug 2016 10:36:21 +0200 Subject: [PATCH] use path primitives when compute workspace relative path, fixes #10446 --- src/vs/base/test/common/paths.test.ts | 1 + src/vs/workbench/api/node/extHostWorkspace.ts | 8 +++---- .../test/node/api/extHostWorkspace.test.ts | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/vs/workbench/test/node/api/extHostWorkspace.test.ts diff --git a/src/vs/base/test/common/paths.test.ts b/src/vs/base/test/common/paths.test.ts index a8a5aa49b49..0816f532485 100644 --- a/src/vs/base/test/common/paths.test.ts +++ b/src/vs/base/test/common/paths.test.ts @@ -13,6 +13,7 @@ suite('Paths', () => { assert.equal(paths.relative('/test/api/files/test', '/test/api/files/lib/foo'), '../lib/foo'); assert.equal(paths.relative('far/boo', 'boo/far'), '../../boo/far'); assert.equal(paths.relative('far/boo', 'far/boo'), ''); + assert.equal(paths.relative('far/boo', 'far/boo/bar/foo'), 'bar/foo'); }); test('dirname', () => { diff --git a/src/vs/workbench/api/node/extHostWorkspace.ts b/src/vs/workbench/api/node/extHostWorkspace.ts index d2aba3b575b..11e5fdeec1b 100644 --- a/src/vs/workbench/api/node/extHostWorkspace.ts +++ b/src/vs/workbench/api/node/extHostWorkspace.ts @@ -5,6 +5,7 @@ 'use strict'; import URI from 'vs/base/common/uri'; +import {relative, isEqualOrParent} from 'vs/base/common/paths'; import {IThreadService} from 'vs/workbench/services/thread/common/threadService'; import {IResourceEdit} from 'vs/editor/common/services/bulkEdit'; import {TPromise} from 'vs/base/common/winjs.base'; @@ -28,7 +29,7 @@ export class ExtHostWorkspace { return this._workspacePath; } - getRelativePath(pathOrUri: string|Uri): string { + getRelativePath(pathOrUri: string | Uri): string { let path: string; if (typeof pathOrUri === 'string') { @@ -37,9 +38,8 @@ export class ExtHostWorkspace { path = pathOrUri.fsPath; } - if (this._workspacePath && this._workspacePath.length < path.length) { - // return relative(workspacePath, path); - return path.substring(this._workspacePath.length); + if (isEqualOrParent(path, this._workspacePath)) { + return relative(this._workspacePath, path); } return path; diff --git a/src/vs/workbench/test/node/api/extHostWorkspace.test.ts b/src/vs/workbench/test/node/api/extHostWorkspace.test.ts new file mode 100644 index 00000000000..d9aece2b3a4 --- /dev/null +++ b/src/vs/workbench/test/node/api/extHostWorkspace.test.ts @@ -0,0 +1,23 @@ +/*--------------------------------------------------------------------------------------------- + * 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 {ExtHostWorkspace} from 'vs/workbench/api/node/extHostWorkspace'; +import {TestThreadService} from './testThreadService'; + +suite('ExtHostWorkspace', function () { + + test('asRelativePath', function () { + + const ws = new ExtHostWorkspace(new TestThreadService(), 'm:/Coding/Applications/NewsWoWBot'); + + assert.equal(ws.getRelativePath('m:/Coding/Applications/NewsWoWBot/bernd/das/brot'), 'bernd/das/brot'); + assert.equal(ws.getRelativePath('m:/Apps/DartPubCache/hosted/pub.dartlang.org/convert-2.0.1/lib/src/hex.dart'), + 'm:/Apps/DartPubCache/hosted/pub.dartlang.org/convert-2.0.1/lib/src/hex.dart'); + + }); +}); -- GitLab