dogelang 0.3.2

The Doge programming language — Python's ease, Rust's engine. Installs the `doge` CLI.
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
//! End-to-end tests for the `doge` binary: they spawn the real compiled
//! executable (via `CARGO_BIN_EXE_doge`) the way a user would.

use std::path::PathBuf;
use std::process::Command;

fn doge() -> Command {
    Command::new(env!("CARGO_BIN_EXE_doge"))
}

/// A `doge` invocation that caches under `CARGO_TARGET_TMPDIR`, out of the real
/// user cache. Shared with `run_examples.rs`, so the runtime compiles once.
fn doge_cached() -> Command {
    let mut cmd = doge();
    cmd.env("DOGE_CACHE_DIR", cache_dir());
    cmd
}

fn cache_dir() -> PathBuf {
    PathBuf::from(concat!(env!("CARGO_TARGET_TMPDIR"), "/doge-cache"))
}

fn examples_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("..")
        .join("examples")
}

/// The compiler's check fixtures, reused for the `check` diagnostic test.
fn fixtures_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("doge-compiler")
        .join("tests")
        .join("fixtures")
}

/// This crate's own test fixtures (the runtime-error script).
fn cli_fixtures_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("tests")
        .join("fixtures")
}

fn module_fixtures_dir() -> PathBuf {
    cli_fixtures_dir().join("modules")
}

#[test]
fn check_on_a_good_program_dumps_the_ast() {
    let hello = examples_dir().join("hello.doge");
    let output = doge()
        .arg("check")
        .arg(&hello)
        .output()
        .expect("the doge binary should run");

    assert!(output.status.success(), "expected exit 0");
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        stdout.starts_with("Script"),
        "dump should start with Script, got:\n{stdout}"
    );
}

#[test]
fn check_on_a_bad_program_prints_a_diagnostic_to_stderr() {
    let bad = fixtures_dir().join("missing_wow.doge");
    let output = doge()
        .arg("check")
        .arg(&bad)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "expected exit 1");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very"),
        "diagnostic should be doge-flavored, got:\n{stderr}"
    );
    assert!(output.stdout.is_empty(), "no AST dump on failure");
}

#[test]
fn an_unknown_command_prints_usage_and_exits_two() {
    // A bare `doge` now starts the REPL, so usage is reported for an unrecognized
    // subcommand instead.
    let output = doge()
        .arg("frobnicate")
        .arg("x.doge")
        .output()
        .expect("the doge binary should run");
    assert_eq!(output.status.code(), Some(2), "expected exit 2");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("such usage"),
        "should print usage, got:\n{stderr}"
    );
}

#[test]
fn bark_runs_and_caches() {
    let hello = examples_dir().join("hello.doge");
    let expected = std::fs::read_to_string(examples_dir().join("hello.out")).expect("hello.out");

    let first = doge_cached()
        .arg("bark")
        .arg(&hello)
        .output()
        .expect("the doge binary should run");
    assert!(first.status.success(), "first run should exit 0");
    assert_eq!(String::from_utf8_lossy(&first.stdout), expected);

    // After the first build, the script's cache entry must exist on disk.
    let scripts = cache_dir().join("scripts");
    assert!(
        scripts.exists() && scripts.read_dir().expect("scripts dir").next().is_some(),
        "a cache entry should have been written under {}",
        scripts.display()
    );

    // The second run is a cache hit and must produce identical output.
    let second = doge_cached()
        .arg("bark")
        .arg(&hello)
        .output()
        .expect("the doge binary should run");
    assert!(second.status.success(), "second run should exit 0");
    assert_eq!(String::from_utf8_lossy(&second.stdout), expected);
}

#[test]
fn runtime_error_reports_path_line_and_source() {
    let fixture = cli_fixtures_dir().join("divide_by_zero.doge");
    let source_line = std::fs::read_to_string(&fixture)
        .expect("divide_by_zero.doge")
        .lines()
        .nth(2)
        .expect("line 3")
        .to_string();

    let output = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "a runtime error exits 1");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very error. much broken."),
        "should be doge-flavored, got:\n{stderr}"
    );
    assert!(
        stderr.contains("divide_by_zero.doge:3"),
        "should carry the script path and line, got:\n{stderr}"
    );
    assert!(
        stderr.contains("by zero"),
        "should explain the division error, got:\n{stderr}"
    );
    assert!(
        stderr.contains(&source_line),
        "should embed the offending line-3 source ({source_line:?}), got:\n{stderr}"
    );
}

