From 4c0a4179e6e170a2f45526adfff434a5a7e00a07 Mon Sep 17 00:00:00 2001 From: Benjamin Pasero Date: Fri, 22 Jan 2021 11:55:06 +0100 Subject: [PATCH] workspace - more tests for identifiers --- .../electron-main/windowsMainService.ts | 39 +++++++------------ .../electron-main/workspacesMainService.ts | 10 +++++ .../workspacesMainService.test.ts | 22 ++++++++++- .../services/workspaces/browser/workspaces.ts | 8 ++++ .../test/browser/workspaces.test.ts | 19 +++++++++ 5 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/vs/workbench/services/workspaces/test/browser/workspaces.test.ts diff --git a/src/vs/platform/windows/electron-main/windowsMainService.ts b/src/vs/platform/windows/electron-main/windowsMainService.ts index 1cf1f849c5f..5b2006952c9 100644 --- a/src/vs/platform/windows/electron-main/windowsMainService.ts +++ b/src/vs/platform/windows/electron-main/windowsMainService.ts @@ -829,12 +829,18 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic return undefined; } - // handle local files in `parsePath` with some extra validation + // handle local openables with some extra validation let uri = this.resourceFromURIToOpen(toOpen); if (uri.scheme === Schemas.file) { return this.resolvePath(uri.fsPath, options, isFileToOpen(toOpen)); } + // handle remote openables + return this.resolveRemoteUri(uri, toOpen, options); + } + + private resolveRemoteUri(uri: URI, toOpen: IWindowOpenable, options: IPathParseOptions = {}): IPathToOpen | undefined { + // open remote if either specified in the cli or if it's a remotehost URI const remoteAuthority = options.remoteAuthority || getRemoteAuthority(uri); @@ -844,7 +850,7 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic // remove trailing slash uri = removeTrailingPathSeparator(uri); - // File + // Remote File if (isFileToOpen(toOpen)) { if (options.gotoLineMode) { const { path, line, column } = parseLineAndColumnAware(uri.path); @@ -857,25 +863,16 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic }; } - return { - fileUri: uri, - remoteAuthority - }; + return { fileUri: uri, remoteAuthority }; } - // Workspace + // Remote Workspace else if (isWorkspaceToOpen(toOpen)) { - return { - workspace: getWorkspaceIdentifier(uri), - remoteAuthority - }; + return { workspace: getWorkspaceIdentifier(uri), remoteAuthority }; } - // Folder - return { - workspace: getSingleFolderWorkspaceIdentifier(uri), - remoteAuthority - }; + // Remote Folder + return { workspace: getSingleFolderWorkspaceIdentifier(uri), remoteAuthority }; } private resourceFromURIToOpen(openable: IWindowOpenable): URI { @@ -984,10 +981,7 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic // are neither file nor folder but some external tools might pass them // over to us) else if (pathStat.isDirectory()) { - return { - workspace: getSingleFolderWorkspaceIdentifier(URI.file(path), pathStat), - exists: true - }; + return { workspace: getSingleFolderWorkspaceIdentifier(URI.file(path), pathStat), exists: true }; } } catch (error) { const fileUri = URI.file(path); @@ -997,10 +991,7 @@ export class WindowsMainService extends Disposable implements IWindowsMainServic // assume this is a file that does not yet exist if (options?.ignoreFileNotFound) { - return { - fileUri, - exists: false - }; + return { fileUri, exists: false }; } } diff --git a/src/vs/platform/workspaces/electron-main/workspacesMainService.ts b/src/vs/platform/workspaces/electron-main/workspacesMainService.ts index 9c3870c6aa0..52a2520259d 100644 --- a/src/vs/platform/workspaces/electron-main/workspacesMainService.ts +++ b/src/vs/platform/workspaces/electron-main/workspacesMainService.ts @@ -320,6 +320,10 @@ export class WorkspacesMainService extends Disposable implements IWorkspacesMain } } +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +// NOTE: DO NOT CHANGE. IDENTIFIERS HAVE TO REMAIN STABLE +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + export function getWorkspaceIdentifier(configPath: URI): IWorkspaceIdentifier { function getWorkspaceId(): string { @@ -337,6 +341,12 @@ export function getWorkspaceIdentifier(configPath: URI): IWorkspaceIdentifier { }; } +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +// NOTE: DO NOT CHANGE. IDENTIFIERS HAVE TO REMAIN STABLE +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +export function getSingleFolderWorkspaceIdentifier(folderUri: URI): ISingleFolderWorkspaceIdentifier | undefined; +export function getSingleFolderWorkspaceIdentifier(folderUri: URI, folderStat: Stats): ISingleFolderWorkspaceIdentifier; export function getSingleFolderWorkspaceIdentifier(folderUri: URI, folderStat?: Stats): ISingleFolderWorkspaceIdentifier | undefined { function getFolderId(): string | undefined { diff --git a/src/vs/platform/workspaces/test/electron-main/workspacesMainService.test.ts b/src/vs/platform/workspaces/test/electron-main/workspacesMainService.test.ts index 413a7dc20ad..50732338482 100644 --- a/src/vs/platform/workspaces/test/electron-main/workspacesMainService.test.ts +++ b/src/vs/platform/workspaces/test/electron-main/workspacesMainService.test.ts @@ -10,7 +10,7 @@ import * as path from 'vs/base/common/path'; import * as pfs from 'vs/base/node/pfs'; import { EnvironmentMainService } from 'vs/platform/environment/electron-main/environmentMainService'; import { parseArgs, OPTIONS } from 'vs/platform/environment/node/argv'; -import { WorkspacesMainService, IStoredWorkspace, getSingleFolderWorkspaceIdentifier } from 'vs/platform/workspaces/electron-main/workspacesMainService'; +import { WorkspacesMainService, IStoredWorkspace, getSingleFolderWorkspaceIdentifier, getWorkspaceIdentifier } from 'vs/platform/workspaces/electron-main/workspacesMainService'; import { WORKSPACE_EXTENSION, IRawFileWorkspaceFolder, IWorkspaceFolderCreationData, IRawUriWorkspaceFolder, rewriteWorkspaceFileForNewLocation, IWorkspaceIdentifier, IStoredWorkspaceFolder } from 'vs/platform/workspaces/common/workspaces'; import { NullLogService } from 'vs/platform/log/common/log'; import { URI } from 'vs/base/common/uri'; @@ -464,4 +464,24 @@ suite('WorkspacesMainService', () => { const localExistingUriId = getSingleFolderWorkspaceIdentifier(localExistingUri); assert.ok(localExistingUriId?.id); }); + + test('workspace identifiers are stable', function () { + + // workspace identifier (local) + assert.strictEqual(getWorkspaceIdentifier(URI.file('/hello/test')).id, 'e36736311be12ff6d695feefe415b3e8'); + + // single folder identifier (local) + const fakeStat = { + ino: 1611312115129, + birthtimeMs: 1611312115129, + birthtime: new Date(1611312115129) + }; + assert.strictEqual(getSingleFolderWorkspaceIdentifier(URI.file('/hello/test'), fakeStat as fs.Stats)?.id, '1d726b3d516dc2a6d343abf4797eaaef'); + + // workspace identifier (remote) + assert.strictEqual(getWorkspaceIdentifier(URI.parse('vscode-remote:/hello/test')).id, '786de4f224d57691f218dc7f31ee2ee3'); + + // single folder identifier (remote) + assert.strictEqual(getSingleFolderWorkspaceIdentifier(URI.parse('vscode-remote:/hello/test'))?.id, '786de4f224d57691f218dc7f31ee2ee3'); + }); }); diff --git a/src/vs/workbench/services/workspaces/browser/workspaces.ts b/src/vs/workbench/services/workspaces/browser/workspaces.ts index 830b3db2b5b..3b90080dc3d 100644 --- a/src/vs/workbench/services/workspaces/browser/workspaces.ts +++ b/src/vs/workbench/services/workspaces/browser/workspaces.ts @@ -7,6 +7,10 @@ import { ISingleFolderWorkspaceIdentifier, IWorkspaceIdentifier } from 'vs/platf import { URI } from 'vs/base/common/uri'; import { hash } from 'vs/base/common/hash'; +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +// NOTE: DO NOT CHANGE. IDENTIFIERS HAVE TO REMAIN STABLE +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + export function getWorkspaceIdentifier(workspacePath: URI): IWorkspaceIdentifier { return { id: getWorkspaceId(workspacePath), @@ -14,6 +18,10 @@ export function getWorkspaceIdentifier(workspacePath: URI): IWorkspaceIdentifier }; } +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +// NOTE: DO NOT CHANGE. IDENTIFIERS HAVE TO REMAIN STABLE +// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + export function getSingleFolderWorkspaceIdentifier(folderPath: URI): ISingleFolderWorkspaceIdentifier { return { id: getWorkspaceId(folderPath), diff --git a/src/vs/workbench/services/workspaces/test/browser/workspaces.test.ts b/src/vs/workbench/services/workspaces/test/browser/workspaces.test.ts new file mode 100644 index 00000000000..fdfcf9e336f --- /dev/null +++ b/src/vs/workbench/services/workspaces/test/browser/workspaces.test.ts @@ -0,0 +1,19 @@ +/*--------------------------------------------------------------------------------------------- + * 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 { URI } from 'vs/base/common/uri'; +import { getWorkspaceIdentifier, getSingleFolderWorkspaceIdentifier } from 'vs/workbench/services/workspaces/browser/workspaces'; + +suite('Workspaces', () => { + test('workspace identifiers are stable', function () { + + // workspace identifier + assert.strictEqual(getWorkspaceIdentifier(URI.parse('vscode-remote:/hello/test')).id, '474434e4'); + + // single folder identifier + assert.strictEqual(getSingleFolderWorkspaceIdentifier(URI.parse('vscode-remote:/hello/test'))?.id, '474434e4'); + }); +});