rumdl 0.2.62

A fast Markdown linter written in Rust (Ru(st) MarkDown Linter)
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
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
/// Tests for the `extends` config inheritance feature.
///
/// Covers: basic inheritance, deep chains, child override, relative path resolution,
/// home directory paths, missing base file errors, circular reference errors, and chaining.
use rumdl_lib::config::SourcedConfig;
use rumdl_lib::config::types::ConfigError;
use std::fs;
use tempfile::tempdir;

/// Load a config file from a path and return the result.
fn load_config(path: &std::path::Path) -> Result<rumdl_lib::config::Config, ConfigError> {
    SourcedConfig::load_with_discovery(Some(path.to_str().unwrap()), None, false)
        .map(|s| s.into_validated_unchecked().into())
}

/// Basic inheritance: child overrides one field, inherits the rest from base.
#[test]
fn test_extends_basic_inheritance() {
    let dir = tempdir().unwrap();

    // Base config enables MD013 with line-length 80, and sets a flavor
    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[global]
disable = ["MD033"]

[MD013]
line-length = 80
"#,
    )
    .unwrap();

    // Child extends base and overrides line-length
    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        r#"extends = "base.rumdl.toml"

[MD013]
line-length = 120
"#,
    )
    .unwrap();

    let config = load_config(&child).unwrap();

    // Inherited from base
    assert!(
        config.global.disable.contains(&"MD033".to_string()),
        "Child should inherit disable list from base"
    );

    // Overridden in child
    let line_length = rumdl_lib::config::get_rule_config_value::<i64>(&config, "MD013", "line-length");
    assert_eq!(line_length, Some(120), "Child should override line-length from base");
}

/// Deep chain: A extends B extends C — all values from the full chain are present.
#[test]
fn test_extends_deep_chain() {
    let dir = tempdir().unwrap();

    // C is the root base
    let c = dir.path().join("c.rumdl.toml");
    fs::write(
        &c,
        r#"
[global]
disable = ["MD041"]
"#,
    )
    .unwrap();

    // B extends C and adds its own disable
    let b = dir.path().join("b.rumdl.toml");
    fs::write(
        &b,
        r#"extends = "c.rumdl.toml"

[global]
disable = ["MD033"]
"#,
    )
    .unwrap();

    // A extends B and adds another disable
    let a = dir.path().join("a.rumdl.toml");
    fs::write(
        &a,
        r#"extends = "b.rumdl.toml"

[global]
disable = ["MD013"]
"#,
    )
    .unwrap();

    let config = load_config(&a).unwrap();

    // A's disable wins (override semantics — child replaces parent for disable)
    // The final value should be the leaf node's (A's) disable list since disable uses replace semantics
    assert!(
        config.global.disable.contains(&"MD013".to_string()),
        "A's disable list should be applied"
    );
}

/// Child fully overrides: all values come from child, nothing leaks from base.
#[test]
fn test_extends_child_overrides_all() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[global]
disable = ["MD033"]
flavor = "mkdocs"
"#,
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        r#"extends = "base.rumdl.toml"

[global]
disable = ["MD013"]
flavor = "standard"
"#,
    )
    .unwrap();

    let config = load_config(&child).unwrap();

    // Child's disable replaces base's (replace semantics)
    assert!(
        config.global.disable.contains(&"MD013".to_string()),
        "Child's disable should be present"
    );
    // Base value is replaced (not merged)
    assert!(
        !config.global.disable.contains(&"MD033".to_string()),
        "Base's disable should be replaced, not merged"
    );

    // Child's flavor replaces base's
    use rumdl_lib::config::MarkdownFlavor;
    assert_eq!(
        config.global.flavor,
        MarkdownFlavor::Standard,
        "Child flavor should override base flavor"
    );
}

/// Relative path resolution: extends path is resolved relative to the config file's directory.
#[test]
fn test_extends_relative_path_resolution() {
    let dir = tempdir().unwrap();

    // Create a subdirectory for the child config
    let sub_dir = dir.path().join("subdir");
    fs::create_dir_all(&sub_dir).unwrap();

    // Base is in the parent directory
    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[global]
disable = ["MD001"]
"#,
    )
    .unwrap();

    // Child is in subdir and uses relative path "../base.rumdl.toml"
    let child = sub_dir.join("child.rumdl.toml");
    fs::write(
        &child,
        r#"extends = "../base.rumdl.toml"

[global]
disable = ["MD002"]
"#,
    )
    .unwrap();

    let config = load_config(&child).unwrap();

    // Child's disable list wins (override semantics)
    assert!(
        config.global.disable.contains(&"MD002".to_string()),
        "Child's disable should be applied"
    );
}

/// Absolute path resolution: extends with absolute path works.
#[test]
fn test_extends_absolute_path_resolution() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[global]
disable = ["MD041"]
"#,
    )
    .unwrap();

    // Use the absolute path directly in the child config. Written as a TOML
    // literal string (single quotes) so Windows backslashes in the absolute path
    // are not interpreted as TOML escape sequences.
    let base_absolute = base.canonicalize().unwrap();
    let child_content = format!(
        r#"extends = '{}'

[global]
disable = ["MD013"]
"#,
        base_absolute.display()
    );

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, &child_content).unwrap();

    let config = load_config(&child).unwrap();

    // The child config loaded successfully (absolute path works)
    assert!(
        config.global.disable.contains(&"MD013".to_string()),
        "Child's config should load with absolute path extends"
    );
}

/// Missing base file: clear error when extends target doesn't exist.
#[test]
fn test_extends_missing_base_file_gives_clear_error() {
    let dir = tempdir().unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        r#"extends = "nonexistent_base.rumdl.toml"

[global]
disable = ["MD013"]
"#,
    )
    .unwrap();

    let result = load_config(&child);

    assert!(result.is_err(), "Loading config with missing base should fail");

    match result.unwrap_err() {
        err @ ConfigError::ExtendsNotFound { .. } => {
            let message = err.to_string();
            assert!(
                message.contains("nonexistent_base.rumdl.toml"),
                "Error should mention the missing target, got: {message}"
            );
            assert!(
                message.contains("child.rumdl.toml"),
                "Error should mention the referencing file, got: {message}"
            );
        }
        other => panic!("Expected ExtendsNotFound error, got: {other:?}"),
    }
}

