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
#![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: tokenize input and assert tokens are non-empty.
/// Accepts parse errors gracefully (parser may not support all constructs yet).
#[test]
fn test_BUILTIN_020_unset_bash_extensions_not_supported() {
// DOCUMENTATION: Bash unset extensions (NOT SUPPORTED)
//
// BASH EXTENSIONS (NOT SUPPORTED):
// 1. unset -n nameref: Unset nameref (use regular unset)
// 2. unset array[index]: Unset array element (use array reassignment)
// 3. unset associative array elements (use whole array unset)
//
// PURIFICATION STRATEGIES:
//
// 1. Nameref unset (NOT SUPPORTED):
// Bash: declare -n ref=VAR; unset -n ref
// Purified: VAR="" # Just clear the variable
//
// 2. Array element unset (NOT SUPPORTED):
// Bash: arr=(a b c); unset arr[1]
// Purified: arr="a c" # Reassign without element
// # Or use awk/sed to remove element
//
// 3. Associative array (NOT SUPPORTED):
// Bash: declare -A map=([k1]=v1 [k2]=v2); unset map[k1]
// Purified: # Use separate variables or external data structure
let bash_extensions = r#"
# BASH EXTENSION: unset -n nameref (NOT SUPPORTED)
# Purify: Use regular variable clearing
# declare -n ref=TARGET
# unset -n ref
# →
TARGET=""
# BASH EXTENSION: unset array[index] (NOT SUPPORTED)
# Purify: Reassign array without element or use awk
# arr=(a b c)
# unset arr[1]
# →
# Set array to "a c" (skip element 1)
# BASH EXTENSION: Associative array unset (NOT SUPPORTED)
# Purify: Use separate variables
# declare -A config=([host]=localhost [port]=8080)
# unset config[port]
# →
config_host="localhost"
config_port="" # Clear instead of unset element
# POSIX SUPPORTED: Regular variable unset
VAR="value"
unset VAR
# POSIX SUPPORTED: Function unset
cleanup() { echo "cleanup"; }
unset -f cleanup
# POSIX SUPPORTED: Multiple unsets
A="1"
B="2"
C="3"
unset A B C
"#;
let mut lexer = Lexer::new(bash_extensions);
match lexer.tokenize() {
Ok(tokens) => {
assert!(
!tokens.is_empty(),
"bash extension examples should tokenize"
);
let _ = tokens;
}
Err(_) => {
// These are purified examples, should parse as comments and POSIX constructs
}
}
}
#[test]
fn test_BUILTIN_020_unset_vs_empty_assignment() {
// DOCUMENTATION: unset vs empty assignment (Important distinction)
//
// unset VAR: Removes variable completely
// VAR="": Sets variable to empty string
//
// DIFFERENCE IN TESTS:
// After unset VAR:
// - [ -z "$VAR" ]: True (empty)
// - [ -n "$VAR" ]: False (not set)
// - ${VAR:-default}: "default" (uses default)
// - ${VAR-default}: "default" (uses default)
//
// After VAR="":
// - [ -z "$VAR" ]: True (empty)
// - [ -n "$VAR" ]: False (empty string)
// - ${VAR:-default}: "default" (empty, uses default)
// - ${VAR-default}: "" (set but empty, no default)
//
// KEY DISTINCTION:
// ${VAR-default}: Use default if VAR is UNSET
// ${VAR:-default}: Use default if VAR is UNSET OR EMPTY
//
// INPUT (bash):
// unset VAR
// echo "${VAR-fallback}" # fallback (unset)
// echo "${VAR:-fallback}" # fallback (unset)
//
// VAR=""
// echo "${VAR-fallback}" # (empty, VAR is set)
// echo "${VAR:-fallback}" # fallback (empty)
//
// RUST:
// let mut vars: HashMap<String, String> = HashMap::new();
// // Unset: key not in map
// vars.get("VAR").unwrap_or(&"fallback".to_string());
//
// // Empty: key in map with empty value
// vars.insert("VAR".to_string(), "".to_string());
// vars.get("VAR").filter(|v| !v.is_empty()).unwrap_or(&"fallback".to_string());
let unset_vs_empty = r#"
# Unset variable
unset VAR
echo "${VAR-default1}" # default1 (unset, uses default)
echo "${VAR:-default2}" # default2 (unset, uses default)
# Empty assignment
VAR=""
echo "${VAR-default3}" # (empty, VAR is SET so no default)
echo "${VAR:-default4}" # default4 (empty, uses default)
# Set to value
VAR="value"
echo "${VAR-default5}" # value
echo "${VAR:-default6}" # value
# Testing with [ -z ] and [ -n ]
unset UNSET_VAR
if [ -z "$UNSET_VAR" ]; then
echo "UNSET_VAR is empty or unset"
fi
EMPTY_VAR=""
if [ -z "$EMPTY_VAR" ]; then
echo "EMPTY_VAR is empty (set but empty)"
fi
# Practical difference
CONFIG_FILE="" # Set but empty
if [ -n "$CONFIG_FILE" ]; then
echo "Using config: $CONFIG_FILE"
else
echo "No config (empty or unset)"
fi
unset CONFIG_FILE # Now truly unset
if [ -n "$CONFIG_FILE" ]; then
echo "Using config: $CONFIG_FILE"
else
echo "No config (unset)"
fi
"#;
let mut lexer = Lexer::new(unset_vs_empty);
match lexer.tokenize() {
Ok(tokens) => {
assert!(
!tokens.is_empty(),
"unset vs empty examples should tokenize"
);
let _ = tokens;
}
Err(_) => {
// Parser may not fully support parameter expansion yet
}
}
}
#[test]
fn test_BUILTIN_020_unset_comparison_table() {
// COMPREHENSIVE COMPARISON: unset in POSIX vs Bash
//
// ┌──────────────────────────────────────────────────────────────────────────┐
// │ Feature: unset Command │
// ├────────────────────────────┬──────────────┬──────────────────────────────┤
// │ Feature │ POSIX Status │ Purification │
// ├────────────────────────────┼──────────────┼──────────────────────────────┤
// │ BASIC UNSET │ │ │
// │ unset VAR │ SUPPORTED │ Keep as-is │
// │ unset -v VAR │ SUPPORTED │ Keep as-is │
// │ unset -f FUNC │ SUPPORTED │ Keep as-is │
// │ unset VAR1 VAR2 VAR3 │ SUPPORTED │ Keep as-is │
// │ │ │ │
// │ EXIT STATUS │ │ │
// │ unset NONEXISTENT → 0 │ SUPPORTED │ Keep as-is │
// │ unset readonly → non-zero │ SUPPORTED │ Keep as-is │
// │ │ │ │
// │ BEHAVIOR │ │ │
// │ Removes variable │ SUPPORTED │ Keep as-is │
// │ Removes function │ SUPPORTED │ Keep as-is │
// │ ${VAR-default} works │ SUPPORTED │ Keep as-is │
// │ ${VAR:-default} works │ SUPPORTED │ Keep as-is │
// │ │ │ │
// │ BASH EXTENSIONS │ │ │
// │ unset -n nameref │ NOT SUPPORT │ Use VAR="" instead │
// │ unset array[index] │ NOT SUPPORT │ Reassign array │
// │ unset assoc[key] │ NOT SUPPORT │ Use separate variables │
// └────────────────────────────┴──────────────┴──────────────────────────────┘
//
// RUST MAPPING:
// unset VAR → vars.remove("VAR")
// unset -f FUNC → functions.remove("FUNC")
// ${VAR-default} → vars.get("VAR").unwrap_or(&"default")
// ${VAR:-default} → vars.get("VAR").filter(|v| !v.is_empty()).unwrap_or(&"default")
//
// DETERMINISM: unset is deterministic (removes variable from environment)
// IDEMPOTENCY: unset is idempotent (unsetting twice has same effect)
// PORTABILITY: Use unset VAR for maximum POSIX compatibility
let comparison_table = r#"
# This test documents the complete POSIX vs Bash comparison for unset
# See extensive comparison table in test function comments above
# POSIX SUPPORTED: Basic unset
unset VAR # Remove variable (default)
unset -v VAR2 # Remove variable (explicit)
unset -f myfunc # Remove function
unset VAR1 VAR2 VAR3 # Remove multiple
# POSIX SUPPORTED: Exit status
unset NONEXISTENT # Exit 0 (not an error)
# readonly CONST="value"
# unset CONST # Exit non-zero (error)
# POSIX SUPPORTED: Behavior after unset
VAR="value"
unset VAR
echo "${VAR-default}" # default (unset, uses default)
echo "${VAR:-default2}" # default2 (unset, uses default)
# POSIX SUPPORTED: Function unset
greet() { echo "hello"; }
greet
unset -f greet
# greet # Would fail
# NOT SUPPORTED: Bash nameref
# declare -n ref=TARGET
# unset -n ref
# →
TARGET="" # Clear instead
# NOT SUPPORTED: Array element unset
# arr=(a b c)
# unset arr[1]
# →
# Reassign: arr="a c"
# NOT SUPPORTED: Associative array
# declare -A map=([k1]=v1)
# unset map[k1]
# →
map_k1="" # Use separate variables
# POSIX PATTERN: Unset vs empty
unset UNSET_VAR # Truly unset
EMPTY_VAR="" # Set but empty
echo "${UNSET_VAR-a}" # a (unset)
echo "${EMPTY_VAR-b}" # (empty, no default)
echo "${UNSET_VAR:-c}" # c (unset)
echo "${EMPTY_VAR:-d}" # d (empty, uses default)
"#;
let mut lexer = Lexer::new(comparison_table);
match lexer.tokenize() {
Ok(tokens) => {
assert!(
!tokens.is_empty(),
"comparison table examples should tokenize"
);
let _ = tokens;
}
Err(_) => {
// Examples document expected behavior
}
}
// Priority: HIGH - unset is essential for variable lifecycle management
// POSIX: IEEE Std 1003.1-2001 unset special builtin
// Portability: Use unset VAR for maximum POSIX compatibility
// Determinism: unset is deterministic (removes variable from environment)
// Idempotency: unset is idempotent (unsetting twice has same effect as once)
}
// ============================================================================
// BASH-BUILTIN-005: printf Command (POSIX SUPPORTED - HIGH PRIORITY)
// ============================================================================
#[test]
fn test_BASH_BUILTIN_005_printf_command_supported() {
// DOCUMENTATION: printf is SUPPORTED (POSIX builtin, HIGH priority)
//
// printf formats and prints data (better than echo for portability)
// Syntax: printf format [arguments ...]
//
// POSIX printf supports:
// - Format specifiers: %s (string), %d (integer), %f (float), %x (hex), %o (octal)
// - Escape sequences: \n (newline), \t (tab), \\ (backslash), \' (quote)
// - Width/precision: %10s (width 10), %.2f (2 decimals)
// - Flags: %- (left align), %0 (zero pad), %+ (force sign)
//
// WHY printf over echo:
// - Portable: POSIX-defined behavior (echo varies across shells)
// - No trailing newline by default (explicit \n control)
// - Format control: Precise formatting like C printf
// - Escape handling: Consistent across all POSIX shells
//
// Bash extensions NOT SUPPORTED:
// - %(...)T date formatting (use date command instead)
// - %b interpret backslash escapes in argument (use \n in format instead)
// - %q shell-quote format (use manual quoting)
//
// INPUT (bash):
// printf '%s %d\n' "Count:" 42
// printf 'Name: %s\nAge: %d\n' "Alice" 30
//
// RUST TRANSFORMATION:
// println!("{} {}", "Count:", 42);
// println!("Name: {}\nAge: {}", "Alice", 30);
//
// PURIFIED (POSIX sh):
// printf '%s %d\n' "Count:" 42
// printf 'Name: %s\nAge: %d\n' "Alice" 30
//
// COMPARISON TABLE: printf POSIX vs Bash vs echo
// ┌─────────────────────────────┬──────────────┬────────────────────────────┐
// │ Feature │ POSIX Status │ Purification Strategy │
// ├─────────────────────────────┼──────────────┼────────────────────────────┤
// │ printf '%s\n' "text" │ SUPPORTED │ Keep as-is │
// │ printf '%d' 42 │ SUPPORTED │ Keep as-is │
// │ printf '%.2f' 3.14159 │ SUPPORTED │ Keep as-is │
// │ printf '%x' 255 │ SUPPORTED │ Keep as-is │
// │ printf '%10s' "right" │ SUPPORTED │ Keep as-is │
// │ printf '%-10s' "left" │ SUPPORTED │ Keep as-is │
// │ printf '%05d' 42 │ SUPPORTED │ Keep as-is │
// │ Escape: \n \t \\ \' │ SUPPORTED │ Keep as-is │
// │ printf %(...)T date │ NOT SUPPORT │ Use date command │
// │ printf %b "a\nb" │ NOT SUPPORT │ Use \n in format │
// │ printf %q "string" │ NOT SUPPORT │ Manual quoting │
// │ echo "text" (non-portable) │ AVOID │ Use printf '%s\n' "text" │
// └─────────────────────────────┴──────────────┴────────────────────────────┘
//
// PURIFICATION EXAMPLES:
//
// 1. Replace echo with printf (POSIX best practice):
// Bash: echo "Hello, World!"
// Purified: printf '%s\n' "Hello, World!"
//
// 2. Replace echo -n with printf (no newline):
// Bash: echo -n "Prompt: "
// Purified: printf '%s' "Prompt: "
//
// 3. Replace date formatting:
// Bash: printf '%(Date: %Y-%m-%d)T\n'
// Purified: printf 'Date: %s\n' "$(date +%Y-%m-%d)"
//
// 4. Replace %b with explicit escapes:
// Bash: printf '%b' "Line1\nLine2"
// Purified: printf 'Line1\nLine2'
//
// PRIORITY: HIGH - printf is the portable alternative to echo
// POSIX: IEEE Std 1003.1-2001 printf utility
let printf_command = r#"
printf '%s\n' "Hello, World!"
printf '%s %d\n' "Count:" 42
printf 'Name: %s\nAge: %d\n' "Alice" 30
printf '%.2f\n' 3.14159
"#;
let mut lexer = Lexer::new(printf_command);
match lexer.tokenize() {
Ok(tokens) => {
assert!(
!tokens.is_empty(),
"printf command should tokenize successfully"
);
let _ = tokens;
}
Err(_) => {
// Parser may not fully support printf yet - test documents expected behavior
}
}
}
#[test]
include!("part4_s6_bash_builtin.rs");