#[test]
fn caught_error_exposes_type_file_and_line() {
    let fixture = cli_fixtures_dir().join("caught_error_fields.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert!(
        output.status.success(),
        "a caught error runs cleanly, exit={:?}",
        output.status.code()
    );
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        stdout.contains("IndexOutOfBounds"),
        "err.type names the category, got:\n{stdout}"
    );
    assert!(
        stdout.contains("caught_error_fields.doge"),
        "err.file carries the script path, got:\n{stdout}"
    );
    assert!(
        stdout.lines().any(|line| line == "3"),
        "err.line is the raise line (3), got:\n{stdout}"
    );
}

#[test]
fn bark_prints_a_function_value() {
    // func_value.doge uses a bare function name as a value — now a first-class
    // function that `bark` prints as `<function name>`.
    let fixture = cli_fixtures_dir().join("func_value.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert!(output.status.success(), "a function value runs cleanly");
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert_eq!(stdout, "<function shout>\n");
}

#[test]
fn bark_prints_a_class_value_and_it_constructs() {
    // class_value.doge uses a bare class name as a value — a callable that `bark`
    // prints as `<class Name>` and that builds an instance when called.
    let fixture = cli_fixtures_dir().join("class_value.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert!(output.status.success(), "a class value runs cleanly");
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert_eq!(stdout, "<class Shibe>\nbork\n");
}

#[test]
fn bark_forwards_command_line_arguments_to_the_script() {
    // Everything after the script path reaches the program through `env.args()`.
    let fixture = cli_fixtures_dir().join("args_echo.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .arg("alpha")
        .arg("beta")
        .output()
        .expect("the doge binary should run");

    assert!(
        output.status.success(),
        "a script reading args runs cleanly, stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert_eq!(stdout, "alpha\nbeta\n", "env.args() should echo both args");
}

#[test]
fn gib_reads_a_line_of_input_and_none_at_end() {
    use std::io::Write;
    use std::process::Stdio;

    // Two lines then EOF: the first two gib calls read them, the third is `none`.
    let fixture = cli_fixtures_dir().join("gib_echo.doge");
    let mut child = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("the doge binary should start");
    child
        .stdin
        .take()
        .expect("piped stdin")
        .write_all(b"kabosu\ndoge\n")
        .expect("write the input");
    let output = child.wait_with_output().expect("the script should finish");

    assert!(
        output.status.success(),
        "a script reading input runs cleanly, stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    // The prompt prints inline before the first bark; EOF yields `none`.
    assert_eq!(stdout, "name? much hello kabosu\ndoge\nnone\n");
}

#[test]
fn uncaught_bonk_reports_path_and_line() {
    let fixture = cli_fixtures_dir().join("bonk.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "an uncaught bonk exits 1");
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert_eq!(stdout, "before\n", "the bark before the bonk still runs");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very error. much broken."),
        "should be doge-flavored, got:\n{stderr}"
    );
    assert!(
        stderr.contains("bonk.doge:2"),
        "should carry the script path and line, got:\n{stderr}"
    );
    assert!(
        stderr.contains("such bad"),
        "should show the bonked message, got:\n{stderr}"
    );
}

#[test]
fn uncaught_amaze_reports_path_line_and_message() {
    let fixture = cli_fixtures_dir().join("amaze_fail.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "a failed amaze exits 1");
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert_eq!(stdout, "before\n", "the bark before the amaze still runs");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very error. much broken."),
        "should be doge-flavored, got:\n{stderr}"
    );
    assert!(
        stderr.contains("amaze_fail.doge:3"),
        "should carry the script path and line, got:\n{stderr}"
    );
    assert!(
        stderr.contains("age much wrong"),
        "should show the amaze message, got:\n{stderr}"
    );
}

#[test]
fn recursion_limit_is_a_catchable_doge_error() {
    let fixture = cli_fixtures_dir().join("deep_recursion.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "runaway recursion exits 1");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very error. much broken."),
        "should be doge-flavored, got:\n{stderr}"
    );
    assert!(
        stderr.contains("too much recursion"),
        "should explain the recursion limit, got:\n{stderr}"
    );
    // The Rust stack never overflows — the user sees no rustc/abort noise.
    assert!(
        !stderr.contains("stack overflow") && !stderr.contains("panicked"),
        "no Rust abort should leak, got:\n{stderr}"
    );
}

#[test]
fn build_produces_standalone_binary() {
    // Its own fixture, not hello.doge: the cache is keyed by source, and another
    // test builds hello.doge concurrently — sharing a source would race two
    // parallel builds on the same cached binary.
    let script = cli_fixtures_dir()
        .join("standalone.doge")
        .canonicalize()
        .expect("standalone.doge should exist");
    let expected = "much standalone. very wow.\n";
    let workdir = PathBuf::from(env!("CARGO_TARGET_TMPDIR"));

    let build = doge_cached()
        .arg("build")
        .arg(&script)
        .current_dir(&workdir)
        .output()
        .expect("the doge binary should run");
    assert!(
        build.status.success(),
        "build should exit 0, stderr:\n{}",
        String::from_utf8_lossy(&build.stderr)
    );

    let dropped = workdir.join(format!("standalone{}", std::env::consts::EXE_SUFFIX));
    assert!(
        dropped.exists(),
        "build should drop ./standalone in the work dir"
    );

    // The dropped binary runs standalone, with no doge involvement.
    let run = Command::new(&dropped)
        .output()
        .expect("the built binary should run");
    assert!(run.status.success(), "the standalone binary should exit 0");
    assert_eq!(String::from_utf8_lossy(&run.stdout), expected);
}

#[test]
fn concurrent_builds_of_one_script_all_succeed() {
    // Several `doge` processes building the same script share one cache entry.
    // Without the build lock, one process's cargo relink races another's run
    // ("os error 2"); with it, one builds and the rest reuse the binary.
    let fixture = cli_fixtures_dir().join("concurrent.doge");
    let expected = "much concurrent. very safe.\n";

    let racers: Vec<_> = (0..4)
        .map(|_| {
            let fixture = fixture.clone();
            std::thread::spawn(move || {
                doge_cached()
                    .arg("bark")
                    .arg(&fixture)
                    .output()
                    .expect("the doge binary should run")
            })
        })
        .collect();

    for racer in racers {
        let output = racer.join().expect("a build thread should not panic");
        assert!(
            output.status.success(),
            "every concurrent build should exit 0, stderr:\n{}",
            String::from_utf8_lossy(&output.stderr)
        );
        assert_eq!(String::from_utf8_lossy(&output.stdout), expected);
    }
}

#[test]
fn bark_runs_a_program_with_imported_modules() {
    let entry = examples_dir().join("modules.doge");
    let expected =
        std::fs::read_to_string(examples_dir().join("modules.out")).expect("modules.out");

    let output = doge_cached()
        .arg("bark")
        .arg(&entry)
        .output()
        .expect("the doge binary should run");

    assert!(
        output.status.success(),
        "a multi-file program should run, stderr:\n{}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert_eq!(String::from_utf8_lossy(&output.stdout), expected);
}

#[test]
fn runtime_error_in_a_module_reports_that_module_and_line() {
    let entry = module_fixtures_dir().join("rterr_entry.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&entry)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "a runtime error exits 1");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    // The error surfaces the *module's* file and line, not the entry's.
    assert!(
        stderr.contains("rterr_lib.doge:2"),
        "should point into the module, got:\n{stderr}"
    );
    assert!(
        stderr.contains("by zero"),
        "should explain the division error, got:\n{stderr}"
    );
}

#[test]
fn importing_a_missing_module_is_a_doge_diagnostic() {
    let entry = module_fixtures_dir().join("missing_entry.doge");
    let output = doge()
        .arg("check")
        .arg(&entry)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "an unknown module exits 1");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very import. much unknown."),
        "should be doge-flavored, got:\n{stderr}"
    );
    assert!(
        stderr.contains("no module named nope"),
        "should name the missing module, got:\n{stderr}"
    );
}

#[test]
fn a_circular_import_is_a_doge_diagnostic() {
    let entry = module_fixtures_dir().join("cycle_entry.doge");
    let output = doge()
        .arg("check")
        .arg(&entry)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "a cycle exits 1");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very loop. much import."),
        "should be doge-flavored, got:\n{stderr}"
    );
    assert!(
        stderr.contains("import cycle:"),
        "should spell out the cycle, got:\n{stderr}"
    );
}

#[test]
fn a_loose_statement_in_a_module_is_a_doge_diagnostic() {
    let entry = module_fixtures_dir().join("loose_entry.doge");
    let output = doge()
        .arg("check")
        .arg(&entry)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "a loose statement exits 1");
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very loose. much module."),
        "should be doge-flavored, got:\n{stderr}"
    );
}