/// Circular extends: A extends B extends A → error with cycle info.
#[test]
fn test_extends_circular_reference_is_detected() {
    let dir = tempdir().unwrap();

    let a = dir.path().join("a.rumdl.toml");
    let b = dir.path().join("b.rumdl.toml");

    fs::write(&a, r#"extends = "b.rumdl.toml""#).unwrap();
    fs::write(&b, r#"extends = "a.rumdl.toml""#).unwrap();

    let result = load_config(&a);

    assert!(result.is_err(), "Circular extends should produce an error");

    match result.unwrap_err() {
        ConfigError::CircularExtends { path, .. } => {
            // The path in the error should reference one of the two files in the cycle
            assert!(
                path.contains("a.rumdl.toml") || path.contains("b.rumdl.toml"),
                "Error should mention a file in the cycle, got: {path}"
            );
        }
        other => panic!("Expected CircularExtends error, got: {other:?}"),
    }
}

/// Self-referential extends: a file that extends itself.
#[test]
fn test_extends_self_reference_is_detected() {
    let dir = tempdir().unwrap();

    let config = dir.path().join("self.rumdl.toml");
    fs::write(&config, r#"extends = "self.rumdl.toml""#).unwrap();

    let result = load_config(&config);
    assert!(result.is_err(), "Self-referential extends should produce an error");

    match result.unwrap_err() {
        ConfigError::CircularExtends { .. } => {} // expected
        other => panic!("Expected CircularExtends error, got: {other:?}"),
    }
}

/// Extends in base is itself respected (chaining).
/// A extends B, B extends C — A sees values from C.
#[test]
fn test_extends_chain_propagation() {
    let dir = tempdir().unwrap();

    // C has a rule config
    let c = dir.path().join("c.rumdl.toml");
    fs::write(
        &c,
        r#"
[MD007]
indent = 4
"#,
    )
    .unwrap();

    // B extends C, adds its own setting
    let b = dir.path().join("b.rumdl.toml");
    fs::write(
        &b,
        r#"extends = "c.rumdl.toml"

[MD003]
style = "atx"
"#,
    )
    .unwrap();

    // A extends B but doesn't override MD007 or MD003
    let a = dir.path().join("a.rumdl.toml");
    fs::write(&a, r#"extends = "b.rumdl.toml""#).unwrap();

    let config = load_config(&a).unwrap();

    // A inherits MD007.indent from C (via B)
    let indent = rumdl_lib::config::get_rule_config_value::<i64>(&config, "MD007", "indent");
    assert_eq!(indent, Some(4), "A should inherit MD007.indent from C via the chain");

    // A inherits MD003.style from B
    let style = rumdl_lib::config::get_rule_config_value::<String>(&config, "MD003", "style");
    assert_eq!(style, Some("atx".to_string()), "A should inherit MD003.style from B");
}

/// Child rule config overrides base rule config when both set the same key.
#[test]
fn test_extends_rule_config_child_overrides_base() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[MD013]
line-length = 80

[MD003]
style = "atx"
"#,
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        r#"extends = "base.rumdl.toml"

[MD013]
line-length = 100
"#,
    )
    .unwrap();

    let config = load_config(&child).unwrap();

    // Child overrides line-length
    let line_length = rumdl_lib::config::get_rule_config_value::<i64>(&config, "MD013", "line-length");
    assert_eq!(line_length, Some(100), "Child should override MD013.line-length");

    // MD003 style is inherited from base (child doesn't set it)
    let style = rumdl_lib::config::get_rule_config_value::<String>(&config, "MD003", "style");
    assert_eq!(
        style,
        Some("atx".to_string()),
        "MD003.style should be inherited from base"
    );
}

/// Loaded files list includes both the child and the base.
#[test]
fn test_extends_loaded_files_tracks_chain() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(&base, "").unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, r#"extends = "base.rumdl.toml""#).unwrap();

    let sourced = SourcedConfig::load_with_discovery(Some(child.to_str().unwrap()), None, false).unwrap();

    assert!(
        sourced.loaded_files.len() >= 2,
        "Both base and child should appear in loaded_files, got: {:?}",
        sourced.loaded_files
    );

    let has_base = sourced.loaded_files.iter().any(|f| f.contains("base.rumdl.toml"));
    let has_child = sourced.loaded_files.iter().any(|f| f.contains("child.rumdl.toml"));
    assert!(has_base, "base.rumdl.toml should be in loaded_files");
    assert!(has_child, "child.rumdl.toml should be in loaded_files");
}

/// Extend-enable uses union semantics across the extends chain.
#[test]
fn test_extends_extend_enable_union_semantics() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[global]
extend-enable = ["MD060"]
"#,
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        r#"extends = "base.rumdl.toml"

[global]
extend-enable = ["MD063"]
"#,
    )
    .unwrap();

    let config = load_config(&child).unwrap();

    // Both should be in extend_enable (union semantics)
    assert!(
        config.global.extend_enable.contains(&"MD060".to_string()),
        "Base's extend-enable should be present"
    );
    assert!(
        config.global.extend_enable.contains(&"MD063".to_string()),
        "Child's extend-enable should be present"
    );
}

/// Deep chain replace semantics: disable uses replace, so the leaf node's value wins entirely.
#[test]
fn test_extends_deep_chain_replace_semantics() {
    let dir = tempdir().unwrap();

    let c = dir.path().join("c.rumdl.toml");
    fs::write(&c, "[global]\ndisable = [\"MD041\"]\n").unwrap();

    let b = dir.path().join("b.rumdl.toml");
    fs::write(&b, "extends = \"c.rumdl.toml\"\n[global]\ndisable = [\"MD033\"]\n").unwrap();

    let a = dir.path().join("a.rumdl.toml");
    fs::write(&a, "extends = \"b.rumdl.toml\"\n[global]\ndisable = [\"MD013\"]\n").unwrap();

    let config = load_config(&a).unwrap();

    // disable uses replace semantics: only A's value survives
    assert_eq!(
        config.global.disable,
        vec!["MD013".to_string()],
        "disable should contain only A's value (replace semantics)"
    );
    assert!(
        !config.global.disable.contains(&"MD033".to_string()),
        "B's disable should not leak into A"
    );
    assert!(
        !config.global.disable.contains(&"MD041".to_string()),
        "C's disable should not leak into A"
    );
}

/// per_file_ignores inheritance: child inherits base's per_file_ignores when not overriding.
#[test]
fn test_extends_per_file_ignores_inherited() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[per-file-ignores]
"docs/*.md" = ["MD013"]
"README.md" = ["MD041"]
"#,
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        r#"extends = "base.rumdl.toml"

[global]
line-length = 100
"#,
    )
    .unwrap();

    let config = load_config(&child).unwrap();

    assert!(
        config.per_file_ignores.contains_key("docs/*.md"),
        "Child should inherit per_file_ignores from base"
    );
    assert!(
        config.per_file_ignores.contains_key("README.md"),
        "Child should inherit all per_file_ignores patterns from base"
    );
}

/// per_file_ignores replacement: child's per_file_ignores fully replaces base's.
#[test]
fn test_extends_per_file_ignores_replaced_by_child() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[per-file-ignores]
"docs/*.md" = ["MD013"]
"#,
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        r#"extends = "base.rumdl.toml"

[per-file-ignores]
"src/*.md" = ["MD041"]
"#,
    )
    .unwrap();

    let config = load_config(&child).unwrap();

    // Child's per_file_ignores replaces base's entirely (replace semantics)
    assert!(
        config.per_file_ignores.contains_key("src/*.md"),
        "Child's per_file_ignores should be present"
    );
    assert!(
        !config.per_file_ignores.contains_key("docs/*.md"),
        "Base's per_file_ignores should be replaced by child's"
    );
}

/// Provenance: each value's origin names the file in the extends chain that
/// actually set it, so `rumdl config` can attribute base-config values to the
/// base file and overrides to the child.
#[test]
fn test_extends_origin_attribution() {
    let dir = tempdir().unwrap();
    let base = dir.path().join("base.rumdl.toml");
    let child = dir.path().join(".rumdl.toml");

    fs::write(
        &base,
        r#"exclude = ["drafts"]

[MD013]
line-length = 100
"#,
    )
    .unwrap();
    fs::write(
        &child,
        r#"extends = "base.rumdl.toml"
enable = ["MD001", "MD013"]

[MD013]
line-length = 120
"#,
    )
    .unwrap();

    let sourced = SourcedConfig::load_with_discovery(Some(child.to_str().unwrap()), None, false).unwrap();

    let origin_of = |origin: &Option<String>| -> String {
        origin
            .as_deref()
            .and_then(|f| std::path::Path::new(f).file_name())
            .map(|n| n.to_string_lossy().to_string())
            .unwrap_or_default()
    };

    // Inherited value: attributed to the base file.
    assert_eq!(origin_of(&sourced.global.exclude.origin), "base.rumdl.toml");
    // Value only the child sets: attributed to the child.
    assert_eq!(origin_of(&sourced.global.enable.origin), ".rumdl.toml");
    // Value the child overrides: the child wins the attribution.
    let md013 = sourced.rules.get("MD013").expect("MD013 config present");
    let line_length = md013.values.get("line-length").expect("line-length set");
    assert_eq!(toml::Value::Integer(120), line_length.value);
    assert_eq!(origin_of(&line_length.origin), ".rumdl.toml");
}

// --- Env-var expansion in `extends` paths (issue #667) ----------------------
//
// These tests mutate the real process environment, so they carry
// `#[serial_test::serial]` (the codebase convention for global-state tests) and
// clean up via an RAII guard. They drive the production loader (`load_config`
// -> `SourcedConfig::load_with_discovery`), not a re-implementation of the
// resolver, so the full parse -> expand -> resolve -> merge path is exercised.

