Skip to content

Commit

Permalink
Allow secrets in settings
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyBrobston committed Oct 9, 2020
1 parent acb2327 commit 0a572e6
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function readSettings<T>(serviceName: string, settingsFileName: string):
throw new Error(`[${serviceName}] Unable to load configuration file ${settingsFileName}.`);
}

let parseErrors: JSONC.ParseError[];
const parseErrors: JSONC.ParseError[] = [];

const settings = JSONC.parse(rawConfig, parseErrors) as T;

Expand All @@ -36,7 +36,10 @@ export function readSettings<T>(serviceName: string, settingsFileName: string):
if (parseErrors && parseErrors.length > 0) {
throw new Error(
`[${serviceName}] Unable to load configuration file: ${parseErrors
.map(error => log.error("${serviceName}", `${error?.error}`))
.map(error => {
log.error(`${serviceName}`, `${error?.error}`);
return JSON.stringify(error);
})
.join("\n")}`,
);
}
Expand Down
115 changes: 115 additions & 0 deletions tests/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Neil Enns. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import {closeSync, existsSync, openSync, unlinkSync, writeFileSync} from 'fs';
import * as JSONC from "jsonc-parser";
import * as helpers from "./../src/helpers";

describe("helpers", () => {
const settingsFilePath = `${__dirname}/settings.json`;

beforeEach(() => {
closeSync(openSync(settingsFilePath, 'w'));
});

afterEach(() => {
jest.clearAllMocks();
if (existsSync(settingsFilePath)) {
unlinkSync(settingsFilePath);
}
});

test("Verify can load settings.json", () => {
const serviceName = "Settings";
const expectedSettings = {"foo": "bar"};
writeFileSync(settingsFilePath, JSON.stringify(expectedSettings));

const actualSettings = helpers.readSettings(serviceName, settingsFilePath);

expect(actualSettings).toEqual(expectedSettings);
});

test("Verify cannot load settings.json because it does not exist", () => {
//eslint-disable-next-line no-console
console.log = jest.fn();
const serviceName = "Settings";
unlinkSync(settingsFilePath);

const actualSettings = helpers.readSettings(serviceName, settingsFilePath);

//eslint-disable-next-line no-console
expect(console.log).toHaveBeenCalledWith(expect.stringContaining("[Settings] Unable to read the configuration file: ENOENT: no such file or directory"));
expect(actualSettings).toBeNull();
});

test("Verify throws if rawConfig empty", () => {
const serviceName = "Settings";
const expectedSettings = "";
writeFileSync(settingsFilePath, expectedSettings);

expect(() => {helpers.readSettings(serviceName, settingsFilePath)}).toThrow(Error);
});

test("Verify throws with message if rawConfig empty", () => {
const serviceName = "Settings";
const expectedSettings = "";
writeFileSync(settingsFilePath, expectedSettings);

try {
helpers.readSettings(serviceName, settingsFilePath);
} catch (error) {
expect(error.message).toBe(`[${serviceName}] Unable to load configuration file ${settingsFilePath}.`);
}
});

test("Verify throws if json invalid", () => {
const serviceName = "Settings";
const expectedSettings = {};
writeFileSync(settingsFilePath, JSON.stringify(expectedSettings));
const mockAddListener = jest.spyOn(JSONC, 'parse');
mockAddListener.mockImplementation((rawConfig, parseErrors) => {
parseErrors.push({
error: 1,
offset: 2,
length: 3,
});
return {};
});

expect(() => {helpers.readSettings(serviceName, settingsFilePath)}).toThrow(Error);
});

test("Verify throws with message if json invalid", () => {
const serviceName = "Settings";
const expectedSettings = {};
writeFileSync(settingsFilePath, JSON.stringify(expectedSettings));
const mockAddListener = jest.spyOn(JSONC, 'parse');
const parseError1 = {
error: 1,
offset: 2,
length: 3,
};
const parseError2 = {
error: 3,
offset: 2,
length: 1,
};
mockAddListener.mockImplementation((rawConfig, parseErrors) => {
parseErrors.push(parseError1);
parseErrors.push(parseError2);
return {};
});
//eslint-disable-next-line no-console
console.log = jest.fn();

try {
helpers.readSettings(serviceName, settingsFilePath);
} catch (error) {
//eslint-disable-next-line no-console
expect(console.log).toHaveBeenCalledWith(expect.stringContaining("[Settings] 1"));
expect(error.message).toBe(`[${serviceName}] Unable to load configuration file: ${JSON.stringify(parseError1)}\n${JSON.stringify(parseError2)}`);
}
});
});

0 comments on commit 0a572e6

Please sign in to comment.