|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 4 | + *--------------------------------------------------------*/ |
| 5 | + |
| 6 | +import * as assert from 'assert'; |
| 7 | +import * as path from 'path'; |
| 8 | +import * as fs from 'fs'; |
| 9 | +import * as os from 'os'; |
| 10 | +import { parseEnvFiles } from '../../src/utils/envUtils'; |
| 11 | + |
| 12 | +suite('parseEnvFiles Tests', () => { |
| 13 | + let tmpDir: string; |
| 14 | + |
| 15 | + setup(() => { |
| 16 | + tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'go-test-env-')); |
| 17 | + }); |
| 18 | + |
| 19 | + teardown(() => { |
| 20 | + if (tmpDir && fs.existsSync(tmpDir)) { |
| 21 | + // Use rmdir for compatibility with older Node.js versions |
| 22 | + const rimraf = (dir: string) => { |
| 23 | + if (fs.existsSync(dir)) { |
| 24 | + fs.readdirSync(dir).forEach((file) => { |
| 25 | + const curPath = path.join(dir, file); |
| 26 | + if (fs.lstatSync(curPath).isDirectory()) { |
| 27 | + rimraf(curPath); |
| 28 | + } else { |
| 29 | + fs.unlinkSync(curPath); |
| 30 | + } |
| 31 | + }); |
| 32 | + fs.rmdirSync(dir); |
| 33 | + } |
| 34 | + }; |
| 35 | + rimraf(tmpDir); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + test('should handle empty array', () => { |
| 40 | + const result = parseEnvFiles([]); |
| 41 | + assert.deepStrictEqual(result, {}); |
| 42 | + }); |
| 43 | + |
| 44 | + test('should handle undefined input', () => { |
| 45 | + const result = parseEnvFiles(undefined); |
| 46 | + assert.deepStrictEqual(result, {}); |
| 47 | + }); |
| 48 | + |
| 49 | + test('should handle single string input', () => { |
| 50 | + const envFile = path.join(tmpDir, 'single.env'); |
| 51 | + fs.writeFileSync(envFile, 'SINGLE_VAR=single_value'); |
| 52 | + |
| 53 | + const result = parseEnvFiles(envFile); |
| 54 | + assert.strictEqual(result.SINGLE_VAR, 'single_value'); |
| 55 | + }); |
| 56 | + |
| 57 | + test('should handle array of files', () => { |
| 58 | + const envFile1 = path.join(tmpDir, 'first.env'); |
| 59 | + const envFile2 = path.join(tmpDir, 'second.env'); |
| 60 | + |
| 61 | + fs.writeFileSync(envFile1, 'VAR1=value1\nSHARED=from_first'); |
| 62 | + fs.writeFileSync(envFile2, 'VAR2=value2\nSHARED=from_second'); |
| 63 | + |
| 64 | + const result = parseEnvFiles([envFile1, envFile2]); |
| 65 | + |
| 66 | + assert.strictEqual(result.VAR1, 'value1'); |
| 67 | + assert.strictEqual(result.VAR2, 'value2'); |
| 68 | + // Later files should override earlier ones |
| 69 | + assert.strictEqual(result.SHARED, 'from_second'); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should handle mixed valid and invalid files', () => { |
| 73 | + const validFile = path.join(tmpDir, 'valid.env'); |
| 74 | + const invalidFile = path.join(tmpDir, 'nonexistent.env'); |
| 75 | + |
| 76 | + fs.writeFileSync(validFile, 'VALID_VAR=valid_value'); |
| 77 | + |
| 78 | + // This should throw when trying to parse invalid file |
| 79 | + assert.throws(() => { |
| 80 | + parseEnvFiles([validFile, invalidFile]); |
| 81 | + }); |
| 82 | + }); |
| 83 | +}); |
0 commit comments