@smoothbricks/nx-plugin 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +35 -8
- package/dist/bounded-test-policy.d.ts +32 -0
- package/dist/bounded-test-policy.d.ts.map +1 -0
- package/dist/bounded-test-policy.js +184 -0
- package/dist/executors/bounded-exec/executor.d.ts +15 -0
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -0
- package/dist/executors/bounded-exec/executor.js +179 -0
- package/dist/executors/bounded-exec/schema.d.ts +11 -0
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -0
- package/dist/executors/bounded-exec/schema.js +1 -0
- package/dist/generators/bounded-test-targets/generator.d.ts +7 -0
- package/dist/generators/bounded-test-targets/generator.d.ts.map +1 -0
- package/dist/generators/bounded-test-targets/generator.js +40 -0
- package/dist/generators/bun-test-tracing/generator.d.ts.map +1 -1
- package/dist/generators/bun-test-tracing/generator.js +5 -4
- package/dist/generators/create-package/generator.d.ts +4 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -0
- package/dist/generators/create-package/generator.js +204 -0
- package/dist/generators/make-public/generator.d.ts +4 -0
- package/dist/generators/make-public/generator.d.ts.map +1 -0
- package/dist/generators/make-public/generator.js +55 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +78 -7
- package/dist/package-target-policy.d.ts +31 -0
- package/dist/package-target-policy.d.ts.map +1 -0
- package/dist/package-target-policy.js +930 -0
- package/dist/release-config-policy.d.ts +32 -0
- package/dist/release-config-policy.d.ts.map +1 -0
- package/dist/release-config-policy.js +225 -0
- package/dist/typecheck-test-policy.d.ts +56 -0
- package/dist/typecheck-test-policy.d.ts.map +1 -0
- package/dist/typecheck-test-policy.js +640 -0
- package/dist/version-actions.cjs +78 -0
- package/dist/version-actions.d.cts +6 -0
- package/dist/version-actions.d.cts.map +1 -0
- package/dist/workspace-config-policy.d.ts +33 -0
- package/dist/workspace-config-policy.d.ts.map +1 -0
- package/dist/workspace-config-policy.js +334 -0
- package/executors.json +9 -0
- package/generators.json +15 -0
- package/package.json +45 -11
- package/src/bounded-test-policy.test.ts +257 -0
- package/src/bounded-test-policy.ts +257 -0
- package/src/bun-bootstrap.test.ts +52 -0
- package/src/executors/bounded-exec/executor.test.ts +258 -0
- package/src/executors/bounded-exec/executor.ts +228 -0
- package/src/executors/bounded-exec/schema.json +68 -0
- package/src/executors/bounded-exec/schema.ts +10 -0
- package/src/generators/bounded-test-targets/generator.test.ts +136 -0
- package/src/generators/bounded-test-targets/generator.ts +67 -0
- package/src/generators/bounded-test-targets/schema.d.ts +3 -0
- package/src/generators/bounded-test-targets/schema.json +14 -0
- package/src/generators/bun-test-tracing/generator.test.ts +25 -1
- package/src/generators/bun-test-tracing/generator.ts +6 -5
- package/src/generators/create-package/generator.test.ts +206 -0
- package/src/generators/create-package/generator.ts +251 -0
- package/src/generators/create-package/schema.d.ts +5 -0
- package/src/generators/create-package/schema.json +24 -0
- package/src/generators/make-public/generator.test.ts +137 -0
- package/src/generators/make-public/generator.ts +83 -0
- package/src/generators/make-public/schema.d.ts +3 -0
- package/src/generators/make-public/schema.json +13 -0
- package/src/index.test.ts +85 -18
- package/src/index.ts +99 -8
- package/src/package-target-policy.test.ts +778 -0
- package/src/package-target-policy.ts +1127 -0
- package/src/release-config-policy.test.ts +280 -0
- package/src/release-config-policy.ts +258 -0
- package/src/typecheck-test-policy.test.ts +797 -0
- package/src/typecheck-test-policy.ts +752 -0
- package/src/version-actions.cts +94 -0
- package/src/workspace-config-policy.test.ts +376 -0
- package/src/workspace-config-policy.ts +390 -0
- package/src/test-suite-tracer.ts +0 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { access, mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { dirname, join } from 'node:path';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
type BoundedExecContext,
|
|
8
|
+
createProcessTreeKiller,
|
|
9
|
+
type ProcessTreeKiller,
|
|
10
|
+
runBoundedExec,
|
|
11
|
+
} from './executor.js';
|
|
12
|
+
|
|
13
|
+
const workspaces: string[] = [];
|
|
14
|
+
const originalStdoutWrite = process.stdout.write;
|
|
15
|
+
const originalStderrWrite = process.stderr.write;
|
|
16
|
+
|
|
17
|
+
describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
18
|
+
afterEach(async () => {
|
|
19
|
+
process.stdout.write = originalStdoutWrite;
|
|
20
|
+
process.stderr.write = originalStderrWrite;
|
|
21
|
+
await Promise.all(workspaces.splice(0).map((workspace) => rm(workspace, { recursive: true, force: true })));
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('runs a successful shell command with cwd and env', async () => {
|
|
25
|
+
const workspace = await createWorkspace();
|
|
26
|
+
await workspace.write(
|
|
27
|
+
'package/print.js',
|
|
28
|
+
'console.log(process.cwd()); console.log(process.env.BOUNDED_EXEC_VALUE);\n',
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const result = await runBoundedExec(
|
|
32
|
+
{
|
|
33
|
+
command: 'node print.js',
|
|
34
|
+
cwd: 'package',
|
|
35
|
+
env: { BOUNDED_EXEC_VALUE: 'from-env' },
|
|
36
|
+
timeoutMs: 5_000,
|
|
37
|
+
},
|
|
38
|
+
workspace.context,
|
|
39
|
+
createProcessTreeKiller(),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
expect(result.success).toBe(true);
|
|
43
|
+
expect(result.terminalOutput).toContain(join(workspace.root, 'package'));
|
|
44
|
+
expect(result.terminalOutput).toContain('from-env');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('returns failure for a nonzero command', async () => {
|
|
48
|
+
const workspace = await createWorkspace();
|
|
49
|
+
|
|
50
|
+
const result = await runBoundedExec(
|
|
51
|
+
{ command: 'node -e "process.exit(7)"', timeoutMs: 5_000 },
|
|
52
|
+
workspace.context,
|
|
53
|
+
createProcessTreeKiller(),
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
expect(result.success).toBe(false);
|
|
57
|
+
expect(result.terminalOutput).toContain('Command exited with status 7');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('streams stdout and stderr while collecting terminal output', async () => {
|
|
61
|
+
const workspace = await createWorkspace();
|
|
62
|
+
const stdout: string[] = [];
|
|
63
|
+
const stderr: string[] = [];
|
|
64
|
+
process.stdout.write = captureWrite(stdout);
|
|
65
|
+
process.stderr.write = captureWrite(stderr);
|
|
66
|
+
|
|
67
|
+
const result = await runBoundedExec(
|
|
68
|
+
{ command: "node -e \"console.log('out-value'); console.error('err-value')\"", timeoutMs: 5_000 },
|
|
69
|
+
workspace.context,
|
|
70
|
+
createProcessTreeKiller(),
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
expect(result.success).toBe(true);
|
|
74
|
+
expect(stdout.join('')).toContain('out-value');
|
|
75
|
+
expect(stderr.join('')).toContain('err-value');
|
|
76
|
+
expect(result.terminalOutput).toContain('out-value');
|
|
77
|
+
expect(result.terminalOutput).toContain('err-value');
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('fails on timeout and reports bounded execution details', async () => {
|
|
81
|
+
const workspace = await createWorkspace();
|
|
82
|
+
|
|
83
|
+
const result = await runBoundedExec(
|
|
84
|
+
{ command: 'node -e "setTimeout(() => {}, 5000)"', timeoutMs: 50, killAfterMs: 0 },
|
|
85
|
+
workspace.context,
|
|
86
|
+
createProcessTreeKiller(),
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
expect(result.success).toBe(false);
|
|
90
|
+
expect(result.terminalOutput).toContain('Command timed out after');
|
|
91
|
+
expect(result.terminalOutput).toContain('timeoutMs=50');
|
|
92
|
+
expect(result.terminalOutput).toContain(`cwd=${workspace.root}`);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('uses graceful timeout termination before force-killing on POSIX', async () => {
|
|
96
|
+
if (process.platform === 'win32') {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const workspace = await createWorkspace();
|
|
101
|
+
const calls: string[] = [];
|
|
102
|
+
const killer: ProcessTreeKiller = {
|
|
103
|
+
async kill(pid, signal) {
|
|
104
|
+
calls.push(signal);
|
|
105
|
+
process.kill(-pid, signal);
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const result = await runBoundedExec(
|
|
110
|
+
{
|
|
111
|
+
command: 'node -e "process.on(\'SIGTERM\', () => process.exit(0)); setTimeout(() => {}, 5000)"',
|
|
112
|
+
timeoutMs: 50,
|
|
113
|
+
killAfterMs: 500,
|
|
114
|
+
},
|
|
115
|
+
workspace.context,
|
|
116
|
+
killer,
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
expect(result.success).toBe(false);
|
|
120
|
+
expect(calls[0]).toBe('SIGTERM');
|
|
121
|
+
expect(calls.every((signal) => signal === 'SIGTERM' || signal === 'SIGKILL')).toBe(true);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('force-kills after killAfterMs when graceful termination is ignored on POSIX', async () => {
|
|
125
|
+
if (process.platform === 'win32') {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
const workspace = await createWorkspace();
|
|
130
|
+
const calls: string[] = [];
|
|
131
|
+
const killer: ProcessTreeKiller = {
|
|
132
|
+
async kill(pid, signal) {
|
|
133
|
+
calls.push(signal);
|
|
134
|
+
process.kill(-pid, signal);
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
const result = await runBoundedExec(
|
|
139
|
+
{
|
|
140
|
+
command: 'node -e "process.on(\'SIGTERM\', () => {}); setTimeout(() => {}, 5000)"',
|
|
141
|
+
timeoutMs: 50,
|
|
142
|
+
killAfterMs: 10,
|
|
143
|
+
},
|
|
144
|
+
workspace.context,
|
|
145
|
+
killer,
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
expect(result.success).toBe(false);
|
|
149
|
+
expect(calls).toEqual(['SIGTERM', 'SIGKILL']);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('forwards args by default and can suppress unparsed args', async () => {
|
|
153
|
+
const workspace = await createWorkspace();
|
|
154
|
+
|
|
155
|
+
const forwarded = await runBoundedExec(
|
|
156
|
+
{
|
|
157
|
+
command: 'node -e "console.log(process.argv.slice(1).join(\',\'))"',
|
|
158
|
+
args: ['first'],
|
|
159
|
+
__unparsed__: ['second'],
|
|
160
|
+
timeoutMs: 5_000,
|
|
161
|
+
},
|
|
162
|
+
workspace.context,
|
|
163
|
+
createProcessTreeKiller(),
|
|
164
|
+
);
|
|
165
|
+
const suppressed = await runBoundedExec(
|
|
166
|
+
{
|
|
167
|
+
command: 'node -e "console.log(process.argv.slice(1).join(\',\'))"',
|
|
168
|
+
args: ['first'],
|
|
169
|
+
__unparsed__: ['second'],
|
|
170
|
+
forwardAllArgs: false,
|
|
171
|
+
timeoutMs: 5_000,
|
|
172
|
+
},
|
|
173
|
+
workspace.context,
|
|
174
|
+
createProcessTreeKiller(),
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
expect(forwarded.success).toBe(true);
|
|
178
|
+
expect(forwarded.terminalOutput).toContain('first,second');
|
|
179
|
+
expect(suppressed.success).toBe(true);
|
|
180
|
+
expect(suppressed.terminalOutput).toContain('first');
|
|
181
|
+
expect(suppressed.terminalOutput).not.toContain('second');
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('kills a POSIX child process group on timeout', async () => {
|
|
185
|
+
if (process.platform === 'win32') {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const workspace = await createWorkspace();
|
|
190
|
+
const marker = join(workspace.root, 'marker.txt');
|
|
191
|
+
await workspace.write(
|
|
192
|
+
'spawn-child.js',
|
|
193
|
+
[
|
|
194
|
+
"import { spawn } from 'node:child_process';",
|
|
195
|
+
"spawn(process.execPath, ['-e', `setTimeout(() => require('node:fs').writeFileSync(process.argv[1], 'alive'), 700)` , process.argv[2]], { stdio: 'ignore' });",
|
|
196
|
+
'setTimeout(() => {}, 5000);',
|
|
197
|
+
'',
|
|
198
|
+
].join('\n'),
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
const result = await runBoundedExec(
|
|
202
|
+
{ command: `node spawn-child.js ${marker}`, timeoutMs: 50, killAfterMs: 50 },
|
|
203
|
+
workspace.context,
|
|
204
|
+
createProcessTreeKiller(),
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
await sleep(1_000);
|
|
208
|
+
|
|
209
|
+
expect(result.success).toBe(false);
|
|
210
|
+
expect(await exists(marker)).toBe(false);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
interface WorkspaceFixture {
|
|
215
|
+
root: string;
|
|
216
|
+
context: BoundedExecContext;
|
|
217
|
+
write(filePath: string, contents: string): Promise<void>;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async function createWorkspace(): Promise<WorkspaceFixture> {
|
|
221
|
+
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-exec-'));
|
|
222
|
+
workspaces.push(root);
|
|
223
|
+
return {
|
|
224
|
+
root,
|
|
225
|
+
context: { root },
|
|
226
|
+
async write(filePath: string, contents: string): Promise<void> {
|
|
227
|
+
const absolutePath = join(root, filePath);
|
|
228
|
+
await mkdir(dirname(absolutePath), { recursive: true });
|
|
229
|
+
await writeFile(absolutePath, contents);
|
|
230
|
+
},
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function captureWrite(chunks: string[]): typeof process.stdout.write {
|
|
235
|
+
return ((
|
|
236
|
+
chunk: string | Uint8Array,
|
|
237
|
+
encodingOrCallback?: BufferEncoding | ((error?: Error | null) => void),
|
|
238
|
+
callback?: (error?: Error | null) => void,
|
|
239
|
+
) => {
|
|
240
|
+
chunks.push(chunk.toString());
|
|
241
|
+
const cb = typeof encodingOrCallback === 'function' ? encodingOrCallback : callback;
|
|
242
|
+
cb?.();
|
|
243
|
+
return true;
|
|
244
|
+
}) as typeof process.stdout.write;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async function exists(path: string): Promise<boolean> {
|
|
248
|
+
try {
|
|
249
|
+
await access(path);
|
|
250
|
+
return true;
|
|
251
|
+
} catch {
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function sleep(ms: number): Promise<void> {
|
|
257
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
258
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { isAbsolute, join } from 'node:path';
|
|
3
|
+
|
|
4
|
+
import type { BoundedExecOptions } from './schema.js';
|
|
5
|
+
|
|
6
|
+
const DEFAULT_KILL_AFTER_MS = 10_000;
|
|
7
|
+
const EXIT_CODE_BY_SIGNAL: Partial<Record<NodeJS.Signals, number>> = {
|
|
8
|
+
SIGHUP: 129,
|
|
9
|
+
SIGINT: 130,
|
|
10
|
+
SIGTERM: 143,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export interface BoundedExecContext {
|
|
14
|
+
root: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface BoundedExecResult {
|
|
18
|
+
success: boolean;
|
|
19
|
+
terminalOutput: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
interface RunState {
|
|
23
|
+
settled: boolean;
|
|
24
|
+
timedOut: boolean;
|
|
25
|
+
forceKillNeeded: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface ProcessTreeKiller {
|
|
29
|
+
kill(pid: number, signal: NodeJS.Signals): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export default function boundedExecExecutor(
|
|
33
|
+
options: BoundedExecOptions,
|
|
34
|
+
context: BoundedExecContext,
|
|
35
|
+
): Promise<BoundedExecResult> {
|
|
36
|
+
return runBoundedExec(options, context, createProcessTreeKiller());
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function runBoundedExec(
|
|
40
|
+
options: BoundedExecOptions,
|
|
41
|
+
context: BoundedExecContext,
|
|
42
|
+
killer: ProcessTreeKiller,
|
|
43
|
+
): Promise<BoundedExecResult> {
|
|
44
|
+
const cwd = resolveCwd(options.cwd, context.root);
|
|
45
|
+
const command = buildCommand(options);
|
|
46
|
+
const timeoutMs = options.timeoutMs;
|
|
47
|
+
const killAfterMs = options.killAfterMs ?? DEFAULT_KILL_AFTER_MS;
|
|
48
|
+
const startedAt = Date.now();
|
|
49
|
+
const outputChunks: string[] = [];
|
|
50
|
+
const state: RunState = { settled: false, timedOut: false, forceKillNeeded: false };
|
|
51
|
+
|
|
52
|
+
const child = spawn(command, [], {
|
|
53
|
+
cwd,
|
|
54
|
+
env: mergeEnv(options.env),
|
|
55
|
+
shell: true,
|
|
56
|
+
detached: process.platform !== 'win32',
|
|
57
|
+
windowsHide: true,
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const appendStdout = (chunk: Buffer | string): void => {
|
|
61
|
+
const text = chunk.toString();
|
|
62
|
+
outputChunks.push(text);
|
|
63
|
+
process.stdout.write(text);
|
|
64
|
+
};
|
|
65
|
+
const appendStderr = (chunk: Buffer | string): void => {
|
|
66
|
+
const text = chunk.toString();
|
|
67
|
+
outputChunks.push(text);
|
|
68
|
+
process.stderr.write(text);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
child.stdout?.on('data', appendStdout);
|
|
72
|
+
child.stderr?.on('data', appendStderr);
|
|
73
|
+
|
|
74
|
+
const killChildTree = async (force: boolean): Promise<void> => {
|
|
75
|
+
const pid = child.pid;
|
|
76
|
+
if (!pid) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
await killer.kill(pid, force ? 'SIGKILL' : 'SIGTERM');
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const onProcessExit = (): void => {
|
|
83
|
+
void killChildTree(false);
|
|
84
|
+
};
|
|
85
|
+
const onTerminationSignal = (signal: NodeJS.Signals): void => {
|
|
86
|
+
removeSignalHandlers();
|
|
87
|
+
void killChildTree(false).finally(() => process.kill(process.pid, signal));
|
|
88
|
+
};
|
|
89
|
+
const onSigint = (): void => onTerminationSignal('SIGINT');
|
|
90
|
+
const onSigterm = (): void => onTerminationSignal('SIGTERM');
|
|
91
|
+
const onSighup = (): void => onTerminationSignal('SIGHUP');
|
|
92
|
+
const removeSignalHandlers = (): void => {
|
|
93
|
+
process.removeListener('exit', onProcessExit);
|
|
94
|
+
process.removeListener('SIGINT', onSigint);
|
|
95
|
+
process.removeListener('SIGTERM', onSigterm);
|
|
96
|
+
process.removeListener('SIGHUP', onSighup);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
process.on('exit', onProcessExit);
|
|
100
|
+
process.on('SIGINT', onSigint);
|
|
101
|
+
process.on('SIGTERM', onSigterm);
|
|
102
|
+
process.on('SIGHUP', onSighup);
|
|
103
|
+
|
|
104
|
+
let timeout: NodeJS.Timeout | undefined;
|
|
105
|
+
const timeoutPromise = new Promise<void>((resolve) => {
|
|
106
|
+
timeout = setTimeout(() => {
|
|
107
|
+
state.timedOut = true;
|
|
108
|
+
const elapsedMs = Date.now() - startedAt;
|
|
109
|
+
appendStderr(`\nCommand timed out after ${elapsedMs}ms (timeoutMs=${timeoutMs}, cwd=${cwd}): ${command}\n`);
|
|
110
|
+
void (async () => {
|
|
111
|
+
await ignoreKillError(killChildTree(false));
|
|
112
|
+
if (!state.settled && killAfterMs > 0) {
|
|
113
|
+
await delay(killAfterMs);
|
|
114
|
+
}
|
|
115
|
+
if (!state.settled) {
|
|
116
|
+
state.forceKillNeeded = true;
|
|
117
|
+
appendStderr(`Force-killing timed out command after killAfterMs=${killAfterMs}: ${command}\n`);
|
|
118
|
+
await ignoreKillError(killChildTree(true));
|
|
119
|
+
}
|
|
120
|
+
resolve();
|
|
121
|
+
})();
|
|
122
|
+
}, timeoutMs);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const exitPromise = new Promise<{ code: number | null; signal: NodeJS.Signals | null }>((resolve) => {
|
|
126
|
+
child.once('error', (error) => {
|
|
127
|
+
appendStderr(`${error.message}\n`);
|
|
128
|
+
resolve({ code: 1, signal: null });
|
|
129
|
+
});
|
|
130
|
+
child.once('exit', (code, signal) => {
|
|
131
|
+
resolve({ code, signal });
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
const exit = await exitPromise;
|
|
136
|
+
const exitedGracefullyAfterTimeout = state.timedOut && exit.code === 0 && exit.signal === null;
|
|
137
|
+
state.settled = !state.timedOut || exitedGracefullyAfterTimeout;
|
|
138
|
+
if (timeout) {
|
|
139
|
+
clearTimeout(timeout);
|
|
140
|
+
}
|
|
141
|
+
removeSignalHandlers();
|
|
142
|
+
|
|
143
|
+
if (state.timedOut) {
|
|
144
|
+
await timeoutPromise;
|
|
145
|
+
state.settled = true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const code = exit.code ?? signalToExitCode(exit.signal);
|
|
149
|
+
if (code !== 0 && !state.timedOut) {
|
|
150
|
+
appendStderr(`Command exited with status ${code}: ${command}\n`);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (state.timedOut && !state.forceKillNeeded) {
|
|
154
|
+
appendStderr(`Timed out command exited after graceful termination: ${command}\n`);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return {
|
|
158
|
+
success: !state.timedOut && code === 0,
|
|
159
|
+
terminalOutput: outputChunks.join(''),
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function createProcessTreeKiller(): ProcessTreeKiller {
|
|
164
|
+
return {
|
|
165
|
+
kill(pid, signal) {
|
|
166
|
+
return killProcessGroup(pid, signal);
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function killProcessGroup(pid: number, signal: NodeJS.Signals): Promise<void> {
|
|
172
|
+
return new Promise((resolve) => {
|
|
173
|
+
try {
|
|
174
|
+
// Send signal to the entire process group (negative PID).
|
|
175
|
+
// The executor spawns with detached: true on POSIX, which creates a
|
|
176
|
+
// dedicated process group. Signaling -pid is atomic and catches all
|
|
177
|
+
// descendants, including grandchildren that tree-walk libraries miss
|
|
178
|
+
// when parents die and children get reparented to init.
|
|
179
|
+
process.kill(-pid, signal);
|
|
180
|
+
} catch {
|
|
181
|
+
// ESRCH: process group already exited between timeout and kill.
|
|
182
|
+
}
|
|
183
|
+
resolve();
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function resolveCwd(cwd: string | undefined, root: string): string {
|
|
188
|
+
if (!cwd) {
|
|
189
|
+
return root;
|
|
190
|
+
}
|
|
191
|
+
return isAbsolute(cwd) ? cwd : join(root, cwd);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function buildCommand(options: BoundedExecOptions): string {
|
|
195
|
+
const parts = [options.command];
|
|
196
|
+
if (Array.isArray(options.args)) {
|
|
197
|
+
parts.push(...options.args);
|
|
198
|
+
} else if (options.args) {
|
|
199
|
+
parts.push(options.args);
|
|
200
|
+
}
|
|
201
|
+
if (options.forwardAllArgs !== false && options.__unparsed__?.length) {
|
|
202
|
+
parts.push(...options.__unparsed__);
|
|
203
|
+
}
|
|
204
|
+
return parts.join(' ');
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function mergeEnv(env: Record<string, string> | undefined): NodeJS.ProcessEnv {
|
|
208
|
+
return env ? { ...process.env, ...env } : process.env;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function signalToExitCode(signal: NodeJS.Signals | null): number {
|
|
212
|
+
if (!signal) {
|
|
213
|
+
return 1;
|
|
214
|
+
}
|
|
215
|
+
return EXIT_CODE_BY_SIGNAL[signal] ?? 1;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
async function ignoreKillError(promise: Promise<void>): Promise<void> {
|
|
219
|
+
try {
|
|
220
|
+
await promise;
|
|
221
|
+
} catch {
|
|
222
|
+
// The process tree may have already exited between timeout and kill.
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function delay(ms: number): Promise<void> {
|
|
227
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
228
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 2,
|
|
3
|
+
"title": "Bounded Exec",
|
|
4
|
+
"description": "Run one shell command with a hard timeout and process-tree cleanup.",
|
|
5
|
+
"type": "object",
|
|
6
|
+
"cli": "nx",
|
|
7
|
+
"outputCapture": "pipe",
|
|
8
|
+
"additionalProperties": true,
|
|
9
|
+
"required": ["command", "timeoutMs"],
|
|
10
|
+
"properties": {
|
|
11
|
+
"command": {
|
|
12
|
+
"type": "string",
|
|
13
|
+
"description": "Shell command to run."
|
|
14
|
+
},
|
|
15
|
+
"cwd": {
|
|
16
|
+
"type": "string",
|
|
17
|
+
"description": "Working directory. Relative paths are resolved from the workspace root."
|
|
18
|
+
},
|
|
19
|
+
"env": {
|
|
20
|
+
"type": "object",
|
|
21
|
+
"description": "Environment variables made available to the command.",
|
|
22
|
+
"additionalProperties": {
|
|
23
|
+
"type": "string"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"timeoutMs": {
|
|
27
|
+
"type": "number",
|
|
28
|
+
"minimum": 1,
|
|
29
|
+
"description": "Hard wall-clock timeout in milliseconds."
|
|
30
|
+
},
|
|
31
|
+
"killAfterMs": {
|
|
32
|
+
"type": "number",
|
|
33
|
+
"minimum": 0,
|
|
34
|
+
"default": 10000,
|
|
35
|
+
"description": "Milliseconds to wait after graceful termination before force-killing the process tree."
|
|
36
|
+
},
|
|
37
|
+
"forwardAllArgs": {
|
|
38
|
+
"type": "boolean",
|
|
39
|
+
"default": true,
|
|
40
|
+
"description": "Append unparsed Nx CLI arguments to the command."
|
|
41
|
+
},
|
|
42
|
+
"args": {
|
|
43
|
+
"description": "Extra arguments appended to the command.",
|
|
44
|
+
"oneOf": [
|
|
45
|
+
{
|
|
46
|
+
"type": "string"
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
"type": "array",
|
|
50
|
+
"items": {
|
|
51
|
+
"type": "string"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
"__unparsed__": {
|
|
57
|
+
"hidden": true,
|
|
58
|
+
"type": "array",
|
|
59
|
+
"items": {
|
|
60
|
+
"type": "string"
|
|
61
|
+
},
|
|
62
|
+
"$default": {
|
|
63
|
+
"$source": "unparsed"
|
|
64
|
+
},
|
|
65
|
+
"x-priority": "internal"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { addProjectConfiguration, readJson, type Tree } from 'nx/src/devkit-exports.js';
|
|
3
|
+
import { createTreeWithEmptyWorkspace } from 'nx/src/devkit-testing-exports.js';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
BOUNDED_TEST_EXECUTOR,
|
|
7
|
+
BOUNDED_TEST_KILL_AFTER_MS,
|
|
8
|
+
BOUNDED_TEST_TIMEOUT_MS,
|
|
9
|
+
} from '../../bounded-test-policy.js';
|
|
10
|
+
import generator from './generator.js';
|
|
11
|
+
|
|
12
|
+
describe('bounded-test-targets generator', () => {
|
|
13
|
+
let tree: Tree;
|
|
14
|
+
|
|
15
|
+
beforeEach(() => {
|
|
16
|
+
tree = createTreeWithEmptyWorkspace();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('normalizes test target from existing run-commands target', async () => {
|
|
20
|
+
addProject(tree, 'example', 'packages/example');
|
|
21
|
+
writeJsonFile(tree, 'packages/example/package.json', {
|
|
22
|
+
name: '@scope/example',
|
|
23
|
+
scripts: { test: 'bun test --script' },
|
|
24
|
+
nx: {
|
|
25
|
+
name: 'example',
|
|
26
|
+
targets: {
|
|
27
|
+
test: {
|
|
28
|
+
executor: 'nx:run-commands',
|
|
29
|
+
dependsOn: ['typecheck-tests', '^build'],
|
|
30
|
+
options: { command: 'bun test --target', cwd: 'packages/example' },
|
|
31
|
+
},
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
await generator(tree, { project: '@scope/example' });
|
|
37
|
+
|
|
38
|
+
const packageJson = readJson(tree, 'packages/example/package.json');
|
|
39
|
+
expect(packageJson.scripts?.test).toBe('nx run example:test --tui=false --outputStyle=stream');
|
|
40
|
+
expect(packageJson.nx?.targets?.test).toEqual({
|
|
41
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
42
|
+
dependsOn: ['typecheck-tests', '^build'],
|
|
43
|
+
options: {
|
|
44
|
+
command: 'bun test --target',
|
|
45
|
+
cwd: '{projectRoot}',
|
|
46
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
47
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('normalizes test target from direct script', async () => {
|
|
53
|
+
addProject(tree, 'example', 'packages/example');
|
|
54
|
+
writeJsonFile(tree, 'packages/example/package.json', {
|
|
55
|
+
name: '@scope/example',
|
|
56
|
+
scripts: { test: 'bun test --script' },
|
|
57
|
+
nx: { name: 'example' },
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
await generator(tree, { project: 'packages/example' });
|
|
61
|
+
|
|
62
|
+
const packageJson = readJson(tree, 'packages/example/package.json');
|
|
63
|
+
expect(packageJson.scripts?.test).toBe('nx run example:test --tui=false --outputStyle=stream');
|
|
64
|
+
expect(packageJson.nx?.targets?.test).toEqual({
|
|
65
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
66
|
+
options: {
|
|
67
|
+
command: 'bun test --script',
|
|
68
|
+
cwd: '{projectRoot}',
|
|
69
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
70
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('normalizes project.json test target and rewrites package script alias', async () => {
|
|
76
|
+
addProject(tree, 'example', 'packages/example', { keepProjectJson: true });
|
|
77
|
+
writeJsonFile(tree, 'packages/example/package.json', {
|
|
78
|
+
name: '@scope/example',
|
|
79
|
+
scripts: { test: 'bun test --script' },
|
|
80
|
+
nx: {
|
|
81
|
+
targets: {
|
|
82
|
+
test: {
|
|
83
|
+
executor: 'nx:run-commands',
|
|
84
|
+
options: { command: 'bun test --package-target' },
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
writeJsonFile(tree, 'packages/example/project.json', {
|
|
90
|
+
name: 'example',
|
|
91
|
+
targets: {
|
|
92
|
+
test: {
|
|
93
|
+
executor: 'nx:run-commands',
|
|
94
|
+
dependsOn: ['typecheck-tests'],
|
|
95
|
+
options: { command: 'bun test --project-target', cwd: 'packages/example' },
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
await generator(tree, { project: 'example' });
|
|
101
|
+
|
|
102
|
+
const packageJson = readJson(tree, 'packages/example/package.json');
|
|
103
|
+
const projectJson = readJson(tree, 'packages/example/project.json');
|
|
104
|
+
expect(packageJson.scripts?.test).toBe('nx run example:test --tui=false --outputStyle=stream');
|
|
105
|
+
expect(packageJson.nx?.targets?.test).toEqual({
|
|
106
|
+
executor: 'nx:run-commands',
|
|
107
|
+
options: { command: 'bun test --package-target' },
|
|
108
|
+
});
|
|
109
|
+
expect(projectJson.targets?.test).toEqual({
|
|
110
|
+
executor: BOUNDED_TEST_EXECUTOR,
|
|
111
|
+
dependsOn: ['typecheck-tests'],
|
|
112
|
+
options: {
|
|
113
|
+
command: 'bun test --project-target',
|
|
114
|
+
cwd: '{projectRoot}',
|
|
115
|
+
timeoutMs: BOUNDED_TEST_TIMEOUT_MS,
|
|
116
|
+
killAfterMs: BOUNDED_TEST_KILL_AFTER_MS,
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
function addProject(tree: Tree, name: string, root: string, options: { keepProjectJson?: boolean } = {}): void {
|
|
123
|
+
addProjectConfiguration(tree, name, {
|
|
124
|
+
root,
|
|
125
|
+
sourceRoot: `${root}/src`,
|
|
126
|
+
projectType: 'library',
|
|
127
|
+
targets: {},
|
|
128
|
+
});
|
|
129
|
+
if (!options.keepProjectJson && tree.exists(`${root}/project.json`)) {
|
|
130
|
+
tree.delete(`${root}/project.json`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function writeJsonFile(tree: Tree, filePath: string, value: unknown): void {
|
|
135
|
+
tree.write(filePath, `${JSON.stringify(value, null, 2)}\n`);
|
|
136
|
+
}
|