Skip to content

Commit

Permalink
fix: Don't replace malformed appmap.yml, warn and use defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
zermelo-wisen committed Mar 18, 2024
1 parent b1aa2b9 commit 62d0093
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync, writeFileSync } from "node:fs";
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
import { basename } from "node:path";
import { chdir, cwd } from "node:process";

Expand Down Expand Up @@ -101,6 +101,17 @@ describe(Config, () => {
});
});

it("does not replace malformed appmap.yml but uses defaults", () => {
const malformedAppMapFileContent = "{ appmap-dir: 'abc/xyz' ";
writeFileSync("appmap.yml", malformedAppMapFileContent);

const cfg = new Config();
expect(cfg.fileIsMalformed).toEqual(true);
expect(cfg.relativeAppmapDir).toEqual("tmp/appmap");

expect(readFileSync("appmap.yml").toString()).toEqual(malformedAppMapFileContent);
});

let dir: string;
beforeEach(() => {
chdir((dir = tmp.dirSync().name));
Expand Down
6 changes: 4 additions & 2 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import json5 from "json5";
import YAML from "yaml";

import config from "./config";
import { info } from "./message";
import { info, warn } from "./message";
import { version } from "./metadata";
import forwardSignals from "./util/forwardSignals";
import { readPkgUp } from "./util/readPkgUp";
Expand All @@ -30,7 +30,9 @@ export function main() {
if (config.default) {
info("Writing default config to %s", config.configPath);
writeFileSync(config.configPath, YAML.stringify(config));
} else info("Using config file %s", config.configPath);
} else if (config.fileIsMalformed)
warn("Malformed config file %s, using default values instead", config.configPath);
else info("Using config file %s", config.configPath);
config.export();

// FIXME: Probably there should be a way to remove this altogether
Expand Down
14 changes: 11 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Config {
public readonly root: string;
public readonly configPath: string;
public readonly default: boolean;
public readonly fileIsMalformed: boolean;
public readonly packages: PackageMatcher;
public readonly responseBodyMaxLength: number;

Expand All @@ -34,8 +35,10 @@ export class Config {
const root = (this.root = process.env.APPMAP_ROOT ?? configDir ?? packageDir() ?? pwd);

this.configPath = join(root, "appmap.yml");
const config = readConfigFile(this.configPath);
const readConfigFileResult = readConfigFile(this.configPath);
const config = readConfigFileResult != "malformed" ? readConfigFileResult : undefined;
this.default = !config;
this.fileIsMalformed = readConfigFileResult == "malformed";

this.relativeAppmapDir = config?.appmap_dir ?? "tmp/appmap";

Expand Down Expand Up @@ -94,12 +97,16 @@ interface ConfigFile {
name?: string;
packages?: Package[];
response_body_max_length?: number;
malformed?: boolean;
}

function readConfigFile(path: string | undefined): ConfigFile | undefined {
function readConfigFile(path: string | undefined): ConfigFile | "malformed" | undefined {
if (!path) return;
const config = tryOr(() => YAML.parse(readFileSync(path, "utf8")) as unknown);
const fileContent = tryOr(() => readFileSync(path, "utf8"));
if (!fileContent) return;
const config = tryOr(() => YAML.parse(fileContent) as unknown, "malformed");
if (!config) return;
if (config == "malformed") return "malformed";

const result: ConfigFile = {};
assert(typeof config === "object");
Expand All @@ -110,6 +117,7 @@ function readConfigFile(path: string | undefined): ConfigFile | undefined {
const value = parseInt(String(config.response_body_max_length));
result.response_body_max_length = value >= 0 ? value : undefined;
}
if ("malformed" in config) result.malformed = true;

return result;
}
Expand Down
4 changes: 2 additions & 2 deletions src/util/tryOr.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default function tryOr<T>(fn: () => T): T | undefined {
export default function tryOr<T, E = undefined>(fn: () => T, elseReturn?: E): T | E | undefined {
try {
return fn();
} catch (e) {
return undefined;
return elseReturn;
}
}

0 comments on commit 62d0093

Please sign in to comment.