Skip to content

Commit 14f1337

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

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

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

Lines changed: 26 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,34 @@ import {
1517
} from '../index.mts'
1618

1719
const hookScript = new URL('../index.mts', import.meta.url).pathname
20+
const nodeBin = whichSync('node')
21+
if (!nodeBin || typeof nodeBin !== 'string') {
22+
throw new Error('node binary not found on PATH')
23+
}
1824

19-
// Helper: run the full hook as a subprocess
25+
// Helper: run the full hook as a subprocess.
26+
// Uses spawnSync because we need to pipe stdin content (the hook reads JSON from stdin).
2027
function runHook(
2128
toolInput: Record<string, unknown>,
2229
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-
})
30+
): { code: number | null; stdout: string; stderr: string } {
31+
const input = JSON.stringify({
32+
tool_name: toolName,
33+
tool_input: toolInput,
34+
})
35+
const result = spawnSync(nodeBin, [hookScript], {
36+
input,
37+
timeout: 15_000,
38+
stdio: ['pipe', 'pipe', 'pipe'],
39+
})
40+
return {
41+
code: result.status ?? 1,
42+
stdout: typeof result.stdout === 'string' ? result.stdout : result.stdout.toString(),
43+
stderr: typeof result.stderr === 'string' ? result.stderr : result.stderr.toString(),
44+
}
4545
}
4646

47+
4748
// ============================================================================
4849
// Unit tests: extractNewDeps per ecosystem
4950
// ============================================================================

0 commit comments

Comments
 (0)