Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ export function normalizeConfig(
export async function loadConfig(options: {
explicitPath?: string;
cwd?: string;
}): Promise<{ config: McpAuditConfig; path?: string }> {
} = {}): Promise<{ config: McpAuditConfig; path?: string }> {
const path =
options.explicitPath ?? findConfigFile(options.cwd ?? process.cwd());
if (!path) return { config: DEFAULT_CONFIG };
const raw = await readFile(resolve(path), "utf8");
const parsed = JSON.parse(raw);
let parsed: unknown;
try {
parsed = JSON.parse(raw);
} catch (err) {
throw new Error(`Failed to parse config file ${path}: ${(err as Error).message}`);
}
return { config: normalizeConfig(parsed), path };
}
20 changes: 19 additions & 1 deletion test/config.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { describe, it, expect } from "vitest";
import { normalizeConfig, DEFAULT_CONFIG } from "../src/config.js";
import { normalizeConfig, loadConfig, DEFAULT_CONFIG } from "../src/config.js";
import { writeFile, rm } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";

describe("normalizeConfig", () => {
it("returns defaults for an empty object", () => {
Expand Down Expand Up @@ -40,3 +43,18 @@ describe("normalizeConfig", () => {
expect(overlaid.failOn).toBe("medium");
});
});

describe("loadConfig", () => {
it("wraps JSON syntax errors with the file path", async () => {
const tmpFile = join(tmpdir(), `mcp-audit-test-${Math.random()}.json`);
await writeFile(tmpFile, "{ invalid json ", "utf8");
try {
await loadConfig({ explicitPath: tmpFile });
expect.fail("Should have thrown");
} catch (err) {
expect((err as Error).message).toContain(tmpFile);
} finally {
await rm(tmpFile, { force: true });
}
});
});
Loading