nu-command 0.115.0

Nushell's built-in commands
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
use nu_protocol::{ParseError, ShellError, parser_path::MAX_RUN_SCRIPT_BYTES};
use nu_test_support::{fs::Stub::FileWithContentToBeTrimmed, prelude::*};
use std::io::Write;

#[test]
fn run_script_without_main_in_pipeline() -> Result {
    Playground::setup("run_script_without_main_in_pipeline", |dirs, sandbox| {
        sandbox.with_files(&[FileWithContentToBeTrimmed(
            "up.nu",
            "
                str uppercase
            ",
        )]);

        test()
            .cwd(dirs.test())
            .run(r#""hello" | run up.nu"#)
            .expect_value_eq("HELLO")
    })
}

#[test]
fn run_script_with_main_implicit_in() -> Result {
    Playground::setup("run_script_with_main_implicit_in", |dirs, sandbox| {
        sandbox.with_files(&[FileWithContentToBeTrimmed(
            "main_up.nu",
            "
                def main [] {
                    $in | str uppercase
                }
            ",
        )]);

        test()
            .cwd(dirs.test())
            .run(r#""hello" | run main_up.nu"#)
            .expect_value_eq("HELLO")
    })
}

#[test]
fn run_null_passes_pipeline_input_through() -> Result {
    Playground::setup("run_null_passes_pipeline_input_through", |dirs, _| {
        test()
            .cwd(dirs.test())
            .run(r#""hello" | run null"#)
            .expect_value_eq("hello")
    })
}

#[test]
fn run_script_with_main_parameters_and_flags() -> Result {
    Playground::setup(
        "run_script_with_main_parameters_and_flags",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "format.nu",
                "
                def main [value: string, --char: string] {
                    $\"($in) ($value) ($char)\"
                }
            ",
            )]);

            test()
                .cwd(dirs.test())
                .run(r#""hello" | run format.nu "arg" --char "!" "#)
                .expect_value_eq("hello arg !")
        },
    )
}

#[test]
fn run_script_with_main_parameters_and_short_flags() -> Result {
    Playground::setup(
        "run_script_with_main_parameters_and_short_flags",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "format_short.nu",
                "
                def main [value: string, --char(-c): string] {
                    $\"($in) ($value) ($char)\"
                }
            ",
            )]);

            test()
                .cwd(dirs.test())
                .run(r#""hello" | run format_short.nu "arg" -c "!" "#)
                .expect_value_eq("hello arg !")
        },
    )
}

#[test]
fn run_script_with_main_required_positional_does_not_implicitly_bind_pipeline_input() -> Result {
    Playground::setup(
        "run_script_with_main_required_positional_does_not_implicitly_bind_pipeline_input",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "needs_arg.nu",
                "
                def main [value: string] {
                    $value
                }
            ",
            )]);

            let _ = test()
                .cwd(dirs.test())
                .run(r#""hello" | run needs_arg.nu"#)
                .expect_error()?;
            Ok(())
        },
    )
}

#[test]
fn run_script_with_main_keeps_pipeline_input_in_in_when_positional_is_provided() -> Result {
    Playground::setup(
        "run_script_with_main_keeps_pipeline_input_in_in_when_positional_is_provided",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "in_and_arg.nu",
                "
                def main [file: path] {
                    $\"($in) -> ($file)\"
                }
            ",
            )]);

            test()
                .cwd(dirs.test())
                .run(r#""stream" | run in_and_arg.nu "path.txt""#)
                .expect_value_eq("stream -> path.txt")
        },
    )
}

#[test]
fn run_script_with_exported_main_uses_main_entrypoint() -> Result {
    Playground::setup(
        "run_script_with_exported_main_uses_main_entrypoint",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "exported_main.nu",
                "
                export def main [] {
                    $in | str uppercase
                }
            ",
            )]);

            test()
                .cwd(dirs.test())
                .run(r#""hello" | run exported_main.nu"#)
                .expect_value_eq("HELLO")
        },
    )
}

#[test]
fn run_script_with_exported_env_main_uses_main_entrypoint_without_leaking_env() -> Result {
    Playground::setup(
        "run_script_with_exported_env_main_uses_main_entrypoint_without_leaking_env",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "exported_env_main.nu",
                "
                    export def --env main [] {
                        $env.RUN_LOCAL = 'secret'
                        $in | str uppercase
                    }
                ",
            )]);

            let mut tester = test().cwd(dirs.test());
            tester
                .run(r#""hello" | run exported_env_main.nu"#)
                .expect_value_eq("HELLO")?;
            match tester.run("$env.RUN_LOCAL").expect_shell_error()? {
                ShellError::CantFindColumn { col_name, .. } if col_name == "RUN_LOCAL" => Ok(()),
                err => Err(err.into()),
            }
        },
    )
}

