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
#![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: parse a script and return whether parsing succeeded.
/// Used by documentation tests that only need to verify parsability.
#[test]
fn test_BUILTIN_019_umask_basic() {
// DOCUMENTATION: Basic umask command parsing
//
// Bash: umask 022
// Effect: New files: 644 (rw-r--r--), dirs: 755 (rwxr-xr-x)
// Rust: std::fs::set_permissions() or libc::umask()
// Purified: umask 022
//
// Global State: Modifies file creation mask
// Priority: LOW (works but has global state implications)
let script = r#"umask 022"#;
let result = BashParser::new(script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok(),
"umask should parse successfully: {:?}",
parse_result.err()
);
}
Err(e) => {
panic!("umask parsing failed: {:?}", e);
}
}
// DOCUMENTATION: umask is supported
// Global State: Modifies process-wide permissions
// Best Practice: Set once at script start, document reasoning
}
#[test]
fn test_BUILTIN_019_umask_global_state() {
// DOCUMENTATION: umask modifies global state
//
// Problem: umask affects entire process
// Effect: All file operations after umask use new mask
//
// Example:
// #!/bin/bash
// touch file1.txt # Uses default umask (e.g., 022 → 644)
// umask 077
// touch file2.txt # Uses new umask (077 → 600)
//
// file1.txt: -rw-r--r-- (644)
// file2.txt: -rw------- (600)
let script = r#"umask 077"#;
let result = BashParser::new(script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok(),
"umask with global state documented: {:?}",
parse_result.err()
);
}
Err(_) => {
panic!("umask should parse");
}
}
// DOCUMENTATION: umask has global side effects
// Global State: Cannot be scoped or limited
// Side Effects: Affects all subsequent file operations
// Consideration: May surprise developers unfamiliar with umask
}
#[test]
fn test_BUILTIN_019_umask_idempotency_concern() {
// DOCUMENTATION: umask idempotency considerations
//
// Concern: Running script multiple times
// Issue: umask stacks if not carefully managed
//
// Safe Pattern:
// #!/bin/bash
// old_umask=$(umask)
// umask 022
// # ... script logic ...
// umask "$old_umask"
//
// Unsafe Pattern:
// #!/bin/bash
// umask 022
// # ... script logic ...
// # umask not restored!
let script = r#"old_umask=$(umask); umask 022"#;
let result = BashParser::new(script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"umask save/restore pattern documented"
);
}
Err(_) => {
// May fail due to command substitution
}
}
// DOCUMENTATION: Best practice for umask
// Safe: Save old umask, restore at end
// Unsafe: Set umask without restoration
// Idempotency: Restoration ensures safe re-run
}
#[test]
fn test_BUILTIN_019_umask_explicit_chmod_alternative() {
// DOCUMENTATION: Explicit chmod as alternative to umask
//
// umask (global):
// umask 077
// touch file.txt # Permissions: 600
//
// chmod (explicit, safer):
// touch file.txt
// chmod 600 file.txt # Explicit, clear, localized
//
// Benefits of chmod:
// - Explicit permissions (easier to understand)
// - No global state modification
// - Clear intent in code
// - Easier to audit
let script = r#"chmod 600 file.txt"#;
let mut parser = BashParser::new(script).unwrap();
let result = parser.parse();
assert!(
result.is_ok(),
"Explicit chmod should parse successfully: {:?}",
result.err()
);
let ast = result.unwrap();
assert!(!ast.statements.is_empty());
// DOCUMENTATION: chmod is preferred over umask
// Reason: Explicit, no global state, clear intent
// umask: Global, implicit, affects all operations
// chmod: Localized, explicit, affects specific files
//
// Recommendation:
// - Use chmod for explicit permission control
// - Use umask only when necessary (e.g., security requirements)
// - Document why umask is needed if used
}
// ============================================================================
// BASH-BUILTIN-003: let - Arithmetic Evaluation
// Reference: docs/BASH-INGESTION-ROADMAP.yaml
// Status: DOCUMENTED (prefer $((...)) for POSIX)
//
// let evaluates arithmetic expressions:
// - let "x = 5 + 3" → x=8
// - let "y += 1" → y increments
// - let "z = x * y" → z = x * y
//
// POSIX Alternative: $((...))
// - x=$((5 + 3)) → POSIX-compliant
// - y=$((y + 1)) → POSIX-compliant
// - z=$((x * y)) → POSIX-compliant
//
// Purification Strategy:
// - Convert let to $((...)) for POSIX compliance
// - let "x = expr" → x=$((expr))
// - More portable and widely supported
//
// EXTREME TDD: Document let and POSIX alternative
// ============================================================================
#[test]
fn test_BASH_BUILTIN_003_let_basic() {
// DOCUMENTATION: Basic let command parsing
//
// Bash: let "x = 5 + 3"
// Result: x=8
// Rust: let x = 5 + 3;
// Purified: x=$((5 + 3))
//
// POSIX Alternative: $((arithmetic))
// Priority: LOW (works but $((...)) is preferred)
let script = r#"let "x = 5 + 3""#;
let result = BashParser::new(script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"let command parsing documented"
);
}
Err(_) => {
// May not parse let syntax
}
}
// DOCUMENTATION: let is Bash-specific
// POSIX: Use $((...)) for arithmetic
// Purification: Convert let → $((...))
}
#[test]
fn test_BASH_BUILTIN_003_let_increment() {
// DOCUMENTATION: let with increment operator
//
// Bash: let "y += 1"
// Result: y increments by 1
// Purified: y=$((y + 1))
//
// Common Usage:
// - let "i++" → i=$((i + 1))
// - let "j--" → j=$((j - 1))
// - let "k *= 2" → k=$((k * 2))
let script = r#"let "y += 1""#;
let result = BashParser::new(script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"let increment documented"
);
}
Err(_) => {
// May not parse
}
}
// DOCUMENTATION: let supports C-style operators
// POSIX: Use explicit arithmetic: x=$((x + 1))
// Clarity: Explicit form is more readable
}
#[test]
fn test_BASH_BUILTIN_003_let_posix_alternative() {
// DOCUMENTATION: POSIX $((...)) alternative to let
//
// let (Bash-specific):
// let "x = 5 + 3"
//
// $((...)) (POSIX-compliant):
// x=$((5 + 3))
//
// This test verifies $((...)) works as replacement for let.
let script = r#"x=$((5 + 3))"#;
let result = BashParser::new(script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"POSIX arithmetic documented"
);
}
Err(_) => {
// May not parse arithmetic
}
}
// DOCUMENTATION: $((...)) is preferred over let
// Reason: POSIX-compliant, more portable
// let: Bash-specific extension
// $((...)): Works in sh, dash, bash, zsh
//
// Purification Strategy:
// - let "x = expr" → x=$((expr))
// - More explicit and portable
}
#[test]
fn test_BASH_BUILTIN_003_let_refactoring() {
// DOCUMENTATION: How to refactor let to POSIX
//
// Bash (let):
// let "x = 5 + 3"
// let "y += 1"
// let "z = x * y"
//
// POSIX ($((...)):
// x=$((5 + 3))
// y=$((y + 1))
// z=$((x * y))
//
// Benefits:
// - POSIX-compliant (works everywhere)
// - More explicit and readable
// - No quoting needed
// - Standard shell arithmetic
let script = r#"x=$((5 + 3))"#;
let result = BashParser::new(script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"POSIX arithmetic refactoring documented"
);
}
Err(_) => {
// May not parse
}
}
// DOCUMENTATION: Refactoring strategy for let
// Instead of: let "x = 5 + 3" (Bash-specific)
// Use: x=$((5 + 3)) (POSIX-compliant)
//
// Conversion Rules:
// - let "x = expr" → x=$((expr))
// - let "x += 1" → x=$((x + 1))
// - let "x++" → x=$((x + 1))
// - let "x--" → x=$((x - 1))
//
// Portability:
// - let: Bash, zsh only
// - $((...)): All POSIX shells (sh, dash, bash, zsh, ksh)
}
// ============================================================================
// TASK 1.2: Interactive vs Script Mode
// ============================================================================
//
// Task: 1.2 - Document interactive vs script mode
// Status: DOCUMENTED
// Priority: HIGH (foundational concept)
//
// bashrs philosophy: SCRIPT MODE ONLY (deterministic, non-interactive)
//
// Why script mode only?
// - Determinism: Same input → same output (always)
// - Automation: Works in CI/CD, cron, Docker (no TTY needed)
// - Testing: Can be unit tested (no human input required)
// - Safety: No risk of user typos or unexpected input
//
// Interactive features NOT SUPPORTED:
// - read command (waits for user input) → use command-line args
// - select menus → use config files
// - TTY detection (tty, isatty) → assume non-TTY
// - History navigation (↑↓ arrows) → use git for versioning
// - Tab completion → use IDE/editor completion
//
// Script features FULLY SUPPORTED:
// - Functions, variables, control flow
// - File I/O, process execution
// - Command-line argument parsing ($1, $2, $@)
// - Environment variables
// - Exit codes, error handling
//
// Transformation strategy:
// - Interactive bash → Deterministic script mode only
// - read var → var="$1" (command-line args)
// - select menu → config file or case statement
// - TTY checks → assume batch mode always
#[test]
include!("part2_task_1.rs");