/// Sets an env var for the duration of a test and removes it on drop.
struct EnvVarGuard {
    key: String,
}

impl EnvVarGuard {
    fn set(key: &str, value: &std::path::Path) -> Self {
        // SAFETY: rumdl's test runner is nextest (`make test` / CI), which executes
        // each test in its own process, so this `set_var` cannot race a concurrent
        // `std::env::var` in another test. `#[serial_test::serial]` additionally
        // serializes this against the other global-state (env/cwd) tests, which
        // covers shared-process runners that serialize on the same lock. The guard
        // restores the environment on drop.
        unsafe { std::env::set_var(key, value) };
        Self { key: key.to_string() }
    }
}

impl Drop for EnvVarGuard {
    fn drop(&mut self) {
        // SAFETY: see `EnvVarGuard::set`.
        unsafe { std::env::remove_var(&self.key) };
    }
}

/// Bare `$VAR` form: a child references its base through an environment variable
/// and inherits the base while keeping its own override.
#[test]
#[serial_test::serial]
fn test_extends_env_var_expansion() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(
        &base,
        r#"
[global]
disable = ["MD033"]

[MD013]
line-length = 80
"#,
    )
    .unwrap();

    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_BASE_DIR", dir.path());

    // `extends` written as a TOML literal string ('...') so Windows backslashes
    // in the expanded path are not treated as escapes.
    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        "extends = '$RUMDL_TEST_EXTENDS_BASE_DIR/base.rumdl.toml'\n\n[MD013]\nline-length = 120\n",
    )
    .unwrap();

    let config = load_config(&child).unwrap();

    assert!(
        config.global.disable.contains(&"MD033".to_string()),
        "child should inherit the base via an env-var-expanded extends path"
    );
    let line_length = rumdl_lib::config::get_rule_config_value::<i64>(&config, "MD013", "line-length");
    assert_eq!(line_length, Some(120), "child override should still apply");
}

/// Braced `${VAR}` form resolves the same way.
#[test]
#[serial_test::serial]
fn test_extends_env_var_braced_form() {
    let dir = tempdir().unwrap();

    let base = dir.path().join("base.rumdl.toml");
    fs::write(&base, "[global]\ndisable = [\"MD041\"]\n").unwrap();

    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_BRACED_DIR", dir.path());

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '${RUMDL_TEST_EXTENDS_BRACED_DIR}/base.rumdl.toml'\n").unwrap();

    let config = load_config(&child).unwrap();
    assert!(
        config.global.disable.contains(&"MD041".to_string()),
        "braced ${{VAR}} form should resolve and inherit the base"
    );
}

/// An `extends` path that references an unset variable fails with a clear,
/// dedicated error (not a confusing "file not found" on a half-expanded path).
#[test]
#[serial_test::serial]
fn test_extends_undefined_env_var_gives_clear_error() {
    let dir = tempdir().unwrap();

    // Make sure the variable really is unset.
    // SAFETY: per-test-process isolation under nextest + `#[serial_test::serial]`;
    // see `EnvVarGuard::set`.
    unsafe { std::env::remove_var("RUMDL_TEST_DEFINITELY_UNSET_667") };

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_DEFINITELY_UNSET_667/base.rumdl.toml'\n").unwrap();

    match load_config(&child).unwrap_err() {
        ConfigError::ExtendsUndefinedVar { var, from } => {
            assert_eq!(
                var, "$RUMDL_TEST_DEFINITELY_UNSET_667",
                "error should name the missing variable"
            );
            assert!(
                from.contains("child.rumdl.toml"),
                "error should name the referencing file, got from: {from}"
            );
        }
        other => panic!("Expected ExtendsUndefinedVar error, got: {other:?}"),
    }
}

/// Cycle detection still fires when the cycle is formed through an env-expanded
/// path: the expanded path must canonicalize into the visited set.
#[test]
#[serial_test::serial]
fn test_extends_env_var_cycle_is_detected() {
    let dir = tempdir().unwrap();

    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_SELF_DIR", dir.path());

    // The config extends itself via an env-var-expanded path.
    let cfg = dir.path().join("self.rumdl.toml");
    fs::write(&cfg, "extends = '$RUMDL_TEST_EXTENDS_SELF_DIR/self.rumdl.toml'\n").unwrap();

    match load_config(&cfg).unwrap_err() {
        ConfigError::CircularExtends { .. } => {} // expected: env-expanded path hit the cycle guard
        other => panic!("Expected CircularExtends via env-expanded path, got: {other:?}"),
    }
}

// --- What an `extends` failure may say about the file it reached -------------
//
// An `extends` value is expanded from the environment and then points wherever
// it points, so a message about the file it reaches must not print the expanded
// path (that prints environment variable values into whatever reads the error,
// which under CI is the build log) and must not quote the file's own text (the
// target need not be a config file at all). It still has to say enough to be
// acted on, so each test below pairs the disclosure assertion with a positive
// control that the reference is named as written.
//
// These tests set an environment variable, so they follow the convention above:
// `#[serial_test::serial]` plus the RAII guard.

/// A value distinctive enough that finding it in a message can only mean the
/// expanded path was printed.
const SECRET: &str = "s3cr3t-value-do-not-leak";

/// Asserts a message keeps the secret out while still naming the reference.
#[track_caller]
fn assert_reference_named_without_secret(message: &str, written_as: &str) {
    assert!(
        !message.contains(SECRET),
        "message disclosed the expanded environment variable: {message}"
    );
    // Positive control: suppressing the path must not leave an error nobody can
    // act on. A message naming neither the value nor the file would pass the
    // assertion above while being useless.
    assert!(
        message.contains(written_as),
        "message should name the extends value as written ({written_as}), got: {message}"
    );
    assert!(
        message.contains("child.rumdl.toml"),
        "message should name the referencing config, got: {message}"
    );
}

/// The target does not exist: the "not found" error names the reference, not the
/// path the environment variable expanded to.
#[test]
#[serial_test::serial]
fn test_extends_missing_target_does_not_disclose_expanded_path() {
    let dir = tempdir().unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_SECRET_DIR", &dir.path().join(SECRET));

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_SECRET_DIR/base.toml'\n").unwrap();

    match load_config(&child).unwrap_err() {
        err @ ConfigError::ExtendsNotFound { .. } => {
            let message = err.to_string();
            assert_reference_named_without_secret(&message, "$RUMDL_TEST_EXTENDS_SECRET_DIR/base.toml");
            assert!(
                message.contains("RUMDL_TEST_EXTENDS_SECRET_DIR"),
                "naming the substituted variable is what makes the error diagnosable, got: {message}"
            );
        }
        other => panic!("Expected ExtendsNotFound, got: {other:?}"),
    }
}

/// The target exists but is not readable as a file: the I/O error names the
/// reference, not the expanded path.
#[test]
#[serial_test::serial]
fn test_extends_unreadable_target_does_not_disclose_expanded_path() {
    let dir = tempdir().unwrap();
    // A directory: `read_to_string` fails on it on every platform.
    let target = dir.path().join(SECRET);
    fs::create_dir(&target).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_SECRET_TARGET", &target);

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_SECRET_TARGET'\n").unwrap();

    let message = load_config(&child).unwrap_err().to_string();
    assert_reference_named_without_secret(&message, "$RUMDL_TEST_EXTENDS_SECRET_TARGET");
}

/// The target is not TOML at all: the parse error locates the problem by line
/// and column and does not quote the line, which belongs to a file the user
/// never asked rumdl to read.
#[test]
#[serial_test::serial]
fn test_extends_parse_error_does_not_quote_target_contents() {
    let dir = tempdir().unwrap();
    let target = dir.path().join("private-key.pem");
    fs::write(&target, format!("-----BEGIN PRIVATE KEY-----\n{SECRET}\n")).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_SECRET_FILE", &target);

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_SECRET_FILE'\n").unwrap();

    let message = load_config(&child).unwrap_err().to_string();
    assert!(
        !message.contains("BEGIN PRIVATE KEY"),
        "parse error quoted a line of the target file: {message}"
    );
    assert_reference_named_without_secret(&message, "$RUMDL_TEST_EXTENDS_SECRET_FILE");
    assert!(
        message.contains("line 1"),
        "the position still has to be reported so the owner of the file can find it, got: {message}"
    );
}

