|
| 1 | +import { assert } from 'chai' |
| 2 | +import { describe, it, beforeEach, afterEach } from 'mocha' |
| 3 | + |
| 4 | +// Inline the function for testing without vscode dependency |
| 5 | +function resolveEnvVariables(value: string): string { |
| 6 | + return value.replace(/\$\{env:([^}]+)\}/g, (match, envVar) => { |
| 7 | + const envValue = process.env[envVar] |
| 8 | + return envValue !== undefined ? envValue : match |
| 9 | + }) |
| 10 | +} |
| 11 | + |
| 12 | +describe('Environment Variable Resolution', () => { |
| 13 | + let originalEnv: NodeJS.ProcessEnv |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + originalEnv = { ...process.env } |
| 17 | + }) |
| 18 | + |
| 19 | + afterEach(() => { |
| 20 | + process.env = originalEnv |
| 21 | + }) |
| 22 | + |
| 23 | + it('should resolve ${env:VAR_NAME} with existing environment variable', () => { |
| 24 | + process.env.TEST_VAR = '/test/path' |
| 25 | + const result = resolveEnvVariables('${env:TEST_VAR}/subdir') |
| 26 | + assert.equal(result, '/test/path/subdir') |
| 27 | + }) |
| 28 | + |
| 29 | + it('should keep ${env:VAR_NAME} if environment variable does not exist', () => { |
| 30 | + delete process.env.NONEXISTENT_VAR |
| 31 | + const result = resolveEnvVariables('${env:NONEXISTENT_VAR}/subdir') |
| 32 | + assert.equal(result, '${env:NONEXISTENT_VAR}/subdir') |
| 33 | + }) |
| 34 | + |
| 35 | + it('should resolve multiple environment variables', () => { |
| 36 | + process.env.VAR1 = '/path1' |
| 37 | + process.env.VAR2 = '/path2' |
| 38 | + const result = resolveEnvVariables('${env:VAR1}/${env:VAR2}') |
| 39 | + assert.equal(result, '/path1//path2') |
| 40 | + }) |
| 41 | + |
| 42 | + it('should handle text without environment variables', () => { |
| 43 | + const result = resolveEnvVariables('/var/www/html') |
| 44 | + assert.equal(result, '/var/www/html') |
| 45 | + }) |
| 46 | + |
| 47 | + it('should handle environment variables with underscores and numbers', () => { |
| 48 | + process.env.MY_VAR_123 = '/custom/path' |
| 49 | + const result = resolveEnvVariables('${env:MY_VAR_123}') |
| 50 | + assert.equal(result, '/custom/path') |
| 51 | + }) |
| 52 | + |
| 53 | + it('should handle empty environment variable value', () => { |
| 54 | + process.env.EMPTY_VAR = '' |
| 55 | + const result = resolveEnvVariables('${env:EMPTY_VAR}/test') |
| 56 | + assert.equal(result, '/test') |
| 57 | + }) |
| 58 | + |
| 59 | + it('should handle mixed content', () => { |
| 60 | + process.env.DOCKER_ROOT = '/var/www/html' |
| 61 | + const result = resolveEnvVariables('prefix/${env:DOCKER_ROOT}/suffix') |
| 62 | + assert.equal(result, 'prefix//var/www/html/suffix') |
| 63 | + }) |
| 64 | + |
| 65 | + it('should handle pathMapping use case', () => { |
| 66 | + process.env.DOCKER_WEB_ROOT = '/var/www/html' |
| 67 | + process.env.LOCAL_PROJECT = '/Users/developer/myproject' |
| 68 | + |
| 69 | + const serverPath = '${env:DOCKER_WEB_ROOT}' |
| 70 | + const localPath = '${env:LOCAL_PROJECT}' |
| 71 | + |
| 72 | + assert.equal(resolveEnvVariables(serverPath), '/var/www/html') |
| 73 | + assert.equal(resolveEnvVariables(localPath), '/Users/developer/myproject') |
| 74 | + }) |
| 75 | +}) |
0 commit comments