Skip to content

Commit 1425d04

Browse files
author
Benjamin Pasero
committed
storage - add test for migration
1 parent b5aa0f0 commit 1425d04

4 files changed

Lines changed: 125 additions & 68 deletions

File tree

src/vs/platform/storage/node/storageService.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ import { Action } from 'vs/base/common/actions';
1515
import { IWindowService } from 'vs/platform/windows/common/windows';
1616
import { localize } from 'vs/nls';
1717
import { mark, getDuration } from 'vs/base/common/performance';
18-
import { IWorkspaceIdentifier } from 'vs/platform/workspaces/common/workspaces';
1918
import { join, basename } from 'path';
20-
import { mkdirp, copy } from 'vs/base/node/pfs';
19+
import { copy } from 'vs/base/node/pfs';
2120

2221
export class StorageService extends Disposable implements IStorageService {
2322
_serviceBrand: any;
@@ -59,7 +58,7 @@ export class StorageService extends Disposable implements IStorageService {
5958
constructor(
6059
workspaceStoragePath: string,
6160
@ILogService logService: ILogService,
62-
@IEnvironmentService private environmentService: IEnvironmentService
61+
@IEnvironmentService environmentService: IEnvironmentService
6362
) {
6463
super();
6564

@@ -197,26 +196,23 @@ export class StorageService extends Disposable implements IStorageService {
197196
});
198197
}
199198

200-
migrate(toWorkspace: IWorkspaceIdentifier): Promise<void> {
199+
migrate(toWorkspaceStorageFolder: string): Promise<void> {
201200
if (this.workspaceStoragePath === StorageService.IN_MEMORY_PATH) {
202201
return Promise.resolve(); // no migration needed if running in memory
203202
}
204203

205204
// Compute new workspace storage path based on workspace identifier
206-
const newWorkspaceStorageHome = join(this.environmentService.workspaceStorageHome, toWorkspace.id);
207-
const newWorkspaceStoragePath = join(newWorkspaceStorageHome, basename(this.workspaceStoragePath));
205+
const newWorkspaceStoragePath = join(toWorkspaceStorageFolder, basename(this.workspaceStoragePath));
208206
if (this.workspaceStoragePath === newWorkspaceStoragePath) {
209207
return Promise.resolve(); // guard against migrating to same path
210208
}
211209

212210
// Close workspace DB to be able to copy
213211
return this.workspaceStorage.close().then(() => {
214-
return mkdirp(newWorkspaceStorageHome).then(() => {
215-
return copy(this.workspaceStoragePath, newWorkspaceStoragePath).then(() => {
216-
this.createWorkspaceStorage(newWorkspaceStoragePath);
212+
return copy(this.workspaceStoragePath, newWorkspaceStoragePath).then(() => {
213+
this.createWorkspaceStorage(newWorkspaceStoragePath);
217214

218-
return this.workspaceStorage.init();
219-
});
215+
return this.workspaceStorage.init();
220216
});
221217
});
222218
}

src/vs/platform/storage/test/common/storageService.test.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import { strictEqual, ok, equal } from 'assert';
7+
import { StorageScope } from 'vs/platform/storage/common/storage';
8+
import { TestStorageService, TestEnvironmentService } from 'vs/workbench/test/workbenchTestServices';
9+
import { StorageService } from 'vs/platform/storage/node/storageService';
10+
import { generateUuid } from 'vs/base/common/uuid';
11+
import { join } from 'path';
12+
import { tmpdir } from 'os';
13+
import { mkdirp, del } from 'vs/base/node/pfs';
14+
import { NullLogService } from 'vs/platform/log/common/log';
15+
16+
suite('StorageService', () => {
17+
18+
test('Remove Data (global, in-memory)', () => {
19+
removeData(StorageScope.GLOBAL);
20+
});
21+
22+
test('Remove Data (workspace, in-memory)', () => {
23+
removeData(StorageScope.WORKSPACE);
24+
});
25+
26+
function removeData(scope: StorageScope): void {
27+
const storage = new TestStorageService();
28+
29+
storage.store('Monaco.IDE.Core.Storage.Test.remove', 'foobar', scope);
30+
strictEqual('foobar', storage.get('Monaco.IDE.Core.Storage.Test.remove', scope));
31+
32+
storage.remove('Monaco.IDE.Core.Storage.Test.remove', scope);
33+
ok(!storage.get('Monaco.IDE.Core.Storage.Test.remove', scope));
34+
}
35+
36+
test('Get Data, Integer, Boolean (global, in-memory)', () => {
37+
storeData(StorageScope.GLOBAL);
38+
});
39+
40+
test('Get Data, Integer, Boolean (workspace, in-memory)', () => {
41+
storeData(StorageScope.WORKSPACE);
42+
});
43+
44+
function storeData(scope: StorageScope): void {
45+
const storage = new TestStorageService();
46+
47+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.get', scope, 'foobar'), 'foobar');
48+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.get', scope, ''), '');
49+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.getInteger', scope, 5), 5);
50+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.getInteger', scope, 0), 0);
51+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.getBoolean', scope, true), true);
52+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.getBoolean', scope, false), false);
53+
54+
storage.store('Monaco.IDE.Core.Storage.Test.get', 'foobar', scope);
55+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.get', scope), 'foobar');
56+
57+
storage.store('Monaco.IDE.Core.Storage.Test.get', '', scope);
58+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.get', scope), '');
59+
60+
storage.store('Monaco.IDE.Core.Storage.Test.getInteger', 5, scope);
61+
strictEqual(storage.getInteger('Monaco.IDE.Core.Storage.Test.getInteger', scope), 5);
62+
63+
storage.store('Monaco.IDE.Core.Storage.Test.getInteger', 0, scope);
64+
strictEqual(storage.getInteger('Monaco.IDE.Core.Storage.Test.getInteger', scope), 0);
65+
66+
storage.store('Monaco.IDE.Core.Storage.Test.getBoolean', true, scope);
67+
strictEqual(storage.getBoolean('Monaco.IDE.Core.Storage.Test.getBoolean', scope), true);
68+
69+
storage.store('Monaco.IDE.Core.Storage.Test.getBoolean', false, scope);
70+
strictEqual(storage.getBoolean('Monaco.IDE.Core.Storage.Test.getBoolean', scope), false);
71+
72+
strictEqual(storage.get('Monaco.IDE.Core.Storage.Test.getDefault', scope, 'getDefault'), 'getDefault');
73+
strictEqual(storage.getInteger('Monaco.IDE.Core.Storage.Test.getIntegerDefault', scope, 5), 5);
74+
strictEqual(storage.getBoolean('Monaco.IDE.Core.Storage.Test.getBooleanDefault', scope, true), true);
75+
}
76+
77+
function uniqueStorageDir(): string {
78+
const id = generateUuid();
79+
80+
return join(tmpdir(), 'vsctests', id, 'storage2', id);
81+
}
82+
83+
test('Migrate Data', async () => {
84+
const storageDir = uniqueStorageDir();
85+
await mkdirp(storageDir);
86+
87+
const storage = new StorageService(join(storageDir, 'storage.db'), new NullLogService(), TestEnvironmentService);
88+
await storage.init();
89+
90+
storage.store('bar', 'foo', StorageScope.WORKSPACE);
91+
storage.store('barNumber', 55, StorageScope.WORKSPACE);
92+
storage.store('barBoolean', true, StorageScope.GLOBAL);
93+
94+
const newStorageDir = uniqueStorageDir();
95+
await mkdirp(newStorageDir);
96+
97+
await storage.migrate(newStorageDir);
98+
99+
equal(storage.get('bar', StorageScope.WORKSPACE), 'foo');
100+
equal(storage.getInteger('barNumber', StorageScope.WORKSPACE), 55);
101+
equal(storage.getBoolean('barBoolean', StorageScope.GLOBAL), true);
102+
103+
await storage.close();
104+
await del(newStorageDir, tmpdir());
105+
await del(storageDir, tmpdir());
106+
});
107+
});

src/vs/workbench/services/workspace/node/workspaceEditingService.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ import { distinct } from 'vs/base/common/arrays';
2525
import { isLinux } from 'vs/base/common/platform';
2626
import { isEqual } from 'vs/base/common/resources';
2727
import { INotificationService, Severity } from 'vs/platform/notification/common/notification';
28+
import { join } from 'path';
29+
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
30+
import { mkdirp } from 'vs/base/node/pfs';
2831

2932
export class WorkspaceEditingService implements IWorkspaceEditingService {
3033

@@ -39,7 +42,8 @@ export class WorkspaceEditingService implements IWorkspaceEditingService {
3942
@IExtensionService private extensionService: IExtensionService,
4043
@IBackupFileService private backupFileService: IBackupFileService,
4144
@INotificationService private notificationService: INotificationService,
42-
@ICommandService private commandService: ICommandService
45+
@ICommandService private commandService: ICommandService,
46+
@IEnvironmentService private environmentService: IEnvironmentService
4347
) {
4448
}
4549

@@ -237,9 +241,13 @@ export class WorkspaceEditingService implements IWorkspaceEditingService {
237241
}
238242

239243
private migrateStorage(toWorkspace: IWorkspaceIdentifier): TPromise<void> {
240-
const storageImpl = this.storageService as DelegatingStorageService;
244+
const newWorkspaceStorageHome = join(this.environmentService.workspaceStorageHome, toWorkspace.id);
241245

242-
return storageImpl.storage.migrate(toWorkspace);
246+
return mkdirp(newWorkspaceStorageHome).then(() => {
247+
const storageImpl = this.storageService as DelegatingStorageService;
248+
249+
return storageImpl.storage.migrate(newWorkspaceStorageHome);
250+
});
243251
}
244252

245253
private migrateWorkspaceSettings(toWorkspace: IWorkspaceIdentifier): TPromise<void> {

0 commit comments

Comments
 (0)