kaish-kernel 0.16.0

Core kernel for kaish: lexer, parser, interpreter, and runtime
Documentation
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
//! Compat tests that run the same script through kaish AND bash, so we can
//! spot where the two diverge.
//!
//! Each `shell_compat!` invocation generates a submodule with two tests:
//!   `<name>::kaish` — always runs, executes the script via the kaish kernel.
//!   `<name>::bash`  — runs only when `KAISH_BASH_COMPAT=1` is set, executes
//!                     the script via `bash -c` and applies the matching
//!                     assertions to bash's stdout/exit code.
//!
//! Usage:
//!   cargo test --test shell_compat_tests                     # kaish only
//!   KAISH_BASH_COMPAT=1 cargo test --test shell_compat_tests # both sides
//!
//! `cargo test bash` filters to just the bash side and surfaces divergences.
//!
//! Available clauses (mix and match in any order):
//!   eq: "..."         — both sides: trimmed stdout equals the literal
//!   kaish_eq: "..."   — kaish-only stdout assertion (pair with bash_eq:
//!                       to capture an intended divergence)
//!   bash_eq: "..."    — bash-only stdout assertion
//!   contains: "..."   — both sides: stdout contains the substring
//!   absent: "..."     — both sides: stdout does not contain the substring
//!   exit: N           — both sides: process exit code equals N
//!
//! Skipped from this suite (kaish-only by design — no benefit comparing):
//!   - `local x = inner` (kaish syntax with spaces; bash uses `local x=inner`)
//!   - `--json` output, kaish-only builtins (jq, glob, write, kaish-trash)
//!   - VFS paths (`/v/...`)
//!   - find / split / seq structured-iteration behavior

mod common;

// ---- $() full statement grammar -------------------------------------------
// $() bodies accept the full statement grammar — &&/|| chains, ; sequences,
// multi-line bodies, and # comments — not just a single pipeline.

shell_compat! {
    name: cmdsubst_and_chain,
    script: "echo $(true && echo hi)",
    eq: "hi",
}

shell_compat! {
    name: cmdsubst_and_chain_short_circuits,
    script: "echo $(false && echo nope)",
    eq: "",
}

shell_compat! {
    name: cmdsubst_or_chain,
    script: "echo $(false || echo fallback)",
    eq: "fallback",
}

shell_compat! {
    name: cmdsubst_semicolon_sequence,
    script: "echo $(printf a; printf b)",
    eq: "ab",
}

shell_compat! {
    name: cmdsubst_multiline_body,
    script: "echo $(printf a
printf b)",
    eq: "ab",
}

shell_compat! {
    name: cmdsubst_comment_in_body,
    script: "echo $(printf a # trailing note
printf b)",
    eq: "ab",
}

shell_compat! {
    name: cmdsubst_chain_in_string_interpolation,
    script: r#"echo "result: $(false || echo recovered)""#,
    eq: "result: recovered",
}

// ---- $? and ${?}: exit-code variable forms --------------------------------

shell_compat! {
    name: braced_last_exit_code_after_success,
    script: "true; echo ${?}",
    eq: "0",
}

shell_compat! {
    name: braced_last_exit_code_after_failure,
    script: "false; echo ${?}",
    eq: "1",
}

shell_compat! {
    name: exit_code_in_arithmetic,
    script: "false; echo $(( $? + 10 ))",
    eq: "11",
}

shell_compat! {
    name: exit_code_in_arithmetic_after_success,
    script: "true; echo $(( $? * 5 ))",
    eq: "0",
}

shell_compat! {
    name: braced_exit_code_in_arithmetic,
    script: "false; echo $(( ${?} + 5 ))",
    eq: "6",
}

shell_compat! {
    name: exit_code_in_string_interpolation,
    script: r#"false; echo "exit code: $?""#,
    eq: "exit code: 1",
}

shell_compat! {
    name: braced_exit_code_in_string_interpolation,
    script: r#"false; echo "exit code: ${?}""#,
    eq: "exit code: 1",
}

// ---- `:` — the null command -----------------------------------------------
// `:` does nothing and exits 0: another spelling of `true`. The two differ in
// bash only under `set -o posix` (special- vs regular-builtin rules), and
// kaish has neither a POSIX mode nor a special-builtin class, so every row
// here agrees with bash.

shell_compat! {
    name: colon_exits_zero,
    script: ": ; echo $?",
    eq: "0",
}

shell_compat! {
    name: colon_ignores_its_arguments,
    script: ": one two three; echo $?",
    eq: "0",
}

shell_compat! {
    name: colon_stands_in_for_an_empty_branch,
    script: "if false; then echo taken; else :; fi; echo $?",
    eq: "0",
}

shell_compat! {
    name: colon_stands_in_for_an_empty_loop_body,
    script: "for i in a b; do :; done; echo done",
    eq: "done",
}