#[test]
fn an_object_defined_in_a_module_is_importable() {
    let entry = module_fixtures_dir().join("obj_entry.doge");
    let output = doge_cached()
        .arg("bark")
        .arg(&entry)
        .output()
        .expect("the doge binary should run");

    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        output.status.success(),
        "a module object should construct and dispatch, got:\n{stderr}"
    );
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert_eq!(stdout, "1\n", "utils.Shibe().woof() should print 1");
}

#[test]
fn new_scaffolds_a_project_that_runs() {
    // `doge new` creates a project; a bare `doge bark` inside it runs the entry.
    let workdir = PathBuf::from(concat!(env!("CARGO_TARGET_TMPDIR"), "/new-project"));
    let _ = std::fs::remove_dir_all(&workdir);
    std::fs::create_dir_all(&workdir).expect("scratch dir");

    let created = doge()
        .arg("new")
        .arg("demo")
        .current_dir(&workdir)
        .output()
        .expect("the doge binary should run");
    assert!(created.status.success(), "doge new should exit 0");
    let project = workdir.join("demo");
    assert!(project.join("doge.toml").is_file(), "a manifest is written");
    assert!(project.join("main.doge").is_file(), "an entry is written");

    let run = doge_cached()
        .arg("bark")
        .current_dir(&project)
        .output()
        .expect("the doge binary should run");
    assert!(
        run.status.success(),
        "the scaffolded project should run, stderr:\n{}",
        String::from_utf8_lossy(&run.stderr)
    );
    let stdout = String::from_utf8(run.stdout).expect("utf-8 stdout");
    assert!(
        stdout.contains("much hello from demo"),
        "the scaffold greets, got:\n{stdout}"
    );
}

