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
#![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_VAR_004_purification_removes_prompts() {
// DOCUMENTATION: Purification removes all prompt variables
//
// Before (with interactive prompts):
// #!/bin/bash
// PS1='\u@\h:\w\$ '
// PS2='> '
// PS3='Select: '
// PS4='+ '
//
// echo "Hello World"
//
// After (purified, prompts removed):
// #!/bin/sh
// printf '%s\n' "Hello World"
//
// Prompts removed because:
// - Not needed in non-interactive scripts
// - Scripts run in batch mode (no prompts displayed)
// - POSIX sh doesn't use prompts in scripts
let purified_no_prompts = r#"
#!/bin/sh
printf '%s\n' "Hello World"
"#;
let result = BashParser::new(purified_no_prompts);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Purified scripts have no prompt variables"
);
}
// Purification removes:
// - PS1, PS2, PS3, PS4 assignments
// - PROMPT_COMMAND
// - PROMPT_DIRTRIM
// - PS0
// - Any prompt customization code
}
#[test]
fn test_VAR_004_script_mode_only_philosophy() {
// DOCUMENTATION: Script mode has no prompts
//
// Interactive shell (has prompts):
// $ PS1='custom> '
// custom> echo "hello"
// hello
// custom>
//
// Script mode (no prompts):
// $ ./script.sh
// hello
// $
//
// Scripts run non-interactively:
// - No prompts displayed
// - No user input during execution
// - Output goes to stdout (no interactive display)
//
// bashrs philosophy:
// - Script mode only (no interactive features)
// - No prompts (PS1, PS2, PS3, PS4)
// - No interactive input (read, select)
// - Fully automated execution
let script_mode = r#"
#!/bin/sh
# No prompts in script mode
# Runs non-interactively
printf '%s\n' "Processing..."
printf '%s\n' "Done"
"#;
let result = BashParser::new(script_mode);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Script mode has no interactive prompts"
);
}
// Script mode characteristics:
// - No prompts (PS1, PS2, PS3, PS4)
// - No user interaction (read, select)
// - Automated execution (no waiting for input)
// - Works in CI/CD, cron, Docker (no TTY)
}
// ============================================================================
// PROMPT-001: PROMPT_COMMAND (Interactive Hook, NOT SUPPORTED)
// ============================================================================
//
// Task: PROMPT-001 - Document PROMPT_COMMAND
// Status: DOCUMENTED (NOT SUPPORTED - interactive only)
// Priority: LOW (prompt hook not needed in scripts)
//
// PROMPT_COMMAND is a Bash variable containing commands to execute before each
// primary prompt (PS1) is displayed. It's interactive-only.
//
// Bash behavior:
// - Executed before each PS1 prompt
// - Can be a single command or array (PROMPT_COMMAND=(cmd1 cmd2))
// - Common uses: update window title, show git branch, timing info
// - Only works in interactive shells
//
// bashrs policy:
// - NOT SUPPORTED (interactive only)
// - Purification removes all PROMPT_COMMAND assignments
// - Script mode has no prompts, so no hook needed
// - POSIX sh has no equivalent (interactive feature)
//
// Transformation:
// Bash input:
// PROMPT_COMMAND='date'
// PROMPT_COMMAND='history -a; date'
//
// Purified POSIX sh:
// (removed - not needed in script mode)
//
// Related features:
// - PS1, PS2, PS3, PS4 (prompt variables, VAR-004)
// - PS0 (executed after command read but before execution)
// - PROMPT_DIRTRIM (truncate long paths in PS1)
#[test]
fn test_PROMPT_001_prompt_command_not_supported() {
// DOCUMENTATION: PROMPT_COMMAND is NOT SUPPORTED (interactive only)
//
// PROMPT_COMMAND is executed before each prompt display:
// $ PROMPT_COMMAND='date'
// Mon Oct 27 10:00:00 UTC 2025
// $
// Mon Oct 27 10:00:05 UTC 2025
// $
//
// NOT SUPPORTED because:
// - Interactive-only feature
// - Scripts don't display prompts
// - No POSIX equivalent
// - Not needed in automated execution
let prompt_command_script = r#"PROMPT_COMMAND='date'"#;
let result = BashParser::new(prompt_command_script);
match result {
Ok(mut parser) => {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"PROMPT_COMMAND is interactive only, NOT SUPPORTED in scripts"
);
}
Err(_) => {
// Parse error acceptable - interactive feature
}
}
// PROMPT_COMMAND use cases (all interactive):
// 1. Update window title: PROMPT_COMMAND='echo -ne "\033]0;${PWD}\007"'
// 2. Show git branch: PROMPT_COMMAND='__git_ps1'
// 3. Command timing: PROMPT_COMMAND='echo "Last: $SECONDS sec"'
// 4. History sync: PROMPT_COMMAND='history -a'
//
// All of these are interactive-only and NOT SUPPORTED in bashrs.
}
#[test]
fn test_PROMPT_001_prompt_command_array_form() {
// DOCUMENTATION: PROMPT_COMMAND array form (Bash 4.4+)
//
// Bash 4.4+ supports array form:
// PROMPT_COMMAND=(cmd1 cmd2 cmd3)
//
// Each command executed in order before prompt:
// $ PROMPT_COMMAND=('date' 'pwd' 'echo "ready"')
// Mon Oct 27 10:00:00 UTC 2025
// /home/user
// ready
// $
let prompt_command_array = r#"PROMPT_COMMAND=('date' 'pwd' 'echo "ready"')"#;
let result = BashParser::new(prompt_command_array);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"PROMPT_COMMAND array form is interactive only, NOT SUPPORTED"
);
}
// Array form allows multiple hooks:
// - Separates concerns (window title, git info, timing)
// - Executed in array order
// - Still interactive-only
// - NOT SUPPORTED in bashrs (scripts have no prompts)
}
#[test]
fn test_PROMPT_001_purification_removes_prompt_command() {
// DOCUMENTATION: Purification removes PROMPT_COMMAND
//
// Before (with PROMPT_COMMAND):
// #!/bin/bash
// PROMPT_COMMAND='date'
// echo "Starting script"
// do_work() {
// echo "Working..."
// }
// do_work
//
// After (purified, PROMPT_COMMAND removed):
// #!/bin/sh
// printf '%s\n' "Starting script"
// do_work() {
// printf '%s\n' "Working..."
// }
// do_work
//
// Removed because:
// - Scripts don't display prompts
// - No interactive execution
// - POSIX sh has no equivalent
// - Not needed in automated mode
let purified_no_prompt_command = r#"
#!/bin/sh
printf '%s\n' "Starting script"
do_work() {
printf '%s\n' "Working..."
}
do_work
"#;
let result = BashParser::new(purified_no_prompt_command);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Purified scripts have no PROMPT_COMMAND"
);
}
// Purification strategy:
// 1. Remove PROMPT_COMMAND assignment
// 2. Remove PROMPT_COMMAND array assignments
// 3. Keep actual work logic
// 4. Scripts run without prompts
}
#[test]
fn test_PROMPT_001_common_prompt_command_patterns() {
// DOCUMENTATION: Common PROMPT_COMMAND patterns (all interactive)
//
// Pattern 1: Window title updates
// PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"'
//
// Pattern 2: Git status in prompt
// PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'
//
// Pattern 3: Command timing
// PROMPT_COMMAND='echo "Duration: $SECONDS sec"'
//
// Pattern 4: History management
// PROMPT_COMMAND='history -a; history -c; history -r'
//
// Pattern 5: Multiple commands (semicolon-separated)
// PROMPT_COMMAND='date; uptime; echo "ready"'
//
// All patterns are interactive-only, NOT SUPPORTED in bashrs.
let window_title = r#"PROMPT_COMMAND='echo -ne "\033]0;${PWD}\007"'"#;
let git_status = r#"PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "'"#;
let timing = r#"PROMPT_COMMAND='echo "Duration: $SECONDS sec"'"#;
let history_sync = r#"PROMPT_COMMAND='history -a; history -c; history -r'"#;
let multiple = r#"PROMPT_COMMAND='date; uptime; echo "ready"'"#;
// None of these work in script mode:
for prompt_cmd in [window_title, git_status, timing, history_sync, multiple] {
let result = BashParser::new(prompt_cmd);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"PROMPT_COMMAND patterns are interactive only"
);
}
}
// Why these don't work in scripts:
// - Window title: Scripts run in background (no terminal)
// - Git status: No prompt to display status in
// - Timing: Scripts time with 'time' command instead
// - History: Scripts don't have interactive history
// - Multiple: No prompt to execute before
}
#[test]
fn test_PROMPT_001_script_alternatives_to_prompt_command() {
// DOCUMENTATION: Script alternatives to PROMPT_COMMAND functionality
//
// PROMPT_COMMAND use case → Script alternative
//
// 1. Window title updates → Not needed (scripts run headless)
// Interactive: PROMPT_COMMAND='echo -ne "\033]0;${PWD}\007"'
// Script: N/A (no window title in headless mode)
//
// 2. Command timing → Use 'time' command
// Interactive: PROMPT_COMMAND='echo "Duration: $SECONDS sec"'
// Script: time ./my_script.sh
//
// 3. Progress updates → Use explicit logging
// Interactive: PROMPT_COMMAND='echo "Current dir: $PWD"'
// Script: printf '%s\n' "Processing $file..."
//
// 4. History sync → Not applicable (scripts have no history)
// Interactive: PROMPT_COMMAND='history -a'
// Script: N/A (use logging instead)
let timing_alternative = r#"
#!/bin/sh
# Time the entire script
# Run as: time ./script.sh
start_time=$(date +%s)
printf '%s\n' "Starting work..."
# Do work here
printf '%s\n' "Work complete"
end_time=$(date +%s)
duration=$((end_time - start_time))
printf 'Total duration: %d seconds\n' "$duration"
"#;
let result = BashParser::new(timing_alternative);
if let Ok(mut parser) = result {
let parse_result = parser.parse();
assert!(
parse_result.is_ok() || parse_result.is_err(),
"Scripts use explicit timing instead of PROMPT_COMMAND"
);
}
// Key principle:
// PROMPT_COMMAND is implicit (runs automatically before each prompt)
// Scripts are explicit (log when you need to log)
}
#[test]
include!("part2_s4_prompt_001.rs");