shell_compat! {
    name: colon_stands_in_for_an_empty_function_body,
    script: "f() { :; }; f; echo $?",
    eq: "0",
}

shell_compat! {
    name: colon_runs_the_and_chain,
    script: ": && echo AND",
    eq: "AND",
}

// The colon stays an ordinary character everywhere a command name is not
// expected — no quoting needed for `host:port`, `a:b`, or `awk -F:`.
shell_compat! {
    name: colon_inside_a_word_is_not_a_command,
    script: "echo a:b",
    eq: "a:b",
}

shell_compat! {
    name: colon_in_an_assignment_value_is_not_a_command,
    script: "x=host:8080; echo $x",
    eq: "host:8080",
}

// `$()` has its own command-name production, so the null command needs its own
// row there — the top-level rows do not reach it.
shell_compat! {
    name: colon_inside_command_substitution,
    script: r#"echo "[$(:)]""#,
    eq: "[]",
}

shell_compat! {
    name: colon_as_a_pipeline_stage,
    script: ": | cat; echo rc=$?",
    eq: "rc=0",
}

shell_compat! {
    name: colon_as_a_case_arm_body,
    script: "case a in a) : ;; esac; echo $?",
    eq: "0",
}

// Flag-shaped operands are ignored too — `:` resolves as a special form, so
// nothing clap-parses its arguments. This is the row that would catch `:`
// being demoted to an ordinary registry builtin.
shell_compat! {
    name: colon_ignores_flag_shaped_operands,
    script: ": -x --nope; echo $?",
    eq: "0",
}

// ---- Command substitution -------------------------------------------------

shell_compat! {
    name: nested_command_substitution,
    script: "echo $(echo $(echo hello))",
    eq: "hello",
}

shell_compat! {
    name: deeply_nested_command_substitution,
    script: "echo $(echo $(echo $(echo deep)))",
    eq: "deep",
}

shell_compat! {
    name: cmd_subst_with_true,
    script: "VAR=$(true); echo \"exit: $?\"",
    eq: "exit: 0",
}

shell_compat! {
    name: cmd_subst_with_false,
    script: "VAR=$(false); echo \"exit: $?\"",
    eq: "exit: 1",
}

// ---- Bare-assignment exit status: last command substitution wins ----------
// An assignment with no command name takes the status of the LAST command
// substitution performed in its value, or 0 if there was none. This is what
// lets `x="$(cmd)" || x="FALLBACK"` work: a failed substitution must make the
// assignment fail so `||` fires. Every row re-probed against bash.

shell_compat! {
    name: assignment_failed_subst_fires_or_fallback,
    script: r#"x="$(false)" || echo FIRED"#,
    eq: "FIRED",
}

shell_compat! {
    name: assignment_ok_subst_holds_or_fallback,
    script: r#"x="$(true)" || echo FIRED"#,
    eq: "",
}

shell_compat! {
    name: assignment_failed_subst_gates_and_chain,
    script: r#"x="$(false)" && echo RAN"#,
    eq: "",
}

shell_compat! {
    name: assignment_plain_resets_stale_status,
    script: "false; x=5; echo $?",
    eq: "0",
}

shell_compat! {
    name: assignment_last_subst_wins_success,
    script: r#"x="$(false)$(true)"; echo $?"#,
    eq: "0",
}

shell_compat! {
    name: assignment_last_subst_wins_failure,
    script: r#"x="$(true)$(false)"; echo $?"#,
    eq: "1",
}

shell_compat! {
    name: assignment_nested_subst_outer_status_wins,
    script: "x=$(echo $(false)); echo $?",
    eq: "0",
}

shell_compat! {
    name: assignment_subst_body_ends_in_failed_assignment,
    // Quoted form: the bare `$(y=$(false))` spelling is a parse gap today —
    // the grammar reachable inside an unquoted `$()` body has no assignment
    // production (part of the GH #194 story). The quoted path re-parses the
    // body with the full grammar, so it exercises the runtime semantics now.
    script: r#"x="$(y=$(false))"; echo $?"#,
    eq: "1",
}

shell_compat! {
    name: assignment_with_command_name_takes_command_status,
    script: "x=$(false) true; echo $?",
    eq: "0",
}

shell_compat! {
    name: assignment_failed_subst_trips_errexit,
    script: "set -e; x=$(false); echo unreachable",
    absent: "unreachable",
    exit: 1,
}

shell_compat! {
    name: assignment_failed_subst_in_or_chain_skips_errexit,
    script: "set -e; x=$(false) || x=FALLBACK; echo \"x=$x\"",
    eq: "x=FALLBACK",
}

// `export x=$(false)` is a command named `export`, so it takes `export`'s
// status, not the substitution's — the bash behavior SC2155 warns about, and
// the counterweight to the `local` divergence in shell_bugs_tests.rs. Only a
// BARE assignment consumes the substitution's status.
shell_compat! {
    name: export_assignment_takes_command_status,
    script: "export x=$(false); echo $?",
    eq: "0",
}

