|
1 | | -import {injectGclVariableEnvVars} from "../../../src/argv.js"; |
| 1 | +import {injectGclVariableEnvVars, injectGclEnvVars} from "../../../src/argv.js"; |
2 | 2 | import {execFile} from "child_process"; |
3 | 3 | import {promisify} from "util"; |
4 | 4 |
|
@@ -75,6 +75,89 @@ describe("injectGclVariableEnvVars unit tests", () => { |
75 | 75 | }); |
76 | 76 | }); |
77 | 77 |
|
| 78 | +describe("injectGclEnvVars unit tests", () => { |
| 79 | + const baseOptions = { |
| 80 | + array: ["volume"], |
| 81 | + boolean: ["quiet"], |
| 82 | + number: ["concurrency"], |
| 83 | + default: {quiet: false, concurrency: 0, cwd: "."}, |
| 84 | + key: {quiet: true, concurrency: true, cwd: true, volume: true, _: true, $0: true, "some-kebab": true}, |
| 85 | + }; |
| 86 | + |
| 87 | + test("injects string env var", () => { |
| 88 | + const argv: Record<string, any> = {cwd: ".", quiet: false}; |
| 89 | + injectGclEnvVars(argv, baseOptions, {"GCL_CWD": "/tmp/test"}, {cwd: true, quiet: true}); |
| 90 | + expect(argv.cwd).toBe("/tmp/test"); |
| 91 | + }); |
| 92 | + |
| 93 | + test("injects boolean env var", () => { |
| 94 | + const argv: Record<string, any> = {quiet: false}; |
| 95 | + injectGclEnvVars(argv, baseOptions, {"GCL_QUIET": "true"}, {quiet: true}); |
| 96 | + expect(argv.quiet).toBe(true); |
| 97 | + }); |
| 98 | + |
| 99 | + test("injects boolean env var from '1'", () => { |
| 100 | + const argv: Record<string, any> = {quiet: false}; |
| 101 | + injectGclEnvVars(argv, baseOptions, {"GCL_QUIET": "1"}, {quiet: true}); |
| 102 | + expect(argv.quiet).toBe(true); |
| 103 | + }); |
| 104 | + |
| 105 | + test("injects number env var", () => { |
| 106 | + const argv: Record<string, any> = {concurrency: 0}; |
| 107 | + injectGclEnvVars(argv, baseOptions, {"GCL_CONCURRENCY": "4"}, {concurrency: true}); |
| 108 | + expect(argv.concurrency).toBe(4); |
| 109 | + }); |
| 110 | + |
| 111 | + test("splits array env var on semicolons", () => { |
| 112 | + const argv: Record<string, any> = {volume: []}; |
| 113 | + injectGclEnvVars(argv, baseOptions, {"GCL_VOLUME": "/a:/b;/c:/d"}, {volume: true}); |
| 114 | + expect(argv.volume).toEqual(["/a:/b", "/c:/d"]); |
| 115 | + }); |
| 116 | + |
| 117 | + test("merges array env var with CLI values", () => { |
| 118 | + const argv: Record<string, any> = {volume: ["/cli:/path"]}; |
| 119 | + injectGclEnvVars(argv, baseOptions, {"GCL_VOLUME": "/env:/path"}, {}); |
| 120 | + expect(argv.volume).toEqual(["/env:/path", "/cli:/path"]); |
| 121 | + }); |
| 122 | + |
| 123 | + test("CLI explicit value takes precedence over env", () => { |
| 124 | + const argv: Record<string, any> = {concurrency: 8}; |
| 125 | + injectGclEnvVars(argv, baseOptions, {"GCL_CONCURRENCY": "4"}, {}); |
| 126 | + expect(argv.concurrency).toBe(8); |
| 127 | + }); |
| 128 | + |
| 129 | + test("CLI explicit value takes precedence even when matching default", () => { |
| 130 | + const argv: Record<string, any> = {concurrency: 0}; |
| 131 | + injectGclEnvVars(argv, baseOptions, {"GCL_CONCURRENCY": "4"}, {}); |
| 132 | + expect(argv.concurrency).toBe(0); |
| 133 | + }); |
| 134 | + |
| 135 | + test("env overrides when value was defaulted", () => { |
| 136 | + const argv: Record<string, any> = {concurrency: 0}; |
| 137 | + injectGclEnvVars(argv, baseOptions, {"GCL_CONCURRENCY": "4"}, {concurrency: true}); |
| 138 | + expect(argv.concurrency).toBe(4); |
| 139 | + }); |
| 140 | + |
| 141 | + test("skips keys with hyphens", () => { |
| 142 | + const argv: Record<string, any> = {}; |
| 143 | + injectGclEnvVars(argv, baseOptions, {"GCL_SOME_KEBAB": "val"}, {}); |
| 144 | + expect(argv["some-kebab"]).toBeUndefined(); |
| 145 | + }); |
| 146 | + |
| 147 | + test("skips _ and $0 keys", () => { |
| 148 | + const argv: Record<string, any> = {_: [], $0: "bin"}; |
| 149 | + injectGclEnvVars(argv, baseOptions, {"GCL__": "x", "GCL_$0": "y"}, {}); |
| 150 | + expect(argv._).toEqual([]); |
| 151 | + expect(argv.$0).toBe("bin"); |
| 152 | + }); |
| 153 | + |
| 154 | + test("skips undefined env values", () => { |
| 155 | + const argv: Record<string, any> = {quiet: false}; |
| 156 | + injectGclEnvVars(argv, baseOptions, {"GCL_QUIET": undefined}, {quiet: true}); |
| 157 | + expect(argv.quiet).toBe(false); |
| 158 | + }); |
| 159 | +}); |
| 160 | + |
78 | 161 | test("GCL_VARIABLE_* env vars are injected into job output via CLI", async () => { |
79 | 162 | const {stdout} = await execFileAsync("bun", ["src/index.ts", "test-job", "--cwd", "tests/test-cases/gcl-variable-env"], { |
80 | 163 | env: { |
|
0 commit comments