#[test]
fn run_script_without_main_large_input_in_each() -> Result {
    Playground::setup(
        "run_script_without_main_large_input_in_each",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "double.nu",
                "
                $in * 2
            ",
            )]);

            test()
                .cwd(dirs.test())
                .run("1..1000 | each { run double.nu } | math sum")
                .expect_value_eq(1001000)
        },
    )
}

#[test]
fn run_does_not_leak_env_from_script_without_main() -> Result {
    Playground::setup(
        "run_does_not_leak_env_from_script_without_main",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "set_env.nu",
                "
                $env.RUN_LOCAL = 'secret'
                $in
            ",
            )]);

            let mut tester = test().cwd(dirs.test());
            tester
                .run(r#""hello" | run set_env.nu"#)
                .expect_value_eq("hello")?;
            match tester.run("$env.RUN_LOCAL").expect_shell_error()? {
                ShellError::CantFindColumn { col_name, .. } if col_name == "RUN_LOCAL" => Ok(()),
                err => Err(err.into()),
            }
        },
    )
}

#[test]
fn run_does_not_leak_env_from_script_main() -> Result {
    Playground::setup("run_does_not_leak_env_from_script_main", |dirs, sandbox| {
        sandbox.with_files(&[FileWithContentToBeTrimmed(
            "set_env_main.nu",
            "
                def main [] {
                    $env.RUN_LOCAL = 'secret'
                    $in
                }
            ",
        )]);

        let mut tester = test().cwd(dirs.test());
        tester
            .run(r#""hello" | run set_env_main.nu"#)
            .expect_value_eq("hello")?;
        match tester.run("$env.RUN_LOCAL").expect_shell_error()? {
            ShellError::CantFindColumn { col_name, .. } if col_name == "RUN_LOCAL" => Ok(()),
            err => Err(err.into()),
        }
    })
}

#[test]
fn run_missing_script_reports_error() -> Result {
    Playground::setup("run_missing_script_reports_error", |dirs, _| {
        let err = test()
            .cwd(dirs.test())
            .run(r#""hello" | run does_not_exist.nu"#)
            .expect_parse_error()?;
        assert!(
            matches!(err, ParseError::SourcedFileNotFound(ref path, _) if path == "does_not_exist.nu"),
            "expected missing script error, got: {err:?}"
        );
        Ok(())
    })
}

#[test]
fn run_script_parse_error_reports_error() -> Result {
    Playground::setup("run_script_parse_error_reports_error", |dirs, sandbox| {
        sandbox.with_files(&[FileWithContentToBeTrimmed(
            "bad.nu",
            "
                def main [ {
                    $in
                }
            ",
        )]);

        let _ = test()
            .cwd(dirs.test())
            .run(r#""hello" | run bad.nu"#)
            .expect_parse_error()?;
        Ok(())
    })
}

#[test]
fn run_script_runtime_error_reports_error() -> Result {
    Playground::setup("run_script_runtime_error_reports_error", |dirs, sandbox| {
        sandbox.with_files(&[FileWithContentToBeTrimmed(
            "runtime_fail.nu",
            "
                def main [] {
                    error make { msg: 'boom from run' }
                }
            ",
        )]);

        let err = test()
            .cwd(dirs.test())
            .run(r#""hello" | run runtime_fail.nu"#)
            .expect_error()?;
        assert_contains("boom from run", err.to_string());
        Ok(())
    })
}

#[test]
fn run_multiple_scripts_in_pipeline() -> Result {
    Playground::setup("run_multiple_scripts_in_pipeline", |dirs, sandbox| {
        sandbox.with_files(&[
            FileWithContentToBeTrimmed(
                "up.nu",
                "
                    str uppercase
                ",
            ),
            FileWithContentToBeTrimmed(
                "len.nu",
                "
                    def main [] {
                        str length
                    }
                ",
            ),
        ]);

        test()
            .cwd(dirs.test())
            .run(r#""hello" | run up.nu | run len.nu"#)
            .expect_value_eq(5)
    })
}

#[test]
fn run_nested_pipeline_with_each() -> Result {
    Playground::setup("run_nested_pipeline_with_each", |dirs, sandbox| {
        sandbox.with_files(&[
            FileWithContentToBeTrimmed(
                "up.nu",
                "
                    str uppercase
                ",
            ),
            FileWithContentToBeTrimmed(
                "len.nu",
                "
                    def main [] {
                        str length
                    }
                ",
            ),
        ]);

        test()
            .cwd(dirs.test())
            .run("['a', 'bb', 'ccc'] | each { |x| $x | run up.nu | run len.nu } | math sum")
            .expect_value_eq(6)
    })
}

#[test]
fn run_does_not_cross_script_main_between_invocations() -> Result {
    Playground::setup(
        "run_does_not_cross_script_main_between_invocations",
        |dirs, sandbox| {
            sandbox.with_files(&[
                FileWithContentToBeTrimmed(
                    "run-test1.nu",
                    "
                        str uppercase
                    ",
                ),
                FileWithContentToBeTrimmed(
                    "run-test2.nu",
                    "
                        def main [] {
                            str length
                        }
                    ",
                ),
            ]);

            let mut tester = test().cwd(dirs.test());
            tester
                .run(r#""hello" | run run-test1.nu"#)
                .expect_value_eq("HELLO")?;
            tester
                .run(r#""hello" | run run-test2.nu"#)
                .expect_value_eq(5)?;
            tester
                .run(r#""hello" | run run-test1.nu"#)
                .expect_value_eq("HELLO")
        },
    )
}

#[test]
fn run_main_script_can_be_invoked_repeatedly() -> Result {
    Playground::setup(
        "run_main_script_can_be_invoked_repeatedly",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "run-test2.nu",
                "
                def main [] {
                    str length
                }
            ",
            )]);

            let mut tester = test().cwd(dirs.test());
            tester
                .run(r#""hello" | run run-test2.nu"#)
                .expect_value_eq(5)?;
            tester
                .run(r#""hello" | run run-test2.nu"#)
                .expect_value_eq(5)?;
            tester
                .run(r#""hello" | run run-test2.nu"#)
                .expect_value_eq(5)
        },
    )
}

#[test]
fn run_main_script_tracks_file_edits_in_repl_session() -> Result {
    Playground::setup(
        "run_main_script_tracks_file_edits_in_repl_session",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "run-edit.nu",
                "
                def main [] {
                    'hello'
                }
            ",
            )]);

            let mut tester = test().cwd(dirs.test());
            tester.run("run run-edit.nu").expect_value_eq("hello")?;
            tester.run::<()>(r#"'def main [] { "hello world" }' | save --force run-edit.nu"#)?;
            tester
                .run("run run-edit.nu")
                .expect_value_eq("hello world")?;
            tester.run::<()>(r#"'def main [] { "hello" }' | save --force run-edit.nu"#)?;
            tester.run("run run-edit.nu").expect_value_eq("hello")
        },
    )
}

#[test]
fn run_main_script_in_reused_closure_keeps_cached_parse_by_default() -> Result {
    Playground::setup(
        "run_main_script_in_reused_closure_keeps_cached_parse_by_default",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "run-edit.nu",
                "
                def main [] {
                    'hello'
                }
            ",
            )]);

            let mut tester = test().cwd(dirs.test());
            tester.run::<()>("let runner = { run run-edit.nu }")?;
            tester.run("do $runner").expect_value_eq("hello")?;
            tester.run::<()>(r#"'def main [] { "hello world" }' | save --force run-edit.nu"#)?;
            tester.run("do $runner").expect_value_eq("hello")
        },
    )
}

#[test]
fn run_main_script_in_reused_closure_reloads_with_full_reparse() -> Result {
    Playground::setup(
        "run_main_script_in_reused_closure_reloads_with_full_reparse",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "run-edit.nu",
                "
                def main [] {
                    'hello'
                }
            ",
            )]);

            let mut tester = test().cwd(dirs.test());
            tester.run::<()>("let runner = { run --full-reparse run-edit.nu }")?;
            tester.run("do $runner").expect_value_eq("hello")?;
            tester.run::<()>(r#"'def main [] { "hello world" }' | save --force run-edit.nu"#)?;
            tester.run("do $runner").expect_value_eq("hello world")?;
            tester.run::<()>(r#"'def main [] { "hello again" }' | save --force run-edit.nu"#)?;
            tester.run("do $runner").expect_value_eq("hello again")
        },
    )
}

#[test]
fn run_script_without_main_tracks_file_edits_with_full_reparse() -> Result {
    Playground::setup(
        "run_script_without_main_tracks_file_edits_with_full_reparse",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "run-no-main.nu",
                "
                str uppercase
            ",
            )]);

            let mut tester = test().cwd(dirs.test());
            tester
                .run(r#""hello" | run --full-reparse run-no-main.nu"#)
                .expect_value_eq("HELLO")?;
            tester.run::<()>("'str downcase' | save --force run-no-main.nu")?;
            tester
                .run(r#""HELLO" | run --full-reparse run-no-main.nu"#)
                .expect_value_eq("hello")
        },
    )
}