// …and that status is the one `||`/`&&` see, not just the one `$?` reports.
shell_compat! {
    name: export_assignment_holds_or_fallback,
    script: "export x=$(false) || echo FIRED",
    eq: "",
}

shell_compat! {
    name: export_assignment_runs_and_chain,
    script: "export x=$(false) && echo OK",
    eq: "OK",
}

// A substitution body's own last statement decides the outer status. The two
// rows are deliberately crossed — inner and final statement disagree in both
// — so each one discriminates "last statement wins" from "the innermost
// substitution wins" and from the old swallowed-status behavior. A pair that
// agreed (`$(y=$(false); false)`) would pass under all three rules.
shell_compat! {
    name: assignment_subst_body_outer_statement_wins,
    script: r#"x="$(y=$(false); true)"; echo $?"#,
    eq: "0",
}

shell_compat! {
    name: assignment_subst_body_failing_last_statement_wins,
    script: r#"x="$(y=$(true); false)"; echo $?"#,
    eq: "1",
}

// A substitution outside an assignment must not reach the next assignment's
// status: `x=5` has no substitution of its own, so it is 0 whatever ran
// before it. The carrier is `true`, not `echo`, so both runners print nothing
// but the `0` — `echo $(false)` would emit a bare newline in bash and nothing
// in kaish, a real divergence that `eq:`'s trim would hide inside this row.
shell_compat! {
    name: bare_subst_does_not_leak_into_later_assignment,
    script: "true $(false); x=5; echo $?",
    eq: "0",
}

shell_compat! {
    name: pipeline_subst_does_not_leak_into_later_assignment,
    script: r#"echo "$(false)" | cat; x=5; echo $?"#,
    eq: "0",
}

// Substitutions in a word evaluate left to right and each one updates `$?`
// as it completes, so a later `$?` in the SAME word sees the substitution
// beside it — not the statement that ran before the word.
shell_compat! {
    name: subst_updates_exit_status_within_the_same_word,
    script: r#"false; echo "$(true) $?""#,
    eq: "0",
}

shell_compat! {
    name: failed_subst_updates_exit_status_within_the_same_word,
    script: r#"true; echo "$(false) $?""#,
    eq: "1",
}

// The ordering cuts one way only: a `$?` BEFORE a substitution in the same
// word still reads the statement that ran before the word.
shell_compat! {
    name: exit_status_before_a_subst_in_the_same_word_is_the_prior_statement,
    script: r#"true; echo "[$?:$(false)]""#,
    eq: "[0:]",
}

// errexit fires on a failed substitution (pinned above); it must not fire on
// a successful one.
shell_compat! {
    name: assignment_ok_subst_does_not_trip_errexit,
    script: "set -e; x=$(true); echo reached",
    eq: "reached",
}

// ---- A compound statement writes `$?` even when it runs no body -----------
// `if`/`while`/`for`/`case` are commands: each exits 0 when no body statement
// ran, and the previous statement's status must not survive it. The stale
// status was invisible in the statement's own result — `||` never fired and
// `set -e` never tripped — so only `$?` could see it, which is exactly the
// failure that does not announce itself.
//
// The for-loop's empty-list source must be a command that FAILS, not merely
// one that prints nothing: `$(printf '')` succeeds, so it overwrites the
// stale status on its own and the row passes with the fix removed. `$(false)`
// is the honest source — empty AND non-zero, the shape of `$(grep pat file)`
// with no matches, without baking in a host path.

shell_compat! {
    name: if_with_no_branch_taken_clears_stale_status,
    script: "false; if false; then true; fi; echo $?",
    eq: "0",
}

shell_compat! {
    name: while_with_zero_iterations_clears_stale_status,
    script: "false; while false; do true; done; echo $?",
    eq: "0",
}

shell_compat! {
    name: for_with_zero_iterations_clears_stale_status,
    script: "for i in $(false); do true; done; echo $?",
    eq: "0",
}

shell_compat! {
    name: case_with_no_matching_branch_clears_stale_status,
    script: "false; case a in b) true;; esac; echo $?",
    eq: "0",
}

// `break` on the first iteration runs no body statement either.
shell_compat! {
    name: while_broken_on_first_iteration_clears_stale_status,
    script: "false; while true; do break; done; echo $?",
    eq: "0",
}

shell_compat! {
    name: for_broken_on_first_iteration_clears_stale_status,
    script: "false; for i in a; do break; done; echo $?",
    eq: "0",
}

// The other direction: a body that DID run still decides the status, so the
// fix cannot be "compound statements are always 0".
shell_compat! {
    name: if_else_branch_status_survives,
    script: "if false; then true; else false; fi; echo $?",
    eq: "1",
}

shell_compat! {
    name: for_body_status_survives,
    script: "for i in a; do false; done; echo $?",
    eq: "1",
}

