mds-cli 0.1.0

MDS (Markdown Script) compiler — command-line interface
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
mod common;
use common::{fixture, mds_bin};

#[test]
fn build_to_file() {
    let dir = tempfile::tempdir().unwrap();
    let output_path = dir.path().join("output.md");

    let output = mds_bin()
        .args([
            "build",
            fixture("simple.mds").to_str().unwrap(),
            "-o",
            output_path.to_str().unwrap(),
        ])
        .output()
        .unwrap();

    assert!(output.status.success(), "build to file should succeed");
    let content = std::fs::read_to_string(&output_path).unwrap();
    assert!(
        content.contains("Hello Alice!"),
        "output file should contain compiled content, got: {content}"
    );
}

#[test]
fn build_from_stdin() {
    let mut child = mds_bin()
        .args(["build", "-"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();

    use std::io::Write;
    child
        .stdin
        .take()
        .unwrap()
        .write_all(b"---\nname: World\n---\nHello {name}!\n")
        .unwrap();

    let output = child.wait_with_output().unwrap();
    assert!(output.status.success(), "build from stdin should succeed");
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("Hello World!"),
        "stdin build should produce 'Hello World!', got: {stdout}"
    );
}

#[test]
fn build_with_vars_file() {
    let dir = tempfile::tempdir().unwrap();
    let vars_path = dir.path().join("vars.json");
    std::fs::write(&vars_path, r#"{"name": "Overridden"}"#).unwrap();

    let output = mds_bin()
        .args([
            "build",
            fixture("simple.mds").to_str().unwrap(),
            "-o",
            "-",
            "--vars",
            vars_path.to_str().unwrap(),
        ])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "build with vars file should succeed"
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("Hello Overridden!"),
        "vars file should override frontmatter 'name', got: {stdout}"
    );
}

#[test]
fn build_invalid_input_exits_nonzero() {
    let output = mds_bin()
        .args(["build", "nonexistent_file_that_does_not_exist.mds"])
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "build with nonexistent file should exit non-zero"
    );
}

#[test]
fn build_quiet_flag() {
    let dir = tempfile::tempdir().unwrap();
    let output_path = dir.path().join("output.md");

    let output = mds_bin()
        .args([
            "build",
            fixture("simple.mds").to_str().unwrap(),
            "-o",
            output_path.to_str().unwrap(),
            "-q",
        ])
        .output()
        .unwrap();

    assert!(output.status.success(), "quiet build should succeed");
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.is_empty(),
        "quiet flag should produce no stderr output, got: {stderr}"
    );
}

#[test]
fn build_auto_detects_single_mds_file_in_directory() {
    // Create a temp directory with exactly one .mds file.
    // The default output is <name>.md next to the source, so auto.md should be created.
    let dir = tempfile::tempdir().expect("create temp dir");
    let mds_path = dir.path().join("auto.mds");
    std::fs::write(&mds_path, "---\nname: World\n---\nHello {name}!\n").expect("write fixture");

    let output = mds_bin()
        .current_dir(dir.path())
        .arg("build")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .expect("run mds build");

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        output.status.success(),
        "auto-detect should succeed with one .mds file; stderr: {stderr}"
    );
    // Default output: auto.md written next to auto.mds
    let md_path = dir.path().join("auto.md");
    assert!(
        md_path.exists(),
        "auto.md should be created next to auto.mds; stderr: {stderr}"
    );
    let content = std::fs::read_to_string(&md_path).unwrap();
    assert!(
        content.contains("Hello World!"),
        "auto-detect output file should contain 'Hello World!', got: {content}"
    );
}

#[test]
fn build_errors_when_no_mds_files_in_directory() {
    let dir = tempfile::tempdir().expect("create temp dir");

    let output = mds_bin()
        .current_dir(dir.path())
        .arg("build")
        .output()
        .expect("run mds build");

    assert!(
        !output.status.success(),
        "build with no .mds files should fail"
    );
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.contains("no .mds files") || stderr.contains("No .mds files"),
        "error should mention missing .mds files, got: {stderr}"
    );
}