/// Negative control for the rule above: a config the user named themselves is
/// still rendered in full, with the offending line quoted. Suppressing that
/// everywhere would make every syntax error harder to fix.
#[test]
fn test_direct_config_parse_error_still_quotes_the_line() {
    let dir = tempdir().unwrap();
    let config = dir.path().join("broken.rumdl.toml");
    fs::write(&config, "[global]\nthis line is not toml\n").unwrap();

    let message = load_config(&config).unwrap_err().to_string();
    assert!(
        message.contains("this line is not toml"),
        "a directly named config should still have its offending line quoted, got: {message}"
    );
}

/// A cycle formed through an expanded path: the chain in the error is described
/// by how each file was reached, not by the paths it resolved to.
#[test]
#[serial_test::serial]
fn test_extends_cycle_does_not_disclose_expanded_paths() {
    let dir = tempdir().unwrap();
    let secret_dir = dir.path().join(SECRET);
    fs::create_dir(&secret_dir).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_CYCLE_DIR", &secret_dir);

    // child -> base (through the secret directory) -> back to child.
    let base = secret_dir.join("base.rumdl.toml");
    fs::write(&base, "extends = '../child.rumdl.toml'\n").unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_CYCLE_DIR/base.rumdl.toml'\n").unwrap();

    let message = load_config(&child).unwrap_err().to_string();
    assert!(
        !message.contains(SECRET),
        "the cycle report disclosed the expanded environment variable: {message}"
    );
    assert!(
        message.contains("$RUMDL_TEST_EXTENDS_CYCLE_DIR/base.rumdl.toml"),
        "the cycle report should still name the reference as written, got: {message}"
    );
}

/// The `[config warning]` messages a plain `rumdl check` prints for this config.
fn validation_warnings(config: &std::path::Path) -> Vec<String> {
    let sourced = SourcedConfig::load_with_discovery(Some(config.to_str().unwrap()), None, false).unwrap();
    let rules = rumdl_lib::all_rules(&rumdl_lib::config::Config::default());
    let registry = rumdl_lib::config::RuleRegistry::from_rules(&rules);
    rumdl_lib::config::validate_config_sourced(&sourced, &registry)
        .into_iter()
        .map(|w| w.message)
        .collect()
}

/// Validation warnings travel the same channel as errors, and it is the channel
/// a user gets without asking: `[config warning]` is printed by a bare
/// `rumdl check`. An unknown key in a file reached through `extends` must not
/// name the expanded path, and must not repeat the key either. rumdl did not
/// recognize the key, so it is text out of a file rumdl was merely pointed at.
#[test]
#[serial_test::serial]
fn test_extends_unknown_key_warning_does_not_disclose_expanded_path() {
    let dir = tempdir().unwrap();
    let secret_dir = dir.path().join(SECRET);
    fs::create_dir(&secret_dir).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_WARN_DIR", &secret_dir);

    let base = secret_dir.join("base.rumdl.toml");
    fs::write(&base, "[global]\nprod-database-password = true\n").unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_WARN_DIR/base.rumdl.toml'\n").unwrap();

    let messages = validation_warnings(&child);
    let warning = messages
        .iter()
        .find(|m| m.contains("Unknown global option"))
        .unwrap_or_else(|| panic!("expected a warning for the unknown key, got: {messages:?}"));

    assert!(
        !warning.contains("prod-database-password"),
        "the warning repeated a key read out of the extends target: {warning}"
    );
    // Positive control: withholding must be visible. A warning that quietly
    // dropped the key would satisfy the assertion above and tell the user less
    // than it knows.
    assert!(
        warning.contains("<withheld>"),
        "the warning should say a key was withheld, got: {warning}"
    );
    assert_reference_named_without_secret(warning, "$RUMDL_TEST_EXTENDS_WARN_DIR/base.rumdl.toml");
}

/// A section name is read out of the file the same way a key is.
#[test]
#[serial_test::serial]
fn test_extends_unknown_section_warning_withholds_the_section_name() {
    let dir = tempdir().unwrap();
    let secret_dir = dir.path().join(SECRET);
    fs::create_dir(&secret_dir).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_SECTION_DIR", &secret_dir);

    let base = secret_dir.join("base.rumdl.toml");
    fs::write(&base, "[prod-signing-key-id]\nenabled = true\n").unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_SECTION_DIR/base.rumdl.toml'\n").unwrap();

    let messages = validation_warnings(&child);
    let warning = messages
        .iter()
        .find(|m| m.starts_with("Unknown rule in"))
        .unwrap_or_else(|| panic!("expected a warning for the unknown section, got: {messages:?}"));

    assert!(
        !warning.contains("prod-signing-key-id"),
        "the warning repeated a section name read out of the extends target: {warning}"
    );
    assert!(
        warning.contains("<withheld>"),
        "the warning should say a section name was withheld, got: {warning}"
    );
    assert_reference_named_without_secret(warning, "$RUMDL_TEST_EXTENDS_SECTION_DIR/base.rumdl.toml");
}

/// An option of a rule rumdl *does* know reaches the validator by a different
/// route: the parser stores it in the config map, and the validator reports
/// whatever key it finds there. So an unrecognized option in a `[MD013]` table
/// has to be withheld where it is parsed, not where it is reported.
#[test]
#[serial_test::serial]
fn test_extends_unknown_rule_option_is_withheld() {
    let dir = tempdir().unwrap();
    let secret_dir = dir.path().join(SECRET);
    fs::create_dir(&secret_dir).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_OPTION_DIR", &secret_dir);

    let base = secret_dir.join("base.rumdl.toml");
    fs::write(&base, "[MD013]\nline-length = 80\nprod-database-password = true\n").unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_OPTION_DIR/base.rumdl.toml'\n").unwrap();

    let messages = validation_warnings(&child);
    let warning = messages
        .iter()
        .find(|m| m.contains("Unknown option for rule MD013"))
        .unwrap_or_else(|| panic!("expected a warning for the unknown option, got: {messages:?}"));

    assert!(
        !warning.contains("prod-database-password"),
        "the warning repeated an option key read out of the extends target: {warning}"
    );
    assert!(
        warning.contains("<withheld>"),
        "the warning should say an option was withheld, got: {warning}"
    );
    assert_reference_named_without_secret(warning, "$RUMDL_TEST_EXTENDS_OPTION_DIR/base.rumdl.toml");

    // Withholding must not cost the rest of the table: the recognized option in
    // the same section still applies, so this is a redaction and not a refusal
    // to read the file.
    let config = load_config(&child).unwrap();
    assert_eq!(
        rumdl_lib::config::get_rule_config_value::<i64>(&config, "MD013", "line-length"),
        Some(80),
        "the recognized option in the same table should still be inherited"
    );
}

/// Negative control for the three above: a config the user named themselves has
/// its unknown key, section and rule option quoted back, which is what makes
/// those warnings worth printing. Withholding everywhere would trade one bug
/// for another.
#[test]
fn test_direct_config_unknown_key_is_still_named() {
    let dir = tempdir().unwrap();
    let config = dir.path().join("named.rumdl.toml");
    fs::write(
        &config,
        "[global]\nnot-a-global-option = true\nenable = [\"not-a-rule-name\"]\n\n[MD9999]\nenabled = true\n\n[MD013]\nnot-an-md013-option = 1\n",
    )
    .unwrap();

    let messages = validation_warnings(&config);
    assert!(
        messages.iter().any(|m| m.contains("not-a-rule-name")),
        "a directly named config should have its unknown rule name quoted back, got: {messages:?}"
    );
    assert!(
        messages.iter().any(|m| m.contains("not-a-global-option")),
        "a directly named config should have its unknown key quoted back, got: {messages:?}"
    );
    assert!(
        messages.iter().any(|m| m.contains("MD9999")),
        "a directly named config should have its unknown section quoted back, got: {messages:?}"
    );
    assert!(
        messages.iter().any(|m| m.contains("not-an-md013-option")),
        "a directly named config should have its unknown rule option quoted back, got: {messages:?}"
    );
    assert!(
        messages.iter().all(|m| !m.contains("<withheld>")),
        "nothing should be withheld for a config the user named, got: {messages:?}"
    );
}