shell_compat! {
    name: case_matched_branch_status_survives,
    script: "case a in a) false;; esac; echo $?",
    eq: "1",
}

// `while` needs its own body-status guard — the other three arms have one, and
// without this a change that made `while` always report 0 would go unnoticed.
// The condition has to stop on its own, so the body flips the variable it
// tests.
shell_compat! {
    name: while_body_status_survives,
    script: "x=0; while [[ $x -eq 0 ]]; do x=1; false; done; echo $?",
    eq: "1",
}

// A branch that matches but is EMPTY runs no body statement either — the
// matched-return site, not the no-match one.
shell_compat! {
    name: case_matched_empty_branch_clears_stale_status,
    script: "false; case a in a) ;; esac; echo $?",
    eq: "0",
}

shell_compat! {
    name: cmd_subst_in_string,
    script: r#"echo "inner: $(echo nested)""#,
    eq: "inner: nested",
}

shell_compat! {
    name: cmd_subst_in_string_with_var,
    script: r#"VAL=world; echo "hello $(echo $VAL)""#,
    eq: "hello world",
}

shell_compat! {
    name: var_in_default_with_cmd_subst,
    script: r#"echo "${UNSET:-$(echo computed)}""#,
    eq: "computed",
}

// ---- Functions: return / exit-code propagation ----------------------------

shell_compat! {
    name: return_does_not_leak_to_capture,
    script: "f() {\n    echo output\n    return 5\n}\nX=$(f)\necho \"captured: [$X]\"\n",
    contains: "captured: [output]",
    absent: "captured: [5",
}

shell_compat! {
    name: return_sets_exit_code,
    script: "f() { return 42; }\nf\necho $?\n",
    eq: "42",
}

shell_compat! {
    name: cmd_subst_in_return,
    script: "f() { return $(echo 42); }\nf\necho $?\n",
    eq: "42",
}

shell_compat! {
    name: return_without_value,
    script: "f() { echo hi; return; }\nX=$(f)\necho \"got: [$X]\"\n",
    eq: "got: [hi]",
}

// ---- Positional params in arithmetic --------------------------------------

shell_compat! {
    name: positional_params_arithmetic_add,
    script: "add() {\n    echo $(($1 + $2))\n}\nadd 3 4\n",
    eq: "7",
}

shell_compat! {
    name: positional_params_arithmetic_mul,
    script: "mul() {\n    echo $(($1 * $2))\n}\nmul 5 6\n",
    eq: "30",
}

// ---- export ---------------------------------------------------------------

shell_compat! {
    name: export_with_value,
    script: "export FOO=\"bar\"; echo $FOO",
    eq: "bar",
}

shell_compat! {
    name: export_multiple_vars,
    script: "export A=1; export B=2; echo \"$A $B\"",
    eq: "1 2",
}

// ---- Inline env prefix `FOO=bar cmd` --------------------------------------
// An inline assignment prefix is scoped to the one command — it does NOT leak
// into the rest of the script (the bash-divergence bug this fixes).

shell_compat! {
    name: env_prefix_does_not_leak,
    script: r#"FOO=bar echo hi; echo "[$FOO]""#,
    eq: "hi\n[]",
}

shell_compat! {
    name: env_prefix_multiple_do_not_leak,
    script: r#"A=1 B=2 echo hi; echo "[$A$B]""#,
    eq: "hi\n[]",
}

// A plain assignment (no command following) still persists — only the
// prefixed-command form is command-scoped.
shell_compat! {
    name: plain_assignment_still_persists,
    script: "FOO=bar; echo $FOO",
    eq: "bar",
}

// kaish divergence (documented): the prefixed variable is visible to the
// command itself, including its argument expansion, because kaish builtins read
// from scope. bash expands the command's args before applying the prefix, so it
// prints empty. The leak-free guarantee above is the shared, load-bearing part.
shell_compat! {
    name: env_prefix_visible_to_command_args,
    script: "FOO=bar echo $FOO",
    kaish_eq: "bar",
    bash_eq: "",
}

// ---- Nested ${VAR:-default} -----------------------------------------------

shell_compat! {
    name: nested_var_default_all_unset,
    script: r#"echo "${A:-${B:-deep}}""#,
    eq: "deep",
}

shell_compat! {
    name: nested_var_default_outer_set,
    script: r#"A=outer; echo "${A:-${B:-deep}}""#,
    eq: "outer",
}

shell_compat! {
    name: nested_var_default_middle_set,
    script: r#"B=middle; echo "${A:-${B:-deep}}""#,
    eq: "middle",
}

shell_compat! {
    name: deeply_nested_var_defaults,
    script: r#"echo "${A:-${B:-${C:-deepest}}}""#,
    eq: "deepest",
}

// ---- Command substitution in [[ ]], case ----------------------------------

shell_compat! {
    name: cmd_subst_in_test_eq,
    script: r#"[[ $(echo hello) == "hello" ]] && echo "match" || echo "nope""#,
    eq: "match",
}

