Skip to content

Commit

Permalink
Added parsing of environment variables in configuration (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
fr43nk authored Oct 16, 2024
1 parent bf35455 commit d328f0e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ node_modules
*.vsix
npm-debug.log
webpack.config.js*
findmerge*
9 changes: 5 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
],
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"preLaunchTask": "npm: webpack"
]
},
{
"name": "Run Extension",
Expand Down Expand Up @@ -47,15 +46,17 @@
"name": "Extension Tests (webpack)",
"type": "extensionHost",
"request": "launch",
"env": {
"WS_ROOT": "${workspaceFolder}"
},
"runtimeExecutable": "${execPath}",
"args": [
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
"outFiles": [
"${workspaceFolder}/out/**/*.js"
],
"preLaunchTask": "npm: pretest"
]
}
]
}
28 changes: 27 additions & 1 deletion src/ccConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@ export class PathMapping {
wsl = "";
}

export class Variables {
static parse<T>(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<T> {
private mChanged: boolean;

Expand All @@ -16,7 +42,7 @@ export class ConfigurationProperty<T> {

set value(value: T) {
if (this.mProp !== value) {
this.mProp = value;
this.mProp = Variables.parse<T>(value);
this.mChanged = true;
}
}
Expand Down
39 changes: 39 additions & 0 deletions src/test/suite/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});

});

0 comments on commit d328f0e

Please sign in to comment.