|
1 | 1 | import path from "node:path"; |
2 | 2 | import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 3 | +import { applyPathPrepend, findPathKey } from "../infra/path-prepend.js"; |
3 | 4 | import { peekSystemEvents, resetSystemEventsForTest } from "../infra/system-events.js"; |
4 | 5 | import { captureEnv } from "../test-utils/env.js"; |
5 | 6 | import { getFinishedSession, resetProcessRegistryForTests } from "./bash-process-registry.js"; |
@@ -547,3 +548,57 @@ describe("exec PATH handling", () => { |
547 | 548 | } |
548 | 549 | }); |
549 | 550 | }); |
| 551 | + |
| 552 | +describe("findPathKey", () => { |
| 553 | + it("returns PATH when key is uppercase", () => { |
| 554 | + expect(findPathKey({ PATH: "/usr/bin" })).toBe("PATH"); |
| 555 | + }); |
| 556 | + |
| 557 | + it("returns Path when key is mixed-case (Windows style)", () => { |
| 558 | + expect(findPathKey({ Path: "C:\\Windows\\System32" })).toBe("Path"); |
| 559 | + }); |
| 560 | + |
| 561 | + it("returns PATH as default when no PATH-like key exists", () => { |
| 562 | + expect(findPathKey({ HOME: "/home/user" })).toBe("PATH"); |
| 563 | + }); |
| 564 | + |
| 565 | + it("prefers uppercase PATH when both PATH and Path exist", () => { |
| 566 | + expect(findPathKey({ PATH: "/usr/bin", Path: "C:\\Windows" })).toBe("PATH"); |
| 567 | + }); |
| 568 | +}); |
| 569 | + |
| 570 | +describe("applyPathPrepend with case-insensitive PATH key", () => { |
| 571 | + it("prepends to Path key on Windows-style env (no uppercase PATH)", () => { |
| 572 | + const env: Record<string, string> = { Path: "C:\\Windows\\System32" }; |
| 573 | + applyPathPrepend(env, ["C:\\custom\\bin"]); |
| 574 | + // Should write back to the same `Path` key, not create a new `PATH` |
| 575 | + expect(env.Path).toContain("C:\\custom\\bin"); |
| 576 | + expect(env.Path).toContain("C:\\Windows\\System32"); |
| 577 | + expect("PATH" in env).toBe(false); |
| 578 | + }); |
| 579 | + |
| 580 | + it("preserves all existing entries when prepending via Path key", () => { |
| 581 | + // Use platform-appropriate paths and delimiters |
| 582 | + const delim = path.delimiter; |
| 583 | + const existing = isWin |
| 584 | + ? ["C:\\Windows\\System32", "C:\\Windows", "C:\\Program Files\\nodejs"] |
| 585 | + : ["/usr/bin", "/usr/local/bin", "/opt/node/bin"]; |
| 586 | + const prepend = isWin ? ["C:\\custom\\bin"] : ["/custom/bin"]; |
| 587 | + const existingPath = existing.join(delim); |
| 588 | + const env: Record<string, string> = { Path: existingPath }; |
| 589 | + applyPathPrepend(env, prepend); |
| 590 | + const parts = env.Path.split(delim); |
| 591 | + expect(parts[0]).toBe(prepend[0]); |
| 592 | + for (const entry of existing) { |
| 593 | + expect(parts).toContain(entry); |
| 594 | + } |
| 595 | + }); |
| 596 | + |
| 597 | + it("respects requireExisting option with Path key", () => { |
| 598 | + const env: Record<string, string> = { HOME: "/home/user" }; |
| 599 | + applyPathPrepend(env, ["C:\\custom\\bin"], { requireExisting: true }); |
| 600 | + // No Path/PATH key exists, so nothing should be written |
| 601 | + expect("PATH" in env).toBe(false); |
| 602 | + expect("Path" in env).toBe(false); |
| 603 | + }); |
| 604 | +}); |
0 commit comments