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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#![allow(clippy::unwrap_used)]
#![allow(clippy::expect_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::*;
#[test]
fn test_JOB_001_background_jobs_not_supported() {
// Background jobs (&) are NOT SUPPORTED (non-deterministic, race conditions)
let background_jobs = concat!(
"# NOT SUPPORTED: Background job (non-deterministic)\n",
"long_running_task &\n",
"echo \"Task started in background\"\n",
"\n",
"# NOT SUPPORTED: Multiple background jobs (race conditions)\n",
"task1 &\n",
"task2 &\n",
"task3 &\n",
"wait # Wait for all background jobs\n",
"\n",
"# NOT SUPPORTED: Background job with no wait (orphan process)\n",
"cleanup_temp_files &\n",
"\n",
"# NOT SUPPORTED: Fire-and-forget background job\n",
"send_notification &\n",
"exit 0\n",
);
let mut lexer = Lexer::new(background_jobs);
// Parser may not support & - both Ok and Err are acceptable
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Background jobs should tokenize (even though NOT SUPPORTED)"
);
}
}
#[test]
fn test_JOB_001_background_jobs_purification_strategies() {
// DOCUMENTATION: Background job purification strategies (4 strategies)
//
// STRATEGY 1: Convert to foreground execution (RECOMMENDED)
// Use case: Task doesn't need to run in background
// INPUT: long_task &; do_work; wait
// PURIFIED: long_task; do_work
// Pros: Deterministic, simple, no race conditions
// Cons: May be slower (sequential vs parallel)
//
// STRATEGY 2: Sequential execution (RECOMMENDED)
// Use case: Multiple independent tasks
// INPUT: task1 &; task2 &; task3 &; wait
// PURIFIED: task1; task2; task3
// Pros: Deterministic, reproducible, no race conditions
// Cons: Slower than parallel (if tasks are independent)
//
// STRATEGY 3: Remove background job entirely
// Use case: Background job is non-essential (cleanup, notification)
// INPUT: send_notification &; exit 0
// PURIFIED: exit 0 # Remove non-essential background task
// Pros: Simplest, no complexity
// Cons: Loses functionality
//
// STRATEGY 4: Use make -j for parallelism (if needed)
// Use case: Need actual parallelism for performance
// INPUT: for file in *.txt; do process "$file" & done; wait
// PURIFIED: Write Makefile with parallel targets, use make -j4
// Pros: Deterministic parallelism, explicit dependencies
// Cons: Requires Makefile, more complex
let purification_strategies = r#"
# STRATEGY 1: Convert to foreground (RECOMMENDED)
# INPUT: long_task &; do_work; wait
long_task
do_work
# STRATEGY 2: Sequential execution (RECOMMENDED)
# INPUT: task1 &; task2 &; task3 &; wait
task1
task2
task3
# STRATEGY 3: Remove background job
# INPUT: send_notification &; exit 0
exit 0 # Remove non-essential background task
# STRATEGY 4: Use make for parallelism (if needed)
# Create Makefile:
# all: file1.out file2.out file3.out
# %.out: %.txt
# process $< > $@
#
# Then: make -j4 # Deterministic parallelism with explicit dependencies
# REAL-WORLD EXAMPLE: Log processing
# BAD (non-deterministic):
# for log in *.log; do
# process_log "$log" &
# done
# wait
# GOOD (deterministic):
for log in *.log; do
process_log "$log"
done
"#;
let mut lexer = Lexer::new(purification_strategies);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Purification strategies should tokenize successfully"
);
let _ = tokens;
}
// All strategies are DETERMINISTIC
// PREFERRED: Strategies 1-2 (foreground execution)
// Strategy 4 acceptable if parallelism required (use make -j)
}
#[test]
fn test_JOB_001_background_jobs_race_conditions() {
// DOCUMENTATION: Background job race conditions (5 critical race conditions)
//
// RACE 1: Output interleaving
// task1 &
// task2 &
// wait
// Output from task1 and task2 interleaves unpredictably
// PROBLEM: Cannot predict output order
//
// RACE 2: File access conflicts
// process file.txt &
// modify file.txt &
// wait
// Both jobs access file.txt simultaneously
// PROBLEM: Data corruption, race condition
//
// RACE 3: Resource contention
// heavy_task &
// heavy_task &
// heavy_task &
// wait
// All tasks compete for CPU/memory
// PROBLEM: Timing varies, non-deterministic performance
//
// RACE 4: Dependency violations
// generate_data &
// process_data & # Depends on generate_data output
// wait
// process_data may run before generate_data completes
// PROBLEM: Missing dependency, wrong results
//
// RACE 5: Exit status ambiguity
// task1 &
// task2 &
// wait
// If task1 fails, exit status is non-deterministic (depends on timing)
// PROBLEM: Cannot reliably check for errors
let race_conditions = r#"
# RACE 1: Output interleaving (non-deterministic)
echo "Task 1 starting" &
echo "Task 2 starting" &
wait
# Output order unpredictable:
# Task 1 starting
# Task 2 starting
# OR
# Task 2 starting
# Task 1 starting
# RACE 2: File access conflicts
{
echo "Process 1" >> output.txt
} &
{
echo "Process 2" >> output.txt
} &
wait
# output.txt content order unpredictable
# RACE 3: Resource contention
heavy_computation &
heavy_computation &
heavy_computation &
wait
# Timing varies based on system load
# RACE 4: Dependency violations
generate_input_data &
process_input_data & # Depends on generate_input_data!
wait
# process_input_data may run before data is ready
# RACE 5: Exit status ambiguity
false & # Fails immediately
true & # Succeeds
wait $! # Which job's exit status?
# Non-deterministic error handling
"#;
let mut lexer = Lexer::new(race_conditions);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Race conditions should tokenize successfully"
);
let _ = tokens;
}
// Background jobs introduce RACE CONDITIONS
// bashrs FORBIDS background jobs to prevent races
// CRITICAL: Sequential execution is deterministic
}
#[test]
fn test_JOB_001_background_jobs_testing_implications() {
// DOCUMENTATION: Background job testing implications (4 critical issues)
//
// ISSUE 1: Cannot assert on intermediate state
// test_background_job() {
// process_data &
// # Cannot assert on process_data state here (still running!)
// wait
// }
// PROBLEM: Test cannot check state while background job runs
//
// ISSUE 2: Flaky tests due to timing
// test_parallel_processing() {
// task1 & task2 & wait
// # Test may pass/fail depending on task completion order
// }
// PROBLEM: Tests are non-deterministic
//
// ISSUE 3: Cannot isolate failures
// test_multiple_jobs() {
// job1 & job2 & job3 & wait
// # If one job fails, which one? Cannot tell!
// }
// PROBLEM: Cannot debug failures
//
// ISSUE 4: Cleanup issues
// test_background_cleanup() {
// long_task &
// # Test exits before long_task completes
// # Background job becomes orphan
// }
// PROBLEM: Background jobs outlive tests, pollute environment
let testing_implications = r#"
# BAD TEST: Cannot assert on intermediate state
test_bad_intermediate_state() {
process_data &
# PROBLEM: Cannot check if process_data is working
# Job is still running, state is unknown
wait
}
# GOOD TEST: Foreground execution (deterministic)
test_good_foreground() {
process_data
# Can assert on result after completion
[ -f output.txt ] || exit 1
}
# BAD TEST: Flaky due to timing
test_flaky_parallel() {
task1 &
task2 &
wait
# PROBLEM: Order of completion is non-deterministic
# Test may pass sometimes, fail others
}
# GOOD TEST: Sequential (deterministic)
test_deterministic_sequential() {
task1
task2
# Order is guaranteed, reproducible
[ -f task1.out ] || exit 1
[ -f task2.out ] || exit 1
}
# BAD TEST: Cannot isolate failures
test_cannot_isolate() {
job1 &
job2 &
job3 &
wait
# PROBLEM: If wait fails, which job failed?
}
# GOOD TEST: Isolated failures
test_isolated() {
job1 || exit 1
job2 || exit 2
job3 || exit 3
# Each job checked individually
}
"#;
let mut lexer = Lexer::new(testing_implications);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Testing implications should tokenize successfully"
);
let _ = tokens;
}
// Background jobs make tests NON-REPRODUCIBLE and FLAKY
// bashrs enforces DETERMINISTIC testing (foreground execution)
// NEVER use background jobs in test code
}
#[test]
fn test_JOB_001_background_jobs_portability_issues() {
// DOCUMENTATION: Background job portability issues (3 critical issues)
//
// ISSUE 1: Job control availability
// Job control (&, jobs, fg, bg) may not be available in all shells
// Non-interactive shells: job control often disabled
// Dash: Limited job control support
// POSIX: Job control is OPTIONAL (not all shells support it)
//
// ISSUE 2: wait behavior varies
// bash: wait with no args waits for all background jobs
// dash: wait requires PID (wait $pid)
// POSIX: wait behavior varies across shells
//
// ISSUE 3: Background job process groups
// bash: Background jobs in separate process group
// dash: Process group handling differs
// PROBLEM: Signal handling is shell-dependent
let portability_issues = r#"
#!/bin/sh
# This script has PORTABILITY ISSUES (uses background jobs)
# ISSUE 1: Job control may not be available
long_task &
# Non-interactive shell: May not support job control
# Dash: Limited support
# ISSUE 2: wait behavior varies
task1 &
task2 &
wait # bash: waits for all, dash: may require PID
# ISSUE 3: Process groups
task &
pid=$!
# Process group handling varies by shell
# PURIFIED (POSIX-compliant, portable):
# Use foreground execution (no job control needed)
task1
task2
# Deterministic, portable, works in all shells
"#;
let mut lexer = Lexer::new(portability_issues);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Portability issues should tokenize successfully"
);
let _ = tokens;
}
// Background jobs have PORTABILITY ISSUES
// Job control is OPTIONAL in POSIX (not all shells support)
// PURIFICATION: Use foreground execution (portable, deterministic)
}
// DOCUMENTATION: Comprehensive background jobs comparison (Bash vs POSIX vs Purified)
//
// FEATURE | Bash | POSIX | Purified
// Background jobs (&) | SUPPORTED | OPTIONAL | NOT SUPPORTED
// Determinism | NO | NO | YES (enforced)
// Reproducibility | NO | NO | YES
// Testing | Flaky | Flaky | Reproducible
// Portability | bash | Optional | POSIX (portable)
// Error handling | Silent | Silent | Immediate
// Race conditions | YES | YES | NO
// Resource management | Manual | Manual | Automatic
//
// RUST MAPPING:
// Background jobs (&) -> NOT MAPPED (use sequential execution)
// Parallelism needs -> Use Rayon (deterministic parallelism)
// Async I/O -> Use tokio (structured concurrency)
// Job control -> Remove or convert to sequential
//
// PURIFICATION RULES:
// 1. Background jobs (&) -> DISCOURAGED (convert to foreground)
// 2. Parallel tasks -> Sequential execution (deterministic)
// 3. wait command -> Remove (sequential execution doesn't need wait)
// 4. Fire-and-forget jobs -> Remove or make synchronous
// 5. Parallelism for performance -> Use make -j or Rayon (deterministic)
#[test]
fn test_JOB_001_background_jobs_comparison_table() {
// Comparison examples: bash (non-deterministic) vs purified (sequential)
let comparison_table = concat!(
"#!/bin/sh\n",
"# COMPARISON EXAMPLES\n",
"\n",
"# PURIFIED (DETERMINISTIC):\n",
"# Sequential execution (deterministic)\n",
"long_task\n",
"short_task\n",
"# Guaranteed order, reproducible\n",
"\n",
"# PURIFIED (reproducible tests):\n",
"test_sequential() {\n",
" task1\n",
" task2\n",
" [ -f task1.out ] || exit 1\n",
" [ -f task2.out ] || exit 1\n",
"}\n",
"\n",
"# PURIFIED (immediate error detection):\n",
"risky_operation || exit 1\n",
);
let mut lexer = Lexer::new(comparison_table);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Comparison table should tokenize successfully"
);
}
}
// ============================================================================
// PARAM-SPEC-006: $- (Shell Options) Purification
// ============================================================================
// DOCUMENTATION: $- (shell options) is NOT SUPPORTED (LOW priority purification)
//
// $-: Special parameter that expands to current shell option flags
// Contains single letters representing active shell options
// Set by: Shell at startup, modified by set command
//
// WHAT $- CONTAINS (each letter = an active option):
// h: hashall, i: interactive, m: monitor mode, B: brace expansion,
// H: history substitution, s: read from stdin, c: read from -c arg,
// e: exit on error, u: error on unset vars, x: print commands,
// v: print input lines, n: no execution, f: no globbing,
// a: auto-export all, t: exit after one command
//
// EXAMPLE VALUES:
// Interactive bash: "himBH", Script: "hB", set -e script: "ehB", sh: "h"
//
// WHY NOT SUPPORTED:
// 1. Runtime-specific (value depends on how shell was invoked)
// 2. Non-deterministic (different shells = different flags)
// 3. Shell-dependent (bash has different flags than sh/dash)
// 4. Implementation detail (exposes internal shell state)
// 5. Not needed for pure scripts (purified scripts don't rely on shell modes)
//
// POSIX COMPLIANCE: $- is POSIX SUPPORTED but FLAGS DIFFER between shells
// bash: himBH (many extensions), sh/dash: h (minimal)
//
// PURIFICATION STRATEGY:
// 1. Remove $- entirely (RECOMMENDED)
// 2. Replace with explicit option checks
// 3. Use set -e explicitly (don't check "e" in $-)
//
// PURIFICATION EXAMPLES:
// BEFORE: echo "Shell options: $-" -> AFTER: (removed, not needed)
// BEFORE: `case "$-" in *i*) ... esac` -> AFTER: echo "Non-interactive"
// BEFORE: `case "$-" in *e*) ... esac` -> AFTER: set -e (explicit)
#[test]
fn test_PARAM_SPEC_006_shell_options_not_supported() {
// $- is NOT SUPPORTED by the current lexer
// Special parameters like $-, $$, $?, $! are not yet implemented
// This test documents that $- is NOT SUPPORTED and verifies the lexer doesn't crash
let bash_input = r#"echo $-"#;
let mut lexer = Lexer::new(bash_input);
let tokens = lexer.tokenize().unwrap();
assert!(
!tokens.is_empty(),
"Lexer should produce tokens without crashing"
);
}