shell_compat! {
    name: cmd_subst_in_test_neq,
    script: r#"[[ $(echo hello) != "world" ]] && echo "diff" || echo "same""#,
    eq: "diff",
}

shell_compat! {
    name: cmd_subst_in_case,
    script: "case $(echo hello) in\n    hello) echo \"matched\" ;;\n    *) echo \"nope\" ;;\nesac\n",
    eq: "matched",
}

// ---- Dash/plus bare-word patterns in case (GH #144) ------------------------
// `case_parser`'s `pattern_part` had no arm for the lexer's flag-shaped
// tokens or its dash/plus bare-word fallbacks, so a case pattern that
// happened to look like a flag (or a dash-only bareword) failed to parse.

shell_compat! {
    name: case_triple_dash_matches,
    script: "x=\"---\"; case $x in\n    ---) echo \"match\" ;;\n    *) echo \"nope\" ;;\nesac",
    eq: "match",
}

shell_compat! {
    name: case_dash_alternation_matches,
    script: "x=\"--help\"; case $x in\n    -h|--help) echo \"match\" ;;\n    *) echo \"nope\" ;;\nesac",
    eq: "match",
}

shell_compat! {
    name: case_dash_pattern_star_fallback_still_works,
    script: "x=\"other\"; case $x in\n    ---) echo \"dash\" ;;\n    *) echo \"fallback\" ;;\nesac",
    eq: "fallback",
}

// ---- Compound test expressions: [[ A && B ]], [[ A || B ]], [[ ! A ]] ----
//
// Use `/` and `/tmp` (universal on POSIX-ish systems) instead of
// per-test tempdirs so the scripts are pure and deterministic on both sides.

shell_compat! {
    name: compound_and_both_true,
    script: r#"[[ -d / && -d /tmp ]] && echo "both""#,
    eq: "both",
}

shell_compat! {
    name: compound_and_one_false,
    script: r#"[[ -d / && -f /nonexistent_kaish_test ]] && echo "both" || echo "failed""#,
    eq: "failed",
}

shell_compat! {
    name: compound_or_first_true,
    script: r#"[[ -d / || -f /nonexistent_kaish_test ]] && echo "one""#,
    eq: "one",
}

shell_compat! {
    name: compound_or_second_true,
    script: r#"[[ -f /nonexistent_kaish_test || -d / ]] && echo "one""#,
    eq: "one",
}

shell_compat! {
    name: compound_or_both_false,
    script: r#"[[ -f /nonexistent_kaish_test || -f /also_nonexistent_kaish ]] && echo "yes" || echo "no""#,
    eq: "no",
}

shell_compat! {
    name: compound_not_true,
    script: r#"[[ ! -f /nonexistent_kaish_test ]] && echo "correct""#,
    eq: "correct",
}

shell_compat! {
    name: compound_not_false,
    script: r#"[[ ! -d / ]] && echo "yes" || echo "no""#,
    eq: "no",
}

shell_compat! {
    name: compound_double_not,
    script: r#"[[ ! ! -d / ]] && echo "yes" || echo "no""#,
    eq: "yes",
}

shell_compat! {
    name: compound_string_tests,
    script: r#"VAR=""; [[ -z "$VAR" || -n "default" ]] && echo "ok""#,
    eq: "ok",
}

shell_compat! {
    name: compound_with_comparison,
    script: r#"X=5; [[ $X -gt 3 && $X -lt 10 ]] && echo "in range""#,
    eq: "in range",
}

shell_compat! {
    name: compound_in_if,
    script: "X=5\nif [[ $X -gt 0 && $X -lt 10 ]]; then\n    echo valid\nelse\n    echo invalid\nfi\n",
    eq: "valid",
}

// Numeric test operators (-eq/-ne/-gt/-lt/-ge/-le) coerce string operands
// to numbers. The lex-vs-numeric distinction matters once operands have
// more than one digit — `"15".cmp("2")` is Less, but 15 >= 2 numerically.

shell_compat! {
    name: numeric_ge_multidigit_unquoted,
    script: "X=15; [[ $X -ge 2 ]] && echo ok || echo nope",
    eq: "ok",
}

shell_compat! {
    name: numeric_ge_multidigit_quoted,
    script: r#"X=15; [[ "$X" -ge 2 ]] && echo ok || echo nope"#,
    eq: "ok",
}

shell_compat! {
    name: numeric_ge_string_literals,
    script: r#"[[ "15" -ge "2" ]] && echo ok || echo nope"#,
    eq: "ok",
}

shell_compat! {
    name: numeric_lt_multidigit_via_cmd_subst,
    script: r#"COUNT=$(echo 5); [[ "$COUNT" -lt 10 ]] && echo ok || echo nope"#,
    eq: "ok",
}

shell_compat! {
    name: numeric_gt_two_quoted_strings,
    script: r#"[[ "15" -gt "2" ]] && echo ok || echo nope"#,
    eq: "ok",
}

