Skip to content

Commit

Permalink
Remove the intermediate updateFromCommandLine function with mocks
Browse files Browse the repository at this point in the history
- mock `fs.writeFileSync` so that `updateFromCommandLine` can call
  `settings.save()` and unit tests don't do crazy things to the pref set.

Signed-off-by: Eric Promislow <[email protected]>
  • Loading branch information
ericpromislow committed Apr 13, 2022
1 parent bedfbdc commit 92470e6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Electron.app.whenReady().then(async() => {
cfg = settings.init();
if (commandLineArgs.length) {
try {
cfg = settings.updateFromCommandLineAndSave(cfg, commandLineArgs);
cfg = settings.updateFromCommandLine(cfg, commandLineArgs);
} catch (err) {
console.log(`Failed to update command from argument ${ commandLineArgs } `, err);
}
Expand Down
18 changes: 11 additions & 7 deletions src/config/__tests__/settings.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import fs from 'fs';

import * as settings from '../settings';
import { PathManagementStrategy } from '@/integrations/pathManager';
import { RecursivePartial } from '~/utils/typeUtils';

describe('updateFromCommandLine', () => {
let prefs: settings.Settings;
let configDir: string;

beforeEach(() => {
jest.spyOn(fs, 'writeFileSync').mockImplementation(() => { });
prefs = {
version: 4,
kubernetes: {
Expand Down Expand Up @@ -81,24 +85,24 @@ describe('updateFromCommandLine', () => {
});
});

test('no args should leave prefs unchanged', () => {
test('no command-line args should leave prefs unchanged', () => {
const newPrefs = settings.updateFromCommandLine(prefs, []);

expect(newPrefs).toMatchObject(prefs);
});

test('one option with embedded equal sign should change only one value', () => {
const newPrefs = settings.updateFromCommandLine(prefs, ['--kubernetes-version=1.23.5']);
const newPrefs = settings.updateFromCommandLine(prefs, ['--kubernetes-version=1.23.6']);

expect(newPrefs.kubernetes.version).toBe('1.23.5');
expect(newPrefs.kubernetes.version).toBe('1.23.6');
newPrefs.kubernetes.version = prefs.kubernetes.version;
expect(newPrefs).toMatchObject(prefs);
});

test('one option over two args should change only one value', () => {
const newPrefs = settings.updateFromCommandLine(prefs, ['--kubernetes-version', '1.23.5']);
const newPrefs = settings.updateFromCommandLine(prefs, ['--kubernetes-version', '1.23.7']);

expect(newPrefs.kubernetes.version).toBe('1.23.5');
expect(newPrefs.kubernetes.version).toBe('1.23.7');
newPrefs.kubernetes.version = prefs.kubernetes.version;
expect(newPrefs).toMatchObject(prefs);
});
Expand All @@ -111,15 +115,15 @@ describe('updateFromCommandLine', () => {
expect(newPrefs).toMatchObject(prefs);
});

test('boolean option to implicit true should change only that value', () => {
test('boolean option set to implicit true should change only that value', () => {
const newPrefs = settings.updateFromCommandLine(prefs, ['--kubernetes-suppressSudo']);

expect(newPrefs.kubernetes.suppressSudo).toBeTruthy();
newPrefs.kubernetes.suppressSudo = false;
expect(newPrefs).toMatchObject(prefs);
});

test('boolean option to false should change only that value', () => {
test('boolean option set to false should change only that value', () => {
const newPrefs = settings.updateFromCommandLine(prefs, ['--kubernetes-options-traefik=false']);

expect(newPrefs.kubernetes.options.traefik).toBeFalsy();
Expand Down
13 changes: 4 additions & 9 deletions src/config/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ export function getUpdatableNode(cfg: Settings, fqFieldAccessor: string): [Recur
return null;
}

export function updateFromCommandLineAndSave(cfg: Settings, args: string[]): Settings {
const newConfig = updateFromCommandLine(cfg, args);

save(newConfig);

return newConfig;
}

// Export for testing purposes only
export function updateFromCommandLine(cfg: Settings, args: string[]): Settings {
let i = 0;
Expand Down Expand Up @@ -199,7 +191,7 @@ export function updateFromCommandLine(cfg: Settings, args: string[]): Settings {
case 'boolean':
// --some-boolean-setting ==> --some-boolean-setting=true
if (!value) {
finalValue = 'true'; // JSON.parse to the boolean a few lines later.
finalValue = 'true'; // JSON.parse to boolean `true` a few lines later.
}
break;
default:
Expand Down Expand Up @@ -230,6 +222,9 @@ export function updateFromCommandLine(cfg: Settings, args: string[]): Settings {
(lhs as Record<string, any>)[finalFieldName] = finalValue;
i += 1;
}
if (lim > 0) {
save(cfg);
}

return cfg;
}
Expand Down

0 comments on commit 92470e6

Please sign in to comment.