From ea15676da8ebce2c095d4eb1c3aa70e85008aed3 Mon Sep 17 00:00:00 2001 From: Sewon Park Date: Sun, 21 Oct 2018 06:41:06 +0900 Subject: [PATCH] go.gopath and go.toolsGopath now expand ${env:XXX} (#1743) * go.gopath and go.toolsGopath now expand ${env:XXX} * Fix linting errors * remove 'm' flag from substituteEnv * add test for substituteEnv * add newline on end of utils.test.ts --- src/util.ts | 10 ++++++++-- test/unit/utils.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/unit/utils.test.ts diff --git a/src/util.ts b/src/util.ts index 4cdf3c750..ea6f2df9d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -348,7 +348,7 @@ export function getToolsGopath(useCache: boolean = true): string { function resolveToolsGopath(): string { - let toolsGopathForWorkspace = vscode.workspace.getConfiguration('go')['toolsGopath'] || ''; + let toolsGopathForWorkspace = substituteEnv(vscode.workspace.getConfiguration('go')['toolsGopath'] || ''); // In case of single root if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length <= 1) { @@ -408,6 +408,12 @@ export function getToolsEnvVars(): any { return envVars; } +export function substituteEnv(input: string): string { + return input.replace(/\${env:([^}]+)}/g, function (match, capture) { + return process.env[capture.trim()] || ''; + }); +} + export function getCurrentGoPath(workspaceUri?: vscode.Uri): string { let currentFilePath: string; if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) { @@ -443,7 +449,7 @@ export function getCurrentGoPath(workspaceUri?: vscode.Uri): string { } } - const configGopath = config['gopath'] ? resolvePath(config['gopath'], currentRoot) : ''; + let configGopath = config['gopath'] ? resolvePath(substituteEnv(config['gopath']), currentRoot) : ''; return inferredGopath ? inferredGopath : (configGopath || process.env['GOPATH']); } diff --git a/test/unit/utils.test.ts b/test/unit/utils.test.ts new file mode 100644 index 000000000..9daffd9d1 --- /dev/null +++ b/test/unit/utils.test.ts @@ -0,0 +1,25 @@ +/*--------------------------------------------------------- + * Copyright (C) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------*/ + +import * as assert from 'assert'; +import { substituteEnv } from '../../src/util'; + +suite('utils Tests', () => { + test('substituteEnv: default', () => { + + // prepare test + const env = Object.assign({}, process.env); + process.env['test1'] = 'abcd'; + process.env['test2'] = 'defg'; + + let actual = substituteEnv(' ${env:test1} \r\n ${env:test2}\r\n${env:test1}'); + let expected = ' abcd \r\n defg\r\nabcd'; + + assert.equal(actual, expected); + + // test completed + process.env = env; + }); +});