|
| 1 | +import {stripGclVariableEnvVars, injectGclVariableEnvVars} from "../../../src/argv.js"; |
| 2 | +import {execFile} from "child_process"; |
| 3 | +import {promisify} from "util"; |
| 4 | + |
| 5 | +const execFileAsync = promisify(execFile); |
| 6 | + |
| 7 | +describe("stripGclVariableEnvVars", () => { |
| 8 | + test("strips GCL_VARIABLE_* entries and returns them", () => { |
| 9 | + const env: Record<string, string | undefined> = { |
| 10 | + "HOME": "/home/user", |
| 11 | + "GCL_CWD": "/tmp", |
| 12 | + "GCL_VARIABLE_MY_VAR": "hello", |
| 13 | + "GCL_VARIABLE_OTHER": "world", |
| 14 | + }; |
| 15 | + const stripped = stripGclVariableEnvVars(env); |
| 16 | + expect(stripped).toEqual({ |
| 17 | + "GCL_VARIABLE_MY_VAR": "hello", |
| 18 | + "GCL_VARIABLE_OTHER": "world", |
| 19 | + }); |
| 20 | + expect(env["GCL_VARIABLE_MY_VAR"]).toBeUndefined(); |
| 21 | + expect(env["GCL_VARIABLE_OTHER"]).toBeUndefined(); |
| 22 | + expect(env["HOME"]).toBe("/home/user"); |
| 23 | + expect(env["GCL_CWD"]).toBe("/tmp"); |
| 24 | + }); |
| 25 | + |
| 26 | + test("strips GCL_VARIABLE_ with empty name from env but does not return it", () => { |
| 27 | + const env: Record<string, string | undefined> = {"GCL_VARIABLE_": "empty"}; |
| 28 | + const stripped = stripGclVariableEnvVars(env); |
| 29 | + expect(stripped).toEqual({}); |
| 30 | + expect(env["GCL_VARIABLE_"]).toBeUndefined(); |
| 31 | + }); |
| 32 | + |
| 33 | + test("skips null/undefined values", () => { |
| 34 | + const env: Record<string, string | undefined> = {"GCL_VARIABLE_FOO": undefined}; |
| 35 | + const stripped = stripGclVariableEnvVars(env); |
| 36 | + expect(stripped).toEqual({}); |
| 37 | + }); |
| 38 | + |
| 39 | + test("returns empty object when no matches", () => { |
| 40 | + const env: Record<string, string | undefined> = {"HOME": "/home/user", "GCL_CWD": "/tmp"}; |
| 41 | + const stripped = stripGclVariableEnvVars(env); |
| 42 | + expect(stripped).toEqual({}); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe("injectGclVariableEnvVars", () => { |
| 47 | + test("injects single entry", () => { |
| 48 | + const argv: {variable?: string[]} = {}; |
| 49 | + injectGclVariableEnvVars(argv, {"GCL_VARIABLE_MY_VAR": "hello"}); |
| 50 | + expect(argv.variable).toEqual(["MY_VAR=hello"]); |
| 51 | + }); |
| 52 | + |
| 53 | + test("injects multiple entries", () => { |
| 54 | + const argv: {variable?: string[]} = {}; |
| 55 | + injectGclVariableEnvVars(argv, { |
| 56 | + "GCL_VARIABLE_VAR1": "one", |
| 57 | + "GCL_VARIABLE_VAR2": "two", |
| 58 | + }); |
| 59 | + expect(argv.variable).toEqual(expect.arrayContaining(["VAR1=one", "VAR2=two"])); |
| 60 | + expect(argv.variable).toHaveLength(2); |
| 61 | + }); |
| 62 | + |
| 63 | + test("prepends to existing variable array", () => { |
| 64 | + const argv: {variable?: string[]} = {variable: ["EXISTING=val"]}; |
| 65 | + injectGclVariableEnvVars(argv, {"GCL_VARIABLE_NEW": "injected"}); |
| 66 | + expect(argv.variable).toEqual(["NEW=injected", "EXISTING=val"]); |
| 67 | + }); |
| 68 | + |
| 69 | + test("CLI --variable takes precedence over GCL_VARIABLE_", () => { |
| 70 | + const argv: {variable?: string[]} = {variable: ["SAME=from_cli"]}; |
| 71 | + injectGclVariableEnvVars(argv, {"GCL_VARIABLE_SAME": "from_env"}); |
| 72 | + // "SAME=from_env" is prepended, "SAME=from_cli" comes last and wins |
| 73 | + expect(argv.variable).toEqual(["SAME=from_env", "SAME=from_cli"]); |
| 74 | + }); |
| 75 | + |
| 76 | + test("handles empty value", () => { |
| 77 | + const argv: {variable?: string[]} = {}; |
| 78 | + injectGclVariableEnvVars(argv, {"GCL_VARIABLE_FOO": ""}); |
| 79 | + expect(argv.variable).toEqual(["FOO="]); |
| 80 | + }); |
| 81 | + |
| 82 | + test("handles value with equals sign", () => { |
| 83 | + const argv: {variable?: string[]} = {}; |
| 84 | + injectGclVariableEnvVars(argv, {"GCL_VARIABLE_FOO": "key=value"}); |
| 85 | + expect(argv.variable).toEqual(["FOO=key=value"]); |
| 86 | + }); |
| 87 | + |
| 88 | + test("does not split on semicolons", () => { |
| 89 | + const argv: {variable?: string[]} = {}; |
| 90 | + injectGclVariableEnvVars(argv, {"GCL_VARIABLE_FOO": "a;b;c"}); |
| 91 | + expect(argv.variable).toEqual(["FOO=a;b;c"]); |
| 92 | + }); |
| 93 | +}); |
| 94 | + |
| 95 | +test("GCL_VARIABLE_* env vars are injected into job output via CLI", async () => { |
| 96 | + const {stdout} = await execFileAsync("bun", ["src/index.ts", "test-job", "--cwd", "tests/test-cases/gcl-variable-env"], { |
| 97 | + env: { |
| 98 | + ...process.env, |
| 99 | + GCL_VARIABLE_MY_VAR: "hello", |
| 100 | + GCL_VARIABLE_ANOTHER_VAR: "world", |
| 101 | + }, |
| 102 | + }); |
| 103 | + |
| 104 | + expect(stdout).toContain("hello"); |
| 105 | + expect(stdout).toContain("world"); |
| 106 | +}, 30_000); |
0 commit comments