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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#![allow(clippy::unwrap_used)]
#![allow(unused_imports)]
use super::super::ast::Redirect;
use super::super::lexer::Lexer;
use super::super::parser::BashParser;
use super::super::semantic::SemanticAnalyzer;
use super::super::*;
/// Helper: assert that BashParser handles the input without panicking.
/// Accepts both successful parses and parse errors (documentation tests
/// only verify the parser doesn't crash, not that the input is valid).
#[test]
fn test_PIPE_001_bash_vs_posix_pipes() {
// DOCUMENTATION: Bash vs POSIX pipeline features
//
// Feature | POSIX sh | Bash extensions
// -------------------------|-------------------|------------------
// Basic pipe (|) | ✅ Supported | ✅ Supported
// Multi-stage (a|b|c) | ✅ Supported | ✅ Supported
// Exit status ($?) | ✅ Rightmost cmd | ✅ Rightmost cmd
// PIPESTATUS array | ❌ Not available | ✅ ${PIPESTATUS[@]}
// pipefail option | ❌ Not available | ✅ set -o pipefail
// lastpipe option | ❌ Not available | ✅ shopt -s lastpipe
// |& (pipe stderr too) | ❌ Not available | ✅ Bash 4.0+
// Process substitution | ❌ Not available | ✅ <(cmd) >(cmd)
//
// bashrs policy:
// - Support POSIX pipes (|) fully
// - NOT SUPPORTED: PIPESTATUS, pipefail, lastpipe, |&, process substitution
// - Generate POSIX-compliant pipelines only
let posix_pipe = r#"cat file.txt | grep "pattern" | wc -l"#;
let bash_pipestatus = r#"cat file.txt | grep "pattern"; echo ${PIPESTATUS[@]}"#;
// POSIX pipe - SUPPORTED
let posix_result = BashParser::new(posix_pipe);
assert!(posix_result.is_ok(), "POSIX pipe should parse");
// Bash PIPESTATUS - NOT SUPPORTED (Bash extension)
let bash_result = BashParser::new(bash_pipestatus);
match bash_result {
Ok(mut parser) => {
let _ = parser.parse();
// PIPESTATUS is Bash extension, may or may not parse
}
Err(_) => {
// Parse error acceptable for Bash extensions
}
}
// Summary:
// POSIX pipes: Fully supported (|, multi-stage, $? exit status)
// Bash extensions: NOT SUPPORTED (PIPESTATUS, pipefail, |&, etc.)
// bashrs: Generate POSIX-compliant pipelines only
}
// ============================================================================
// CMD-LIST-001: Command Lists (&&, ||, ;) (POSIX, SUPPORTED)
// ============================================================================
//
// Task: CMD-LIST-001 (3.2.3.1) - Document command lists (&&, ||, ;)
// Status: DOCUMENTED (SUPPORTED - POSIX compliant)
// Priority: HIGH (fundamental control flow)
//
// Command lists connect multiple commands with control flow operators.
// These are core POSIX features available in all shells.
//
// POSIX operators:
// - ; (semicolon): Execute sequentially, ignore exit status
// - && (AND): Execute second command only if first succeeds (exit 0)
// - || (OR): Execute second command only if first fails (exit non-zero)
// - Newline: Equivalent to semicolon
//
// bashrs policy:
// - FULLY SUPPORTED (POSIX compliant)
// - Quote all variables in generated shell
// - Preserve short-circuit evaluation semantics
// - Map to if statements in Rust
#[test]
fn test_CMD_LIST_001_semicolon_sequential() {
// DOCUMENTATION: Semicolon (;) executes commands sequentially
//
// Semicolon executes commands in sequence, regardless of exit status:
// $ cmd1 ; cmd2 ; cmd3
// (All three commands execute, regardless of success/failure)
//
// $ false ; echo "Still runs"
// Still runs
//
// Newline is equivalent to semicolon:
// $ cmd1
// $ cmd2
// (Same as: cmd1 ; cmd2)
//
// POSIX-compliant: Works in sh, dash, ash, bash
let sequential = r#"
echo "First"
echo "Second"
false
echo "Third"
"#;
let result = BashParser::new(sequential);
assert!(result.is_ok(), "Sequential commands should parse (POSIX)");
let mut parser = result.unwrap();
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Semicolon/newline separation is POSIX-compliant"
);
}
#[test]
fn test_CMD_LIST_001_and_operator_short_circuit() {
// DOCUMENTATION: AND operator (&&) with short-circuit evaluation
//
// AND (&&) executes second command only if first succeeds:
// $ test -f file.txt && echo "File exists"
// (echo only runs if test succeeds)
//
// $ false && echo "Never printed"
// (echo never runs because false returns 1)
//
// Short-circuit: Right side only evaluated if left succeeds
// Exit status: Status of last executed command
//
// POSIX-compliant: SUSv3, IEEE Std 1003.1-2001
let and_operator = r#"
test -f file.txt && echo "File exists"
true && echo "This prints"
false && echo "This does not print"
"#;
let result = BashParser::new(and_operator);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"AND operator is POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable - && may not be fully implemented yet
}
}
}
#[test]
fn test_CMD_LIST_001_or_operator_short_circuit() {
// DOCUMENTATION: OR operator (||) with short-circuit evaluation
//
// OR (||) executes second command only if first fails:
// $ test -f file.txt || echo "File not found"
// (echo only runs if test fails)
//
// $ true || echo "Never printed"
// (echo never runs because true returns 0)
//
// Short-circuit: Right side only evaluated if left fails
// Exit status: Status of last executed command
//
// POSIX-compliant: SUSv3, IEEE Std 1003.1-2001
let or_operator = r#"
test -f missing.txt || echo "File not found"
false || echo "This prints"
true || echo "This does not print"
"#;
let result = BashParser::new(or_operator);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"OR operator is POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable - || may not be fully implemented yet
}
}
}
#[test]
fn test_CMD_LIST_001_combined_operators() {
// DOCUMENTATION: Combining &&, ||, and ; operators
//
// Operators can be combined with precedence rules:
// - && and || have equal precedence, evaluated left-to-right
// - ; has lower precedence (separates complete lists)
//
// Example: cmd1 && cmd2 || cmd3 ; cmd4
// Meaning: (cmd1 AND cmd2) OR cmd3, THEN cmd4
// 1. If cmd1 succeeds, run cmd2
// 2. If either cmd1 or cmd2 fails, run cmd3
// 3. Always run cmd4 (semicolon ignores previous exit status)
//
// Common pattern (error handling):
// command && echo "Success" || echo "Failed"
let combined = r#"
#!/bin/sh
# Try command, report success or failure
test -f file.txt && echo "Found" || echo "Not found"
# Multiple steps with fallback
mkdir -p /tmp/test && cd /tmp/test || exit 1
# Always cleanup, regardless of previous status
process_data && echo "Done" || echo "Error" ; cleanup
"#;
let result = BashParser::new(combined);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Combined operators are POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable - complex lists may not be fully implemented
}
}
}
#[test]
fn test_CMD_LIST_001_exit_status_semantics() {
// DOCUMENTATION: Exit status with command lists
//
// Exit status rules:
// - Semicolon (;): Status of last command in list
// - AND (&&): Status of last executed command
// - OR (||): Status of last executed command
//
// Examples:
// $ true ; false
// $ echo $?
// 1 (status of 'false')
//
// $ true && echo "yes"
// yes
// $ echo $?
// 0 (status of 'echo')
//
// $ false || echo "fallback"
// fallback
// $ echo $?
// 0 (status of 'echo')
let exit_status = r#"
#!/bin/sh
# Exit status examples
true ; false
if [ $? -ne 0 ]; then
echo "Last command failed"
fi
true && echo "Success"
if [ $? -eq 0 ]; then
echo "Previous succeeded"
fi
false || echo "Fallback"
if [ $? -eq 0 ]; then
echo "Fallback succeeded"
fi
"#;
let result = BashParser::new(exit_status);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Exit status semantics are POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable
}
}
}
// DOCUMENTATION: Rust if statement mapping for command lists
//
// Bash AND (&&):
// test -f file.txt && echo "File exists"
//
// Rust equivalent:
// fn handle() { if test_file("file.txt") { println!("File exists"); } }
//
// Bash OR (||):
// test -f file.txt || echo "File not found"
//
// Rust equivalent:
// fn handle() { if !test_file("file.txt") { println!("File not found"); } }
//
// Bash combined (&&/||):
// cmd1 && cmd2 || cmd3
//
// Rust equivalent:
// fn handle() { if cmd1() { cmd2(); } else { cmd3(); } }
//
// bashrs strategy:
// - Map && to statement
// - Map || to negated condition
// - Preserve short-circuit evaluation semantics
#[test]
fn test_CMD_LIST_001_rust_if_statement_mapping() {
// This test documents the Rust mapping strategy
}
#[test]
fn test_CMD_LIST_001_common_patterns() {
// DOCUMENTATION: Common command list patterns
//
// Pattern 1: Error checking
// command || exit 1
// (Exit if command fails)
//
// Pattern 2: Success confirmation
// command && echo "Done"
// (Print message only if succeeds)
//
// Pattern 3: Try-catch style
// command && echo "Success" || echo "Failed"
// (Report outcome either way)
//
// Pattern 4: Safe directory change
// cd /path || exit 1
// (Exit if cd fails)
//
// Pattern 5: Create and enter
// mkdir -p dir && cd dir
// (Only cd if mkdir succeeds)
//
// Pattern 6: Cleanup always runs
// process ; cleanup
// (Cleanup runs regardless of process exit status)
let common_patterns = r#"
#!/bin/sh
# Pattern 1: Error checking
command || exit 1
# Pattern 2: Success confirmation
command && echo "Done"
# Pattern 3: Try-catch style
command && echo "Success" || echo "Failed"
# Pattern 4: Safe directory change
cd /path || exit 1
# Pattern 5: Create and enter
mkdir -p dir && cd dir
# Pattern 6: Cleanup always runs
process_data ; cleanup_resources
"#;
let result = BashParser::new(common_patterns);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Common patterns are POSIX-compliant"
);
}
Err(_) => {
// Parse error acceptable
}
}
}
#[test]
include!("part3_cmd_list.rs");