test: harden Windows CI output sanitization (#567) (thanks @erikpr1994)
This commit is contained in:
@@ -9,34 +9,50 @@ function sanitizeWindowsCIOutput(text: string): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (process.platform === "win32" && process.env.GITHUB_ACTIONS === "true") {
|
if (process.platform === "win32" && process.env.GITHUB_ACTIONS === "true") {
|
||||||
|
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 originalStdoutWrite = process.stdout.write.bind(process.stdout);
|
||||||
const originalStderrWrite = process.stderr.write.bind(process.stderr);
|
const originalStderrWrite = process.stderr.write.bind(process.stderr);
|
||||||
|
|
||||||
process.stdout.write = ((chunk: unknown, ...args: unknown[]) => {
|
process.stdout.write = ((chunk: unknown, ...args: unknown[]) => {
|
||||||
if (typeof chunk === "string") {
|
const text = decodeUtf8Text(chunk);
|
||||||
return originalStdoutWrite(sanitizeWindowsCIOutput(chunk), ...args);
|
if (text !== null)
|
||||||
}
|
return originalStdoutWrite(sanitizeWindowsCIOutput(text), ...args);
|
||||||
if (Buffer.isBuffer(chunk)) {
|
return originalStdoutWrite(chunk as never, ...args); // passthrough
|
||||||
return originalStdoutWrite(
|
|
||||||
sanitizeWindowsCIOutput(chunk.toString("utf-8")),
|
|
||||||
...args,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return originalStdoutWrite(chunk as never, ...args);
|
|
||||||
}) as typeof process.stdout.write;
|
}) as typeof process.stdout.write;
|
||||||
|
|
||||||
process.stderr.write = ((chunk: unknown, ...args: unknown[]) => {
|
process.stderr.write = ((chunk: unknown, ...args: unknown[]) => {
|
||||||
if (typeof chunk === "string") {
|
const text = decodeUtf8Text(chunk);
|
||||||
return originalStderrWrite(sanitizeWindowsCIOutput(chunk), ...args);
|
if (text !== null)
|
||||||
}
|
return originalStderrWrite(sanitizeWindowsCIOutput(text), ...args);
|
||||||
if (Buffer.isBuffer(chunk)) {
|
return originalStderrWrite(chunk as never, ...args); // passthrough
|
||||||
return originalStderrWrite(
|
|
||||||
sanitizeWindowsCIOutput(chunk.toString("utf-8")),
|
|
||||||
...args,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return originalStderrWrite(chunk as never, ...args);
|
|
||||||
}) as typeof process.stderr.write;
|
}) 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;
|
||||||
|
|||||||
Reference in New Issue
Block a user