2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2020-03-13 10:57:32 -04:00
|
|
|
|
2024-07-25 01:30:28 -04:00
|
|
|
import * as colors from "@std/fmt/colors";
|
|
|
|
import { assert } from "@std/assert";
|
2020-06-12 11:58:04 -04:00
|
|
|
export { colors };
|
2024-07-25 01:30:28 -04:00
|
|
|
import { join, resolve } from "@std/path";
|
2019-03-06 20:48:46 -05:00
|
|
|
export {
|
|
|
|
assert,
|
2019-05-17 14:03:01 -04:00
|
|
|
assertEquals,
|
2022-09-28 08:11:12 -04:00
|
|
|
assertFalse,
|
2023-03-22 00:13:24 -04:00
|
|
|
AssertionError,
|
2023-11-17 17:24:10 -05:00
|
|
|
assertIsError,
|
2019-08-27 11:33:39 -04:00
|
|
|
assertMatch,
|
2019-05-17 14:03:01 -04:00
|
|
|
assertNotEquals,
|
2023-01-29 09:15:01 -05:00
|
|
|
assertNotStrictEquals,
|
2021-09-05 16:43:46 -04:00
|
|
|
assertRejects,
|
2020-06-05 23:43:00 -04:00
|
|
|
assertStrictEquals,
|
2020-10-26 11:03:30 -04:00
|
|
|
assertStringIncludes,
|
2020-09-27 06:22:32 -04:00
|
|
|
assertThrows,
|
2020-03-28 13:03:49 -04:00
|
|
|
fail,
|
2021-02-22 07:26:17 -05:00
|
|
|
unimplemented,
|
2020-09-27 06:22:32 -04:00
|
|
|
unreachable,
|
2024-07-25 01:30:28 -04:00
|
|
|
} from "@std/assert";
|
|
|
|
export { delay } from "@std/async/delay";
|
|
|
|
export { readLines } from "@std/io/read-lines";
|
|
|
|
export { parseArgs } from "@std/cli/parse-args";
|
2018-08-23 19:47:43 -04:00
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
export function pathToAbsoluteFileUrl(path: string): URL {
|
|
|
|
path = resolve(path);
|
|
|
|
|
|
|
|
return new URL(`file://${Deno.build.os === "windows" ? "/" : ""}${path}`);
|
|
|
|
}
|
2022-03-11 11:18:49 -05:00
|
|
|
|
2022-12-02 12:41:52 -05:00
|
|
|
export function execCode(code: string): Promise<readonly [number, string]> {
|
|
|
|
return execCode2(code).finished();
|
|
|
|
}
|
2022-03-11 11:18:49 -05:00
|
|
|
|
2024-05-28 16:46:04 -04:00
|
|
|
export function execCode3(cmd: string, args: string[]) {
|
|
|
|
const command = new Deno.Command(cmd, {
|
|
|
|
args,
|
2022-12-02 12:41:52 -05:00
|
|
|
stdout: "piped",
|
|
|
|
stderr: "inherit",
|
|
|
|
});
|
|
|
|
|
|
|
|
const child = command.spawn();
|
|
|
|
const stdout = child.stdout.pipeThrough(new TextDecoderStream()).getReader();
|
|
|
|
let output = "";
|
|
|
|
|
|
|
|
return {
|
|
|
|
async waitStdoutText(text: string) {
|
|
|
|
while (true) {
|
|
|
|
const readData = await stdout.read();
|
|
|
|
if (readData.value) {
|
|
|
|
output += readData.value;
|
|
|
|
if (output.includes(text)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (readData.done) {
|
|
|
|
throw new Error(`Did not find text '${text}' in stdout.`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async finished() {
|
|
|
|
while (true) {
|
|
|
|
const readData = await stdout.read();
|
|
|
|
if (readData.value) {
|
|
|
|
output += readData.value;
|
|
|
|
}
|
|
|
|
if (readData.done) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const status = await child.status;
|
|
|
|
return [status.code, output] as const;
|
|
|
|
},
|
|
|
|
};
|
2022-03-11 11:18:49 -05:00
|
|
|
}
|
2023-10-03 22:37:39 -04:00
|
|
|
|
2024-05-28 16:46:04 -04:00
|
|
|
export function execCode2(code: string) {
|
2024-09-05 05:34:12 -04:00
|
|
|
return execCode3(Deno.execPath(), ["eval", code]);
|
2024-05-28 16:46:04 -04:00
|
|
|
}
|
|
|
|
|
2023-10-03 22:37:39 -04:00
|
|
|
export function tmpUnixSocketPath(): string {
|
|
|
|
const folder = Deno.makeTempDirSync();
|
|
|
|
return join(folder, "socket");
|
|
|
|
}
|
2024-03-07 08:58:46 -05:00
|
|
|
|
|
|
|
export async function curlRequest(args: string[]) {
|
|
|
|
const { success, stdout, stderr } = await new Deno.Command("curl", {
|
|
|
|
args,
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "piped",
|
|
|
|
}).output();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
assert(
|
|
|
|
success,
|
|
|
|
`Failed to cURL ${args}: stdout\n\n${
|
|
|
|
decoder.decode(stdout)
|
|
|
|
}\n\nstderr:\n\n${decoder.decode(stderr)}`,
|
|
|
|
);
|
|
|
|
return decoder.decode(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function curlRequestWithStdErr(args: string[]) {
|
|
|
|
const { success, stdout, stderr } = await new Deno.Command("curl", {
|
|
|
|
args,
|
|
|
|
stdout: "piped",
|
|
|
|
stderr: "piped",
|
|
|
|
}).output();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
assert(
|
|
|
|
success,
|
|
|
|
`Failed to cURL ${args}: stdout\n\n${
|
|
|
|
decoder.decode(stdout)
|
|
|
|
}\n\nstderr:\n\n${decoder.decode(stderr)}`,
|
|
|
|
);
|
|
|
|
return [decoder.decode(stdout), decoder.decode(stderr)];
|
|
|
|
}
|