forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubprocess.test.ts
More file actions
98 lines (81 loc) · 3.07 KB
/
Copy pathsubprocess.test.ts
File metadata and controls
98 lines (81 loc) · 3.07 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
import {
checkSubprocess,
checkSubprocessSync,
runSubprocess,
runSubprocessCapture,
runSubprocessCaptureSync,
} from '../subprocess.js';
import { describe, expect, it } from 'vitest';
describe('runSubprocess', () => {
it('resolves on success (exit code 0)', async () => {
await expect(runSubprocess('true', [], { stdio: 'ignore' })).resolves.toBeUndefined();
});
it('rejects on non-zero exit code', async () => {
await expect(runSubprocess('false', [], { stdio: 'ignore' })).rejects.toThrow('exited with code 1');
});
it('rejects on unknown command', async () => {
await expect(runSubprocess('__nonexistent_command_xyz__', [], { stdio: 'ignore' })).rejects.toThrow();
});
});
describe('checkSubprocess', () => {
it('returns true for exit code 0', async () => {
expect(await checkSubprocess('true', [])).toBe(true);
});
it('returns false for non-zero exit code', async () => {
expect(await checkSubprocess('false', [])).toBe(false);
});
it('returns false for unknown command', async () => {
expect(await checkSubprocess('__nonexistent_command_xyz__', [])).toBe(false);
});
});
describe('runSubprocessCapture', () => {
it('captures stdout', async () => {
const result = await runSubprocessCapture('echo', ['hello']);
expect(result.stdout.trim()).toBe('hello');
expect(result.code).toBe(0);
});
it('captures stderr', async () => {
const result = await runSubprocessCapture('sh', ['-c', 'echo error >&2']);
expect(result.stderr.trim()).toBe('error');
});
it('returns non-zero code for failing command', async () => {
const result = await runSubprocessCapture('false', []);
expect(result.code).not.toBe(0);
});
it('returns code -1 for unknown command', async () => {
const result = await runSubprocessCapture('__nonexistent_command_xyz__', []);
expect(result.code).toBe(-1);
});
it('respects cwd option', async () => {
const result = await runSubprocessCapture('pwd', [], { cwd: '/tmp' });
// /tmp might resolve to /private/tmp on macOS
expect(result.stdout.trim()).toMatch(/\/tmp$/);
expect(result.code).toBe(0);
});
});
describe('runSubprocessCaptureSync', () => {
it('captures stdout synchronously', () => {
const result = runSubprocessCaptureSync('echo', ['hello']);
expect(result.stdout.trim()).toBe('hello');
expect(result.code).toBe(0);
});
it('returns non-zero code for failing command', () => {
const result = runSubprocessCaptureSync('false', []);
expect(result.code).not.toBe(0);
});
it('captures stderr synchronously', () => {
const result = runSubprocessCaptureSync('sh', ['-c', 'echo err >&2']);
expect(result.stderr.trim()).toBe('err');
});
});
describe('checkSubprocessSync', () => {
it('returns true for exit code 0', () => {
expect(checkSubprocessSync('true', [])).toBe(true);
});
it('returns false for non-zero exit code', () => {
expect(checkSubprocessSync('false', [])).toBe(false);
});
it('returns false for unknown command', () => {
expect(checkSubprocessSync('__nonexistent_command_xyz__', [])).toBe(false);
});
});