-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell-settings.test.mjs
More file actions
271 lines (226 loc) · 8.18 KB
/
shell-settings.test.mjs
File metadata and controls
271 lines (226 loc) · 8.18 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { test, expect, describe, beforeEach } from 'bun:test';
import './test-helper.mjs'; // Automatically sets up beforeEach/afterEach cleanup
import { $, shell, set, unset } from '../src/$.mjs';
describe('Shell Settings (set -e / set +e equivalent)', () => {
beforeEach(() => {
// Reset all shell settings before each test
shell.errexit(false);
shell.verbose(false);
shell.xtrace(false);
shell.pipefail(false);
shell.nounset(false);
});
describe('Error Handling (set -e / set +e)', () => {
test('should continue execution by default (like bash without set -e)', async () => {
const result1 = await $`exit 1`;
expect(result1.code).toBe(1);
const result2 = await $`echo "continued"`;
expect(result2.code).toBe(0);
expect(result2.stdout.trim()).toBe('continued');
});
test('should throw on error when errexit enabled (set -e)', async () => {
shell.errexit(true);
try {
await $`exit 42`;
expect(true).toBe(false); // Should not reach here
} catch (error) {
expect(error.code).toBe(42);
expect(error.message).toContain('Command failed with exit code 42');
expect(error.result).toBeDefined();
expect(error.result.code).toBe(42);
}
});
test('should stop throwing when errexit disabled (set +e)', async () => {
shell.errexit(true);
shell.errexit(false);
const result = await $`exit 1`;
expect(result.code).toBe(1);
// Should not throw
});
test('should allow mid-script changes (like bash)', async () => {
// Start without errexit
const result1 = await $`exit 1`;
expect(result1.code).toBe(1);
// Enable errexit
shell.errexit(true);
try {
await $`exit 2`;
expect(true).toBe(false); // Should not reach here
} catch (error) {
expect(error.code).toBe(2);
}
// Disable errexit again
shell.errexit(false);
const result3 = await $`exit 3`;
expect(result3.code).toBe(3);
// Should not throw
});
});
describe('Verbose Mode (set -v)', () => {
test('should not print commands by default', async () => {
// Capture console output
const originalLog = console.log;
let capturedLogs = [];
console.log = (...args) => capturedLogs.push(args.join(' '));
try {
await $`echo "silent"`;
expect(capturedLogs).toHaveLength(0);
} finally {
console.log = originalLog;
}
});
test('should print commands when verbose enabled', async () => {
// Ensure clean state before intercepting console.log
shell.errexit(false);
shell.verbose(false);
shell.xtrace(false);
shell.pipefail(false);
shell.nounset(false);
const originalLog = console.log;
let capturedLogs = [];
console.log = (...args) => capturedLogs.push(args.join(' '));
try {
shell.verbose(true);
await $`echo "verbose test"`;
expect(capturedLogs.length).toBeGreaterThan(0);
expect(capturedLogs.some(log => log.includes('echo "verbose test"'))).toBe(true);
} finally {
console.log = originalLog;
}
});
});
describe('Trace Mode (set -x)', () => {
test('should not trace commands by default', async () => {
const originalLog = console.log;
let capturedLogs = [];
console.log = (...args) => capturedLogs.push(args.join(' '));
try {
await $`echo "no trace"`;
expect(capturedLogs).toHaveLength(0);
} finally {
console.log = originalLog;
}
});
test('should trace commands when xtrace enabled', async () => {
// Ensure clean state before intercepting console.log
shell.errexit(false);
shell.verbose(false);
shell.xtrace(false);
shell.pipefail(false);
shell.nounset(false);
const originalLog = console.log;
let capturedLogs = [];
console.log = (...args) => capturedLogs.push(args.join(' '));
try {
shell.xtrace(true);
await $`echo "trace test"`;
expect(capturedLogs.length).toBeGreaterThan(0);
expect(capturedLogs.some(log => log.startsWith('+ '))).toBe(true);
expect(capturedLogs.some(log => log.includes('echo "trace test"'))).toBe(true);
} finally {
console.log = originalLog;
}
});
});
describe('Settings API', () => {
test('should allow setting options with set() function', () => {
set('e');
expect(shell.settings().errexit).toBe(true);
set('v');
expect(shell.settings().verbose).toBe(true);
set('x');
expect(shell.settings().xtrace).toBe(true);
});
test('should allow unsetting options with unset() function', () => {
shell.errexit(true);
shell.verbose(true);
unset('e');
expect(shell.settings().errexit).toBe(false);
unset('v');
expect(shell.settings().verbose).toBe(false);
});
test('should support long option names', () => {
set('errexit');
expect(shell.settings().errexit).toBe(true);
set('verbose');
expect(shell.settings().verbose).toBe(true);
unset('errexit');
expect(shell.settings().errexit).toBe(false);
});
test('should return current settings', () => {
shell.errexit(true);
shell.verbose(true);
const settings = shell.settings();
expect(settings.errexit).toBe(true);
expect(settings.verbose).toBe(true);
expect(settings.xtrace).toBe(false);
expect(settings.pipefail).toBe(false);
expect(settings.nounset).toBe(false);
});
});
describe('Shell Replacement Benefits', () => {
test('should provide better error objects than bash', async () => {
shell.errexit(true);
try {
await $`sh -c "echo 'stdout'; echo 'stderr' >&2; exit 5"`;
expect(true).toBe(false);
} catch (error) {
expect(error.code).toBe(5);
expect(error.stdout).toContain('stdout');
expect(error.stderr).toContain('stderr');
expect(error.result).toBeDefined();
expect(error.result.code).toBe(5);
}
});
test('should allow JavaScript control flow with shell semantics', async () => {
const results = [];
// Test a list of commands with error handling
const commands = [
'echo "success1"',
'exit 1', // This will fail
'echo "success2"'
];
for (const cmd of commands) {
try {
shell.errexit(true);
const result = await $`sh -c ${cmd}`;
results.push({ cmd, success: true, output: result.stdout.trim() });
} catch (error) {
results.push({ cmd, success: false, code: error.code });
// Decide whether to continue or not
if (error.code === 1) {
shell.errexit(false); // Continue on this specific error
}
}
}
expect(results).toHaveLength(3);
expect(results[0].success).toBe(true);
expect(results[0].output).toBe('success1');
expect(results[1].success).toBe(false);
expect(results[1].code).toBe(1);
expect(results[2].success).toBe(true);
expect(results[2].output).toBe('success2');
});
});
describe('Real-world Shell Script Pattern', () => {
test('should support common shell script patterns', async () => {
// Typical shell script pattern:
// set -e # exit on error
// optional commands with set +e
// set -e # back to strict mode
shell.errexit(true);
// Critical setup command
const setup = await $`echo "setup complete"`;
expect(setup.code).toBe(0);
// Optional command that might fail
shell.errexit(false);
const optional = await $`ls /nonexistent 2>/dev/null`;
expect(optional.code).not.toBe(0); // Should fail but not throw
// Back to strict mode for critical operations
shell.errexit(true);
const critical = await $`echo "critical operation"`;
expect(critical.code).toBe(0);
expect(critical.stdout.trim()).toBe('critical operation');
});
});
});