Skip to content

Commit ca12e96

Browse files
authored
feat: add envFile support for test runner (#30)
* add envFile support for test runner * also support passing env variables from the config
1 parent 7f17c31 commit ca12e96

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

package-lock.json

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,19 @@
125125
}
126126
}
127127
}
128+
},
129+
"nodejs-testing.envFile": {
130+
"type": "string",
131+
"markdownDescription": "Absolute path to a file containing environment variable definitions.\n\nNote: template parameters like ${workspaceFolder} will be resolved.",
132+
"default": ""
133+
},
134+
"nodejs-testing.env": {
135+
"type": "object",
136+
"markdownDescription": "Environment variables passed to the program. The value null removes the variable from the environment.\n\nNote: This takes precedence over envFile.",
137+
"additionalProperties": {
138+
"type": "string"
139+
},
140+
"default": {}
128141
}
129142
}
130143
}
@@ -184,6 +197,7 @@
184197
"acorn-loose": "^8.4.0",
185198
"ansi-colors": "^4.1.3",
186199
"data-uri-to-buffer": "^6.0.2",
200+
"dotenv": "^16.4.5",
187201
"estraverse": "^5.3.0",
188202
"picomatch": "^4.0.1",
189203
"pretty-format": "^29.7.0",

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export async function activate(context: vscode.ExtensionContext) {
2424
new ConfigValue("style", Style.Spec),
2525
context.extensionUri.fsPath,
2626
new ConfigValue("nodejsParameters", []),
27+
new ConfigValue("envFile", ""),
28+
new ConfigValue("env", {}),
2729
extensions,
2830
);
2931

src/runner-protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ export const contract = makeContract({
9292
parameters: s.sArrayOf(s.sString()),
9393
}),
9494
),
95+
extraEnv: s.sMap(s.sString()),
9596
}),
9697
result: s.sObject({
9798
status: s.sNumber(),

src/runner-worker.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const start: (typeof contract)["TClientHandler"]["start"] = async ({
4242
files,
4343
extensions,
4444
verbose,
45+
extraEnv,
4546
}) => {
4647
const majorVersion = /^v([0-9]+)/.exec(process.version);
4748
if (!majorVersion || Number(majorVersion[1]) < 19) {
@@ -51,7 +52,7 @@ const start: (typeof contract)["TClientHandler"]["start"] = async ({
5152
const todo: Promise<void>[] = [];
5253
for (let i = 0; i < concurrency && i < files.length; i++) {
5354
const prefix = colors[i % colors.length](`worker${i + 1}> `);
54-
todo.push(doWork(prefix, files, extensions, verbose));
55+
todo.push(doWork(prefix, files, extensions, verbose, extraEnv));
5556
}
5657
await Promise.all(todo);
5758

@@ -63,6 +64,7 @@ async function doWork(
6364
queue: ITestRunFile[],
6465
extensions: ExtensionConfig[],
6566
verbose: boolean,
67+
extraEnv: Record<string, string>,
6668
) {
6769
while (queue.length) {
6870
const next = queue.pop()!;
@@ -96,6 +98,7 @@ async function doWork(
9698
// enable color for modules that use `supports-color` or similar
9799
FORCE_COLOR: "true",
98100
...process.env,
101+
...extraEnv,
99102
},
100103
});
101104

src/runner.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { replaceVariables } from "@c4312/vscode-variables";
22
import { Contract } from "@hediet/json-rpc";
33
import { NodeJsMessageStream } from "@hediet/json-rpc-streams/src";
44
import { spawn } from "child_process";
5-
import { promises as fs } from "fs";
5+
import { parse as parseEnv } from "dotenv";
6+
import fs from "fs/promises";
67
import { createServer } from "net";
78
import { cpus, tmpdir } from "os";
89
import { join } from "path";
@@ -36,6 +37,8 @@ export class TestRunner {
3637
private readonly style: ConfigValue<Style>,
3738
extensionDir: string,
3839
private readonly nodejsParameters: ConfigValue<string[]>,
40+
private readonly envFile: ConfigValue<string>,
41+
private readonly env: ConfigValue<Record<string, string>>,
3942
private readonly extensions: ConfigValue<ExtensionConfig[]>,
4043
) {
4144
this.workerPath = join(extensionDir, "out", "runner-worker.js");
@@ -84,13 +87,23 @@ export class TestRunner {
8487

8588
try {
8689
const outputQueue = new OutputQueue();
90+
91+
const extensions = this.extensions.value;
92+
const envFile = this.envFile.value
93+
? await fs.readFile(replaceVariables(this.envFile.value))
94+
: null;
95+
const envFileValues = envFile ? parseEnv(envFile) : {};
96+
const extraEnv = {
97+
...envFileValues,
98+
...this.env.value,
99+
};
100+
87101
await new Promise<void>((resolve, reject) => {
88102
const socket = getRandomPipe();
89103
run.token.onCancellationRequested(() => fs.unlink(socket).catch(() => {}));
90104

91105
const server = createServer((stream) => {
92106
run.token.onCancellationRequested(stream.end, stream);
93-
const extensions = this.extensions.value;
94107

95108
const onLog = (test: vscode.TestItem | undefined, prefix: string, log: ILog) => {
96109
const location = log.sf?.file
@@ -185,6 +198,7 @@ export class TestRunner {
185198
concurrency,
186199
extensions,
187200
verbose: this.verbose.value,
201+
extraEnv,
188202
})
189203
.then(({ status, message }) => {
190204
switch (status) {

0 commit comments

Comments
 (0)