#[test]
fn new_refuses_to_overwrite_an_existing_directory() {
    let workdir = PathBuf::from(concat!(env!("CARGO_TARGET_TMPDIR"), "/new-existing"));
    let _ = std::fs::remove_dir_all(&workdir);
    std::fs::create_dir_all(workdir.join("taken")).expect("scratch dir");

    let output = doge()
        .arg("new")
        .arg("taken")
        .current_dir(&workdir)
        .output()
        .expect("the doge binary should run");
    assert_eq!(output.status.code(), Some(1), "an occupied name exits 1");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("very exists. much occupied."),
        "should be doge-flavored, got:\n{stderr}"
    );
}

#[test]
fn new_rejects_a_path_traversal_name() {
    // A name with path separators must not create dirs outside the working dir.
    let workdir = PathBuf::from(concat!(env!("CARGO_TARGET_TMPDIR"), "/new-traversal"));
    let _ = std::fs::remove_dir_all(&workdir);
    std::fs::create_dir_all(&workdir).expect("scratch dir");

    let output = doge()
        .arg("new")
        .arg("../escaped")
        .current_dir(&workdir)
        .output()
        .expect("the doge binary should run");
    assert_eq!(output.status.code(), Some(1), "an invalid name exits 1");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("very name. much invalid."),
        "should be doge-flavored, got:\n{stderr}"
    );
    assert!(
        !workdir.parent().unwrap().join("escaped").exists(),
        "nothing may be created outside the working directory"
    );
}

/// Drive the interactive REPL by piping a scripted session into it and asserting
/// on the echoed values and printed output.
fn repl_session(input: &str) -> String {
    use std::io::Write;
    use std::process::Stdio;

    let mut child = doge()
        .arg("repl")
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .spawn()
        .expect("the doge binary should start");
    child
        .stdin
        .take()
        .expect("piped stdin")
        .write_all(input.as_bytes())
        .expect("write the session");
    let output = child.wait_with_output().expect("the repl should finish");
    String::from_utf8(output.stdout).expect("utf-8 stdout")
}

#[test]
fn repl_runs_a_session_and_persists_bindings() {
    // Declare a variable, use it, redefine it, and echo a trailing expression —
    // across separate lines, so the session must persist state between them.
    let stdout = repl_session("such x = 20\nbark x + 1\nsuch x = 100\nx * 2\nwow\n");
    assert!(
        stdout.contains("21"),
        "bark should print 21, got:\n{stdout}"
    );
    assert!(
        stdout.contains("200"),
        "the trailing expression should echo 200 after redefinition, got:\n{stdout}"
    );
}