shell_compat! {
    name: numeric_le_two_quoted_strings,
    script: r#"[[ "2" -le "15" ]] && echo ok || echo nope"#,
    eq: "ok",
}

shell_compat! {
    name: numeric_eq_leading_zero_string,
    script: r#"[[ "01" -eq "1" ]] && echo ok || echo nope"#,
    eq: "ok",
}

shell_compat! {
    name: numeric_ne_quoted_strings,
    script: r#"[[ "2" -ne "15" ]] && echo ok || echo nope"#,
    eq: "ok",
}

// `==` does string equality in [[ ]] (bash-compat) and must agree across
// quoted/unquoted RHS — a leading-zero string is NOT equal to a bare
// numeric literal. Use `-eq` to compare numerically.

shell_compat! {
    name: string_eq_leading_zero_vs_int_literal,
    script: r#"X="01"; [[ "$X" == 1 ]] && echo same || echo diff"#,
    eq: "diff",
}

shell_compat! {
    name: string_eq_leading_zero_vs_quoted,
    script: r#"X="01"; [[ "$X" == "1" ]] && echo same || echo diff"#,
    eq: "diff",
}

shell_compat! {
    name: string_eq_quoted_vs_int_literal_agree,
    script: r#"X="1"; [[ "$X" == 1 ]] && [[ "$X" == "1" ]] && echo agree || echo split"#,
    eq: "agree",
}

shell_compat! {
    name: numeric_eq_handles_what_string_eq_does_not,
    script: r#"X="01"; [[ "$X" -eq 1 ]] && echo numeric || echo lex"#,
    eq: "numeric",
}

shell_compat! {
    name: compound_short_circuit_or,
    script: r#"[[ -d / || $(cat /nonexistent_file) == "x" ]] && echo "yes" || echo "no""#,
    eq: "yes",
}

shell_compat! {
    name: compound_short_circuit_and,
    script: r#"[[ -f /nonexistent_kaish_test && $(cat /nonexistent_file) == "x" ]] && echo "yes" || echo "no""#,
    eq: "no",
}

shell_compat! {
    name: compound_not_with_and,
    script: r#"[[ ! -f /nonexistent_kaish_test && -d / ]] && echo "both""#,
    eq: "both",
}

// Precedence: && binds tighter than ||
// [[ -f /nx || -d / && -d /tmp ]] = [[ -f /nx || (-d / && -d /tmp) ]]
// false || (true && true) = true
shell_compat! {
    name: compound_precedence,
    script: r#"[[ -f /nonexistent_kaish_test || -d / && -d /tmp ]] && echo "yes" || echo "no""#,
    eq: "yes",
}

// ---- Tokenization edge cases ----------------------------------------------

shell_compat! {
    name: colon_in_unquoted_arg,
    script: "echo foo::bar",
    eq: "foo::bar",
}

shell_compat! {
    name: colon_port_in_arg,
    script: "echo host:8080",
    eq: "host:8080",
}

shell_compat! {
    name: colon_in_variable_assignment,
    script: "PATH=/usr/bin:/usr/local/bin\necho $PATH",
    eq: "/usr/bin:/usr/local/bin",
}

// ---- Pipelines (large-output deadlock regression) -------------------------

shell_compat! {
    name: pipeline_large_output_no_deadlock,
    script: "seq 1 20000 | wc -l",
    contains: "20000",
}

shell_compat! {
    name: head_n_in_pipeline,
    script: "printf 'a\\nb\\nc\\nd\\ne\\n' | head -n 3",
    eq: "a\nb\nc",
}

shell_compat! {
    name: pipeline_three_stage_large_output_no_deadlock,
    script: "seq 1 20000 | grep '1' | wc -l",
    eq: "13439",
}

// ---- Command-substitution scope/cwd isolation -----------------------------

shell_compat! {
    name: cmd_subst_variable_isolation,
    script: "X=outer\nset_inner() { X=inner; echo $X; }\nY=$(set_inner)\necho $X\n",
    eq: "outer",
}

// ---- for-loop semantics ---------------------------------------------------

shell_compat! {
    name: for_loop_cleanup_after_body_failure,
    script: "for i in 1 2 3; do\n    false\ndone\nfor j in a b c; do\n    echo $j\ndone\n",
    eq: "a\nb\nc",
}

// ---- set -e (errexit) -----------------------------------------------------

shell_compat! {
    name: errexit_after_and_chain,
    script: "set -e\ntrue && true\nfalse\necho \"should not reach\"\n",
    absent: "should not reach",
}

shell_compat! {
    name: errexit_after_or_chain,
    script: "set -e\nfalse || true\nfalse\necho \"should not reach\"\n",
    absent: "should not reach",
}

shell_compat! {
    name: errexit_suppressed_inside_and_chain_left,
    script: "set -e\nfalse && echo right\necho reached\n",
    contains: "reached",
    absent: "right",
}

