Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

add support for multiple test env files #3846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jfoshee wants to merge 8 commits into golang:master
base: master
Choose a base branch
Loading
from jfoshee:master
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extension/package-lock.json
View file Open in desktop

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion extension/package.json
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,17 @@
"go.testEnvFile": {
"type": "string",
"default": null,
"description": "Absolute path to a file containing environment variables definitions. File contents should be of the form key=value.",
"description": "Absolute path to a file containing environment variables definitions. File contents should be of the form key=value. (Deprecated: Use go.testEnvFiles instead)",
"scope": "resource",
"deprecationMessage": "Use go.testEnvFiles instead"
},
"go.testEnvFiles": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"description": "Array of absolute paths to files containing environment variables definitions. File contents should be of the form key=value.",
"scope": "resource"
},
"go.testFlags": {
Expand Down
26 changes: 21 additions & 5 deletions extension/src/testUtils.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getCurrentPackage } from './goModules';
import { GoDocumentSymbolProvider } from './goDocumentSymbols';
import { getNonVendorPackages } from './goPackages';
import { getBinPath, getCurrentGoPath, getTempFilePath, LineBuffer, resolvePath } from './util';
import { parseEnvFile } from './utils/envUtils';
import { parseEnvFile, parseEnvFiles } from './utils/envUtils';
import {
getEnvPath,
expandFilePathInOutput,
Expand Down Expand Up @@ -111,12 +111,28 @@ export function getTestEnvVars(config: vscode.WorkspaceConfiguration): any {
const envVars = toolExecutionEnvironment();
const testEnvConfig = config['testEnvVars'] || {};

let fileEnv: { [key: string]: any } = {};
let testEnvFile = config['testEnvFile'];
// Collect environment files from both settings
const envFiles: string[] = [];

// Add files from the new testEnvFiles setting (array)
const testEnvFiles = config['testEnvFiles'] || [];
if (Array.isArray(testEnvFiles)) {
envFiles.push(...testEnvFiles);
}

// Add the deprecated testEnvFile setting (single file) for backward compatibility
const testEnvFile = config['testEnvFile'];
if (testEnvFile) {
testEnvFile = resolvePath(testEnvFile);
envFiles.push(testEnvFile);
}

// Parse all environment files
let fileEnv: { [key: string]: any } = {};
if (envFiles.length > 0) {
try {
fileEnv = parseEnvFile(testEnvFile, envVars);
// Resolve paths for all files
const resolvedFiles = envFiles.map((file) => resolvePath(file));
fileEnv = parseEnvFiles(resolvedFiles, envVars);
} catch (e) {
console.log(e);
}
Expand Down
61 changes: 61 additions & 0 deletions extension/test/unit/testEnvFiles.test.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/

import assert from 'assert';
import path from 'path';
import fs from 'fs';
import os from 'os';
import { parseEnvFiles } from '../../src/utils/envUtils';

suite('parseEnvFiles Tests', () => {
let tmpDir: string;

setup(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'go-test-env-'));
});

teardown(() => {
if (tmpDir && fs.existsSync(tmpDir)) {
fs.rmdirSync(tmpDir, { recursive: true });
}
});

test('should handle empty array', () => {
const result = parseEnvFiles([]);
assert.deepStrictEqual(result, {});
});

test('should handle undefined input', () => {
const result = parseEnvFiles(undefined);
assert.deepStrictEqual(result, {});
});

test('should handle array of files', () => {
const envFile1 = path.join(tmpDir, 'first.env');
const envFile2 = path.join(tmpDir, 'second.env');

fs.writeFileSync(envFile1, 'VAR1=value1\nSHARED=from_first');
fs.writeFileSync(envFile2, 'VAR2=value2\nSHARED=from_second');

const result = parseEnvFiles([envFile1, envFile2]);

assert.strictEqual(result.VAR1, 'value1');
assert.strictEqual(result.VAR2, 'value2');
// Later files should override earlier ones
assert.strictEqual(result.SHARED, 'from_second');
});

test('should handle mixed valid and invalid files', () => {
const validFile = path.join(tmpDir, 'valid.env');
const invalidFile = path.join(tmpDir, 'nonexistent.env');

fs.writeFileSync(validFile, 'VALID_VAR=valid_value');

// This should throw when trying to parse invalid file
assert.throws(() => {
parseEnvFiles([validFile, invalidFile]);
});
});
});

AltStyle によって変換されたページ (->オリジナル) /