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
#![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_BASH_VAR_003_seconds_common_antipatterns() {
// DOCUMENTATION: Common $SECONDS antipatterns and their fixes (6 antipatterns)
//
// ANTIPATTERN 1: Performance measurement
// BAD: SECONDS=0; run_benchmark; echo "Took $SECONDS seconds"
// GOOD: Use external benchmarking tool (hyperfine, time)
// Why: Benchmarks should be repeatable with controlled environment
//
// ANTIPATTERN 2: Timeouts based on elapsed time
// BAD: start=$SECONDS; while [ $((SECONDS - start)) -lt 60 ]; do ...; done
// GOOD: Use attempt counter: attempt=0; while [ $attempt -lt 60 ]; do ...; attempt=$((attempt + 1)); done
// Why: Attempt counters are deterministic
//
// ANTIPATTERN 3: Log timestamps with $SECONDS
// BAD: echo "[$SECONDS] Operation completed"
// GOOD: Use fixed log format or remove timestamps
// Why: Logs should be reproducible for testing
//
// ANTIPATTERN 4: Rate limiting with $SECONDS
// BAD: if [ $((SECONDS % 10)) -eq 0 ]; then echo "Status"; fi
// GOOD: Use fixed intervals or remove rate limiting
// Why: Rate limiting should be deterministic
//
// ANTIPATTERN 5: Progress indicators with $SECONDS
// BAD: echo "Progress: $((SECONDS * 100 / 300))%"
// GOOD: Use actual progress counter
// Why: Progress should be based on work done, not time
//
// ANTIPATTERN 6: Script execution time reporting
// BAD: echo "Script ran for $SECONDS seconds"
// GOOD: Remove execution time reporting
// Why: Execution time varies, not deterministic
let antipatterns = r#"
# ANTIPATTERN 1: Performance measurement
# BAD: SECONDS=0; run_benchmark; echo "Took $SECONDS seconds"
# GOOD: Use external tool
# hyperfine --warmup 3 './benchmark.sh'
# ANTIPATTERN 2: Timeouts
# BAD: start=$SECONDS; while [ $((SECONDS - start)) -lt 60 ]; do ...; done
# GOOD: Attempt counter
max_attempts=60
attempt=0
while [ $attempt -lt $max_attempts ]; do
check_condition && break
sleep 1
attempt=$((attempt + 1))
done
# ANTIPATTERN 3: Log timestamps
# BAD: echo "[$SECONDS] Operation completed"
# GOOD: Fixed log format
echo "[INFO] Operation completed"
# ANTIPATTERN 4: Rate limiting
# BAD: if [ $((SECONDS % 10)) -eq 0 ]; then echo "Status"; fi
# GOOD: Fixed intervals (deterministic)
counter=0
for item in $items; do
process "$item"
counter=$((counter + 1))
if [ $((counter % 10)) -eq 0 ]; then
echo "Processed $counter items"
fi
done
# ANTIPATTERN 5: Progress indicators
# BAD: echo "Progress: $((SECONDS * 100 / 300))%"
# GOOD: Actual progress
total=100
completed=0
for item in $items; do
process "$item"
completed=$((completed + 1))
progress=$((completed * 100 / total))
echo "Progress: ${progress}%"
done
# ANTIPATTERN 6: Execution time reporting
# BAD: echo "Script ran for $SECONDS seconds"
# GOOD: Remove timing
echo "Script completed successfully"
"#;
let mut lexer = Lexer::new(antipatterns);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Antipatterns should tokenize successfully"
);
let _ = tokens;
}
// All antipatterns involve $SECONDS (time-dependent)
// All fixes are DETERMINISTIC alternatives
// CRITICAL: Never use $SECONDS in production scripts
}
#[test]
fn test_BASH_VAR_003_seconds_determinism_violations() {
// DOCUMENTATION: How $SECONDS violates determinism (4 critical violations)
//
// VIOLATION 1: Time-dependent output
// #!/bin/sh
// echo "Elapsed: $SECONDS seconds"
// Running at different times produces different output
// EXPECTED (deterministic): Same output every run
//
// VIOLATION 2: Cannot replay execution
// Script with $SECONDS cannot be replayed with same timing
// Fast machine vs slow machine produces different results
// EXPECTED: Replay should produce identical results regardless of execution speed
//
// VIOLATION 3: Tests non-reproducible
// test_performance() {
// SECONDS=0
// run_operation
// assert $SECONDS -lt 10 # Flaky! Depends on machine speed
// }
// EXPECTED: Tests should be reproducible regardless of machine speed
//
// VIOLATION 4: Race conditions in timing logic
// Timeout logic using $SECONDS may behave differently on different runs
// EXPECTED: Deterministic retry logic (attempt counters)
let determinism_violations = r#"
# VIOLATION 1: Time-dependent output
#!/bin/sh
echo "Script ran for $SECONDS seconds"
# Run 1 (fast machine): Script ran for 2 seconds
# Run 2 (slow machine): Script ran for 5 seconds
# PROBLEM: Output depends on execution speed
# VIOLATION 2: Cannot replay execution
#!/bin/sh
SECONDS=0
deploy_application
echo "Deployment took $SECONDS seconds"
# PROBLEM: Cannot replay with same timing
# Fast retry: 3 seconds, Slow retry: 10 seconds
# VIOLATION 3: Tests non-reproducible
#!/bin/sh
test_performance() {
SECONDS=0
run_operation
# PROBLEM: Test may pass on fast machine, fail on slow machine
[ $SECONDS -lt 10 ] || exit 1
}
# VIOLATION 4: Timing race conditions
#!/bin/sh
start=$SECONDS
while [ $((SECONDS - start)) -lt 30 ]; do
check_service && break
sleep 1
done
# PROBLEM: Service may start at different times
# Fast run: service starts in 5 seconds
# Slow run: service starts in 25 seconds
# Results in different behavior
"#;
let mut lexer = Lexer::new(determinism_violations);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Determinism violations should tokenize successfully"
);
let _ = tokens;
}
// $SECONDS violates determinism (time-dependent)
// bashrs FORBIDS $SECONDS to enforce determinism
// CRITICAL: Execution time should not affect script output
}
#[test]
fn test_BASH_VAR_003_seconds_portability_issues() {
// DOCUMENTATION: $SECONDS portability issues (3 critical issues)
//
// ISSUE 1: Not POSIX (bash-specific)
// $SECONDS only exists in bash, ksh, zsh
// POSIX sh: $SECONDS is UNDEFINED (may be literal string "$SECONDS")
// dash: $SECONDS is UNDEFINED
// ash: $SECONDS is UNDEFINED
//
// ISSUE 2: Reset behavior differs
// bash: SECONDS=0 resets timer
// ksh: SECONDS=0 resets timer (but may not reset to exactly 0)
// zsh: SECONDS=0 resets timer
// POSIX sh: SECONDS=0 just sets a variable (no timer)
//
// ISSUE 3: Precision varies
// bash: $SECONDS is integer (whole seconds)
// Some shells may have subsecond precision
// Behavior is INCONSISTENT across shells
//
// PURIFICATION STRATEGY:
// Replace ALL $SECONDS with deterministic alternatives
// Use attempt counters, fixed durations, or remove timing logic
let portability_issues = r#"
#!/bin/sh
# This script is NOT PORTABLE (uses $SECONDS)
# ISSUE 1: Not POSIX
echo "Elapsed: $SECONDS seconds" # bash: works, dash: UNDEFINED
# ISSUE 2: Reset behavior
SECONDS=0 # bash: resets timer, dash: just sets variable
operation
echo "Took $SECONDS seconds" # bash: elapsed time, dash: literal "0"
# ISSUE 3: Precision
# bash: integer seconds only
# zsh: may have subsecond precision (non-portable)
# PURIFIED (POSIX-compliant):
# Use attempt counter instead of time
attempts=0
max_attempts=60
while [ $attempts -lt $max_attempts ]; do
check_condition && break
sleep 1
attempts=$((attempts + 1))
done
echo "Took $attempts attempts"
"#;
let mut lexer = Lexer::new(portability_issues);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Portability issues should tokenize successfully"
);
let _ = tokens;
}
// $SECONDS is NOT PORTABLE (bash-specific)
// bashrs targets POSIX sh (no $SECONDS support)
// PURIFICATION: Use attempt counters or fixed durations
}
#[test]
fn test_BASH_VAR_003_seconds_testing_implications() {
// DOCUMENTATION: $SECONDS testing implications (4 critical issues for testing)
//
// ISSUE 1: Non-reproducible tests
// test_deployment() {
// SECONDS=0
// deploy_app
// assert $SECONDS -lt 60 # Flaky! Depends on machine speed
// }
// PROBLEM: Test passes on fast machine, fails on slow machine
//
// ISSUE 2: Cannot assert on output
// output=$(./script.sh) # Script uses $SECONDS
// assert "$output" == "Took 5 seconds" # Flaky! Timing varies
// PROBLEM: Cannot write assertions for time-dependent output
//
// ISSUE 3: Flaky tests (timing heisenbug)
// Test passes 99% of time (fast), fails 1% (slow)
// Due to $SECONDS producing different values based on execution speed
// PROBLEM: Developers lose trust in test suite
//
// ISSUE 4: Cannot replay failures
// Test fails in CI (slow), cannot reproduce locally (fast)
// Bug only occurs with specific timing
// PROBLEM: Cannot debug or fix timing-dependent bug
//
// TESTING BEST PRACTICES:
// 1. Never use $SECONDS in production code
// 2. Use attempt counters instead of timers
// 3. Remove timing assertions from tests
// 4. Use deterministic test data (fixed attempt counts)
let testing_implications = r#"
#!/bin/sh
# TESTING EXAMPLES
# BAD TEST: Time-dependent assertion
test_bad() {
SECONDS=0
operation
# PROBLEM: Assertion depends on execution speed
[ $SECONDS -lt 10 ] || exit 1
}
# GOOD TEST: Deterministic (no timing)
test_good() {
operation
# Assert on actual result, not timing
[ -f /tmp/output.txt ] || exit 1
}
# BAD TEST: Cannot assert on output
test_flaky_output() {
output=$(./script.sh) # Uses $SECONDS
# PROBLEM: Output varies based on timing
# [ "$output" = "Took 5 seconds" ] || exit 1 # Flaky!
}
# GOOD TEST: Deterministic output
test_deterministic_output() {
output=$(./script.sh) # No $SECONDS
[ "$output" = "Operation completed" ] || exit 1
}
# BAD TEST: Performance assertion (flaky)
test_performance_bad() {
SECONDS=0
benchmark
# PROBLEM: Fast machine passes, slow machine fails
[ $SECONDS -lt 30 ] || exit 1
}
# GOOD TEST: No performance assertions
test_correctness_good() {
result=$(benchmark)
# Assert on correctness, not speed
[ "$result" = "expected_output" ] || exit 1
}
# GOOD TEST: Deterministic retry logic
test_retry_deterministic() {
attempts=0
max_attempts=10
while [ $attempts -lt $max_attempts ]; do
check_condition && break
attempts=$((attempts + 1))
done
# Assert on attempts, not time
[ $attempts -lt $max_attempts ] || exit 1
}
"#;
let mut lexer = Lexer::new(testing_implications);
if let Ok(tokens) = lexer.tokenize() {
assert!(
!tokens.is_empty(),
"Testing implications should tokenize successfully"
);
let _ = tokens;
}
// $SECONDS makes tests NON-REPRODUCIBLE and FLAKY
// bashrs enforces DETERMINISTIC testing
// NEVER use $SECONDS in test code
}
#[test]
include!("part5_s3_bash_var.rs");