Skip to content

Commit 41653ab

Browse files
refactor: as co-pilot suggested
test: add unit test
1 parent 5b3df34 commit 41653ab

File tree

2 files changed

+278
-93
lines changed

2 files changed

+278
-93
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/* eslint-disable no-console */
2+
import chalk from 'chalk';
3+
4+
import { SpawnLogger } from './spawnLogged';
5+
6+
describe('tools/link/utils/spawnLogged', () => {
7+
let originalConsoleLog: typeof console.log;
8+
let originalConsoleError: typeof console.error;
9+
let logSpy: jest.SpyInstance;
10+
let errorSpy: jest.SpyInstance;
11+
12+
beforeEach(() => {
13+
originalConsoleLog = console.log;
14+
originalConsoleError = console.error;
15+
logSpy = jest.spyOn(console, 'log').mockImplementation();
16+
errorSpy = jest.spyOn(console, 'error').mockImplementation();
17+
});
18+
19+
afterEach(() => {
20+
logSpy.mockRestore();
21+
errorSpy.mockRestore();
22+
console.log = originalConsoleLog;
23+
console.error = originalConsoleError;
24+
});
25+
26+
test('executes a successful command and logs output', async () => {
27+
const spawnLogger = new SpawnLogger();
28+
29+
await expect(
30+
spawnLogger.spawn('echo', ['hi!'], {
31+
name: 'me',
32+
cwd: process.cwd(),
33+
verbose: false,
34+
}),
35+
).resolves.toBeUndefined();
36+
37+
expect(logSpy).toHaveBeenCalledTimes(2);
38+
39+
expect(logSpy).toHaveBeenNthCalledWith(
40+
1,
41+
'\n' +
42+
`${chalk.cyan('[me]')} ${chalk.dim('→')} ${chalk.dim(process.cwd())}` +
43+
'\n' +
44+
`${chalk.cyan('[me]')} ${chalk.cyan('$')} ${chalk.bold('echo hi!')}` +
45+
'\n',
46+
);
47+
48+
expect(logSpy).toHaveBeenNthCalledWith(
49+
2,
50+
`${chalk.cyan('[me]')} ${chalk.dim('→')} ${chalk.dim(
51+
'finished successfully',
52+
)}` + '\n',
53+
);
54+
});
55+
56+
test('executes a successful command with verbose output', async () => {
57+
const spawnLogger = new SpawnLogger();
58+
59+
await expect(
60+
spawnLogger.spawn('echo', ['hi!'], {
61+
name: 'me',
62+
cwd: process.cwd(),
63+
verbose: true,
64+
}),
65+
).resolves.toBeUndefined();
66+
67+
expect(logSpy).toHaveBeenCalledTimes(3);
68+
69+
expect(logSpy).toHaveBeenNthCalledWith(
70+
1,
71+
'\n' +
72+
`${chalk.cyan('[me]')} ${chalk.dim('→')} ${chalk.dim(process.cwd())}` +
73+
'\n' +
74+
`${chalk.cyan('[me]')} ${chalk.cyan('$')} ${chalk.bold('echo hi!')}` +
75+
'\n',
76+
);
77+
78+
expect(logSpy).toHaveBeenNthCalledWith(
79+
2,
80+
`${chalk.cyan('[me]')} ${chalk.dim('hi!')}`,
81+
);
82+
83+
expect(logSpy).toHaveBeenNthCalledWith(
84+
3,
85+
`${chalk.cyan('[me]')} ${chalk.dim('→')} ${chalk.dim(
86+
'finished successfully',
87+
)}` + '\n',
88+
);
89+
});
90+
91+
test('executes a failing command and throws error', async () => {
92+
const spawnLogger = new SpawnLogger();
93+
94+
await expect(
95+
spawnLogger.spawn('sh', ['-c', 'exit 1'], {
96+
name: 'you',
97+
cwd: process.cwd(),
98+
verbose: false,
99+
}),
100+
).rejects.toThrow('Command failed with exit code 1');
101+
102+
// Should log the command being executed
103+
expect(logSpy).toHaveBeenCalledTimes(2);
104+
105+
expect(logSpy).toHaveBeenNthCalledWith(
106+
1,
107+
'\n' +
108+
`${chalk.cyan('[you]')} ${chalk.dim('→')} ${chalk.dim(process.cwd())}` +
109+
'\n' +
110+
`${chalk.cyan('[you]')} ${chalk.cyan('$')} ${chalk.bold(
111+
'sh -c exit 1',
112+
)}` +
113+
'\n',
114+
);
115+
116+
expect(logSpy).toHaveBeenNthCalledWith(
117+
2,
118+
`${chalk.cyan('[you]')} ${chalk.dim('→')} ${chalk.red(
119+
'finished with exit code 1',
120+
)}\n`,
121+
);
122+
});
123+
124+
test('executes a failing command with verbose output', async () => {
125+
const spawnLogger = new SpawnLogger();
126+
127+
await expect(
128+
spawnLogger.spawn('sh', ['-c', 'echo hi >&2 && exit 10'], {
129+
name: 'you',
130+
cwd: process.cwd(),
131+
verbose: true,
132+
}),
133+
).rejects.toThrow('Command failed with exit code 10');
134+
135+
// Should log the command being executed
136+
expect(logSpy).toHaveBeenCalledTimes(2);
137+
expect(errorSpy).toHaveBeenCalledTimes(1);
138+
139+
expect(errorSpy).toHaveBeenNthCalledWith(
140+
1,
141+
`${chalk.cyan('[you]')} ${chalk.reset('hi')}`,
142+
);
143+
144+
expect(logSpy).toHaveBeenNthCalledWith(
145+
2,
146+
`${chalk.cyan('[you]')} ${chalk.dim('→')} ${chalk.red(
147+
'finished with exit code 10',
148+
)}\n`,
149+
);
150+
});
151+
152+
test('handles environment variables', async () => {
153+
const spawnLogger = new SpawnLogger();
154+
155+
await expect(
156+
spawnLogger.spawn('sh', ['-c', 'echo $TEST_VAR'], {
157+
name: 'env-test',
158+
cwd: process.cwd(),
159+
env: { TEST_VAR: 'test-value', PATH: process.env.PATH },
160+
verbose: true,
161+
}),
162+
).resolves.toBeUndefined();
163+
164+
expect(logSpy).toHaveBeenCalledTimes(3);
165+
166+
expect(logSpy).toHaveBeenNthCalledWith(
167+
2,
168+
`${chalk.cyan('[env-test]')} ${chalk.dim('test-value')}`,
169+
);
170+
});
171+
});

0 commit comments

Comments
 (0)