diff --git a/.gitignore b/.gitignore index 1aa606f..0337329 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ node_modules *.vsix npm-debug.log webpack.config.js* +findmerge* diff --git a/.vscode/launch.json b/.vscode/launch.json index 14acf9e..68e86e2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -15,8 +15,7 @@ ], "outFiles": [ "${workspaceFolder}/dist/**/*.js" - ], - "preLaunchTask": "npm: webpack" + ] }, { "name": "Run Extension", @@ -47,6 +46,9 @@ "name": "Extension Tests (webpack)", "type": "extensionHost", "request": "launch", + "env": { + "WS_ROOT": "${workspaceFolder}" + }, "runtimeExecutable": "${execPath}", "args": [ "--extensionDevelopmentPath=${workspaceFolder}", @@ -54,8 +56,7 @@ ], "outFiles": [ "${workspaceFolder}/out/**/*.js" - ], - "preLaunchTask": "npm: pretest" + ] } ] } \ No newline at end of file diff --git a/src/ccConfiguration.ts b/src/ccConfiguration.ts index 285a891..e8db676 100755 --- a/src/ccConfiguration.ts +++ b/src/ccConfiguration.ts @@ -3,6 +3,32 @@ export class PathMapping { wsl = ""; } +export class Variables { + static parse(value: T): T { + if (value === null || value === "" || typeof value !== "string") { + return value; + } + + // get env variable + const idx = value.indexOf("env:"); + let retVal = value as string; + if (idx > 0) { + const matches = value.matchAll(/\$\{env:(\w+)\}/gi); + for (const subgrp of matches) { + if (subgrp.length > 0) { + if (subgrp[1] in process.env) { + const v = process.env[subgrp[1]]; + if (v !== undefined) { + retVal = retVal.replace(subgrp[0], v); + } + } + } + } + } + return retVal as T; + } +} + export class ConfigurationProperty { private mChanged: boolean; @@ -16,7 +42,7 @@ export class ConfigurationProperty { set value(value: T) { if (this.mProp !== value) { - this.mProp = value; + this.mProp = Variables.parse(value); this.mChanged = true; } } diff --git a/src/test/suite/extension.test.ts b/src/test/suite/extension.test.ts index 740c036..014e754 100644 --- a/src/test/suite/extension.test.ts +++ b/src/test/suite/extension.test.ts @@ -11,6 +11,10 @@ import { after, before, beforeEach } from "mocha"; import SuiteOutputChannel from "../mock/SuiteOutputChannel"; // import * as myExtension from '../../extension'; +//const WS_ROOT = process.env["WS_ROOT"] ? process.env["WS_ROOT"] : ""; +const TEST_HOME = process.env["HOME"] ? process.env["HOME"] : "-"; +const TEST_USER = process.env["USER"] ? process.env["USER"] : "-"; + suite("Extension Test Suite", () => { vscode.window.showInformationMessage("Start all tests."); let extensionContext: vscode.ExtensionContext; @@ -128,4 +132,39 @@ suite("Extension Test Suite", () => { ); }); + test("Extension: Path names with environment variable", async () => { + configHandler.configuration.tempDir.value = "${env:HOME}/tmp"; + assert.strictEqual(`${TEST_HOME}/tmp`, configHandler.configuration.tempDir.value); + }); + + test("Extension: Path names with multiple environment variables", async () => { + configHandler.configuration.tempDir.value = "${env:HOME}/tmp/${env:USER}"; + assert.strictEqual(`${TEST_HOME}/tmp/${TEST_USER}`, configHandler.configuration.tempDir.value); + }); + + test("Extension: Path names with invalid variable 1", async () => { + configHandler.configuration.tempDir.value = "${HOME}/tmp"; + assert.strictEqual('${HOME}/tmp', configHandler.configuration.tempDir.value); + }); + + test("Extension: Path names with invalid variable 2", async () => { + configHandler.configuration.tempDir.value = "{HOME}/tmp"; + assert.strictEqual('{HOME}/tmp', configHandler.configuration.tempDir.value); + }); + + test("Extension: Path names with invalid variable 3", async () => { + configHandler.configuration.tempDir.value = "{env:}/tmp"; + assert.strictEqual('{env:}/tmp', configHandler.configuration.tempDir.value); + }); + + test("Extension: Path names with invalid variable 4", async () => { + configHandler.configuration.tempDir.value = "${env:}/tmp"; + assert.strictEqual('${env:}/tmp', configHandler.configuration.tempDir.value); + }); + + test("Extension: Path names with invalid variable 5", async () => { + configHandler.configuration.tempDir.value = "${env:}/tmp/${env:USER}"; + assert.strictEqual('${env:}/tmp' + `/${TEST_USER}`, configHandler.configuration.tempDir.value); + }); + });