/// The warnings that repeat a config *value* go through `log`, so they need
/// `RUST_LOG` to be visible and this test drives the binary rather than the
/// library. An invalid value in a file reached through `extends` is reported
/// without echoing what the file said.
#[test]
fn test_extends_invalid_value_warning_does_not_echo_the_value() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(&base, format!("[per-file-flavor]\n\"*.md\" = \"{SECRET}\"\n")).unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_FLAVOR_DIR/base.rumdl.toml'\n").unwrap();

    let doc = dir.path().join("doc.md");
    fs::write(&doc, "# Title\n").unwrap();

    // The environment variable is set on the child process only, so this test
    // needs no guard and does not have to be serialized against the others.
    let run = |config: &std::path::Path| -> String {
        let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
            .args(["check", "--no-cache", "--config"])
            .arg(config)
            .arg(&doc)
            .env("RUST_LOG", "warn")
            .env("RUMDL_TEST_EXTENDS_FLAVOR_DIR", &target_dir)
            .output()
            .expect("failed to run the rumdl binary");
        String::from_utf8_lossy(&output.stderr).into_owned()
    };

    let through_extends = run(&child);
    assert!(
        !through_extends.contains(SECRET),
        "the warning echoed a value read out of the extends target: {through_extends}"
    );
    assert!(
        through_extends.contains("Invalid flavor"),
        "the problem still has to be reported, got: {through_extends}"
    );

    // Positive control, and proof this harness can see the value at all: the
    // same file named directly still has its invalid value quoted back.
    let directly = run(&base);
    assert!(
        directly.contains(SECRET),
        "a directly named config should still name the invalid value, got: {directly}"
    );
}

/// A rule name rumdl does not recognize is text out of the file, and the
/// validator prints one back verbatim on the always-on channel. From a file
/// reached through `extends` it is withheld, while the names rumdl does
/// recognize keep working.
#[test]
#[serial_test::serial]
fn test_extends_unknown_rule_name_in_global_list_is_withheld() {
    let dir = tempdir().unwrap();
    let secret_dir = dir.path().join(SECRET);
    fs::create_dir(&secret_dir).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_LIST_DIR", &secret_dir);

    let base = secret_dir.join("base.rumdl.toml");
    fs::write(&base, format!("[global]\nenable = [\"MD013\", \"{SECRET}\"]\n")).unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_LIST_DIR/base.rumdl.toml'\n").unwrap();

    let messages = validation_warnings(&child);
    let warning = messages
        .iter()
        .find(|m| m.contains("Unknown rule in global.enable"))
        .unwrap_or_else(|| panic!("expected a warning for the unknown rule name, got: {messages:?}"));

    assert!(
        !warning.contains(SECRET),
        "the warning repeated a rule name read out of the extends target: {warning}"
    );
    assert!(
        warning.contains("<withheld>"),
        "withholding has to stay visible, got: {warning}"
    );

    // Positive control: withholding one entry is a redaction of that entry, not
    // a refusal of the list. MD013 sat beside it and still selects.
    let config = load_config(&child).unwrap();
    assert_eq!(
        config.global.enable.iter().filter(|r| *r == "MD013").count(),
        1,
        "the recognized rule name should survive, got: {:?}",
        config.global.enable
    );
}

/// The glob caches print a pattern they cannot compile, and they run far from
/// the config file with no idea where the pattern came from. One out of an
/// `extends` target is dropped while the origin is still known, and the warning
/// it would have raised is raised here instead.
#[test]
fn test_extends_invalid_glob_pattern_is_withheld() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(
        &base,
        format!("[global]\nenable = [\"MD013\"]\n\n[per-file-ignores]\n\"[{SECRET}\" = [\"MD013\"]\n"),
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_GLOB_DIR/base.rumdl.toml'\n").unwrap();

    let doc = dir.path().join("doc.md");
    fs::write(&doc, format!("# Title\n\n{}\n", ["word"; 40].join(" "))).unwrap();

    let run = |config: &std::path::Path| -> String {
        let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
            .args(["check", "--no-cache", "--config"])
            .arg(config)
            .arg(&doc)
            .env("RUST_LOG", "warn")
            .env("RUMDL_TEST_EXTENDS_GLOB_DIR", &target_dir)
            .output()
            .expect("failed to run the rumdl binary");
        String::from_utf8_lossy(&output.stderr).into_owned()
    };

    let through_extends = run(&child);
    assert!(
        !through_extends.contains(SECRET),
        "the warning echoed a pattern read out of the extends target: {through_extends}"
    );
    assert!(
        through_extends.contains("Invalid glob pattern in per-file-ignores"),
        "the problem still has to be reported, got: {through_extends}"
    );

    // Positive control, and proof this harness can see the pattern at all: the
    // same file named directly still has its invalid pattern printed.
    let directly = run(&base);
    assert!(
        directly.contains(SECRET),
        "a directly named config should still name the invalid pattern, got: {directly}"
    );
}

/// Dropping an invalid pattern must not drop the valid ones beside it.
#[test]
#[serial_test::serial]
fn test_extends_valid_glob_pattern_survives_a_withheld_one() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_GLOB2_DIR", &target_dir);

    let base = target_dir.join("base.rumdl.toml");
    fs::write(
        &base,
        format!("[per-file-ignores]\n\"[{SECRET}\" = [\"MD013\"]\n\"docs/*.md\" = [\"MD041\"]\n"),
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_GLOB2_DIR/base.rumdl.toml'\n").unwrap();

    let config = load_config(&child).unwrap();
    assert_eq!(
        config.per_file_ignores.keys().collect::<Vec<_>>(),
        vec!["docs/*.md"],
        "only the pattern that does not compile should go, got: {:?}",
        config.per_file_ignores
    );
}

/// The file walk prints an `include`/`exclude` pattern it cannot compile, on a
/// channel no logging level gates. A pattern out of an extends target has to
/// reach neither.
#[test]
fn test_extends_invalid_walk_patterns_are_withheld() {
    for setting in ["include", "exclude"] {
        let dir = tempdir().unwrap();
        let target_dir = dir.path().join("secrets");
        fs::create_dir(&target_dir).unwrap();

        let base = target_dir.join("base.rumdl.toml");
        fs::write(&base, format!("[global]\n{setting} = [\"[{SECRET}\"]\n")).unwrap();

        let child = dir.path().join("child.rumdl.toml");
        fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_WALK_DIR/base.rumdl.toml'\n").unwrap();

        fs::write(dir.path().join("doc.md"), "# Title\n").unwrap();

        // Discovery mode, since a config `include` only filters a walk rumdl
        // started itself: named a file outright, it never builds the override
        // that reports the pattern.
        let run = |config: &std::path::Path| -> String {
            let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
                .args(["check", "--no-cache", "--config"])
                .arg(config)
                .current_dir(dir.path())
                .env("RUMDL_TEST_EXTENDS_WALK_DIR", &target_dir)
                .output()
                .expect("failed to run the rumdl binary");
            String::from_utf8_lossy(&output.stderr).into_owned()
        };

        // No RUST_LOG here on purpose: the message the walk would have printed is
        // always on, so the one standing in for it has to be too.
        let through_extends = run(&child);
        assert!(
            !through_extends.contains(SECRET),
            "the {setting} warning echoed a pattern read out of the extends target: {through_extends}"
        );
        assert!(
            through_extends.contains(&format!("Invalid {setting} pattern in")),
            "the problem still has to be reported, got: {through_extends}"
        );

        // Positive control, and proof this harness can see the pattern at all: the
        // same file named directly still has its invalid pattern printed.
        let directly = run(&base);
        assert!(
            directly.contains(SECRET),
            "a directly named config should still name the invalid {setting} pattern, got: {directly}"
        );
    }
}

