|
1 | | -import { pickEnvVars } from './env-utils'; |
| 1 | +import * as fs from 'fs'; |
| 2 | +import * as os from 'os'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { WrapperConfig } from './types'; |
| 5 | +import { getConfigEnvValue, getLowerCaseProcessEnvValue, pickEnvVars } from './env-utils'; |
| 6 | + |
| 7 | +function makeWrapperConfig(overrides: Partial<WrapperConfig> = {}): WrapperConfig { |
| 8 | + return { |
| 9 | + agentCommand: 'echo test', |
| 10 | + allowedDomains: [], |
| 11 | + keepContainers: false, |
| 12 | + logLevel: 'info', |
| 13 | + workDir: '/tmp/env-utils-test', |
| 14 | + ...overrides, |
| 15 | + }; |
| 16 | +} |
2 | 17 |
|
3 | 18 | describe('pickEnvVars', () => { |
4 | 19 | let savedEnv: Record<string, string | undefined>; |
@@ -61,3 +76,76 @@ describe('pickEnvVars', () => { |
61 | 76 | expect(pickEnvVars('TEST_PICK_SINGLE')).toEqual({ TEST_PICK_SINGLE: 'only-one' }); |
62 | 77 | }); |
63 | 78 | }); |
| 79 | + |
| 80 | +describe('getConfigEnvValue', () => { |
| 81 | + let savedEnv: string | undefined; |
| 82 | + |
| 83 | + beforeEach(() => { |
| 84 | + savedEnv = process.env.TEST_CONFIG_ENV_VALUE; |
| 85 | + delete process.env.TEST_CONFIG_ENV_VALUE; |
| 86 | + }); |
| 87 | + |
| 88 | + afterEach(() => { |
| 89 | + if (savedEnv !== undefined) { |
| 90 | + process.env.TEST_CONFIG_ENV_VALUE = savedEnv; |
| 91 | + } else { |
| 92 | + delete process.env.TEST_CONFIG_ENV_VALUE; |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + it('prefers additionalEnv over envFile and process.env, trimming the result', () => { |
| 97 | + const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'env-utils-')); |
| 98 | + const envFilePath = path.join(tempDir, '.env'); |
| 99 | + fs.writeFileSync(envFilePath, 'TEST_CONFIG_ENV_VALUE= from-file \n'); |
| 100 | + process.env.TEST_CONFIG_ENV_VALUE = ' from-process '; |
| 101 | + |
| 102 | + try { |
| 103 | + const config = makeWrapperConfig({ |
| 104 | + additionalEnv: { TEST_CONFIG_ENV_VALUE: ' from-additional ' }, |
| 105 | + envAll: true, |
| 106 | + envFile: envFilePath, |
| 107 | + }); |
| 108 | + |
| 109 | + expect(getConfigEnvValue(config, 'TEST_CONFIG_ENV_VALUE')).toBe('from-additional'); |
| 110 | + } finally { |
| 111 | + fs.rmSync(tempDir, { recursive: true, force: true }); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + it('falls back to process.env only when envAll is enabled and omits blank values', () => { |
| 116 | + process.env.TEST_CONFIG_ENV_VALUE = ' '; |
| 117 | + const config = makeWrapperConfig({ envAll: true }); |
| 118 | + expect(getConfigEnvValue(config, 'TEST_CONFIG_ENV_VALUE')).toBeUndefined(); |
| 119 | + |
| 120 | + process.env.TEST_CONFIG_ENV_VALUE = ' from-process '; |
| 121 | + expect(getConfigEnvValue(config, 'TEST_CONFIG_ENV_VALUE')).toBe('from-process'); |
| 122 | + expect(getConfigEnvValue(makeWrapperConfig({ envAll: false }), 'TEST_CONFIG_ENV_VALUE')).toBeUndefined(); |
| 123 | + }); |
| 124 | +}); |
| 125 | + |
| 126 | +describe('getLowerCaseProcessEnvValue', () => { |
| 127 | + let savedEnv: string | undefined; |
| 128 | + |
| 129 | + beforeEach(() => { |
| 130 | + savedEnv = process.env.TEST_LOWERCASE_ENV_VALUE; |
| 131 | + delete process.env.TEST_LOWERCASE_ENV_VALUE; |
| 132 | + }); |
| 133 | + |
| 134 | + afterEach(() => { |
| 135 | + if (savedEnv !== undefined) { |
| 136 | + process.env.TEST_LOWERCASE_ENV_VALUE = savedEnv; |
| 137 | + } else { |
| 138 | + delete process.env.TEST_LOWERCASE_ENV_VALUE; |
| 139 | + } |
| 140 | + }); |
| 141 | + |
| 142 | + it('trims and lowercases process env values', () => { |
| 143 | + process.env.TEST_LOWERCASE_ENV_VALUE = ' GitHub-OIDC '; |
| 144 | + expect(getLowerCaseProcessEnvValue('TEST_LOWERCASE_ENV_VALUE')).toBe('github-oidc'); |
| 145 | + }); |
| 146 | + |
| 147 | + it('returns undefined for blank process env values', () => { |
| 148 | + process.env.TEST_LOWERCASE_ENV_VALUE = ' '; |
| 149 | + expect(getLowerCaseProcessEnvValue('TEST_LOWERCASE_ENV_VALUE')).toBeUndefined(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments