|
| 1 | +import {describe, expect, it} from 'vitest'; |
| 2 | + |
| 3 | +import {interpolateEnvironmentVariables} from '../../app/common/renderer/utils/env-utils'; |
| 4 | + |
| 5 | +describe('interpolateEnvironmentVariables', function () { |
| 6 | + const envVars = [ |
| 7 | + {key: 'HOME', value: '/home/user'}, |
| 8 | + {key: 'PATH', value: '/usr/bin'}, |
| 9 | + {key: 'APP_ENV', value: 'test'}, |
| 10 | + ]; |
| 11 | + |
| 12 | + it('should interpolate environment variables in strings', function () { |
| 13 | + const input = 'My home is ${HOME} and path is ${PATH}'; |
| 14 | + const expected = 'My home is /home/user and path is /usr/bin'; |
| 15 | + expect(interpolateEnvironmentVariables(input, envVars)).to.equal(expected); |
| 16 | + }); |
| 17 | + |
| 18 | + it('should leave unmatched variables unchanged', function () { |
| 19 | + const input = 'Value: ${UNKNOWN_VAR}'; |
| 20 | + expect(interpolateEnvironmentVariables(input, envVars)).to.equal('Value: ${UNKNOWN_VAR}'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should interpolate variables in arrays', function () { |
| 24 | + const input = ['${HOME}/files', '${PATH}/python']; |
| 25 | + const expected = ['/home/user/files', '/usr/bin/python']; |
| 26 | + expect(interpolateEnvironmentVariables(input, envVars)).to.deep.equal(expected); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should interpolate variables in nested objects', function () { |
| 30 | + const input = { |
| 31 | + home: '${HOME}', |
| 32 | + paths: { |
| 33 | + bin: '${PATH}', |
| 34 | + config: '${HOME}/.config', |
| 35 | + }, |
| 36 | + env: '${APP_ENV}', |
| 37 | + }; |
| 38 | + const expected = { |
| 39 | + home: '/home/user', |
| 40 | + paths: { |
| 41 | + bin: '/usr/bin', |
| 42 | + config: '/home/user/.config', |
| 43 | + }, |
| 44 | + env: 'test', |
| 45 | + }; |
| 46 | + expect(interpolateEnvironmentVariables(input, envVars)).to.deep.equal(expected); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should handle null values', function () { |
| 50 | + expect(interpolateEnvironmentVariables(null, envVars)).to.equal(null); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should handle undefined values', function () { |
| 54 | + expect(interpolateEnvironmentVariables(undefined, envVars)).to.equal(undefined); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle number values', function () { |
| 58 | + expect(interpolateEnvironmentVariables(42, envVars)).to.equal(42); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should handle boolean values', function () { |
| 62 | + expect(interpolateEnvironmentVariables(true, envVars)).to.equal(true); |
| 63 | + expect(interpolateEnvironmentVariables(false, envVars)).to.equal(false); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should handle empty arrays', function () { |
| 67 | + expect(interpolateEnvironmentVariables([], envVars)).to.deep.equal([]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should handle empty objects', function () { |
| 71 | + expect(interpolateEnvironmentVariables({}, envVars)).to.deep.equal({}); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should handle multiple variables in single string', function () { |
| 75 | + const input = '${HOME}:${PATH}:${APP_ENV}'; |
| 76 | + const expected = '/home/user:/usr/bin:test'; |
| 77 | + expect(interpolateEnvironmentVariables(input, envVars)).to.equal(expected); |
| 78 | + }); |
| 79 | +}); |
0 commit comments