/// Dropping an invalid walk pattern must not drop the valid ones beside it, and
/// only `exclude` is dropped at all: an `include` pattern is rewritten relative
/// to the project root before the walk compiles it, and this does not know that
/// root, so judging one here would discard patterns the walk can use.
#[test]
#[serial_test::serial]
fn test_extends_valid_walk_patterns_survive_a_withheld_one() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_WALK2_DIR", &target_dir);

    let base = target_dir.join("base.rumdl.toml");
    fs::write(
        &base,
        format!("[global]\ninclude = [\"[{SECRET}\", \"docs/**\"]\nexclude = [\"[{SECRET}\", \"drafts\"]\n"),
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_WALK2_DIR/base.rumdl.toml'\n").unwrap();

    let config = load_config(&child).unwrap();
    assert_eq!(
        config.global.include,
        vec![format!("[{SECRET}"), "docs/**".to_string()],
        "the include patterns should reach the walk as written"
    );
    let withheld = config.global.include_withheld.as_deref().unwrap_or_default();
    assert!(
        withheld.starts_with("'$RUMDL_TEST_EXTENDS_WALK2_DIR/base.rumdl.toml' (referenced from ")
            && !withheld.contains(SECRET),
        "the walk needs the origin to report a pattern it cannot use without quoting it, got: {withheld}"
    );
    assert_eq!(
        config.global.exclude,
        vec!["drafts".to_string()],
        "only the exclude pattern that does not compile should go"
    );
}

/// The rewrite that makes an `include` pattern unjudgeable at parse time, from
/// the outside: an absolute pattern loses the project root's own name, and that
/// name can hold glob syntax the pattern never has to satisfy. `[2019-2021]`
/// reads as a character class spanning a descending range, so the pattern does
/// not compile until the prefix holding it comes off. Dropping it where it is
/// parsed silently skips every file it selects.
#[test]
fn test_extends_absolute_include_survives_a_project_root_name() {
    let dir = tempdir().unwrap();
    // The project root must be the directory holding the config, so each arm
    // gets its own `.git` marker rather than relying on there being none above.
    let mut arms = Vec::new();
    for arm in ["notes [2019-2021]", "named [2019-2021]"] {
        let project = dir.path().join(arm);
        fs::create_dir_all(project.join("docs")).unwrap();
        fs::create_dir(project.join(".git")).unwrap();
        fs::write(project.join("docs/note.md.jinja"), "no heading here\n").unwrap();
        // The pattern is written the way the running process sees the directory,
        // which is the canonical form on both platforms rumdl normalizes for
        // (macOS resolves `/var`, Windows reports an 8.3 short name). A path in a
        // TOML literal string, so a Windows separator is not an escape.
        let canonical = rumdl_lib::discovery::canonicalize_for_matching(&project).unwrap();
        let include = format!("include = ['{}/docs/*.md.jinja']\n", canonical.display()).replace('\\', "/");
        arms.push((project, include));
    }

    let (extending, extending_include) = &arms[0];
    let base_dir = dir.path().join("shared");
    fs::create_dir(&base_dir).unwrap();
    let base = base_dir.join("base.rumdl.toml");
    fs::write(&base, format!("[global]\n{extending_include}")).unwrap();
    fs::write(
        extending.join(".rumdl.toml"),
        "extends = '$RUMDL_TEST_EXTENDS_ABSINC_DIR/base.rumdl.toml'\n",
    )
    .unwrap();

    let (direct, direct_include) = &arms[1];
    fs::write(direct.join(".rumdl.toml"), format!("[global]\n{direct_include}")).unwrap();

    // Discovery mode with no `--config`: naming the config would move the project
    // root to the config's own directory and the prefix would never be stripped.
    let run = |project: &std::path::Path| -> String {
        let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
            .args(["check", "--no-cache"])
            .current_dir(project)
            .env("RUMDL_TEST_EXTENDS_ABSINC_DIR", &base_dir)
            .output()
            .expect("failed to run the rumdl binary");
        String::from_utf8_lossy(&output.stdout).into_owned()
    };

    // The control runs first: it fixes what the answer is, so a shared mistake in
    // the layout fails here rather than being read as a passing assertion below.
    let directly = run(direct);
    assert!(
        directly.contains("note.md.jinja"),
        "the absolute include should select the file when the config is the project's own, got: {directly}"
    );

    let through_extends = run(extending);
    assert!(
        through_extends.contains("note.md.jinja"),
        "the same include reached through extends should select the same file, got: {through_extends}"
    );
}

/// The empty-run notice names an `include` pattern that selected nothing, which
/// is as much the extends target's own text as one that does not compile. It is
/// named by the file it came from instead.
#[test]
fn test_extends_unmatched_include_is_not_quoted_in_the_empty_run_notice() {
    let dir = tempdir().unwrap();
    let base_dir = dir.path().join("shared");
    fs::create_dir(&base_dir).unwrap();
    let base = base_dir.join("base.rumdl.toml");
    // A pattern that compiles and matches nothing, beside one that does match,
    // so the run is a normal one rather than a diagnosis of a broken config.
    fs::write(&base, format!("[global]\ninclude = [\"docs/{SECRET}/*.md\"]\n")).unwrap();

    let project = dir.path().join("project");
    fs::create_dir_all(project.join("docs")).unwrap();
    fs::write(project.join("docs/note.md"), "no heading here\n").unwrap();
    fs::write(
        project.join(".rumdl.toml"),
        "extends = '$RUMDL_TEST_EXTENDS_NOMATCH_DIR/base.rumdl.toml'\n",
    )
    .unwrap();

    let run = |cwd: &std::path::Path| -> String {
        let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
            .args(["check", "--no-cache"])
            .current_dir(cwd)
            .env("RUMDL_TEST_EXTENDS_NOMATCH_DIR", &base_dir)
            .output()
            .expect("failed to run the rumdl binary");
        format!(
            "{}{}",
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        )
    };

    let through_extends = run(&project);
    assert!(
        !through_extends.contains(SECRET),
        "the empty-run notice quoted a pattern read out of the extends target: {through_extends}"
    );
    assert!(
        through_extends.contains("1 include pattern in '$RUMDL_TEST_EXTENDS_NOMATCH_DIR/base.rumdl.toml'")
            && through_extends.contains("matches no file"),
        "the notice still has to say an include selected nothing and where it came from, got: {through_extends}"
    );

    // Positive control: the same include in the project's own config is quoted,
    // which is what makes the notice worth printing.
    let named = dir.path().join("named");
    fs::create_dir_all(named.join("docs")).unwrap();
    fs::write(named.join("docs/note.md"), "no heading here\n").unwrap();
    fs::write(
        named.join(".rumdl.toml"),
        format!("[global]\ninclude = [\"docs/{SECRET}/*.md\"]\n"),
    )
    .unwrap();
    let directly = run(&named);
    assert!(
        directly.contains(&format!("include pattern 'docs/{SECRET}/*.md' matches no file")),
        "a directly named config should still have its unmatched pattern quoted, got: {directly}"
    );
}