#[test]
fn build_errors_when_multiple_mds_files_in_directory() {
    let dir = tempfile::tempdir().expect("create temp dir");
    std::fs::write(dir.path().join("a.mds"), "---\n---\nhello\n").expect("write a.mds");
    std::fs::write(dir.path().join("b.mds"), "---\n---\nworld\n").expect("write b.mds");

    let output = mds_bin()
        .current_dir(dir.path())
        .arg("build")
        .output()
        .expect("run mds build");

    assert!(
        !output.status.success(),
        "build with multiple .mds files should fail"
    );
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.contains("multiple") || stderr.contains("Multiple"),
        "error should mention multiple .mds files, got: {stderr}"
    );
}

#[test]
fn build_auto_detect_writes_file() {
    // Auto-detect + default file output: auto.mds → auto.md.
    let dir = tempfile::tempdir().unwrap();
    let mds_path = dir.path().join("auto.mds");
    std::fs::write(&mds_path, "---\nname: World\n---\nHello {name}!\n").unwrap();

    let output = mds_bin()
        .current_dir(dir.path())
        .arg("build")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        output.status.success(),
        "auto-detect build should succeed; stderr: {stderr}"
    );
    let md_path = dir.path().join("auto.md");
    assert!(
        md_path.exists(),
        "auto.md should be written next to auto.mds"
    );
    let content = std::fs::read_to_string(&md_path).unwrap();
    assert!(
        content.contains("Hello World!"),
        "auto-detect default output should contain 'Hello World!', got: {content}"
    );
}

#[test]
fn build_default_writes_file_next_to_source() {
    // With no -o or --out-dir, `mds build foo.mds` writes `foo.md` next to the source.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("hello.mds");
    std::fs::write(&src, "---\nname: World\n---\nHello {name}!\n").unwrap();

    let output = mds_bin()
        .arg("build")
        .arg(&src)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        output.status.success(),
        "default build should succeed; stderr: {stderr}"
    );

    let md_path = dir.path().join("hello.md");
    assert!(
        md_path.exists(),
        "hello.md should be written next to hello.mds"
    );
    let content = std::fs::read_to_string(&md_path).unwrap();
    assert!(
        content.contains("Hello World!"),
        "output file should contain compiled content, got: {content}"
    );
    // stdout should be empty (output went to file)
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.is_empty(),
        "stdout should be empty when writing to file, got: {stdout}"
    );
    // stderr should mention the output path
    assert!(
        stderr.contains("hello.md"),
        "stderr should mention the output file, got: {stderr}"
    );
}

#[test]
fn build_dash_o_dash_writes_to_stdout() {
    // `-o -` forces stdout regardless of the default file-output behavior.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("hello.mds");
    std::fs::write(&src, "---\nname: World\n---\nHello {name}!\n").unwrap();

    let output = mds_bin()
        .args(["build", src.to_str().unwrap(), "-o", "-"])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "build -o - should succeed; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("Hello World!"),
        "-o - should produce output on stdout, got: {stdout}"
    );
    // No .md file should be written
    assert!(
        !dir.path().join("hello.md").exists(),
        "no hello.md should be written when -o - is used"
    );
}

#[test]
fn build_stdin_defaults_to_stdout() {
    // `mds build -` (stdin) with no -o writes to stdout.
    let mut child = mds_bin()
        .args(["build", "-"])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();

    use std::io::Write;
    child
        .stdin
        .take()
        .unwrap()
        .write_all(b"---\nname: World\n---\nHello {name}!\n")
        .unwrap();

    let output = child.wait_with_output().unwrap();
    assert!(output.status.success(), "stdin build should succeed");
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.contains("Hello World!"),
        "stdin input with no -o should default to stdout, got: {stdout}"
    );
}

#[test]
fn build_stdin_with_output_writes_file() {
    // `echo "..." | mds build - -o out.md` writes to a file.
    let dir = tempfile::tempdir().unwrap();
    let out_path = dir.path().join("out.md");

    let mut child = mds_bin()
        .args(["build", "-", "-o", out_path.to_str().unwrap()])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();

    use std::io::Write;
    child
        .stdin
        .take()
        .unwrap()
        .write_all(b"---\nname: World\n---\nHello {name}!\n")
        .unwrap();

    let output = child.wait_with_output().unwrap();
    assert!(
        output.status.success(),
        "stdin build with -o should succeed"
    );
    assert!(out_path.exists(), "output file should be created");
    let content = std::fs::read_to_string(&out_path).unwrap();
    assert!(
        content.contains("Hello World!"),
        "output file should contain compiled content, got: {content}"
    );
    // stdout should be empty
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.is_empty(),
        "stdout should be empty when -o writes to file, got: {stdout}"
    );
}

