Skip to content

Commit

Permalink
Merge pull request #168 from mrc-ide/mrc-4489
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz authored Aug 10, 2023
2 parents 77ca1d7 + a690c30 commit b7a5272
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
15 changes: 14 additions & 1 deletion app/server/src/configReader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import * as fs from "fs";
import * as path from "path";

export function stripBom(str: string): string {
// Catches EFBBBF (UTF-8 BOM) because the buffer-to-string
// conversion translates it to FEFF (UTF-16 BOM).
if (str.charCodeAt(0) === 0xFEFF) {
return str.slice(1);
}
return str;
}

export function readFile(filename: string): string {
return stripBom(fs.readFileSync(filename, { encoding: "utf-8" }));
}

export class ConfigReader {
rootDir: string;

Expand All @@ -15,7 +28,7 @@ export class ConfigReader {
const fullPath = [this.rootDir, ...filePath];
const filename = path.join(...fullPath);
if (fs.existsSync(filename)) {
const configText = fs.readFileSync(filename, { encoding: "utf-8" });
const configText = readFile(filename);
return { ...JSON.parse(configText), ...this.overrides };
}
return null;
Expand Down
7 changes: 6 additions & 1 deletion app/server/tests/configReader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConfigReader } from "../src/configReader";

describe("configReader", () => {
beforeEach(() => {
jest.resetAllMocks();
jest.restoreAllMocks();
});

it("returns null when config not found", () => {
Expand Down Expand Up @@ -31,4 +31,9 @@ describe("configReader", () => {
expect(mockReadFileSync.mock.calls[0][0]).toBe("root/test.config");
expect(mockReadFileSync.mock.calls[0][1]).toStrictEqual({ encoding: "utf-8" });
});

it("copes with BOM markers", () => {
const res: any = new ConfigReader(__dirname).readConfigFile("examples/bom.json");
expect((res as any).title).toBe("Ebola Project");
});
});
6 changes: 6 additions & 0 deletions app/server/tests/examples/bom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"appType": "fit",
"title": "Ebola Project",
"fitProp": "",
"readOnlyCode": false
}

0 comments on commit b7a5272

Please sign in to comment.