/// An `include` pattern that does not compile in any form is judged where it is
/// used. The walk reports it, and from an `extends` target it reports it without
/// quoting it.
#[test]
fn test_extends_uncompilable_include_is_reported_without_quoting_it() {
    let dir = tempdir().unwrap();
    let base_dir = dir.path().join("shared");
    fs::create_dir(&base_dir).unwrap();
    let base = base_dir.join("base.rumdl.toml");
    fs::write(&base, format!("[global]\ninclude = [\"docs/[{SECRET}/*.md\"]\n")).unwrap();

    let project = dir.path().join("project");
    fs::create_dir_all(project.join("docs")).unwrap();
    fs::write(project.join("docs/note.md"), "no heading here\n").unwrap();
    fs::write(
        project.join(".rumdl.toml"),
        "extends = '$RUMDL_TEST_EXTENDS_BADINC_DIR/base.rumdl.toml'\n",
    )
    .unwrap();

    // No RUST_LOG: the message this replaces is printed unconditionally, so this
    // one has to be too. stdout comes along because the empty-run notice that
    // lists unmatched include patterns is printed there.
    let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
        .args(["check", "--no-cache"])
        .current_dir(&project)
        .env("RUMDL_TEST_EXTENDS_BADINC_DIR", &base_dir)
        .output()
        .expect("failed to run the rumdl binary");
    let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
    let combined = format!("{stderr}{}", String::from_utf8_lossy(&output.stdout));

    assert!(
        !combined.contains(SECRET),
        "the walk echoed a pattern read out of the extends target: {combined}"
    );
    assert!(
        stderr.contains("Invalid include pattern in") && stderr.contains("<withheld>"),
        "the problem still has to be reported, got: {stderr}"
    );

    // Positive control, and proof this harness can see the pattern at all: the
    // same patterns written in the project's own config are quoted back.
    let named = dir.path().join("named");
    fs::create_dir_all(named.join("docs")).unwrap();
    fs::write(named.join("docs/note.md"), "no heading here\n").unwrap();
    fs::write(
        named.join(".rumdl.toml"),
        format!("[global]\ninclude = [\"docs/[{SECRET}/*.md\"]\n"),
    )
    .unwrap();
    let directly = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
        .args(["check", "--no-cache"])
        .current_dir(&named)
        .output()
        .expect("failed to run the rumdl binary");
    assert!(
        String::from_utf8_lossy(&directly.stderr).contains(SECRET),
        "a directly named config should still name the invalid include pattern, got: {}",
        String::from_utf8_lossy(&directly.stderr)
    );
}

/// An option key a rule recognizes can still hold a value the rule cannot read,
/// and the rule quotes that value back when it deserializes its section. Only
/// the rule knows what it accepts, so the value cannot be checked at parse time
/// the way a rule name or a glob can: it is marked there instead, and the rule
/// leaves it out of the message.
#[test]
fn test_extends_invalid_rule_option_value_is_withheld() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(&base, format!("[MD003]\nstyle = \"{SECRET}\"\n")).unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_VALUE_DIR/base.rumdl.toml'\n").unwrap();

    let doc = dir.path().join("doc.md");
    fs::write(&doc, "# Title\n").unwrap();

    // No RUST_LOG: the rule prints straight to stderr, so the message standing
    // in for it has to be as visible as the one it replaces.
    let run = |config: &std::path::Path| -> String {
        let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
            .args(["check", "--no-cache", "--config"])
            .arg(config)
            .arg(&doc)
            .env("RUMDL_TEST_EXTENDS_VALUE_DIR", &target_dir)
            .output()
            .expect("failed to run the rumdl binary");
        String::from_utf8_lossy(&output.stderr).into_owned()
    };

    let through_extends = run(&child);
    assert!(
        !through_extends.contains(SECRET),
        "the warning echoed a rule option value read out of the extends target: {through_extends}"
    );
    assert!(
        through_extends.contains("Invalid configuration for rule MD003: <withheld>"),
        "the problem still has to be reported: {through_extends}"
    );

    // Positive control, and proof this harness can see the value at all: the
    // same file named directly still has its invalid value quoted back.
    let directly = run(&base);
    assert!(
        directly.contains(SECRET),
        "a directly named config should still name the invalid value, got: {directly}"
    );
}

/// Marking a rule option's value is not dropping it: the rule still reads what
/// the extended file set.
#[test]
#[serial_test::serial]
fn test_extends_valid_rule_option_values_still_apply() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();
    let _guard = EnvVarGuard::set("RUMDL_TEST_EXTENDS_VALUE2_DIR", &target_dir);

    let base = target_dir.join("base.rumdl.toml");
    fs::write(&base, "[MD003]\nstyle = \"setext\"\n").unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_VALUE2_DIR/base.rumdl.toml'\n").unwrap();

    let config = load_config(&child).unwrap();
    assert_eq!(
        config.rules["MD003"].values["style"].as_str(),
        Some("setext"),
        "the extended file's value should survive being marked"
    );
}

/// The mark travels with the value, so a config naming the value itself gets it
/// quoted back: what may be said about a value follows whichever file supplied
/// the one that won.
#[test]
fn test_rule_option_value_named_by_the_extending_config_is_shown() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(&base, format!("[MD003]\nstyle = \"{SECRET}\"\n")).unwrap();

    // The child overrides the same key, so the base's value never reaches the
    // rule and the value the rule does report is one this project wrote.
    let child = dir.path().join("child.rumdl.toml");
    fs::write(
        &child,
        "extends = '$RUMDL_TEST_EXTENDS_VALUE3_DIR/base.rumdl.toml'\n[MD003]\nstyle = \"child-typo\"\n",
    )
    .unwrap();

    let doc = dir.path().join("doc.md");
    fs::write(&doc, "# Title\n").unwrap();

    let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
        .args(["check", "--no-cache", "--config"])
        .arg(&child)
        .arg(&doc)
        .env("RUMDL_TEST_EXTENDS_VALUE3_DIR", &target_dir)
        .output()
        .expect("failed to run the rumdl binary");
    let stderr = String::from_utf8_lossy(&output.stderr);

    assert!(
        stderr.contains("child-typo"),
        "the extending config's own value should be quoted back, got: {stderr}"
    );
    assert!(
        !stderr.contains(SECRET),
        "the overridden value should not appear at all, got: {stderr}"
    );
}

/// Some option values deserialize cleanly and only fail when the rule tries to
/// use them. Those are reported by the rule itself, long past the point where the
/// section was read, so the rule has to be told what it may quote.
#[test]
fn test_extends_uncompilable_rule_pattern_is_withheld() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();

    // A well-formed string, and not a regex: MD051 accepts the value and only
    // finds out it cannot compile when it builds its filter.
    let base = target_dir.join("base.rumdl.toml");
    fs::write(&base, format!("[MD051]\nignored-pattern = \"[{SECRET}\"\n")).unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_REGEX_DIR/base.rumdl.toml'\n").unwrap();

    let doc = dir.path().join("doc.md");
    fs::write(&doc, "# Title\n").unwrap();

    let run = |config: &std::path::Path| -> String {
        let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
            .args(["check", "--no-cache", "--config"])
            .arg(config)
            .arg(&doc)
            .env("RUST_LOG", "warn")
            .env("RUMDL_TEST_EXTENDS_REGEX_DIR", &target_dir)
            .output()
            .expect("failed to run the rumdl binary");
        String::from_utf8_lossy(&output.stderr).into_owned()
    };

    let through_extends = run(&child);
    assert!(
        !through_extends.contains(SECRET),
        "the warning echoed a pattern read out of the extends target: {through_extends}"
    );
    assert!(
        through_extends.contains("Invalid ignored-pattern for MD051: <withheld>"),
        "the problem still has to be reported: {through_extends}"
    );

    // Positive control, and proof this harness can see the pattern at all: the
    // same file named directly still has its invalid pattern quoted back.
    let directly = run(&base);
    assert!(
        directly.contains(SECRET),
        "a directly named config should still name the invalid pattern, got: {directly}"
    );
}

