From defcd1a90ad42a3bd150bbc418896e3cfa7e5391 Mon Sep 17 00:00:00 2001 From: Salah Benmoussati <51402489+sbenmoussati@users.noreply.github.com> Date: Mon, 15 Jul 2024 09:31:54 +0200 Subject: [PATCH] SDA-4606 Setting programmatically default values of new config fields (#2171) * SDA-4606 Setting programmatically default values of new config fields * Upgrade electron to patch reported file picker issue --- package.json | 2 +- spec/config.spec.ts | 32 +++++++++++++++++++++++++++++++- src/app/config-handler.ts | 27 +++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 829c4df6..c1ebfe60 100644 --- a/package.json +++ b/package.json @@ -178,7 +178,7 @@ "builder-util-runtime": "^9.0.3", "cross-env": "7.0.3", "del": "3.0.0", - "electron": "31.1.0", + "electron": "31.2.0", "electron-builder": "^24.13.2", "electron-devtools-installer": "^3.2.0", "electron-icon-maker": "0.0.5", diff --git a/spec/config.spec.ts b/spec/config.spec.ts index 230f115e..70b957e1 100644 --- a/spec/config.spec.ts +++ b/spec/config.spec.ts @@ -1,7 +1,11 @@ import * as fs from 'fs'; import * as os from 'os'; import * as path from 'path'; -import { IConfig, IGlobalConfig } from '../src/app/config-handler'; +import { + ConfigFieldsDefaultValues, + IConfig, + IGlobalConfig, +} from '../src/app/config-handler'; jest.mock('electron-log'); jest.mock('../src/app/auto-update-handler', () => { @@ -88,6 +92,32 @@ describe('config', () => { expect(configField).toEqual({}); }); + it('should retrieve default values when field not present in either user or global config', () => { + const fieldMock: string[] = [ + 'isPodUrlEditable', + 'forceAutoUpdate', + 'enableBrowserLogin', + 'browserLoginAutoConnect', + 'latestAutoUpdateChannelEnabled', + 'betaAutoUpdateChannelEnabled', + ]; + const globalConfig: object = { url: 'test' }; + const userConfig: object = { configVersion: '4.0.1' }; + + // creating temp file + writeConfigFile(globalConfig); + writeUserFile(userConfig); + + // changing path from /Users/.../SymphonyElectron/config/Symphony.config to temp path + configInstance.globalConfigPath = globalConfigDir; + configInstance.userConfigPath = userConfigDir; + configInstance.readGlobalConfig(); + configInstance.readUserConfig(); + + const configField: IConfig = configInstance.getConfigFields(fieldMock); + expect(configField).toEqual({ ...ConfigFieldsDefaultValues }); + }); + it('should succeed when only present in user config', () => { const fieldMock: string[] = ['url']; const globalConfig: object = { url: 'something' }; diff --git a/src/app/config-handler.ts b/src/app/config-handler.ts index 0fe9fbb1..5a366de1 100644 --- a/src/app/config-handler.ts +++ b/src/app/config-handler.ts @@ -31,6 +31,15 @@ export enum CloudConfigDataTypes { DISABLED = 'DISABLED', } +export const ConfigFieldsDefaultValues: Partial = { + isPodUrlEditable: true, + forceAutoUpdate: false, + enableBrowserLogin: false, + browserLoginAutoConnect: false, + latestAutoUpdateChannelEnabled: true, + betaAutoUpdateChannelEnabled: true, +}; + export const ConfigFieldsToRestart = new Set([ 'permissions', 'disableThrottling', @@ -284,6 +293,7 @@ class Config { */ public getConfigFields(fields: string[]): IConfig { const configFields: IConfig = { + ...this.getConfigfromDefaultFields(fields), ...this.getGlobalConfigFields(fields), ...this.getUserConfigFields(fields), ...this.getFilteredCloudConfigFields(fields), @@ -313,6 +323,23 @@ class Config { return userConfigData; } + /** + * Returns default value of specified fields + * + * @param fields {Array} + */ + public getConfigfromDefaultFields(fields: string[]): IGlobalConfig { + const defaultConfigData = pick( + ConfigFieldsDefaultValues, + fields, + ) as IGlobalConfig; + logger.info( + `config-handler: getting default config values for the fields ${fields}`, + defaultConfigData, + ); + return defaultConfigData; + } + /** * Returns the specified fields from global config file *