#[test]
fn run_full_reparse_recovers_after_script_parse_error() -> Result {
    Playground::setup(
        "run_full_reparse_recovers_after_script_parse_error",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "run-edit.nu",
                "
                def main [] {
                    'ok'
                }
            ",
            )]);

            let mut tester = test().cwd(dirs.test());
            tester
                .run("run --full-reparse run-edit.nu")
                .expect_value_eq("ok")?;
            tester.run::<()>("'def main [ {' | save --force run-edit.nu")?;
            let _ = tester
                .run("run --full-reparse run-edit.nu")
                .expect_shell_error()?;
            tester.run::<()>(r#"'def main [] { "ok again" }' | save --force run-edit.nu"#)?;
            tester
                .run("run --full-reparse run-edit.nu")
                .expect_value_eq("ok again")
        },
    )
}

#[test]
fn run_full_reparse_forwards_main_arguments_and_flags() -> Result {
    Playground::setup(
        "run_full_reparse_forwards_main_arguments_and_flags",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "format.nu",
                "
                def main [value: string, --char(-c): string] {
                    $\"($in) ($value) ($char)\"
                }
            ",
            )]);

            test()
                .cwd(dirs.test())
                .run(r#""hello" | run --full-reparse format.nu "arg" -c "!" "#)
                .expect_value_eq("hello arg !")
        },
    )
}

