From acb3bbf881e3a81eeeeca57e18176c2e1e59fbca Mon Sep 17 00:00:00 2001 From: Gulliby-development <20404543+Gulliby@users.noreply.github.com> Date: Thu, 15 Oct 2026 15:49:41 +0200 Subject: [PATCH] Add --duplicates to report keys assigned more than once --- README.md | 27 +++ src/cli.ts | 8 +- src/format.ts | 18 +- src/parse.ts | 25 ++- tests/duplicates.test.ts | 441 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 513 insertions(+), 6 deletions(-) create mode 100644 tests/duplicates.test.ts diff --git a/README.md b/README.md index 9dabb95..ef3970e 100644 --- a/README.md +++ b/README.md @@ -89,6 +89,7 @@ Relative paths are resolved from the current working directory. | `--strict` | Fail on extra keys as well as missing or empty keys | | `--json` | Print JSON instead of the human-readable report | | `--github` | Print GitHub Actions annotations instead of the human-readable report; cannot be combined with `--json` | +| `--duplicates` | Also report keys that `.env` assigns more than once (warnings, errors with `--strict`) | | `--help`, `-h` | Show usage and exit successfully | | `--version`, `-v` | Show the package version and exit successfully | | `--allow-empty ` | Keys that may be empty; repeatable or comma-separated; `PREFIX*` matches by prefix | @@ -200,6 +201,32 @@ Extra keys (warnings): D When everything is fine, envguard prints `Success: required environment keys are present and non-empty.` and exits 0. +### Duplicate keys + +A key assigned twice in `.env` silently uses the last value. With `--duplicates`, envguard also lists such keys. With this `.env.example`: + +```text +A= +``` + +and this `.env`: + +```text +A=1 +B=2 +A=3 +``` + +`envguard --duplicates` prints: + +```text +Success: required environment keys are present and non-empty. +Extra keys (warnings): B +Duplicate keys (warnings): A +``` + +and exits 0; with `--strict` both lists are errors and the exit code is 1. Only assignments that envguard recognizes count, `export A=1` and `A=1` are the same key, and the template file is not checked. `--json` then adds a last field `"duplicate":["A"]` (always present with `--duplicates`, possibly empty); `--github` adds one line per key, `::warning file=.env,title=envguard::Duplicate key A (assigned more than once)` (`::error` with `--strict`). `--ignore-extra` does not affect duplicates. Without `--duplicates` nothing changes. + ### GitHub Actions annotations In a GitHub Actions job, `--github` makes each problem an annotation on the run and pull request. For example, with this `.env.example`: diff --git a/src/cli.ts b/src/cli.ts index ec8052b..7603354 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -4,7 +4,7 @@ import { resolve } from "node:path"; import { parseArgs } from "node:util"; import { compareEnv } from "./compare"; import { createReport, formatGithub, formatHuman, formatJson } from "./format"; -import { parseEnv } from "./parse"; +import { findDuplicateKeys, parseEnv } from "./parse"; const help = `Usage: envguard [options] @@ -16,6 +16,7 @@ Options: --strict Treat extra keys as errors rather than warnings --allow-empty Keys allowed to be empty; repeatable, comma-separated, KEY or PREFIX* --ignore-extra Extra keys to leave out of the report; repeatable, comma-separated, KEY or PREFIX* + --duplicates Report keys assigned more than once in the env file --json Print one JSON report --github Print GitHub Actions annotations (::error and ::warning lines) --help, -h Show this help @@ -49,6 +50,7 @@ function main(): number { "ignore-extra": { type: "string", multiple: true }, json: { type: "boolean", default: false }, github: { type: "boolean", default: false }, + duplicates: { type: "boolean", default: false }, help: { type: "boolean", short: "h" }, version: { type: "boolean", short: "v" }, }, @@ -78,13 +80,15 @@ function main(): number { const envPath = resolve(values.env); const examplePath = resolve(values.example); - const env = parseEnv(readFileSync(envPath, "utf8")); + const envText = readFileSync(envPath, "utf8"); + const env = parseEnv(envText); const example = parseEnv(readFileSync(examplePath, "utf8")); const report = createReport( compareEnv(env, example, allowEmpty, ignoreExtra), envPath, examplePath, values.strict, + values.duplicates ? findDuplicateKeys(envText) : undefined, ); const output = values.github ? formatGithub(report, values.env, values.example) diff --git a/src/format.ts b/src/format.ts index 6298e42..b6e82ca 100644 --- a/src/format.ts +++ b/src/format.ts @@ -5,6 +5,7 @@ export interface Report extends Comparison { strict: boolean; env: string; example: string; + duplicate?: string[]; } /** Copy and sort the lists so report creation never mutates its input. */ @@ -13,16 +14,22 @@ export function createReport( env: string, example: string, strict = false, + duplicate?: readonly string[], ): Report { const { missing, extra, empty } = comparison; + const duplicateKeys = duplicate === undefined ? undefined : [...duplicate].sort(); return { - ok: missing.length === 0 && empty.length === 0 && (!strict || extra.length === 0), + ok: + missing.length === 0 && + empty.length === 0 && + (!strict || (extra.length === 0 && (duplicateKeys?.length ?? 0) === 0)), strict, env, example, missing: [...missing].sort(), extra: [...extra].sort(), empty: [...empty].sort(), + ...(duplicateKeys === undefined ? {} : { duplicate: duplicateKeys }), }; } @@ -59,6 +66,10 @@ export function formatGithub(report: Report, envPath: string, examplePath: strin (key) => `::${report.strict ? "error" : "warning"} file=${file},title=envguard::Extra key ${key} (not in ${escapedExample})`, ), + ...(report.duplicate ?? []).map( + (key) => + `::${report.strict ? "error" : "warning"} file=${file},title=envguard::Duplicate key ${key} (assigned more than once)`, + ), ]; return lines.join("\n"); } @@ -79,5 +90,10 @@ export function formatHuman(report: Report): string { if (report.extra.length > 0) { lines.push(`Extra keys (${report.strict ? "errors" : "warnings"}): ${report.extra.join(", ")}`); } + if (report.duplicate && report.duplicate.length > 0) { + lines.push( + `Duplicate keys (${report.strict ? "errors" : "warnings"}): ${report.duplicate.join(", ")}`, + ); + } return lines.join("\n"); } diff --git a/src/parse.ts b/src/parse.ts index 9782734..d572ada 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -5,16 +5,35 @@ export function parseEnv(text: string): Map { const entries = new Map(); + for (const assignment of parseAssignments(text)) { + entries.set(assignment.key, assignment.value); + } + + return entries; +} + +export function findDuplicateKeys(text: string): string[] { + const counts = new Map(); + + for (const assignment of parseAssignments(text)) { + counts.set(assignment.key, (counts.get(assignment.key) ?? 0) + 1); + } + + return [...counts] + .filter(([, count]) => count > 1) + .map(([key]) => key) + .sort(); +} + +function* parseAssignments(text: string): Generator<{ key: string; value: string }> { for (const line of text.replace(/^\uFEFF/, "").split(/\r?\n/)) { const match = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_.]*)\s*=(.*)$/.exec(line); if (!match) continue; const [, key, raw] = match; const value = parseValue(raw); - if (value !== undefined) entries.set(key, value); + if (value !== undefined) yield { key, value }; } - - return entries; } function parseValue(raw: string): string | undefined { diff --git a/tests/duplicates.test.ts b/tests/duplicates.test.ts new file mode 100644 index 0000000..f1a4d46 --- /dev/null +++ b/tests/duplicates.test.ts @@ -0,0 +1,441 @@ +import { afterEach, beforeEach, describe, expect, test } from "bun:test"; +import { spawnSync } from "node:child_process"; +import { mkdirSync, mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join, resolve } from "node:path"; +import { fileURLToPath } from "node:url"; +import { createReport, formatGithub, formatHuman } from "../src/format"; +import { findDuplicateKeys, parseEnv } from "../src/parse"; + +const cliPath = fileURLToPath(new URL("../src/cli.ts", import.meta.url)); +const successLine = "Success: required environment keys are present and non-empty."; + +describe("duplicate assignment reporting", () => { + let directory: string; + + beforeEach(() => { + directory = realpathSync(mkdtempSync(join(tmpdir(), "envguard-duplicates-"))); + }); + + afterEach(() => { + rmSync(directory, { recursive: true, force: true }); + }); + + function writeDefaults(env: string, example: string): void { + writeFileSync(join(directory, ".env"), env, "utf8"); + writeFileSync(join(directory, ".env.example"), example, "utf8"); + } + + function runCli(args: string[] = [], cwd = directory) { + const result = spawnSync(process.execPath, ["run", cliPath, ...args], { + cwd, + encoding: "utf8", + stdio: ["ignore", "pipe", "pipe"], + timeout: 30_000, + maxBuffer: 1024 * 1024, + }); + expect(result.error).toBeUndefined(); + expect(result.signal).toBeNull(); + expect(result.status).not.toBeNull(); + return result; + } + + function jsonLine(value: object): string { + return [JSON.stringify(value) ?? "", ""].join("\n"); + } + + test("findDuplicateKeys follows accepted assignment parsing and preserves last-value-wins", () => { + const text = [ + "B=first", + "A=first", + "B=second", + "export A='last value'", + "A=third", + "a=lowercase", + "EMPTY=", + "EMPTY=''", + "DOUBLE=one", + 'DOUBLE="two"', + "BAD=accepted", + 'BAD="unterminated', + "SUFFIX=accepted", + 'SUFFIX="closed" trailing', + "not an assignment", + "# KEY=comment", + ].join("\n"); + + expect(findDuplicateKeys(text)).toEqual(["A", "B", "DOUBLE", "EMPTY"]); + expect(parseEnv(text).get("A")).toBe("third"); + expect(parseEnv(text).get("B")).toBe("second"); + expect(parseEnv(text).get("BAD")).toBe("accepted"); + expect(parseEnv(text).get("SUFFIX")).toBe("accepted"); + }); + + test("findDuplicateKeys sorts unique keys, handles BOM and CRLF, and ignores rejected lines", () => { + expect(findDuplicateKeys("\uFEFFA=first\r\nB=first\r\nA=second\r\nB=second\r\n")).toEqual([ + "A", + "B", + ]); + expect(findDuplicateKeys("")).toEqual([]); + expect(findDuplicateKeys('# A=1\ninvalid line\nA="unterminated\n')).toEqual([]); + }); + + test("createReport omits an unspecified duplicate field and retains the existing failure rules", () => { + const report = createReport( + { missing: [], extra: [], empty: ["EMPTY"] }, + ".env", + ".env.example", + ); + + expect(Object.hasOwn(report, "duplicate")).toBe(false); + expect(report.ok).toBe(false); + expect(formatHuman(report)).toBe("Environment problems found.\nEmpty values (errors): EMPTY"); + }); + + test("createReport copies and sorts duplicate keys without mutating its input", () => { + const input = ["B", "A"]; + const report = createReport( + { missing: [], extra: [], empty: [] }, + ".env", + ".env.example", + false, + input, + ); + + expect(report.duplicate).toEqual(["A", "B"]); + expect(report.duplicate).not.toBe(input); + expect(input).toEqual(["B", "A"]); + expect(report.ok).toBe(true); + }); + + test("an explicitly empty duplicate list stays present but does not fail strict reports", () => { + const report = createReport( + { missing: [], extra: [], empty: [] }, + ".env", + ".env.example", + true, + [], + ); + + expect(Object.hasOwn(report, "duplicate")).toBe(true); + expect(report.duplicate).toEqual([]); + expect(report.ok).toBe(true); + expect(formatHuman(report)).toBe(successLine); + expect(formatGithub(report, ".env", ".env.example")).toBe(""); + }); + + test("strict reports treat duplicate-only findings as errors", () => { + const comparison = { missing: [], extra: [], empty: [] }; + const warning = createReport(comparison, ".env", ".env.example", false, ["A"]); + const error = createReport(comparison, ".env", ".env.example", true, ["A"]); + + expect(warning.ok).toBe(true); + expect(formatHuman(warning)).toBe([successLine, "Duplicate keys (warnings): A"].join("\n")); + expect(error.ok).toBe(false); + expect(formatHuman(error)).toBe( + ["Environment problems found.", "Duplicate keys (errors): A"].join("\n"), + ); + }); + + test("human output matches the worked example, including strict and ignored-extra behavior", () => { + writeDefaults("A=1\nB=2\nA=3\nCI=true\nC=\nCI=false\nexport A=4\n", "A=\nB=\nC=\n"); + + const warning = runCli(["--duplicates"]); + expect(warning.status).toBe(1); + expect(warning.stderr).toBe(""); + expect(warning.stdout).toBe( + [ + "Environment problems found.", + "Empty values (errors): C", + "Extra keys (warnings): CI", + "Duplicate keys (warnings): A, CI", + "", + ].join("\n"), + ); + + const strict = runCli(["--duplicates", "--strict"]); + expect(strict.status).toBe(1); + expect(strict.stderr).toBe(""); + expect(strict.stdout).toBe( + [ + "Environment problems found.", + "Empty values (errors): C", + "Extra keys (errors): CI", + "Duplicate keys (errors): A, CI", + "", + ].join("\n"), + ); + + const ignored = runCli(["--duplicates", "--ignore-extra", "CI"]); + expect(ignored.status).toBe(1); + expect(ignored.stderr).toBe(""); + expect(ignored.stdout).toBe( + [ + "Environment problems found.", + "Empty values (errors): C", + "Duplicate keys (warnings): A, CI", + "", + ].join("\n"), + ); + }); + + test("human output keeps duplicates as warnings until strict and omits an empty duplicate line", () => { + writeDefaults("A=1\nB=2\nA=3\n", "A=\n"); + + const normal = runCli(["--duplicates"]); + expect(normal.status).toBe(0); + expect(normal.stderr).toBe(""); + expect(normal.stdout).toBe( + [successLine, "Extra keys (warnings): B", "Duplicate keys (warnings): A", ""].join("\n"), + ); + + const strict = runCli(["--strict", "--duplicates"]); + expect(strict.status).toBe(1); + expect(strict.stderr).toBe(""); + expect(strict.stdout).toBe( + [ + "Environment problems found.", + "Extra keys (errors): B", + "Duplicate keys (errors): A", + "", + ].join("\n"), + ); + + writeDefaults("A=1\n", "A=\n"); + const noDuplicates = runCli(["--duplicates"]); + expect(noDuplicates.status).toBe(0); + expect(noDuplicates.stdout).toBe([successLine, ""].join("\n")); + }); + + test("JSON puts duplicate last when enabled and preserves default output when disabled", () => { + writeDefaults("A=1\nB=2\nA=3\nCI=true\nC=\nCI=false\nexport A=4\n", "A=\nB=\nC=\n"); + + const enabled = runCli(["--duplicates", "--json"]); + expect(enabled.status).toBe(1); + expect(enabled.stderr).toBe(""); + expect(enabled.stdout).toBe( + jsonLine({ + ok: false, + strict: false, + env: resolve(directory, ".env"), + example: resolve(directory, ".env.example"), + missing: [], + extra: ["CI"], + empty: ["C"], + duplicate: ["A", "CI"], + }), + ); + expect(enabled.stdout.endsWith('"empty":["C"],"duplicate":["A","CI"]}\n')).toBe(true); + expect(enabled.stdout).not.toContain("CI=true"); + expect(enabled.stdout).not.toContain("CI=false"); + + const disabled = runCli(["--json"]); + expect(disabled.status).toBe(1); + expect(disabled.stderr).toBe(""); + expect(disabled.stdout).toBe( + jsonLine({ + ok: false, + strict: false, + env: resolve(directory, ".env"), + example: resolve(directory, ".env.example"), + missing: [], + extra: ["CI"], + empty: ["C"], + }), + ); + expect(JSON.parse(disabled.stdout)).not.toHaveProperty("duplicate"); + }); + + test("JSON includes an empty duplicate field and strict duplicate-only reports fail", () => { + writeDefaults("A=1\n", "A=\n"); + + const empty = runCli(["--json", "--duplicates"]); + expect(empty.status).toBe(0); + expect(empty.stderr).toBe(""); + expect(empty.stdout).toBe( + jsonLine({ + ok: true, + strict: false, + env: resolve(directory, ".env"), + example: resolve(directory, ".env.example"), + missing: [], + extra: [], + empty: [], + duplicate: [], + }), + ); + + writeDefaults("A=1\nA=2\n", "A=\n"); + const warning = runCli(["--duplicates", "--json"]); + expect(warning.status).toBe(0); + expect(JSON.parse(warning.stdout)).toMatchObject({ ok: true, duplicate: ["A"] }); + + const strict = runCli(["--duplicates", "--strict", "--json"]); + expect(strict.status).toBe(1); + expect(JSON.parse(strict.stdout)).toMatchObject({ ok: false, strict: true, duplicate: ["A"] }); + expect(strict.stdout).not.toContain("A=1"); + expect(strict.stdout).not.toContain("A=2"); + }); + + test("GitHub output matches the worked annotations and promotes warnings in strict mode", () => { + writeDefaults("A=1\nB=2\nA=3\nCI=true\nC=\nCI=false\nexport A=4\n", "A=\nB=\nC=\n"); + + const warning = runCli(["--duplicates", "--github"]); + expect(warning.status).toBe(1); + expect(warning.stderr).toBe(""); + expect(warning.stdout).toBe( + [ + "::error file=.env,title=envguard::Empty value for key C", + "::warning file=.env,title=envguard::Extra key CI (not in .env.example)", + "::warning file=.env,title=envguard::Duplicate key A (assigned more than once)", + "::warning file=.env,title=envguard::Duplicate key CI (assigned more than once)", + "", + ].join("\n"), + ); + + const strict = runCli(["--github", "--strict", "--duplicates"]); + expect(strict.status).toBe(1); + expect(strict.stderr).toBe(""); + expect(strict.stdout).toBe( + [ + "::error file=.env,title=envguard::Empty value for key C", + "::error file=.env,title=envguard::Extra key CI (not in .env.example)", + "::error file=.env,title=envguard::Duplicate key A (assigned more than once)", + "::error file=.env,title=envguard::Duplicate key CI (assigned more than once)", + "", + ].join("\n"), + ); + expect(strict.stdout).not.toContain("CI=true"); + }); + + test("allow-empty and ignore-extra do not suppress duplicates, regardless of flag position", () => { + writeDefaults("A=1\nA=2\nC=\nC=\nEXTRA=first\nEXTRA=second\n", "A=\nC=required\n"); + const expected = [successLine, "Duplicate keys (warnings): A, C, EXTRA", ""].join("\n"); + + const before = runCli(["--duplicates", "--allow-empty", "C", "--ignore-extra", "EXTRA"]); + expect(before.status).toBe(0); + expect(before.stderr).toBe(""); + expect(before.stdout).toBe(expected); + + const after = runCli(["--allow-empty", "C", "--ignore-extra", "EXTRA", "--duplicates"]); + expect(after.status).toBe(0); + expect(after.stderr).toBe(""); + expect(after.stdout).toBe(expected); + }); + + test("custom env and example paths select the env file and never inspect template duplicates", () => { + const inputDirectory = join(directory, "custom inputs"); + mkdirSync(inputDirectory); + const envPath = join(inputDirectory, "custom env"); + const examplePath = join(inputDirectory, "custom example"); + writeFileSync(join(directory, ".env"), "DEFAULT=once\n", "utf8"); + writeFileSync(join(directory, ".env.example"), "DEFAULT=\n", "utf8"); + writeFileSync(envPath, "A=1\nA=2\nTEMPLATE_ONLY=once\n", "utf8"); + writeFileSync(examplePath, "A=\nTEMPLATE_ONLY=one\nTEMPLATE_ONLY=two\n", "utf8"); + + const result = runCli(["--env", envPath, "--duplicates", "--example", examplePath, "--json"]); + expect(result.status).toBe(0); + expect(result.stderr).toBe(""); + expect(result.stdout).toBe( + jsonLine({ + ok: true, + strict: false, + env: realpathSync(envPath), + example: realpathSync(examplePath), + missing: [], + extra: [], + empty: [], + duplicate: ["A"], + }), + ); + }); + + test("all default formats remain unchanged when the option is absent", () => { + writeDefaults("A=1\nEXTRA=private-first\nEXTRA=private-second\n", "A=\n"); + + const human = runCli(); + expect(human.status).toBe(0); + expect(human.stdout).toBe([successLine, "Extra keys (warnings): EXTRA", ""].join("\n")); + + const json = runCli(["--json"]); + expect(json.status).toBe(0); + expect(json.stdout).toBe( + jsonLine({ + ok: true, + strict: false, + env: resolve(directory, ".env"), + example: resolve(directory, ".env.example"), + missing: [], + extra: ["EXTRA"], + empty: [], + }), + ); + expect(JSON.parse(json.stdout)).not.toHaveProperty("duplicate"); + + const github = runCli(["--github"]); + expect(github.status).toBe(0); + expect(github.stdout).toBe( + "::warning file=.env,title=envguard::Extra key EXTRA (not in .env.example)\n", + ); + for (const output of [human.stdout, json.stdout, github.stdout]) { + expect(output).not.toContain("private-first"); + expect(output).not.toContain("private-second"); + } + }); + + test("GitHub duplicate annotations use the existing file escaping", () => { + const report = createReport( + { missing: [], extra: [], empty: [] }, + ".env", + ".env.example", + false, + ["A"], + ); + + expect(formatGithub(report, ".env%,\r\n:part", ".env.example")).toBe( + "::warning file=.env%25%2C%0D%0A%3Apart,title=envguard::Duplicate key A (assigned more than once)", + ); + }); + + test("invalid arguments and mutually exclusive formats remain usage errors with duplicates enabled", () => { + writeDefaults("A=1\nA=2\n", "A=\n"); + + const invalid = runCli(["--duplicates=invalid"]); + expect(invalid.status).toBe(2); + expect(invalid.stdout).toBe(""); + expect(invalid.stderr.trim().length).toBeGreaterThan(0); + + const positional = runCli(["--duplicates", "unexpected"]); + expect(positional.status).toBe(2); + expect(positional.stdout).toBe(""); + expect(positional.stderr.trim().length).toBeGreaterThan(0); + + const incompatible = runCli(["--duplicates", "--json", "--github"]); + expect(incompatible.status).toBe(2); + expect(incompatible.stdout).toBe(""); + expect(incompatible.stderr).toBe("envguard: --json and --github cannot be used together\n"); + }); + + test("help lists the exact duplicates option immediately before JSON", () => { + for (const flag of ["--help", "-h"]) { + const result = runCli([flag]); + expect(result.status).toBe(0); + expect(result.stderr).toBe(""); + const lines = result.stdout.split(/\r?\n/); + const duplicateIndex = lines.indexOf( + " --duplicates Report keys assigned more than once in the env file", + ); + expect(duplicateIndex).toBeGreaterThanOrEqual(0); + expect(lines[duplicateIndex + 1]).toBe(" --json Print one JSON report"); + } + }); + + test("GitHub annotations stay empty when duplicate reporting finds no duplicate", () => { + writeDefaults("A=1\n", "A=\n"); + const result = runCli(["--duplicates", "--github"]); + + expect(result.status).toBe(0); + expect(result.stderr).toBe(""); + expect(result.stdout).toBe(""); + }); +});