Skip to content

Commit 8048165

Browse files
committed
extension/settings: add support for multiple test env files
New Setting: go.testEnvFiles Added go.testEnvFiles setting that accepts an array of environment file paths Allows loading multiple .env files in a specific order for test execution Later files in the array override variables from earlier files Backward Compatibility Existing go.testEnvFile setting continues to work as before Marked go.testEnvFile as deprecated with appropriate VS Code deprecation messaging When both settings are used together, go.testEnvFile is processed after all files in go.testEnvFiles Fixes #2473
1 parent 57683f2 commit 8048165

File tree

4 files changed

+94
-7
lines changed

4 files changed

+94
-7
lines changed

extension/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

extension/package.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,7 +1438,17 @@
14381438
"go.testEnvFile": {
14391439
"type": "string",
14401440
"default": null,
1441-
"description": "Absolute path to a file containing environment variables definitions. File contents should be of the form key=value.",
1441+
"description": "Absolute path to a file containing environment variables definitions. File contents should be of the form key=value. (Deprecated: Use go.testEnvFiles instead)",
1442+
"scope": "resource",
1443+
"deprecationMessage": "Use go.testEnvFiles instead"
1444+
},
1445+
"go.testEnvFiles": {
1446+
"type": "array",
1447+
"items": {
1448+
"type": "string"
1449+
},
1450+
"default": [],
1451+
"description": "Array of absolute paths to files containing environment variables definitions. File contents should be of the form key=value.",
14421452
"scope": "resource"
14431453
},
14441454
"go.testFlags": {

extension/src/testUtils.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { getCurrentPackage } from './goModules';
1919
import { GoDocumentSymbolProvider } from './goDocumentSymbols';
2020
import { getNonVendorPackages } from './goPackages';
2121
import { getBinPath, getCurrentGoPath, getTempFilePath, LineBuffer, resolvePath } from './util';
22-
import { parseEnvFile } from './utils/envUtils';
22+
import { parseEnvFile, parseEnvFiles } from './utils/envUtils';
2323
import {
2424
getEnvPath,
2525
expandFilePathInOutput,
@@ -111,12 +111,28 @@ export function getTestEnvVars(config: vscode.WorkspaceConfiguration): any {
111111
const envVars = toolExecutionEnvironment();
112112
const testEnvConfig = config['testEnvVars'] || {};
113113

114-
let fileEnv: { [key: string]: any } = {};
115-
let testEnvFile = config['testEnvFile'];
114+
// Collect environment files from both settings
115+
const envFiles: string[] = [];
116+
117+
// Add files from the new testEnvFiles setting (array)
118+
const testEnvFiles = config['testEnvFiles'] || [];
119+
if (Array.isArray(testEnvFiles)) {
120+
envFiles.push(...testEnvFiles);
121+
}
122+
123+
// Add the deprecated testEnvFile setting (single file) for backward compatibility
124+
const testEnvFile = config['testEnvFile'];
116125
if (testEnvFile) {
117-
testEnvFile = resolvePath(testEnvFile);
126+
envFiles.push(testEnvFile);
127+
}
128+
129+
// Parse all environment files
130+
let fileEnv: { [key: string]: any } = {};
131+
if (envFiles.length > 0) {
118132
try {
119-
fileEnv = parseEnvFile(testEnvFile, envVars);
133+
// Resolve paths for all files
134+
const resolvedFiles = envFiles.map((file) => resolvePath(file));
135+
fileEnv = parseEnvFiles(resolvedFiles, envVars);
120136
} catch (e) {
121137
console.log(e);
122138
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 assert from 'assert';
7+
import path from 'path';
8+
import fs from 'fs';
9+
import 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+
fs.rmdirSync(tmpDir, { recursive: true });
22+
}
23+
});
24+
25+
test('should handle empty array', () => {
26+
const result = parseEnvFiles([]);
27+
assert.deepStrictEqual(result, {});
28+
});
29+
30+
test('should handle undefined input', () => {
31+
const result = parseEnvFiles(undefined);
32+
assert.deepStrictEqual(result, {});
33+
});
34+
35+
test('should handle array of files', () => {
36+
const envFile1 = path.join(tmpDir, 'first.env');
37+
const envFile2 = path.join(tmpDir, 'second.env');
38+
39+
fs.writeFileSync(envFile1, 'VAR1=value1\nSHARED=from_first');
40+
fs.writeFileSync(envFile2, 'VAR2=value2\nSHARED=from_second');
41+
42+
const result = parseEnvFiles([envFile1, envFile2]);
43+
44+
assert.strictEqual(result.VAR1, 'value1');
45+
assert.strictEqual(result.VAR2, 'value2');
46+
// Later files should override earlier ones
47+
assert.strictEqual(result.SHARED, 'from_second');
48+
});
49+
50+
test('should handle mixed valid and invalid files', () => {
51+
const validFile = path.join(tmpDir, 'valid.env');
52+
const invalidFile = path.join(tmpDir, 'nonexistent.env');
53+
54+
fs.writeFileSync(validFile, 'VALID_VAR=valid_value');
55+
56+
// This should throw when trying to parse invalid file
57+
assert.throws(() => {
58+
parseEnvFiles([validFile, invalidFile]);
59+
});
60+
});
61+
});

0 commit comments

Comments
 (0)