-
Notifications
You must be signed in to change notification settings - Fork 450
Expand file tree
/
Copy pathtest-isolated.ts
More file actions
131 lines (118 loc) · 4.12 KB
/
test-isolated.ts
File metadata and controls
131 lines (118 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* Filesystem-based test isolated sandbox provider.
*
* Uses a temp directory on the local filesystem as the "sandbox".
* Intended for testing the isolated provider abstraction without
* requiring a real remote environment.
*/
import { execFile, spawn } from "node:child_process";
import { copyFile, cp, mkdir, mkdtemp, rm, stat } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { createInterface } from "node:readline";
import {
createIsolatedSandboxProvider,
type ExecResult,
type IsolatedSandboxHandle,
type IsolatedSandboxProvider,
} from "../SandboxProvider.js";
/**
* Create a filesystem-based test isolated sandbox provider.
*
* The "sandbox" is a temp directory. `exec` runs shell commands in it,
* `copyIn`/`copyFileOut` copy files between host and the temp dir,
* and `close` removes the temp dir.
*/
export const testIsolated = (): IsolatedSandboxProvider =>
createIsolatedSandboxProvider({
name: "test-isolated",
create: async (): Promise<IsolatedSandboxHandle> => {
const sandboxRoot = await mkdtemp(join(tmpdir(), "sandcastle-test-"));
const worktreePath = join(sandboxRoot, "workspace");
await mkdir(worktreePath, { recursive: true });
return {
worktreePath,
exec: (
command: string,
options?: {
onLine?: (line: string) => void;
cwd?: string;
sudo?: boolean;
},
): Promise<ExecResult> => {
if (options?.onLine) {
const onLine = options.onLine;
return new Promise((resolve, reject) => {
const proc = spawn("sh", ["-c", command], {
cwd: options?.cwd ?? worktreePath,
stdio: ["ignore", "pipe", "pipe"],
});
const stdoutChunks: string[] = [];
const stderrChunks: string[] = [];
const rl = createInterface({ input: proc.stdout! });
rl.on("line", (line) => {
stdoutChunks.push(line);
onLine(line);
});
proc.stderr!.on("data", (chunk: Buffer) => {
stderrChunks.push(chunk.toString());
});
proc.on("error", (error) => {
reject(new Error(`exec failed: ${error.message}`));
});
proc.on("close", (code) => {
resolve({
stdout: stdoutChunks.join("\n"),
stderr: stderrChunks.join(""),
exitCode: code ?? 0,
});
});
});
}
return new Promise((resolve, reject) => {
execFile(
"sh",
["-c", command],
{
cwd: options?.cwd ?? worktreePath,
maxBuffer: 10 * 1024 * 1024,
},
(error, stdout, stderr) => {
if (error && error.code === undefined) {
reject(new Error(`exec failed: ${error.message}`));
} else {
resolve({
stdout: stdout.toString(),
stderr: stderr.toString(),
exitCode: typeof error?.code === "number" ? error.code : 0,
});
}
},
);
});
},
copyIn: async (
hostPath: string,
sandboxPath: string,
): Promise<void> => {
const info = await stat(hostPath);
if (info.isDirectory()) {
await cp(hostPath, sandboxPath, { recursive: true });
} else {
await mkdir(dirname(sandboxPath), { recursive: true });
await copyFile(hostPath, sandboxPath);
}
},
copyFileOut: async (
sandboxPath: string,
hostPath: string,
): Promise<void> => {
await mkdir(dirname(hostPath), { recursive: true });
await copyFile(sandboxPath, hostPath);
},
close: async (): Promise<void> => {
await rm(sandboxRoot, { recursive: true, force: true });
},
};
},
});