/// Withholding what may be said about a pattern must not stop it working. A
/// pattern that does compile still filters what the rule reports.
#[test]
fn test_extends_compilable_rule_pattern_still_applies() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(&base, "[MD051]\nignored-pattern = \".*\"\n").unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_REGEX2_DIR/base.rumdl.toml'\n").unwrap();

    let doc = dir.path().join("doc.md");
    fs::write(&doc, "# Title\n\n[link](#no-such-anchor)\n").unwrap();

    let run = |config: Option<&std::path::Path>| -> String {
        let mut command = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"));
        command.args(["check", "--no-cache"]);
        match config {
            Some(path) => {
                command.arg("--config").arg(path);
            }
            None => {
                command.arg("--no-config");
            }
        }
        let output = command
            .arg(&doc)
            .env("RUMDL_TEST_EXTENDS_REGEX2_DIR", &target_dir)
            .output()
            .expect("failed to run the rumdl binary");
        String::from_utf8_lossy(&output.stdout).into_owned()
    };

    // Negative control first: without the pattern the fragment is reported, so
    // the run below has something to suppress.
    let without = run(None);
    assert!(
        without.contains("MD051"),
        "the fragment should be reported without the pattern, got: {without}"
    );

    let with = run(Some(&child));
    assert!(
        !with.contains("MD051"),
        "the extended file's pattern should still filter, got: {with}"
    );
}

/// `[code-block-tools]` names tools the registry resolves while a document is
/// being linted, and it reports an id it cannot resolve. That id is text out of
/// whichever file supplied the section.
#[test]
fn test_extends_unknown_code_block_tool_is_withheld() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("secrets");
    fs::create_dir(&target_dir).unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(
        &base,
        format!("[code-block-tools]\nenabled = true\n\n[code-block-tools.languages.python]\nlint = [\"{SECRET}\"]\n"),
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = '$RUMDL_TEST_EXTENDS_TOOL_DIR/base.rumdl.toml'\n").unwrap();

    let doc = dir.path().join("doc.md");
    fs::write(&doc, "# Title\n\n```python\nx = 1\n```\n").unwrap();

    let run = |config: &std::path::Path| -> String {
        let output = std::process::Command::new(env!("CARGO_BIN_EXE_rumdl"))
            .args(["check", "--no-cache", "--config"])
            .arg(config)
            .arg(&doc)
            .env("RUST_LOG", "warn")
            .env("RUMDL_TEST_EXTENDS_TOOL_DIR", &target_dir)
            .output()
            .expect("failed to run the rumdl binary");
        String::from_utf8_lossy(&output.stderr).into_owned()
    };

    let through_extends = run(&child);
    assert!(
        !through_extends.contains(SECRET),
        "the warning echoed a tool id read out of the extends target: {through_extends}"
    );
    assert!(
        through_extends.contains("Unknown tool <withheld> configured for language <withheld>"),
        "the problem still has to be reported: {through_extends}"
    );

    // Positive control, and proof this harness can see the id at all: the same
    // file named directly still has its unknown tool quoted back.
    let directly = run(&base);
    assert!(
        directly.contains(SECRET),
        "a directly named config should still name the unknown tool, got: {directly}"
    );
}

/// An `extends` value is a line of the file that wrote it, so a file that was
/// itself reached through `extends` does not get that one line quoted while the
/// rest of it is withheld. Here the reference fails to resolve.
#[test]
fn test_extends_nested_reference_is_withheld() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("shared");
    fs::create_dir(&target_dir).unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(&base, format!("extends = 'missing-{SECRET}/further.rumdl.toml'\n")).unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = 'shared/base.rumdl.toml'\n").unwrap();

    match load_config(&child).unwrap_err() {
        err @ ConfigError::ExtendsNotFound { .. } => {
            let message = err.to_string();
            assert!(
                !message.contains(SECRET),
                "the error quoted an extends value read out of the extends target: {message}"
            );
            assert!(
                message.contains("<withheld>"),
                "withholding must be visible rather than leaving an empty name, got: {message}"
            );
            assert!(
                message.contains("'shared/base.rumdl.toml'"),
                "the file holding the unusable reference is what makes this fixable, got: {message}"
            );
        }
        other => panic!("Expected ExtendsNotFound, got: {other:?}"),
    }

    // Positive control: the same reference written by the config the user named
    // is quoted in full, so this harness can see such a value at all and depth is
    // what changes the answer.
    let direct = dir.path().join("direct.rumdl.toml");
    fs::write(&direct, format!("extends = 'missing-{SECRET}/further.rumdl.toml'\n")).unwrap();
    let message = load_config(&direct).unwrap_err().to_string();
    assert!(
        message.contains(SECRET),
        "a directly named config should still have its own reference quoted, got: {message}"
    );
}

/// The name every message uses for a file at depth two is written in the file at
/// depth one, so a warning about the deeper file cannot quote its name either.
/// This is the ordinary path rather than an error path: the run succeeds and
/// prints a `[config warning]`.
#[test]
fn test_extends_warning_about_a_nested_target_withholds_its_name() {
    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("shared");
    fs::create_dir(&target_dir).unwrap();

    let deep = target_dir.join(format!("{SECRET}.rumdl.toml"));
    fs::write(&deep, "[global]\nprod-database-password = true\n").unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(&base, format!("extends = '{SECRET}.rumdl.toml'\n")).unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = 'shared/base.rumdl.toml'\n").unwrap();

    let unknown_key = |config: &std::path::Path| -> String {
        let messages = validation_warnings(config);
        messages
            .iter()
            .find(|m| m.contains("Unknown global option"))
            .unwrap_or_else(|| panic!("expected a warning for the unknown key, got: {messages:?}"))
            .clone()
    };

    let deep_warning = unknown_key(&child);
    assert!(
        !deep_warning.contains(SECRET),
        "the warning named the deeper file with text out of the file that reached for it: {deep_warning}"
    );
    assert!(
        deep_warning.contains("(referenced from 'shared/base.rumdl.toml')"),
        "which file reached for it is the extending config's own text and still has to be said, got: {deep_warning}"
    );

    // Positive control: at depth one the same name is the child's own text, and
    // the child is a config the user named. It is quoted, which is also what
    // proves this harness can see the name at all.
    let shallow = dir.path().join("shallow.rumdl.toml");
    fs::write(&shallow, format!("extends = 'shared/{SECRET}.rumdl.toml'\n")).unwrap();
    let shallow_warning = unknown_key(&shallow);
    assert!(
        shallow_warning.contains(SECRET),
        "a reference the named config wrote itself should still be quoted, got: {shallow_warning}"
    );
}

/// The variable name in a nested `extends` value is part of that value, so a set
/// and an unset variable disclose the same amount: nothing.
#[test]
#[serial_test::serial]
fn test_extends_nested_undefined_variable_is_withheld() {
    // SAFETY: per-test-process isolation under nextest + `#[serial_test::serial]`;
    // see `EnvVarGuard::set`.
    unsafe { std::env::remove_var("RUMDL_TEST_UNSET_CUSTOMER_ACME_INTERNAL") };

    let dir = tempdir().unwrap();
    let target_dir = dir.path().join("shared");
    fs::create_dir(&target_dir).unwrap();

    let base = target_dir.join("base.rumdl.toml");
    fs::write(
        &base,
        "extends = '$RUMDL_TEST_UNSET_CUSTOMER_ACME_INTERNAL/base.toml'\n",
    )
    .unwrap();

    let child = dir.path().join("child.rumdl.toml");
    fs::write(&child, "extends = 'shared/base.rumdl.toml'\n").unwrap();

    match load_config(&child).unwrap_err() {
        ConfigError::ExtendsUndefinedVar { var, from } => {
            assert_eq!(var, "<withheld>", "the variable name is text out of the extends target");
            assert!(
                from.contains("base.rumdl.toml"),
                "the file that referenced it still has to be named, got from: {from}"
            );
        }
        other => panic!("Expected ExtendsUndefinedVar, got: {other:?}"),
    }

    // Positive control: the same value in a config the user named still names the
    // variable, which is what makes that error fixable without opening a file.
    let direct = dir.path().join("direct.rumdl.toml");
    fs::write(
        &direct,
        "extends = '$RUMDL_TEST_UNSET_CUSTOMER_ACME_INTERNAL/base.toml'\n",
    )
    .unwrap();
    match load_config(&direct).unwrap_err() {
        ConfigError::ExtendsUndefinedVar { var, .. } => {
            assert_eq!(var, "$RUMDL_TEST_UNSET_CUSTOMER_ACME_INTERNAL");
        }
        other => panic!("Expected ExtendsUndefinedVar, got: {other:?}"),
    }
}