#[test]
fn repl_recovers_after_an_error() {
    // An unknown name reports an error, then the session keeps going.
    let stdout = repl_session("bark nope\nbark 7\nwow\n");
    assert!(
        stdout.contains("7"),
        "the session should continue after an error, got:\n{stdout}"
    );
}

#[test]
fn a_bare_doge_starts_the_repl() {
    use std::io::Write;
    use std::process::Stdio;

    let mut child = doge()
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()
        .expect("the doge binary should start");
    child
        .stdin
        .take()
        .expect("piped stdin")
        .write_all(b"bark 42\nwow\n")
        .expect("write the session");
    let output = child.wait_with_output().expect("the repl should finish");
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        stdout.contains("42"),
        "a bare `doge` should start the repl, got:\n{stdout}"
    );
}

/// A scratch file under the target tmp dir, seeded with `content`. `doge fmt`
/// rewrites in place, so tests must point it at throwaway files, never at a
/// checked-in fixture.
fn scratch_file(name: &str, content: &str) -> PathBuf {
    let path = PathBuf::from(concat!(env!("CARGO_TARGET_TMPDIR"), "/fmt")).join(name);
    std::fs::create_dir_all(path.parent().expect("scratch parent")).expect("mkdir scratch");
    std::fs::write(&path, content).expect("seed scratch file");
    path
}

const UNFORMATTED: &str = "such xs=[1,2 , 3]\nbark  xs[ 0 ]\nwow\n";
const FORMATTED: &str = "such xs = [1, 2, 3]\nbark xs[0]\nwow\n";

#[test]
fn fmt_rewrites_a_file_in_place() {
    let path = scratch_file("rewrite.doge", UNFORMATTED);
    let output = doge()
        .arg("fmt")
        .arg(&path)
        .output()
        .expect("doge should run");
    assert!(output.status.success(), "fmt should exit 0");
    assert_eq!(
        std::fs::read_to_string(&path).expect("read back"),
        FORMATTED
    );
}

#[test]
fn fmt_check_fails_on_unformatted_and_leaves_the_file() {
    let path = scratch_file("check_bad.doge", UNFORMATTED);
    let output = doge()
        .arg("fmt")
        .arg("--check")
        .arg(&path)
        .output()
        .expect("doge should run");
    assert_eq!(output.status.code(), Some(1), "expected exit 1");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(stderr.contains("very"), "doge-flavored, got:\n{stderr}");
    assert_eq!(
        std::fs::read_to_string(&path).expect("read back"),
        UNFORMATTED,
        "--check must not write"
    );
}

#[test]
fn fmt_check_passes_on_a_formatted_file() {
    let path = scratch_file("check_good.doge", FORMATTED);
    let output = doge()
        .arg("fmt")
        .arg("--check")
        .arg(&path)
        .output()
        .expect("doge should run");
    assert!(output.status.success(), "already-formatted should exit 0");
}

#[test]
fn fmt_on_unparseable_source_reports_a_diagnostic() {
    let path = scratch_file("broken.doge", "such =\nwow\n");
    let output = doge()
        .arg("fmt")
        .arg(&path)
        .output()
        .expect("doge should run");
    assert_eq!(output.status.code(), Some(1), "expected exit 1");
    let stderr = String::from_utf8_lossy(&output.stderr);
    assert!(
        stderr.contains("very"),
        "doge-flavored diagnostic, got:\n{stderr}"
    );
}

#[test]
fn test_runs_a_file_and_ignores_non_test_functions() {
    let fixture = cli_fixtures_dir().join("tests_pass.doge");
    let output = doge()
        .arg("test")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert!(output.status.success(), "all tests pass, so exit 0");
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        stdout.contains("✓ test_addition") && stdout.contains("✓ test_greeting"),
        "should report each test as passing, got:\n{stdout}"
    );
    assert!(
        !stdout.contains("greet") || stdout.contains("test_greeting"),
        "the `greet` helper is not a test and must not run, got:\n{stdout}"
    );
    assert!(
        stdout.contains("2 passed"),
        "should aggregate a pass count, got:\n{stdout}"
    );
}

#[test]
fn test_initializes_top_level_constants_before_running_tests() {
    let fixture = cli_fixtures_dir().join("tests_const.doge");
    let output = doge()
        .arg("test")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert!(
        output.status.success(),
        "the top-level `so` constant is initialized, so the test passes, got:\n{}",
        String::from_utf8_lossy(&output.stdout)
    );
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        stdout.contains("✓ test_uses_top_level_const"),
        "the const-using test should pass, got:\n{stdout}"
    );
}