// ---- Exit-code assertions -------------------------------------------------

shell_compat! {
    name: false_exits_one,
    script: "false",
    exit: 1,
}

shell_compat! {
    name: explicit_exit_code,
    script: "exit 7",
    exit: 7,
}

// ---- Recorded divergences -------------------------------------------------
//
// kaish does NOT split $(...) on whitespace; bash does. Capturing both
// expected outputs in one entry lets the test pass on each side AND fail
// loudly if either side ever changes its behavior unexpectedly.

shell_compat! {
    name: command_subst_no_implicit_split,
    script: "for x in $(echo \"a b c\"); do echo \"[$x]\"; done",
    kaish_eq: "[a b c]",
    bash_eq: "[a]\n[b]\n[c]",
}

// kaish scopes the for-loop variable to the loop frame; bash leaks the
// last bound value into the surrounding scope.

shell_compat! {
    name: for_loop_variable_scoping,
    script: "for i in 1 2 3; do\n    true\ndone\necho \"after: [$i]\"\n",
    kaish_eq: "after: []",
    bash_eq: "after: [3]",
}

// =============================================================================
// `${VAR:-WORD}` default-word quote removal — the quotes are shell syntax,
// not data, so `${X:-"default"}` yields `default` (no literal quotes), same
// as bash. Regression for the 2026-06-09 finding.
// =============================================================================

shell_compat! {
    name: default_word_double_quoted_strips_quotes,
    script: r#"echo ${NAME:-"default"}"#,
    eq: "default",
}

shell_compat! {
    name: default_word_single_quoted_strips_quotes,
    script: "echo ${NAME:-'default'}",
    eq: "default",
}

shell_compat! {
    name: default_word_bare_unchanged,
    script: "echo ${NAME:-default}",
    eq: "default",
}

shell_compat! {
    name: default_word_double_quoted_with_spaces,
    script: r#"echo ${NAME:-"a b c"}"#,
    eq: "a b c",
}

shell_compat! {
    name: default_word_double_quoted_interpolates,
    script: r#"X=hi; echo ${NAME:-"$X there"}"#,
    eq: "hi there",
}

shell_compat! {
    name: default_word_single_quoted_suppresses_interpolation,
    script: "X=hi; echo ${NAME:-'$X there'}",
    eq: "$X there",
}

shell_compat! {
    name: default_word_set_value_wins_over_quoted_default,
    script: r#"NAME=actual; echo ${NAME:-"default"}"#,
    eq: "actual",
}

// `${VAR:-default}` default-word ESCAPED quotes inside DOUBLE quotes (GH #93
// item 5). A backslash-escaped quote inside a double-quoted default word must
// not toggle the quote-tracking state — it's literal data, unescaping to a
// bare quote character, exactly like bash's double-quote escape rule.

shell_compat! {
    name: default_word_double_quoted_escaped_quotes_literal,
    script: r#"echo ${NAME:-"hello \"world\""}"#,
    eq: "hello \"world\"",
}

shell_compat! {
    name: default_word_escaped_backslash_before_quote,
    script: r#"echo ${NAME:-"a\\"}"#,
    eq: "a\\",
}

shell_compat! {
    name: default_word_mixed_single_and_escaped_double_quotes,
    script: r#"echo ${NAME:-"it's \"quoted\""}"#,
    eq: "it's \"quoted\"",
}

// Inside DOUBLE quotes, `'` is not special, so a backslash before it is
// literal — only `\"`, `\$`, `\\`, `` \` `` are escapes in a double-quoted
// region. `${VAR:-"a\'b"}` therefore keeps the backslash (`a\'b`), matching
// bash (kaibo-caught divergence: the escape used to also fire on `\'` here).
shell_compat! {
    name: default_word_double_quoted_backslash_before_squote_literal,
    script: r#"echo ${NAME:-"a\'b"}"#,
    eq: "a\\'b",
}

// Single-quoted default words are a LITERAL region, per shell rules: zero
// interpolation AND zero escape processing. Only the delimiter quotes are
// stripped (syntax, not data); a backslash stays literal and a `'` always
// closes the region — it is never escaped.

shell_compat! {
    name: default_word_single_quoted_strips_delimiters,
    script: "echo ${NAME:-'x'}",
    eq: "x",
}

shell_compat! {
    name: default_word_single_quoted_no_interpolation,
    script: "echo ${NAME:-'$HOME'}",
    eq: "$HOME",
}

shell_compat! {
    name: default_word_single_quoted_backslash_literal,
    script: r#"echo ${NAME:-'a\b'}"#,
    eq: "a\\b",
}

// The shell-correct way to embed a single quote is the `'…'\''…'` idiom:
// close the single-quoted span, emit an UNQUOTED escaped `\'`, reopen. The
// escape fires only outside single quotes — which this fix keeps working.
shell_compat! {
    name: default_word_single_quote_embed_idiom,
    script: r#"echo ${NAME:-'it'\''s'}"#,
    eq: "it's",
}

