Replace NodeJS dict aliases with maps and named field reads
Co-authored-by: Danny Kim <temeddix@gmail.com> Co-committed-by: Danny Kim <temeddix@gmail.com>
This commit was merged in pull request #24.
This commit is contained in:
+28
-10
@@ -1,12 +1,12 @@
|
||||
// Test support: the current user's IDs, a fresh temporary directory, and a
|
||||
// plain command runner. It imports no source module, so the repository-level
|
||||
// tests in tests/, which reach src/ only through `#testing`, never touch
|
||||
// internals.
|
||||
// Test support: the current user's IDs, a fresh temporary directory, the
|
||||
// inherited environment, and a plain command runner. It imports no source
|
||||
// module, so the repository-level tests in tests/, which reach src/ only
|
||||
// through `#testing`, never touch internals.
|
||||
import { spawn } from "node:child_process";
|
||||
import { once } from "node:events";
|
||||
import { mkdtemp } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import nodePath from "node:path";
|
||||
import process from "node:process";
|
||||
import { text } from "node:stream/consumers";
|
||||
|
||||
@@ -24,8 +24,9 @@ export const IS_ROOT: boolean = currentUid() === 0;
|
||||
// Symlinks and POSIX modes, as these tests use them, need a Unix host.
|
||||
export const IS_WINDOWS: boolean = process.platform === "win32";
|
||||
|
||||
export function makeTempDir(): Promise<string> {
|
||||
return mkdtemp(join(tmpdir(), "bot-agents-test-"));
|
||||
export async function makeTempDir(): Promise<string> {
|
||||
const path = await mkdtemp(nodePath.join(tmpdir(), "bot-agents-test-"));
|
||||
return path;
|
||||
}
|
||||
|
||||
export interface CommandResult {
|
||||
@@ -33,15 +34,32 @@ export interface CommandResult {
|
||||
stderr: string;
|
||||
}
|
||||
|
||||
// This process's environment as a map from variable name to value.
|
||||
export function inheritedEnvironment(): Map<string, string> {
|
||||
const result = new Map<string, string>();
|
||||
for (const [name, value] of Object.entries(process.env)) {
|
||||
if (value !== undefined) {
|
||||
result.set(name, value);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Runs a command with stdin closed and only the given environment, and
|
||||
// collects its stderr.
|
||||
export async function runCommand(
|
||||
command: string,
|
||||
args: string[],
|
||||
env: NodeJS.ProcessEnv,
|
||||
env: ReadonlyMap<string, string>,
|
||||
): Promise<CommandResult> {
|
||||
const child = spawn(command, args, { env, stdio: ["ignore", "ignore", "pipe"] });
|
||||
const [stderr, closed] = await Promise.all([text(child.stderr), once(child, "close")]);
|
||||
const child = spawn(command, args, {
|
||||
env: Object.fromEntries(env),
|
||||
stdio: ["ignore", "ignore", "pipe"],
|
||||
});
|
||||
const [stderr, closed] = await Promise.all([
|
||||
text(child.stderr),
|
||||
once(child, "close"),
|
||||
]);
|
||||
const exitCode: unknown = closed[0];
|
||||
return { success: exitCode === 0, stderr };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user