#[test]
fn test_reports_a_failing_assertion_with_location() {
    let fixture = cli_fixtures_dir().join("tests_fail.doge");
    let output = doge()
        .arg("test")
        .arg(&fixture)
        .output()
        .expect("the doge binary should run");

    assert_eq!(output.status.code(), Some(1), "a failing test exits 1");
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        stdout.contains("✓ test_ok"),
        "the passing test still runs, got:\n{stdout}"
    );
    assert!(
        stdout.contains("✗ test_broken") && stdout.contains("one is not two"),
        "should name the failing test and its message, got:\n{stdout}"
    );
    assert!(
        stdout.contains("tests_fail.doge:6"),
        "should carry the failing assertion's location, got:\n{stdout}"
    );
    assert!(
        stdout.contains("1 passed, 1 failed"),
        "should aggregate pass/fail counts, got:\n{stdout}"
    );
}

#[test]
fn test_discovers_test_files_recursively_in_a_directory() {
    let dir = cli_fixtures_dir().join("test_suite");
    let output = doge()
        .arg("test")
        .arg(&dir)
        .output()
        .expect("the doge binary should run");

    assert!(
        output.status.success(),
        "every discovered test passes, exit 0"
    );
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        stdout.contains("✓ test_a") && stdout.contains("✓ test_b") && stdout.contains("✓ test_c"),
        "should discover test_*.doge at any depth, got:\n{stdout}"
    );
    assert!(
        !stdout.contains("test_should_be_ignored"),
        "helper.doge is not a test_*.doge file and must be skipped, got:\n{stdout}"
    );
    assert!(
        stdout.contains("3 passed"),
        "should aggregate across files, got:\n{stdout}"
    );
}

#[test]
fn test_resolves_a_projects_declared_dependency() {
    // A test file inside a project must see that project's dependencies, exactly
    // as `doge bark`/`build`/`check` do — not just sibling files.
    let workdir = PathBuf::from(concat!(env!("CARGO_TARGET_TMPDIR"), "/test-with-deps"));
    let _ = std::fs::remove_dir_all(&workdir);
    let greet = workdir.join("lib/greet");
    std::fs::create_dir_all(&greet).expect("scratch project dirs");

    std::fs::write(
        workdir.join("doge.toml"),
        "[package]\nname = \"app\"\n\n[dependencies]\ngreet = { path = \"lib/greet\" }\n",
    )
    .expect("write app manifest");
    std::fs::write(greet.join("doge.toml"), "[package]\nname = \"greet\"\n")
        .expect("write dep manifest");
    std::fs::write(
        greet.join("main.doge"),
        "such hello much who:\n    return \"hi \" + who\nwow\nwow\n",
    )
    .expect("write dep entry");
    std::fs::write(
        workdir.join("test_greet.doge"),
        "so greet\n\nsuch test_hello:\n    amaze greet.hello(\"x\") == \"hi x\"\nwow\nwow\n",
    )
    .expect("write the test file");

    let output = doge()
        .arg("test")
        .arg("test_greet.doge")
        .current_dir(&workdir)
        .output()
        .expect("the doge binary should run");

    assert!(
        output.status.success(),
        "a test importing a declared dependency should pass, stderr:\n{}\nstdout:\n{}",
        String::from_utf8_lossy(&output.stderr),
        String::from_utf8_lossy(&output.stdout),
    );
    let stdout = String::from_utf8(output.stdout).expect("utf-8 stdout");
    assert!(
        stdout.contains("✓ test_hello") && stdout.contains("1 passed"),
        "should run the dependency-importing test, got:\n{stdout}"
    );
}

#[test]
fn test_with_no_test_functions_reports_an_empty_suite() {
    let hello = examples_dir().join("hello.doge");
    let output = doge()
        .arg("test")
        .arg(&hello)
        .output()
        .expect("the doge binary should run");

    assert_eq!(
        output.status.code(),
        Some(1),
        "an empty suite exits non-zero"
    );
    let stderr = String::from_utf8(output.stderr).expect("utf-8 stderr");
    assert!(
        stderr.contains("very empty. much untested."),
        "should be a doge-flavored empty-suite message, got:\n{stderr}"
    );
}