Skip to content

Commit f1e6e29

Browse files
committed
refactor(tests): use @socketsecurity/lib spawn and whichSync instead of child_process
1 parent c9670ff commit f1e6e29

1 file changed

Lines changed: 23 additions & 25 deletions

File tree

.claude/hooks/check-new-deps/test/extract-deps.test.mts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { describe, it } from 'node:test'
2-
import { strict as assert } from 'node:assert'
3-
import { execFile } from 'node:child_process'
2+
import assert from 'node:assert/strict'
3+
4+
import { whichSync } from '@socketsecurity/lib/bin'
5+
import { spawnSync } from '@socketsecurity/lib/spawn'
46

57
import {
68
cache,
@@ -15,35 +17,31 @@ import {
1517
} from '../index.mts'
1618

1719
const hookScript = new URL('../index.mts', import.meta.url).pathname
20+
const nodeBin = whichSync('node') as string
1821

19-
// Helper: run the full hook as a subprocess
22+
// Helper: run the full hook as a subprocess.
23+
// Uses spawnSync because we need to pipe stdin content (the hook reads JSON from stdin).
2024
function runHook(
2125
toolInput: Record<string, unknown>,
2226
toolName = 'Edit',
23-
): Promise<{ code: number | null; stdout: string; stderr: string }> {
24-
return new Promise((resolve) => {
25-
const child = execFile(
26-
'node',
27-
[hookScript],
28-
{ timeout: 15000 },
29-
(err, stdout, stderr) => {
30-
resolve({
31-
code: child.exitCode
32-
?? (err as NodeJS.ErrnoException)?.code as unknown as number
33-
?? 1,
34-
stdout,
35-
stderr,
36-
})
37-
},
38-
)
39-
child.stdin!.write(JSON.stringify({
40-
tool_name: toolName,
41-
tool_input: toolInput,
42-
}))
43-
child.stdin!.end()
44-
})
27+
): { code: number | null; stdout: string; stderr: string } {
28+
const input = JSON.stringify({
29+
tool_name: toolName,
30+
tool_input: toolInput,
31+
})
32+
const result = spawnSync(nodeBin, [hookScript], {
33+
input,
34+
timeout: 15_000,
35+
stdio: ['pipe', 'pipe', 'pipe'],
36+
})
37+
return {
38+
code: result.status ?? 1,
39+
stdout: typeof result.stdout === 'string' ? result.stdout : result.stdout.toString(),
40+
stderr: typeof result.stderr === 'string' ? result.stderr : result.stderr.toString(),
41+
}
4542
}
4643

44+
4745
// ============================================================================
4846
// Unit tests: extractNewDeps per ecosystem
4947
// ============================================================================

0 commit comments

Comments
 (0)