test: sanitize Windows CI vitest runner output (#567) (thanks @erikpr1994)
This commit is contained in:
@@ -2,58 +2,9 @@ import fs from "node:fs";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
function sanitizeWindowsCIOutput(text: string): string {
|
import { installWindowsCIOutputSanitizer } from "./windows-ci-output-sanitizer";
|
||||||
return text
|
|
||||||
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "?")
|
|
||||||
.replace(/[\uD800-\uDFFF]/g, "?");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.platform === "win32" && process.env.GITHUB_ACTIONS === "true") {
|
installWindowsCIOutputSanitizer();
|
||||||
const decodeUtf8Text = (chunk: unknown): string | null => {
|
|
||||||
if (typeof chunk === "string") return chunk;
|
|
||||||
if (Buffer.isBuffer(chunk)) return chunk.toString("utf-8");
|
|
||||||
if (chunk instanceof Uint8Array)
|
|
||||||
return Buffer.from(chunk).toString("utf-8");
|
|
||||||
if (chunk instanceof ArrayBuffer)
|
|
||||||
return Buffer.from(chunk).toString("utf-8");
|
|
||||||
if (ArrayBuffer.isView(chunk)) {
|
|
||||||
return Buffer.from(
|
|
||||||
chunk.buffer,
|
|
||||||
chunk.byteOffset,
|
|
||||||
chunk.byteLength,
|
|
||||||
).toString("utf-8");
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
|
|
||||||
const originalStderrWrite = process.stderr.write.bind(process.stderr);
|
|
||||||
|
|
||||||
process.stdout.write = ((chunk: unknown, ...args: unknown[]) => {
|
|
||||||
const text = decodeUtf8Text(chunk);
|
|
||||||
if (text !== null)
|
|
||||||
return originalStdoutWrite(sanitizeWindowsCIOutput(text), ...args);
|
|
||||||
return originalStdoutWrite(chunk as never, ...args); // passthrough
|
|
||||||
}) as typeof process.stdout.write;
|
|
||||||
|
|
||||||
process.stderr.write = ((chunk: unknown, ...args: unknown[]) => {
|
|
||||||
const text = decodeUtf8Text(chunk);
|
|
||||||
if (text !== null)
|
|
||||||
return originalStderrWrite(sanitizeWindowsCIOutput(text), ...args);
|
|
||||||
return originalStderrWrite(chunk as never, ...args); // passthrough
|
|
||||||
}) as typeof process.stderr.write;
|
|
||||||
|
|
||||||
const originalWriteSync = fs.writeSync.bind(fs);
|
|
||||||
fs.writeSync = ((fd: number, data: unknown, ...args: unknown[]) => {
|
|
||||||
if (fd === 1 || fd === 2) {
|
|
||||||
const text = decodeUtf8Text(data);
|
|
||||||
if (text !== null) {
|
|
||||||
return originalWriteSync(fd, sanitizeWindowsCIOutput(text), ...args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return originalWriteSync(fd, data as never, ...(args as never[]));
|
|
||||||
}) as typeof fs.writeSync;
|
|
||||||
}
|
|
||||||
|
|
||||||
const originalHome = process.env.HOME;
|
const originalHome = process.env.HOME;
|
||||||
const originalUserProfile = process.env.USERPROFILE;
|
const originalUserProfile = process.env.USERPROFILE;
|
||||||
|
|||||||
5
test/vitest-global-setup.ts
Normal file
5
test/vitest-global-setup.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { installWindowsCIOutputSanitizer } from "./windows-ci-output-sanitizer";
|
||||||
|
|
||||||
|
export default function globalSetup() {
|
||||||
|
installWindowsCIOutputSanitizer();
|
||||||
|
}
|
||||||
59
test/windows-ci-output-sanitizer.ts
Normal file
59
test/windows-ci-output-sanitizer.ts
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
import fs from "node:fs";
|
||||||
|
|
||||||
|
function sanitizeWindowsCIOutput(text: string): string {
|
||||||
|
return text
|
||||||
|
.replace(/[\uD800-\uDBFF][\uDC00-\uDFFF]/g, "?")
|
||||||
|
.replace(/[\uD800-\uDFFF]/g, "?");
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeUtf8Text(chunk: unknown): string | null {
|
||||||
|
if (typeof chunk === "string") return chunk;
|
||||||
|
if (Buffer.isBuffer(chunk)) return chunk.toString("utf-8");
|
||||||
|
if (chunk instanceof Uint8Array) return Buffer.from(chunk).toString("utf-8");
|
||||||
|
if (chunk instanceof ArrayBuffer) return Buffer.from(chunk).toString("utf-8");
|
||||||
|
if (ArrayBuffer.isView(chunk)) {
|
||||||
|
return Buffer.from(
|
||||||
|
chunk.buffer,
|
||||||
|
chunk.byteOffset,
|
||||||
|
chunk.byteLength,
|
||||||
|
).toString("utf-8");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function installWindowsCIOutputSanitizer(): void {
|
||||||
|
if (process.platform !== "win32") return;
|
||||||
|
if (process.env.GITHUB_ACTIONS !== "true") return;
|
||||||
|
|
||||||
|
const globalKey = "__clawdbotWindowsCIOutputSanitizerInstalled";
|
||||||
|
if ((globalThis as Record<string, unknown>)[globalKey] === true) return;
|
||||||
|
(globalThis as Record<string, unknown>)[globalKey] = true;
|
||||||
|
|
||||||
|
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
|
||||||
|
const originalStderrWrite = process.stderr.write.bind(process.stderr);
|
||||||
|
|
||||||
|
process.stdout.write = ((chunk: unknown, ...args: unknown[]) => {
|
||||||
|
const text = decodeUtf8Text(chunk);
|
||||||
|
if (text !== null)
|
||||||
|
return originalStdoutWrite(sanitizeWindowsCIOutput(text), ...args);
|
||||||
|
return originalStdoutWrite(chunk as never, ...args); // passthrough
|
||||||
|
}) as typeof process.stdout.write;
|
||||||
|
|
||||||
|
process.stderr.write = ((chunk: unknown, ...args: unknown[]) => {
|
||||||
|
const text = decodeUtf8Text(chunk);
|
||||||
|
if (text !== null)
|
||||||
|
return originalStderrWrite(sanitizeWindowsCIOutput(text), ...args);
|
||||||
|
return originalStderrWrite(chunk as never, ...args); // passthrough
|
||||||
|
}) as typeof process.stderr.write;
|
||||||
|
|
||||||
|
const originalWriteSync = fs.writeSync.bind(fs);
|
||||||
|
fs.writeSync = ((fd: number, data: unknown, ...args: unknown[]) => {
|
||||||
|
if (fd === 1 || fd === 2) {
|
||||||
|
const text = decodeUtf8Text(data);
|
||||||
|
if (text !== null) {
|
||||||
|
return originalWriteSync(fd, sanitizeWindowsCIOutput(text), ...args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return originalWriteSync(fd, data as never, ...(args as never[]));
|
||||||
|
}) as typeof fs.writeSync;
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ export default defineConfig({
|
|||||||
test: {
|
test: {
|
||||||
include: ["src/**/*.test.ts", "test/format-error.test.ts"],
|
include: ["src/**/*.test.ts", "test/format-error.test.ts"],
|
||||||
setupFiles: ["test/setup.ts"],
|
setupFiles: ["test/setup.ts"],
|
||||||
|
globalSetup: ["test/vitest-global-setup.ts"],
|
||||||
exclude: [
|
exclude: [
|
||||||
"dist/**",
|
"dist/**",
|
||||||
"apps/macos/**",
|
"apps/macos/**",
|
||||||
|
|||||||
Reference in New Issue
Block a user