#[test]
fn run_script_with_toolkit_like_exports_can_be_run_twice_in_repl_session() -> Result {
    // Regression: running a toolkit-style script (with exports) must not break
    // subsequent `run` invocations in the same REPL session. The export must not
    // be named `run` — that is a parser keyword and is rejected.
    Playground::setup(
        "run_script_with_toolkit_like_exports_can_be_run_twice_in_repl_session",
        |dirs, sandbox| {
            sandbox.mkdir("toolkit");
            sandbox.with_files(&[
                FileWithContentToBeTrimmed(
                    "toolkit/wrappers.nu",
                    "
                    export def dev [--experimental-options: string] {
                        'toolkit dev'
                    }
                ",
                ),
                FileWithContentToBeTrimmed(
                    "toolkit/mod.nu",
                    "
                    export use wrappers.nu *

                    export def main [] {
                        'toolkit main'
                    }
                ",
                ),
                FileWithContentToBeTrimmed(
                    "toolkit.nu",
                    "
                    export use toolkit *

                    export def main [] {
                        help toolkit
                        'ok'
                    }
                ",
                ),
            ]);

            let mut tester = test().cwd(dirs.test());
            tester.run("run toolkit.nu").expect_value_eq("ok")?;
            tester.run("run toolkit.nu").expect_value_eq("ok")
        },
    )
}

#[test]
fn run_script_binds_long_flag_by_name_not_declaration_order() -> Result {
    Playground::setup(
        "run_script_binds_long_flag_by_name_not_declaration_order",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "flags.nu",
                "
                def main [--alpha: int, --beta: int, --gamma: int] {
                    $\"($alpha | default 0)/($beta | default 0)/($gamma | default 0)\"
                }
            ",
            )]);

            // `--gamma` must bind to `--gamma` by name. Previously a long flag
            // matched the first declared flag that had no short character
            // (`--alpha`), so the value silently landed in the wrong slot.
            let mut tester = test().cwd(dirs.test());
            tester
                .run("run flags.nu --gamma 3")
                .expect_value_eq("0/0/3")
        },
    )
}

#[test]
fn run_script_binds_switch_by_name_without_shifting_positional() -> Result {
    Playground::setup(
        "run_script_binds_switch_by_name_without_shifting_positional",
        |dirs, sandbox| {
            sandbox.with_files(&[FileWithContentToBeTrimmed(
                "switch.nu",
                "
                def main [word: string, --num: int, --verbose] {
                    $\"word=($word) num=($num | default 0) verbose=($verbose)\"
                }
            ",
            )]);

            // `--verbose` is a switch declared after the value-taking `--num`.
            // It must bind by name; otherwise it matched `--num`, which then
            // swallowed `hello` as its (int) value and left `word` unbound.
            let mut tester = test().cwd(dirs.test());
            tester
                .run("run switch.nu hello --verbose")
                .expect_value_eq("word=hello num=0 verbose=true")
        },
    )
}