// =============================================================================
// `break N` / `continue N` must not discard output printed before the signal.
// The signal used to replace the loop's accumulated result on its way up, so
// `break 2` swallowed everything the inner loop had printed. Regression for
// the 2026-06-09 finding.
// =============================================================================

shell_compat! {
    name: break_2_preserves_inner_output,
    script: "for i in 1 2; do for j in a b; do echo \"$j\"; break 2; done; done",
    eq: "a",
}

shell_compat! {
    name: break_1_preserves_output,
    script: "for i in 1 2 3; do echo \"$i\"; break; done",
    eq: "1",
}

shell_compat! {
    name: break_2_preserves_outer_and_inner_output,
    script: "for i in x y; do echo \"outer$i\"; for j in a b; do echo \"inner$j\"; break 2; done; done",
    eq: "outerx\ninnera",
}

shell_compat! {
    name: continue_2_preserves_inner_output,
    script: "for i in 1 2; do for j in a b; do echo \"i${i}j${j}\"; continue 2; done; done",
    eq: "i1ja\ni2ja",
}

// =============================================================================
// Statement-output joining inserts no artificial separator — `;` and `&&` both
// concatenate raw output, matching bash. `printf` (no trailing newline) is the
// discriminator; `echo` brings its own newline. Regression for the 2026-06-09
// finding that `;` gave `ab` while `&&` gave `a\nb`.
// =============================================================================

shell_compat! {
    name: semicolon_sequence_no_separator,
    script: r#"printf "a"; printf "b""#,
    eq: "ab",
}

shell_compat! {
    name: and_chain_no_separator,
    script: r#"printf "a" && printf "b""#,
    eq: "ab",
}

shell_compat! {
    name: semicolon_and_and_chain_agree,
    script: r#"printf "x" && printf "y"; printf "z""#,
    eq: "xyz",
}

shell_compat! {
    name: echo_sequence_keeps_own_newlines,
    script: "echo a; echo b",
    eq: "a\nb",
}

shell_compat! {
    name: printf_loop_no_separator,
    script: "for i in 1 2 3; do printf \"$i\"; done",
    eq: "123",
}

shell_compat! {
    name: unset_is_silent,
    script: "X=1; unset X; echo done",
    eq: "done",
}

// ---- standalone [[ ]] writes $? (found during PR-D) -----------------------
// A bare test statement must store its result in $? like any command. It
// evaluated the test but never wrote $?, so a following read was stale.

shell_compat! {
    name: standalone_test_failure_writes_status,
    script: "[[ 1 = 2 ]]; echo $?",
    eq: "1",
}

shell_compat! {
    name: standalone_test_success_writes_status,
    script: "false; [[ 1 = 1 ]]; echo $?",
    eq: "0",
}

shell_compat! {
    name: standalone_test_status_captured_in_var,
    script: "[[ a = b ]]; ok=$?; echo $ok",
    eq: "1",
}

shell_compat! {
    name: failing_test_gates_and_chain,
    script: "[[ 1 = 2 ]] && echo skipped; echo done",
    eq: "done",
}

shell_compat! {
    name: failing_test_statement_trips_errexit,
    script: "set -e; [[ 1 = 2 ]]; echo unreachable",
    absent: "unreachable",
    exit: 1,
}

// ---- `#` opens a comment only at the start of a word ----------------------
// POSIX, and bash and /bin/sh agree on every row below. kaish used to start a
// comment at any `#`, which truncated the word AND swallowed the rest of the
// line — `;` separators included — at exit 0. See `hash_word_tests.rs`.

shell_compat! {
    name: hash_mid_word_is_literal,
    script: "echo abc#3",
    eq: "abc#3",
}

shell_compat! {
    name: hash_mid_word_does_not_swallow_the_line,
    script: "echo abc#3; echo two",
    eq: "abc#3\ntwo",
}

shell_compat! {
    name: hash_in_digit_leading_word_is_literal,
    script: "echo 2d25fb02#3",
    eq: "2d25fb02#3",
}

shell_compat! {
    name: hash_in_path_is_literal,
    script: "echo /foo/bar#1",
    eq: "/foo/bar#1",
}

shell_compat! {
    name: repeated_hashes_are_literal,
    script: "echo a#b#c",
    eq: "a#b#c",
}

shell_compat! {
    name: trailing_hash_belongs_to_the_word,
    script: "echo abc# 3",
    eq: "abc# 3",
}

shell_compat! {
    name: spaced_hash_starts_a_comment,
    script: "echo abc #3",
    eq: "abc",
}

shell_compat! {
    name: hash_after_semicolon_starts_a_comment,
    script: "echo a;#c",
    eq: "a",
}

shell_compat! {
    name: quoted_leading_hash_is_literal,
    script: "echo '#3'",
    eq: "#3",
}