#[test]
fn build_stdin_with_out_dir_writes_to_directory() {
    // `echo "..." | mds build - --out-dir dist` should write `dist/output.md`.
    let dir = tempfile::tempdir().unwrap();
    let dist = dir.path().join("dist");

    let mut child = mds_bin()
        .args(["build", "-", "--out-dir", dist.to_str().unwrap()])
        .stdin(std::process::Stdio::piped())
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .spawn()
        .unwrap();

    use std::io::Write;
    child
        .stdin
        .take()
        .unwrap()
        .write_all(b"---\nname: World\n---\nHello {name}!\n")
        .unwrap();

    let output = child.wait_with_output().unwrap();
    assert!(
        output.status.success(),
        "stdin + --out-dir should succeed; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let md_path = dist.join("output.md");
    assert!(
        md_path.exists(),
        "output.md should be written inside dist/ for stdin + --out-dir"
    );
    let content = std::fs::read_to_string(&md_path).unwrap();
    assert!(
        content.contains("Hello World!"),
        "output file should contain compiled content, got: {content}"
    );
    // stdout should be empty (output went to file)
    let stdout = String::from_utf8(output.stdout).unwrap();
    assert!(
        stdout.is_empty(),
        "stdout should be empty when --out-dir writes to file, got: {stdout}"
    );
}

#[test]
fn build_out_dir_writes_to_directory() {
    // `--out-dir dist` writes `dist/foo.md`.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("foo.mds");
    std::fs::write(&src, "---\nname: World\n---\nHello {name}!\n").unwrap();

    let dist = dir.path().join("dist");
    // dist does not exist yet — should be created.

    let output = mds_bin()
        .args([
            "build",
            src.to_str().unwrap(),
            "--out-dir",
            dist.to_str().unwrap(),
        ])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "build --out-dir should succeed; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let md_path = dist.join("foo.md");
    assert!(md_path.exists(), "foo.md should be written inside dist/");
    let content = std::fs::read_to_string(&md_path).unwrap();
    assert!(
        content.contains("Hello World!"),
        "out-dir output should contain compiled content, got: {content}"
    );
}

#[test]
fn build_out_dir_creates_directory() {
    // `--out-dir` auto-creates the directory (including nested paths).
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("template.mds");
    std::fs::write(&src, "Hello!\n").unwrap();

    let nested = dir.path().join("a").join("b").join("c");

    let output = mds_bin()
        .args([
            "build",
            src.to_str().unwrap(),
            "--out-dir",
            nested.to_str().unwrap(),
        ])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "build --out-dir with nested path should succeed; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        nested.join("template.md").exists(),
        "template.md should be written inside nested directory"
    );
}

#[test]
fn build_o_and_out_dir_mutually_exclusive() {
    // `-o` and `--out-dir` cannot be used together.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("foo.mds");
    std::fs::write(&src, "Hello!\n").unwrap();

    let output = mds_bin()
        .args([
            "build",
            src.to_str().unwrap(),
            "-o",
            "out.md",
            "--out-dir",
            "dist",
        ])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "-o and --out-dir should be mutually exclusive"
    );
}

#[test]
fn build_mds_json_output_dir() {
    // mds.json with `build.output_dir` controls where the output goes.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("hello.mds");
    std::fs::write(&src, "---\nname: World\n---\nHello {name}!\n").unwrap();
    std::fs::write(
        dir.path().join("mds.json"),
        r#"{"build": {"output_dir": "out"}}"#,
    )
    .unwrap();

    let output = mds_bin()
        .arg("build")
        .arg(&src)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "build with mds.json output_dir should succeed; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    let md_path = dir.path().join("out").join("hello.md");
    assert!(md_path.exists(), "hello.md should be in out/ per mds.json");
    let content = std::fs::read_to_string(&md_path).unwrap();
    assert!(
        content.contains("Hello World!"),
        "mds.json output should contain compiled content, got: {content}"
    );
}

#[test]
fn build_mds_json_creates_output_dir() {
    // mds.json output_dir is auto-created when it doesn't exist.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("tpl.mds");
    std::fs::write(&src, "Hello!\n").unwrap();
    std::fs::write(
        dir.path().join("mds.json"),
        r#"{"build": {"output_dir": "generated/docs"}}"#,
    )
    .unwrap();

    let output = mds_bin()
        .arg("build")
        .arg(&src)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "build with nested output_dir in mds.json should succeed; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        dir.path()
            .join("generated")
            .join("docs")
            .join("tpl.md")
            .exists(),
        "nested output_dir should be auto-created"
    );
}