/// Oversized paths must not be loaded by `run` (REPL hang / multi-GiB RAM; #18597).
#[test]
fn run_oversized_file_errors_without_loading() -> Result {
    Playground::setup("run_oversized_file_errors_without_loading", |dirs, _| {
        let path = dirs.test().join("huge.bin");
        let file = std::fs::File::create(&path).expect("create huge.bin");
        // Sparse size only — do not write MAX_RUN_SCRIPT_BYTES of data.
        file.set_len(MAX_RUN_SCRIPT_BYTES + 1)
            .expect("set oversized length");

        let err = test()
            .cwd(dirs.test())
            .run("run huge.bin")
            .expect_parse_error()?;
        assert!(
            matches!(
                err,
                ParseError::ScriptFileTooLarge {
                    size,
                    max_size,
                    ..
                } if size == MAX_RUN_SCRIPT_BYTES + 1 && max_size == MAX_RUN_SCRIPT_BYTES
            ),
            "expected ScriptFileTooLarge, got: {err:?}"
        );
        Ok(())
    })
}

/// Binary files must be rejected before the Nu parser runs (#18597).
#[test]
fn run_binary_file_with_nul_errors_without_parsing() -> Result {
    Playground::setup(
        "run_binary_file_with_nul_errors_without_parsing",
        |dirs, _| {
            let path = dirs.test().join("binary.bin");
            let mut file = std::fs::File::create(&path).expect("create binary.bin");
            file.write_all(b"not\0a\0script")
                .expect("write binary content");

            let err = test()
                .cwd(dirs.test())
                .run("run binary.bin")
                .expect_parse_error()?;
            assert!(
                matches!(err, ParseError::ScriptFileNotText { .. }),
                "expected ScriptFileNotText, got: {err:?}"
            );
            Ok(())
        },
    )
}

/// Invalid UTF-8 (no NULs) must also be rejected as non-text for `run`.
#[test]
fn run_invalid_utf8_file_errors_without_parsing() -> Result {
    Playground::setup("run_invalid_utf8_file_errors_without_parsing", |dirs, _| {
        let path = dirs.test().join("bad_utf8.bin");
        // Lone continuation bytes: invalid UTF-8, no NULs.
        std::fs::write(&path, [0x80, 0x81, 0x82, 0x83, 0xFF]).expect("write invalid utf-8");

        let err = test()
            .cwd(dirs.test())
            .run("run bad_utf8.bin")
            .expect_parse_error()?;
        assert!(
            matches!(err, ParseError::ScriptFileNotText { .. }),
            "expected ScriptFileNotText, got: {err:?}"
        );
        Ok(())
    })
}

/// Dense C0 control characters (no NULs, valid UTF-8 bytes) look like binary to `run`.
#[test]
fn run_control_heavy_file_errors_without_parsing() -> Result {
    Playground::setup(
        "run_control_heavy_file_errors_without_parsing",
        |dirs, _| {
            let path = dirs.test().join("controls.bin");
            // Mostly BEL/SOH-style controls; still valid UTF-8 single bytes, no NULs.
            let mut bytes = vec![0x01u8; 100];
            bytes.extend_from_slice(b"\n");
            std::fs::write(&path, bytes).expect("write control-heavy file");

            let err = test()
                .cwd(dirs.test())
                .run("run controls.bin")
                .expect_parse_error()?;
            assert!(
                matches!(err, ParseError::ScriptFileNotText { .. }),
                "expected ScriptFileNotText, got: {err:?}"
            );
            Ok(())
        },
    )
}

/// `--full-reparse` skips parse-time load, so oversized files must still be rejected at runtime.
#[test]
fn run_full_reparse_oversized_file_errors() -> Result {
    Playground::setup("run_full_reparse_oversized_file_errors", |dirs, _| {
        let path = dirs.test().join("huge.bin");
        let file = std::fs::File::create(&path).expect("create huge.bin");
        file.set_len(MAX_RUN_SCRIPT_BYTES + 1)
            .expect("set oversized length");

        let err = test()
            .cwd(dirs.test())
            .run("run --full-reparse huge.bin")
            .expect_shell_error()?;
        assert_contains("too large", err.to_string());
        Ok(())
    })
}