|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +import * as cp from "node:child_process"; |
| 2 | +import fs from "node:fs/promises"; |
| 3 | +import os from "node:os"; |
| 4 | +import path from "node:path"; |
| 5 | +import { promisify } from "node:util"; |
| 6 | +import { beforeAll, describe, expect, it } from "vitest"; |
2 | 7 |
|
3 | 8 | import { |
4 | 9 | expectPathsEqual, |
5 | 10 | exitCommand, |
| 11 | + isWindows, |
6 | 12 | printCommand, |
7 | 13 | printEnvCommand, |
8 | | - isWindows, |
| 14 | + shimExecFile, |
| 15 | + writeExecutable, |
9 | 16 | } from "./platform"; |
10 | 17 |
|
11 | 18 | describe("platform utils", () => { |
@@ -83,4 +90,76 @@ describe("platform utils", () => { |
83 | 90 | }, |
84 | 91 | ); |
85 | 92 | }); |
| 93 | + |
| 94 | + describe("writeExecutable", () => { |
| 95 | + const tmp = path.join(os.tmpdir(), "vscode-coder-tests-platform"); |
| 96 | + |
| 97 | + beforeAll(async () => { |
| 98 | + await fs.rm(tmp, { recursive: true, force: true }); |
| 99 | + await fs.mkdir(tmp, { recursive: true }); |
| 100 | + }); |
| 101 | + |
| 102 | + it("writes a .js file and returns its path", async () => { |
| 103 | + const result = await writeExecutable(tmp, "test-script", "// hello"); |
| 104 | + expect(result).toBe(path.join(tmp, "test-script.js")); |
| 105 | + expect(await fs.readFile(result, "utf-8")).toBe("// hello"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("overwrites existing files", async () => { |
| 109 | + await writeExecutable(tmp, "overwrite", "first"); |
| 110 | + const result = await writeExecutable(tmp, "overwrite", "second"); |
| 111 | + expect(await fs.readFile(result, "utf-8")).toBe("second"); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe("shimExecFile", () => { |
| 116 | + const tmp = path.join(os.tmpdir(), "vscode-coder-tests-shim"); |
| 117 | + const mod = shimExecFile(cp); |
| 118 | + const shimmedExecFile = promisify(mod.execFile); |
| 119 | + |
| 120 | + beforeAll(async () => { |
| 121 | + await fs.rm(tmp, { recursive: true, force: true }); |
| 122 | + await fs.mkdir(tmp, { recursive: true }); |
| 123 | + }); |
| 124 | + |
| 125 | + it("runs .js files through node", async () => { |
| 126 | + const script = await writeExecutable( |
| 127 | + tmp, |
| 128 | + "echo", |
| 129 | + 'process.stdout.write("ok");', |
| 130 | + ); |
| 131 | + const { stdout } = await shimmedExecFile(script); |
| 132 | + expect(stdout).toBe("ok"); |
| 133 | + }); |
| 134 | + |
| 135 | + it("passes args through to the script", async () => { |
| 136 | + const script = await writeExecutable( |
| 137 | + tmp, |
| 138 | + "echo-args", |
| 139 | + "process.stdout.write(process.argv.slice(2).join(','));", |
| 140 | + ); |
| 141 | + const { stdout } = await shimmedExecFile(script, ["a", "b", "c"]); |
| 142 | + expect(stdout).toBe("a,b,c"); |
| 143 | + }); |
| 144 | + |
| 145 | + it("does not rewrite non-.js files", async () => { |
| 146 | + await expect(shimmedExecFile("/nonexistent/binary")).rejects.toThrow( |
| 147 | + "ENOENT", |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + it("preserves the callback form", async () => { |
| 152 | + const script = path.join(tmp, "echo.js"); |
| 153 | + const stdout = await new Promise<string>((resolve, reject) => { |
| 154 | + mod.execFile(script, (err, out) => |
| 155 | + err ? reject(new Error(err.message)) : resolve(out), |
| 156 | + ); |
| 157 | + }); |
| 158 | + expect(stdout).toBe("ok"); |
| 159 | + }); |
| 160 | + |
| 161 | + it("does not touch spawn", () => { |
| 162 | + expect(mod.spawn).toBe(cp.spawn); |
| 163 | + }); |
| 164 | + }); |
86 | 165 | }); |
0 commit comments