Skip to content
Merged
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
17 changes: 17 additions & 0 deletions package-lock.json

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

14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@
}
}
}
},
"nodejs-testing.envFile": {
"type": "string",
"markdownDescription": "Absolute path to a file containing environment variable definitions.\n\nNote: template parameters like ${workspaceFolder} will be resolved.",
"default": ""
},
"nodejs-testing.env": {
"type": "object",
"markdownDescription": "Environment variables passed to the program. The value null removes the variable from the environment.\n\nNote: This takes precedence over envFile.",
"additionalProperties": {
"type": "string"
},
"default": {}
}
}
}
Expand Down Expand Up @@ -184,6 +197,7 @@
"acorn-loose": "^8.4.0",
"ansi-colors": "^4.1.3",
"data-uri-to-buffer": "^6.0.2",
"dotenv": "^16.4.5",
"estraverse": "^5.3.0",
"picomatch": "^4.0.1",
"pretty-format": "^29.7.0",
Expand Down
2 changes: 2 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export async function activate(context: vscode.ExtensionContext) {
new ConfigValue("style", Style.Spec),
context.extensionUri.fsPath,
new ConfigValue("nodejsParameters", []),
new ConfigValue("envFile", ""),
new ConfigValue("env", {}),
extensions,
);

Expand Down
1 change: 1 addition & 0 deletions src/runner-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export const contract = makeContract({
parameters: s.sArrayOf(s.sString()),
}),
),
extraEnv: s.sMap(s.sString()),
}),
result: s.sObject({
status: s.sNumber(),
Expand Down
5 changes: 4 additions & 1 deletion src/runner-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const start: (typeof contract)["TClientHandler"]["start"] = async ({
files,
extensions,
verbose,
extraEnv,
}) => {
const majorVersion = /^v([0-9]+)/.exec(process.version);
if (!majorVersion || Number(majorVersion[1]) < 19) {
Expand All @@ -51,7 +52,7 @@ const start: (typeof contract)["TClientHandler"]["start"] = async ({
const todo: Promise<void>[] = [];
for (let i = 0; i < concurrency && i < files.length; i++) {
const prefix = colors[i % colors.length](`worker${i + 1}> `);
todo.push(doWork(prefix, files, extensions, verbose));
todo.push(doWork(prefix, files, extensions, verbose, extraEnv));
}
await Promise.all(todo);

Expand All @@ -63,6 +64,7 @@ async function doWork(
queue: ITestRunFile[],
extensions: ExtensionConfig[],
verbose: boolean,
extraEnv: Record<string, string>,
) {
while (queue.length) {
const next = queue.pop()!;
Expand Down Expand Up @@ -96,6 +98,7 @@ async function doWork(
// enable color for modules that use `supports-color` or similar
FORCE_COLOR: "true",
...process.env,
...extraEnv,
},
});

Expand Down
18 changes: 16 additions & 2 deletions src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { replaceVariables } from "@c4312/vscode-variables";
import { Contract } from "@hediet/json-rpc";
import { NodeJsMessageStream } from "@hediet/json-rpc-streams/src";
import { spawn } from "child_process";
import { promises as fs } from "fs";
import { parse as parseEnv } from "dotenv";
import fs from "fs/promises";
import { createServer } from "net";
import { cpus, tmpdir } from "os";
import { join } from "path";
Expand Down Expand Up @@ -36,6 +37,8 @@ export class TestRunner {
private readonly style: ConfigValue<Style>,
extensionDir: string,
private readonly nodejsParameters: ConfigValue<string[]>,
private readonly envFile: ConfigValue<string>,
private readonly env: ConfigValue<Record<string, string>>,
private readonly extensions: ConfigValue<ExtensionConfig[]>,
) {
this.workerPath = join(extensionDir, "out", "runner-worker.js");
Expand Down Expand Up @@ -84,13 +87,23 @@ export class TestRunner {

try {
const outputQueue = new OutputQueue();

const extensions = this.extensions.value;
const envFile = this.envFile.value
? await fs.readFile(replaceVariables(this.envFile.value))
: null;
const envFileValues = envFile ? parseEnv(envFile) : {};
const extraEnv = {
...envFileValues,
...this.env.value,
};

await new Promise<void>((resolve, reject) => {
const socket = getRandomPipe();
run.token.onCancellationRequested(() => fs.unlink(socket).catch(() => {}));

const server = createServer((stream) => {
run.token.onCancellationRequested(stream.end, stream);
const extensions = this.extensions.value;

const onLog = (test: vscode.TestItem | undefined, prefix: string, log: ILog) => {
const location = log.sf?.file
Expand Down Expand Up @@ -185,6 +198,7 @@ export class TestRunner {
concurrency,
extensions,
verbose: this.verbose.value,
extraEnv,
})
.then(({ status, message }) => {
switch (status) {
Expand Down