add settings sync tests

This commit is contained in:
Sandeep Somavarapu
2020-03-04 19:10:07 +01:00
parent efbb8ae4b4
commit edd94ce0c4
4 changed files with 366 additions and 8 deletions
@@ -23,7 +23,7 @@ import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { URI } from 'vs/base/common/uri';
import { IExtensionManagementService } from 'vs/platform/extensionManagement/common/extensionManagement';
interface ISettingsSyncContent {
export interface ISettingsSyncContent {
settings: string;
}
@@ -302,13 +302,8 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
return this.synchronisers.filter(s => s.status === SyncStatus.HasConflicts).map(s => s.source);
}
private getSynchroniser(source: SyncSource): IUserDataSynchroniser {
switch (source) {
case SyncSource.Settings: return this.settingsSynchroniser;
case SyncSource.Keybindings: return this.keybindingsSynchroniser;
case SyncSource.Extensions: return this.extensionsSynchroniser;
case SyncSource.GlobalState: return this.globalStateSynchroniser;
}
getSynchroniser(source: SyncSource): IUserDataSynchroniser {
return this.synchronisers.filter(s => s.source === source)[0];
}
private async checkEnablement(): Promise<void> {
@@ -0,0 +1,355 @@
/*---------------------------------------------------------------------------------------------
* 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 { IUserDataSyncStoreService, IUserDataSyncService, SyncSource, UserDataSyncError, UserDataSyncErrorCode } from 'vs/platform/userDataSync/common/userDataSync';
import { UserDataSyncClient, UserDataSyncTestServer } from 'vs/platform/userDataSync/test/common/userDataSyncClient';
import { DisposableStore, toDisposable } from 'vs/base/common/lifecycle';
import { SettingsSynchroniser, ISettingsSyncContent } from 'vs/platform/userDataSync/common/settingsSync';
import { UserDataSyncService } from 'vs/platform/userDataSync/common/userDataSyncService';
import { IFileService } from 'vs/platform/files/common/files';
import { IEnvironmentService } from 'vs/platform/environment/common/environment';
import { VSBuffer } from 'vs/base/common/buffer';
import { ISyncData } from 'vs/platform/userDataSync/common/abstractSynchronizer';
import { Registry } from 'vs/platform/registry/common/platform';
import { IConfigurationRegistry, Extensions, ConfigurationScope } from 'vs/platform/configuration/common/configurationRegistry';
suite('SettingsSync', () => {
const disposableStore = new DisposableStore();
const server = new UserDataSyncTestServer();
let client: UserDataSyncClient;
let testObject: SettingsSynchroniser;
suiteSetup(() => {
Registry.as<IConfigurationRegistry>(Extensions.Configuration).registerConfiguration({
'id': 'settingsSync',
'type': 'object',
'properties': {
'settingsSync.machine': {
'type': 'string',
'scope': ConfigurationScope.MACHINE
},
'settingsSync.machineOverridable': {
'type': 'string',
'scope': ConfigurationScope.MACHINE_OVERRIDABLE
}
}
});
});
setup(async () => {
client = disposableStore.add(new UserDataSyncClient(server));
await client.setUp();
testObject = (client.instantiationService.get(IUserDataSyncService) as UserDataSyncService).getSynchroniser(SyncSource.Settings) as SettingsSynchroniser;
disposableStore.add(toDisposable(() => client.instantiationService.get(IUserDataSyncStoreService).clear()));
});
teardown(() => disposableStore.clear());
test('sync for first time to the server', async () => {
const expected =
`{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Workbench
"workbench.colorTheme": "GitHub Sharp",
"workbench.tree.indent": 20,
"workbench.colorCustomizations": {
"editorLineNumber.activeForeground": "#ff0000",
"[GitHub Sharp]": {
"statusBarItem.remoteBackground": "#24292E",
"editorPane.background": "#f3f1f11a"
}
},
"gitBranch.base": "remote-repo/master",
// Experimental
"workbench.view.experimental.allowMovingToNewContainer": true,
}`;
await updateSettings(expected);
await testObject.sync();
const { content } = await client.read(testObject.resourceKey);
assert.ok(content !== null);
const actual = parseSettings(content!);
assert.deepEqual(actual, expected);
});
test('do not sync machine settings', async () => {
const settingsContent =
`{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Workbench
"workbench.colorTheme": "GitHub Sharp",
// Machine
"settingsSync.machine": "someValue",
"settingsSync.machineOverridable": "someValue"
}`;
await updateSettings(settingsContent);
await testObject.sync();
const { content } = await client.read(testObject.resourceKey);
assert.ok(content !== null);
const actual = parseSettings(content!);
assert.deepEqual(actual, `{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Workbench
"workbench.colorTheme": "GitHub Sharp"
}`);
});
test('do not sync machine settings when spread across file', async () => {
const settingsContent =
`{
// Always
"files.autoSave": "afterDelay",
"settingsSync.machine": "someValue",
"files.simpleDialog.enable": true,
// Workbench
"workbench.colorTheme": "GitHub Sharp",
// Machine
"settingsSync.machineOverridable": "someValue"
}`;
await updateSettings(settingsContent);
await testObject.sync();
const { content } = await client.read(testObject.resourceKey);
assert.ok(content !== null);
const actual = parseSettings(content!);
assert.deepEqual(actual, `{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Workbench
"workbench.colorTheme": "GitHub Sharp"
}`);
});
test('do not sync machine settings when spread across file - 2', async () => {
const settingsContent =
`{
// Always
"files.autoSave": "afterDelay",
"settingsSync.machine": "someValue",
// Workbench
"workbench.colorTheme": "GitHub Sharp",
// Machine
"settingsSync.machineOverridable": "someValue",
"files.simpleDialog.enable": true,
}`;
await updateSettings(settingsContent);
await testObject.sync();
const { content } = await client.read(testObject.resourceKey);
assert.ok(content !== null);
const actual = parseSettings(content!);
assert.deepEqual(actual, `{
// Always
"files.autoSave": "afterDelay",
// Workbench
"workbench.colorTheme": "GitHub Sharp",
"files.simpleDialog.enable": true,
}`);
});
test('sync when all settings are machine settings', async () => {
const settingsContent =
`{
// Machine
"settingsSync.machine": "someValue",
"settingsSync.machineOverridable": "someValue"
}`;
await updateSettings(settingsContent);
await testObject.sync();
const { content } = await client.read(testObject.resourceKey);
assert.ok(content !== null);
const actual = parseSettings(content!);
assert.deepEqual(actual, `{
}`);
});
test('sync when all settings are machine settings with trailing comma', async () => {
const settingsContent =
`{
// Machine
"settingsSync.machine": "someValue",
"settingsSync.machineOverridable": "someValue",
}`;
await updateSettings(settingsContent);
await testObject.sync();
const { content } = await client.read(testObject.resourceKey);
assert.ok(content !== null);
const actual = parseSettings(content!);
assert.deepEqual(actual, `{
,
}`);
});
test('do not sync ignored settings', async () => {
const settingsContent =
`{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Editor
"editor.fontFamily": "Fira Code",
// Terminal
"terminal.integrated.shell.osx": "some path",
// Workbench
"workbench.colorTheme": "GitHub Sharp",
// Ignored
"sync.ignoredSettings": [
"editor.fontFamily",
"terminal.integrated.shell.osx"
]
}`;
await updateSettings(settingsContent);
await testObject.sync();
const { content } = await client.read(testObject.resourceKey);
assert.ok(content !== null);
const actual = parseSettings(content!);
assert.deepEqual(actual, `{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Workbench
"workbench.colorTheme": "GitHub Sharp",
// Ignored
"sync.ignoredSettings": [
"editor.fontFamily",
"terminal.integrated.shell.osx"
]
}`);
});
test('do not sync ignored and machine settings', async () => {
const settingsContent =
`{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Editor
"editor.fontFamily": "Fira Code",
// Terminal
"terminal.integrated.shell.osx": "some path",
// Workbench
"workbench.colorTheme": "GitHub Sharp",
// Ignored
"sync.ignoredSettings": [
"editor.fontFamily",
"terminal.integrated.shell.osx"
],
// Machine
"settingsSync.machine": "someValue",
}`;
await updateSettings(settingsContent);
await testObject.sync();
const { content } = await client.read(testObject.resourceKey);
assert.ok(content !== null);
const actual = parseSettings(content!);
assert.deepEqual(actual, `{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Workbench
"workbench.colorTheme": "GitHub Sharp",
// Ignored
"sync.ignoredSettings": [
"editor.fontFamily",
"terminal.integrated.shell.osx"
],
}`);
});
test('sync throws invalid content error', async () => {
const expected =
`{
// Always
"files.autoSave": "afterDelay",
"files.simpleDialog.enable": true,
// Workbench
"workbench.colorTheme": "GitHub Sharp",
"workbench.tree.indent": 20,
"workbench.colorCustomizations": {
"editorLineNumber.activeForeground": "#ff0000",
"[GitHub Sharp]": {
"statusBarItem.remoteBackground": "#24292E",
"editorPane.background": "#f3f1f11a"
}
}
"gitBranch.base": "remote-repo/master",
// Experimental
"workbench.view.experimental.allowMovingToNewContainer": true,
}`;
await updateSettings(expected);
try {
await testObject.sync();
assert.fail('should fail with invalid content error');
} catch (e) {
assert.ok(e instanceof UserDataSyncError);
assert.deepEqual((<UserDataSyncError>e).code, UserDataSyncErrorCode.LocalInvalidContent);
}
});
function parseSettings(content: string): string {
const syncData: ISyncData = JSON.parse(content);
const settingsSyncContent: ISettingsSyncContent = JSON.parse(syncData.content);
return settingsSyncContent.settings;
}
async function updateSettings(content: string): Promise<void> {
await client.instantiationService.get(IFileService).writeFile(client.instantiationService.get(IEnvironmentService).settingsResource, VSBuffer.fromString(content));
}
});
@@ -114,6 +114,14 @@ export class UserDataSyncClient extends Disposable {
await configurationService.reloadConfiguration();
}
sync(): Promise<void> {
return this.instantiationService.get(IUserDataSyncService).sync();
}
read(key: ResourceKey): Promise<IUserData> {
return this.instantiationService.get(IUserDataSyncStoreService).read(key, null);
}
}
export class UserDataSyncTestServer implements IRequestService {