#[test]
fn build_out_dir_overrides_mds_json() {
    // `--out-dir` takes precedence over mds.json output_dir.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("hello.mds");
    std::fs::write(&src, "Hello!\n").unwrap();
    std::fs::write(
        dir.path().join("mds.json"),
        r#"{"build": {"output_dir": "config_out"}}"#,
    )
    .unwrap();

    let cli_out = dir.path().join("cli_out");

    let output = mds_bin()
        .args([
            "build",
            src.to_str().unwrap(),
            "--out-dir",
            cli_out.to_str().unwrap(),
        ])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "--out-dir should override mds.json; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    // --out-dir wins: output in cli_out/, not config_out/
    assert!(
        cli_out.join("hello.md").exists(),
        "hello.md should be in cli_out/ (--out-dir wins over mds.json)"
    );
    assert!(
        !dir.path().join("config_out").join("hello.md").exists(),
        "config_out/ should not have hello.md when --out-dir overrides"
    );
}

#[test]
fn build_dash_o_overrides_mds_json() {
    // `-o <path>` takes precedence over mds.json output_dir.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("hello.mds");
    std::fs::write(&src, "Hello!\n").unwrap();
    std::fs::write(
        dir.path().join("mds.json"),
        r#"{"build": {"output_dir": "config_out"}}"#,
    )
    .unwrap();

    let explicit_out = dir.path().join("explicit.md");

    let output = mds_bin()
        .args([
            "build",
            src.to_str().unwrap(),
            "-o",
            explicit_out.to_str().unwrap(),
        ])
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "-o should override mds.json; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    assert!(
        explicit_out.exists(),
        "explicit.md should be created by -o flag"
    );
    assert!(
        !dir.path().join("config_out").exists(),
        "config_out/ should not be created when -o overrides"
    );
}

#[test]
fn build_invalid_mds_json_errors() {
    // Invalid JSON in mds.json should produce a hard error.
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("hello.mds");
    std::fs::write(&src, "Hello!\n").unwrap();
    std::fs::write(dir.path().join("mds.json"), "{ invalid json }").unwrap();

    let output = mds_bin()
        .arg("build")
        .arg(&src)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "invalid mds.json should cause a build error"
    );
    let stderr = String::from_utf8(output.stderr).unwrap();
    assert!(
        stderr.contains("mds.json") || stderr.contains("invalid"),
        "error should mention mds.json or 'invalid', got: {stderr}"
    );
}

#[test]
fn build_empty_mds_json_uses_defaults() {
    // `{}` in mds.json is valid and falls back to default behavior (file next to source).
    let dir = tempfile::tempdir().unwrap();
    let src = dir.path().join("hello.mds");
    std::fs::write(&src, "Hello!\n").unwrap();
    std::fs::write(dir.path().join("mds.json"), "{}").unwrap();

    let output = mds_bin()
        .arg("build")
        .arg(&src)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "empty mds.json should not cause errors; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    // Default: hello.md next to hello.mds
    assert!(
        dir.path().join("hello.md").exists(),
        "default output should be hello.md next to source"
    );
}

#[test]
fn build_mds_json_discovery_walks_up() {
    // mds.json found in a parent directory is used.
    let dir = tempfile::tempdir().unwrap();
    let subdir = dir.path().join("sub");
    std::fs::create_dir(&subdir).unwrap();

    let src = subdir.join("hello.mds");
    std::fs::write(&src, "Hello!\n").unwrap();
    // mds.json is in the parent directory
    std::fs::write(
        dir.path().join("mds.json"),
        r#"{"build": {"output_dir": "out"}}"#,
    )
    .unwrap();

    let output = mds_bin()
        .arg("build")
        .arg(&src)
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped())
        .output()
        .unwrap();

    assert!(
        output.status.success(),
        "mds.json in parent dir should be discovered; stderr: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    // output_dir is resolved relative to mds.json location (parent dir)
    let md_path = dir.path().join("out").join("hello.md");
    assert!(
        md_path.exists(),
        "hello.md should be in <mds.json-dir>/out/, got path: {}",
        md_path.display()
    );
}