bash-windows-flags-probe.cjs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Windows 负对照:node bash-windows-flags-probe.cjs;不依赖 adapter,保留旧 append 故障证据。
  2. const assert = require("node:assert/strict");
  3. const { constants } = require("node:fs");
  4. const { mkdtemp, open, readFile, rm } = require("node:fs/promises");
  5. const { spawn } = require("node:child_process");
  6. const { join } = require("node:path");
  7. const { tmpdir } = require("node:os");
  8. async function main() {
  9. assert.equal(process.platform, "win32", "Run this negative control on Windows");
  10. const root = await mkdtemp(join(tmpdir(), "zcode-flags-probe-"));
  11. const cases = [];
  12. const shells = [
  13. [
  14. "bash-builtin",
  15. "C:\\Program Files\\Git\\bin\\bash.exe",
  16. ["--noprofile", "--norc", "-c", "printf out; printf err >&2"],
  17. ],
  18. [
  19. "bash-external",
  20. "C:\\Program Files\\Git\\bin\\bash.exe",
  21. ["--noprofile", "--norc", "-c", "/usr/bin/printf out; /usr/bin/printf err >&2"],
  22. ],
  23. ["cmd", process.env.ComSpec, ["/d", "/s", "/c", "echo out&1>&2 echo err"]],
  24. ];
  25. try {
  26. for (const [name, shell, args] of shells) {
  27. for (const [mode, flags] of [
  28. [
  29. "old-append",
  30. constants.O_WRONLY |
  31. constants.O_CREAT |
  32. constants.O_APPEND |
  33. constants.O_TRUNC |
  34. (constants.O_NOFOLLOW ?? 0),
  35. ],
  36. ["w", "w"],
  37. ]) {
  38. const path = join(root, `${name}-${mode}.log`);
  39. const handle = await open(path, flags, 0o600);
  40. let completion;
  41. try {
  42. const child = spawn(shell, args, {
  43. stdio: ["ignore", handle.fd, handle.fd],
  44. windowsHide: true,
  45. });
  46. completion = new Promise((resolve, reject) => {
  47. child.once("error", reject);
  48. child.once("exit", (code) => resolve(code));
  49. });
  50. } finally {
  51. await handle.close();
  52. }
  53. const exitCode = await completion;
  54. const output = await readFile(path, "utf8");
  55. if (mode === "w" || name === "cmd") {
  56. assert.equal(exitCode, 0);
  57. assert.equal(output.replace(/\r?\n/g, ""), "outerr");
  58. }
  59. // 不把 MSYS 的旧 bug 固化为永远必须失败:若新版修好,报告仍如实保留旧模式结果。
  60. cases.push({
  61. name,
  62. mode,
  63. exitCode,
  64. output,
  65. completeOutput: output.replace(/\r?\n/g, "") === "outerr",
  66. });
  67. }
  68. }
  69. console.log(
  70. JSON.stringify({ node: process.version, platform: process.platform, cases }, null, 2),
  71. );
  72. } finally {
  73. await rm(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
  74. }
  75. }
  76. main().catch((error) => {
  77. console.error(error);
  78. process.exitCode = 1;
  79. });