From 0826feb94d1d71248ecbd2e550086ff10ac0de13 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Fri, 13 Mar 2026 22:06:01 +0000 Subject: [PATCH] test: tighten path prepend helper coverage --- src/infra/path-prepend.test.ts | 48 +++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/infra/path-prepend.test.ts b/src/infra/path-prepend.test.ts index 29dfb504cfb..7f4211a0137 100644 --- a/src/infra/path-prepend.test.ts +++ b/src/infra/path-prepend.test.ts @@ -1,8 +1,20 @@ import path from "node:path"; import { describe, expect, it } from "vitest"; -import { mergePathPrepend, normalizePathPrepend } from "./path-prepend.js"; +import { + applyPathPrepend, + findPathKey, + mergePathPrepend, + normalizePathPrepend, +} from "./path-prepend.js"; describe("path prepend helpers", () => { + it("finds the actual PATH key while preserving original casing", () => { + expect(findPathKey({ PATH: "/usr/bin" })).toBe("PATH"); + expect(findPathKey({ Path: "/usr/bin" })).toBe("Path"); + expect(findPathKey({ PaTh: "/usr/bin" })).toBe("PaTh"); + expect(findPathKey({ HOME: "/tmp" })).toBe("PATH"); + }); + it("normalizes prepend lists by trimming, skipping blanks, and deduping", () => { expect( normalizePathPrepend([ @@ -30,4 +42,38 @@ describe("path prepend helpers", () => { mergePathPrepend(` /usr/bin ${path.delimiter} ${path.delimiter} /opt/bin `, ["/custom/bin"]), ).toBe(["/custom/bin", "/usr/bin", "/opt/bin"].join(path.delimiter)); }); + + it("applies prepends to the discovered PATH key and preserves existing casing", () => { + const env = { + Path: [`/usr/bin`, `/opt/bin`].join(path.delimiter), + }; + + applyPathPrepend(env, ["/custom/bin", "/usr/bin"]); + + expect(env).toEqual({ + Path: ["/custom/bin", "/usr/bin", "/opt/bin"].join(path.delimiter), + }); + }); + + it("respects requireExisting and ignores empty prepend lists", () => { + const envWithoutPath = { HOME: "/tmp/home" }; + applyPathPrepend(envWithoutPath, ["/custom/bin"], { requireExisting: true }); + expect(envWithoutPath).toEqual({ HOME: "/tmp/home" }); + + const envWithPath = { PATH: "/usr/bin" }; + applyPathPrepend(envWithPath, [], { requireExisting: true }); + applyPathPrepend(envWithPath, undefined, { requireExisting: true }); + expect(envWithPath).toEqual({ PATH: "/usr/bin" }); + }); + + it("creates PATH when prepends are provided and no path key exists", () => { + const env = { HOME: "/tmp/home" }; + + applyPathPrepend(env, ["/custom/bin"]); + + expect(env).toEqual({ + HOME: "/tmp/home", + PATH: "/custom/bin", + }); + }); });