source-mesh 1.2.2

Dependency diagrams for Rust projects, powered by rust-analyzer SCIP
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
use std::ffi::OsString;
use std::io;
use std::io::ErrorKind;
use std::io::Write;
use std::num::NonZeroUsize;
use std::path::Path;
use std::path::PathBuf;

use anyhow::Context as _;
use anyhow::Result;
use anyhow::anyhow;
use anyhow::ensure;
use clap::ArgMatches;
use clap::Args;
use clap::FromArgMatches as _;
use clap::Parser;
use clap::ValueEnum;
use getset::CopyGetters;
use serde::Deserialize;
use source_mesh_core::CycleOptions;
use source_mesh_core::DEFAULT_CYCLE_LIMIT;
use source_mesh_core::Exclusions;
use source_mesh_core::Input;
use source_mesh_core::InputFile;
use source_mesh_core::OutgoingDependencyOptions;
use source_mesh_core::Project;
use source_mesh_core::Request;
use source_mesh_core::analyzer_options::Options as AnalyzerOptions;
use source_mesh_core::backend::project_directory;
use source_mesh_core::export::Format as ExportFormat;
use source_mesh_core::outgoing_dependencies::Mode as DependencyMode;

pub(super) const SETTINGS_FILE_NAME: &str = 
    "source-mesh.toml";
pub(super) const BOOLEAN_OPTIONS_DEFAULT_HELP: &str =
    "Without settings, boolean options are disabled. Use --flag=false to override settings.";

#[derive(Clone, Copy, Debug, Deserialize, PartialEq, ValueEnum)]
#[serde(try_from = "String")]
enum Format {
    Dot,
    Mermaid,
    Graphml,
}

impl TryFrom<String> for Format {
    type Error = String;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::from_str(&value, false)
    }
}

impl From<Format> for ExportFormat {
    fn from(format: Format) -> Self {
        match format {
            Format::Dot => Self::Dot,
            Format::Mermaid => Self::Mermaid,
            Format::Graphml => Self::Graphml,
        }
    }
}

#[derive(Clone, Copy, Debug, Deserialize, PartialEq, ValueEnum)]
#[serde(try_from = "String")]
enum OutgoingDependencyMode {
    Files,
    Folders,
}

impl TryFrom<String> for OutgoingDependencyMode {
    type Error = String;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::from_str(&value, false)
    }
}

impl From<OutgoingDependencyMode> for DependencyMode {
    fn from(mode: OutgoingDependencyMode) -> Self {
        match mode {
            OutgoingDependencyMode::Files => Self::Files,
            OutgoingDependencyMode::Folders => Self::Folders,
        }
    }
}

#[derive(Clone, Debug, Deserialize, PartialEq)]
#[serde(from = "PathBuf")]
enum SettingsPath {
    CommandLine(PathBuf),
    SettingsFile(PathBuf),
}

impl From<OsString> for SettingsPath {
    fn from(path: OsString) -> Self {
        Self::CommandLine(path.into())
    }
}

impl From<PathBuf> for SettingsPath {
    fn from(path: PathBuf) -> Self {
        Self::SettingsFile(path)
    }
}

impl SettingsPath {
    #[must_use]
    fn resolve(self, directory: &Path) -> PathBuf {
        match self {
            Self::CommandLine(path) => path,
            Self::SettingsFile(path) => directory.join(path),
        }
    }
}

#[derive(Args, Debug, Default, Deserialize, PartialEq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
struct Settings {
    #[command(flatten)]
    graph: GraphSettings,
    #[command(flatten)]
    export: ExportSettings,
    #[command(flatten)]
    cycles: CycleSettings,
    #[command(flatten)]
    outgoing_dependencies: OutgoingDependencySettings,
    #[command(flatten)]
    analysis: AnalysisSettings,
}

#[derive(Args, Debug, Default, Deserialize, PartialEq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
struct GraphSettings {
    #[arg(long,
        help = "Restrict to a relative index/project subtree, e.g. src (default: .)")]
    root: Option<PathBuf>,
    #[arg(short, long,
        help = "Maximum folder levels below --root: 0 = root only; default = all")]
    depth: Option<usize>,
    #[arg(long, num_args = 0..=1, require_equals = true, default_missing_value = "true",
        help = "Keep edges between distinct files collapsed into the same folder")]
    include_self: Option<bool>,
    #[arg(long, num_args = 0..=1, require_equals = true, default_missing_value = "true",
        help = "Hide dependencies used only by tests")]
    exclude_tests: Option<bool>,
    #[arg(long, value_name = "GLOB",
        help = "Exclude project-relative files; repeat to replace the settings exclusion list")]
    exclude: Vec<String>,
}

#[derive(Args, Debug, Default, Deserialize, PartialEq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
struct ExportSettings {
    #[arg(short, long, value_enum,
        help = "Diagram format (default: dot)")]
    format: Option<Format>,
    #[arg(short, long,
        help = "Output file (otherwise stdout)")]
    output: Option<SettingsPath>,
    #[arg(long, num_args = 0..=1, require_equals = true, default_missing_value = "true",
        help = "Nest subfolders, hide shared ancestor groups, and keep their own files as separate nodes")]
    group_folders: Option<bool>,
}

#[derive(Args, Debug, Default, Deserialize, PartialEq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
struct CycleSettings {
    #[arg(long, num_args = 0..=1, require_equals = true, default_missing_value = "true",
        help = "Highlight cyclic folder components with distinct bright colors")]
    highlight_cycles: Option<bool>,
    #[arg(long, num_args = 0..=1, require_equals = true, default_missing_value = "true",
        help = "Exit unsuccessfully after writing outputs if file or folder dependency cycles are found")]
    fail_on_cycles: Option<bool>,
    #[arg(long,
        help = "Write folder cycle chains to a Markdown report")]
    cycles_output: Option<SettingsPath>,
    #[arg(long,
        help = "Write file cycles before folder folding to a separate Markdown report")]
    file_cycles_output: Option<SettingsPath>,
    #[arg(long,
        help = format!("Maximum listed cycles per report (default: {DEFAULT_CYCLE_LIMIT})"))]
    cycle_limit: Option<NonZeroUsize>,
}

#[derive(Args, Debug, Default, Deserialize, PartialEq)]
#[serde(default, deny_unknown_fields)]
struct OutgoingDependencySettings {
    #[arg(id = "max_outgoing_dependencies", long = "max-outgoing-dependencies", value_name = "N",
        help = "Fail after writing outputs if any node has more than N distinct outgoing targets")]
    maximum: Option<usize>,
    #[arg(id = "outgoing_dependencies_mode", long = "outgoing-dependencies-mode", value_enum,
        help = "Check files before folder folding, or folders after root/depth filtering (default: folders)")]
    mode: Option<OutgoingDependencyMode>,
    #[arg(id = "outgoing_dependencies_output", long = "outgoing-dependencies-output", value_name = "FILE",
        help = "Write outgoing dependency violations to Markdown")]
    output: Option<SettingsPath>,
}

#[derive(Args, Debug, Default, Deserialize, PartialEq)]
#[serde(default, deny_unknown_fields, rename_all = "kebab-case")]
struct AnalysisSettings {
    #[arg(long,
        help = "Reuse saved analysis without reanalyzing sources")]
    from_scip: Option<SettingsPath>,
    #[arg(long,
        help = "Save the index for fast re-export at other depths/formats")]
    save_scip: Option<SettingsPath>,
    #[arg(long,
        help = "rust-analyzer JSON config (features, target, cfg, etc.)")]
    config: Option<SettingsPath>,
    #[arg(short, long, num_args = 0..=1, require_equals = true, default_missing_value = "true",
        help = "Show diagnostic details and analysis timings")]
    verbose: Option<bool>,
}

#[derive(Parser)]
#[command(
    version,
    about = "Draw Rust folder dependencies, detect cycles, and check outgoing dependency limits",
    after_help = BOOLEAN_OPTIONS_DEFAULT_HELP
)]
pub(super) struct Cli {
    #[arg(default_value = ".",
        help = "Project/workspace directory or Cargo.toml; also selects automatic settings")]
    path: PathBuf,
    #[arg(long, value_name = "FILE", conflicts_with = "no_settings",
        help = "Read TOML settings instead of source-mesh.toml")]
    settings: Option<PathBuf>,
    #[arg(long,
        help = "Do not load TOML settings")]
    no_settings: bool,
    #[command(flatten)]
    options: Settings,
}

impl Cli {
    #[must_use]
    pub(super) const fn verbose(&self) -> bool {
        matches!(self.options.analysis.verbose, Some(true))
    }
}

impl Settings {
    fn parse(contents: &str) -> Result<Self> {
        let table = toml::de::DeTable::parse(contents)?;
        for (section, value) in table.get_ref() {
            let section = section.get_ref();
            ensure!(value.get_ref().as_table().is_some(), "Settings section {section:?} must be a table");
        }
        Ok(Self::deserialize(toml::de::Deserializer::from(table))?)
    }

    fn merge(self, mut cli: Cli, mut matches: ArgMatches) -> Result<Cli> {
        cli.options = self;
        cli.options.update_from_arg_matches_mut(&mut matches)?;
        Ok(cli)
    }
}

pub(super) struct LoadedSettings {
    cli: Cli,
    directory: PathBuf,
    protected_inputs: Vec<InputFile>,
}

impl LoadedSettings {
    pub(super) fn load(cli: Cli, matches: ArgMatches) -> Result<Self> {
        let mut loaded = Self { cli, directory: PathBuf::new(), protected_inputs: Vec::new() };
        if loaded.cli.no_settings {
            return Ok(loaded);
        }
        let explicit = loaded.cli.settings.is_some();
        let path = match loaded.cli.settings.as_ref() {
            Some(path) => path.clone(),
            None => project_directory(&loaded.cli.path)?.join(SETTINGS_FILE_NAME),
        };
        let mut input = match InputFile::open(&path) {
            Ok(input) => input,
            Err(error) => {
                if !explicit && error.downcast_ref::<io::Error>().is_some_and(|error| error.kind() == ErrorKind::NotFound) {
                    return Ok(loaded);
                }
                return Err(error).with_context(|| format!("Cannot load settings {}", path.display()));
            }
        };
        let contents = input.read()?;
        let contents = std::str::from_utf8(&contents).with_context(|| format!("Settings {} must be UTF-8", path.display()))?;
        let settings = Settings::parse(contents).map_err(|error| anyhow!("Cannot parse settings {}: {error}", path.display()))?;
        loaded.directory = path.parent().unwrap_or_else(|| Path::new(".")).to_path_buf();
        loaded.cli = settings.merge(loaded.cli, matches)?;
        loaded.protected_inputs.push(input);
        Ok(loaded)
    }

    #[must_use]
    pub(super) const fn verbose(&self) -> bool {
        self.cli.verbose()
    }

    #[must_use]
    pub(super) const fn has_settings(&self) -> bool {
        !self.protected_inputs.is_empty()
    }

    pub(super) fn into_command(self) -> Result<Command> {
        let Self { cli, directory, protected_inputs } = self;
        let Cli { path: project, options, settings: _, no_settings: _ } = cli;
        let Settings {
            graph: GraphSettings { root, depth, include_self, exclude_tests, exclude },
            export: ExportSettings { format, output, group_folders },
            cycles: CycleSettings { highlight_cycles, fail_on_cycles, cycles_output, file_cycles_output, cycle_limit },
            outgoing_dependencies: OutgoingDependencySettings { maximum, mode, output: outgoing_output },
            analysis: AnalysisSettings { from_scip, save_scip, config, verbose },
        } = options;
        ensure!(from_scip.is_none() || save_scip.is_none(), "--from-scip cannot be combined with --save-scip");
        ensure!(from_scip.is_none() || config.is_none(), "--from-scip cannot be combined with --config");
        ensure!(
            maximum.is_some() || (mode.is_none() && outgoing_output.is_none()),
            "Outgoing dependency settings require --max-outgoing-dependencies or [outgoing-dependencies].maximum"
        );
        let resolve = |path: SettingsPath| path.resolve(&directory);
        let from_scip = from_scip.map(resolve);
        let input = from_scip.map_or_else(|| Input::Project(Project::new(project, config.map(resolve))), Input::ScipFile);
        let verbose = verbose.unwrap_or(false);
        let cycles = CycleOptions::builder()
            .maybe_highlight_cycles(highlight_cycles).maybe_fail_on_cycles(fail_on_cycles).maybe_cycle_limit(cycle_limit)
            .maybe_cycles_output(cycles_output.map(resolve)).maybe_file_cycles_output(file_cycles_output.map(resolve)).build();
        let outgoing = maximum.map(|maximum| {
            OutgoingDependencyOptions::builder().maximum(maximum).maybe_mode(mode.map(Into::into))
                .maybe_output(outgoing_output.map(resolve)).build()
        });
        let request = Request::builder()
            .input(input).exclusions(Exclusions::new(&exclude)?).protected_inputs(protected_inputs)
            .analyzer(AnalyzerOptions::new(verbose)).maybe_root(root).maybe_depth(depth)
            .maybe_include_self(include_self).maybe_exclude_tests(exclude_tests)
            .maybe_format(format.map(Into::into)).maybe_output(output.map(resolve)).maybe_group_folders(group_folders)
            .cycles(cycles).maybe_outgoing_dependencies(outgoing).maybe_save_scip(save_scip.map(resolve)).build()?;
        Ok(Command { request, verbose })
    }
}

#[derive(CopyGetters)]
pub(super) struct Command {
    request: Request,
    #[getset(get_copy = "pub(super)")]
    verbose: bool,
}

impl Command {
    pub(super) fn run(&self, standard_output: &mut dyn Write) -> Result<()> {
        source_mesh_core::run(&self.request, standard_output)
    }
}

#[cfg(test)]
mod tests {
    use std::fs;
    use std::io;
    use std::iter;
    use std::num::NonZeroUsize;
    use std::path::Path;
    use std::path::PathBuf;

    use anyhow::Context as _;
    use anyhow::Error;
    use anyhow::Result;
    use assertables::assert_contains;
    use claims::assert_ok;
    use claims::assert_some;
    use clap::ArgMatches;
    use clap::Args as _;
    use clap::Command;
    use clap::CommandFactory as _;
    use clap::FromArgMatches as _;
    use clap::Parser as _;
    use clap::error::ErrorKind;
    use pretty_assertions::assert_eq;
    use rstest::rstest;
    use source_mesh_core::backend::project_directory;
    use tempfile::TempDir;
    use tempfile::tempdir;

    use super::AnalysisSettings;
    use super::Cli;
    use super::CycleSettings;
    use super::ExportSettings;
    use super::Format;
    use super::GraphSettings;
    use super::LoadedSettings;
    use super::OutgoingDependencyMode;
    use super::OutgoingDependencySettings;
    use super::SETTINGS_FILE_NAME;
    use super::Settings;
    use super::SettingsPath;
    use super::SettingsPath::CommandLine;
    use super::SettingsPath::SettingsFile;

    const CONTENTS_COMPLETE_SETTINGS_TEST_FIXTURE: &str = r#"
[graph]
root = "src"
depth = 3
include-self = true
exclude-tests = true
exclude = ["src/generated/**", "**/tests/**"]
[export]
format = "graphml"
output = "diagram.graphml"
group-folders = true
[cycles]
highlight-cycles = true
fail-on-cycles = true
cycles-output = "folders.md"
file-cycles-output = "files.md"
cycle-limit = 7
[outgoing-dependencies]
maximum = 9
mode = "files"
output = "outgoing.md"
[analysis]
from-scip = "input.scip"
save-scip = "saved.scip"
config = "analyzer.json"
verbose = true
"#;

    #[must_use]
    fn saved_index_path_test_fixture() -> PathBuf {
        PathBuf::from_iter([env!("CARGO_MANIFEST_DIR"), "..", "source-mesh-core", "src", "fixtures", "dependency-graph.scip"])
    }

    struct CliTestFixture {
        cli: Cli,
        matches: ArgMatches,
    }

    impl CliTestFixture {
        fn new(cli_arguments: &[&str]) -> Result<Self> {
            let arguments = iter::once(env!("CARGO_PKG_NAME")).chain(cli_arguments.iter().copied());
            let matches = Cli::command().try_get_matches_from(arguments)?;
            let cli = Cli::from_arg_matches(&matches)?;
            Ok(Self { cli, matches })
        }
    }

    struct SettingsMergeTestFixture {
        settings: Settings,
        cli: Cli,
        matches: ArgMatches,
    }

    impl SettingsMergeTestFixture {
        fn new(contents: &str, cli_arguments: &[&str]) -> Result<Self> {
            let settings = Settings::parse(contents).context("Settings merge test fixture must contain valid settings")?;
            let fixture = CliTestFixture::new(cli_arguments);
            let CliTestFixture { cli, matches } = fixture.context("Settings merge test fixture arguments must parse")?;
            Ok(Self { settings, cli, matches })
        }
    }

    struct SettingsDirectoryTestFixture {
        directory: TempDir,
        settings_path: PathBuf,
    }

    impl SettingsDirectoryTestFixture {
        fn new(contents: Option<&[u8]>) -> Result<Self> {
            let directory = tempdir()?;
            let settings_path = directory.path().join(SETTINGS_FILE_NAME);
            if let Some(contents) = contents {
                fs::write(&settings_path, contents)?;
            }
            Ok(Self { directory, settings_path })
        }
    }

    struct SettingsLoadingTestFixture {
        directory: TempDir,
        settings_path: PathBuf,
        cli: Cli,
        matches: ArgMatches,
    }

    impl SettingsLoadingTestFixture {
        fn new(settings_file_contents: Option<&[u8]>, cli_arguments: &[&str]) -> Result<Self> {
            let files = SettingsDirectoryTestFixture::new(settings_file_contents);
            let files = files.context("Settings loading test files must be prepared")?;
            let fixture = CliTestFixture::new(cli_arguments);
            let CliTestFixture { mut cli, matches } = fixture.context("Settings loading test fixture arguments must parse")?;
            cli.path = files.directory.path().to_path_buf();
            Ok(Self { directory: files.directory, settings_path: files.settings_path, cli, matches })
        }
    }

    #[track_caller]
    fn assert_missing_file_error(error: &Error, expected_path: &Path) {
        let cause = error.downcast_ref::<io::Error>();
        let cause = assert_some!(cause, "Missing-file errors must retain their I/O cause: {error:#}");
        let diagnostic = error.to_string();
        let path = expected_path.display().to_string();
        assert_eq!(cause.kind(), io::ErrorKind::NotFound, "Missing-file errors must retain their I/O kind");
        assert_contains!(diagnostic, &path, "Missing-file errors must identify {path}: {diagnostic}");
    }

    #[test]
    fn test_settings_arguments_have_no_defaults() {
        let mut system_under_test = Settings::augment_args(Command::new(env!("CARGO_PKG_NAME")));
        system_under_test.build();

        for argument in system_under_test.get_arguments() {
            assert!(argument.get_default_values().is_empty(),
                "Settings argument {} must not override file values when absent", argument.get_id());
        }
    }

    #[rstest]
    #[case(&["--depth=-1"])]
    #[case(&["--depth", "not-a-number"])]
    #[case(&["--format", "unknown"])]
    #[case(&["--unknown-option"])]
    #[case(&["--cycle-limit", "0"])]
    #[case(&["--max-outgoing-dependencies=-1"])]
    #[case(&["--max-outgoing-dependencies", "not-a-number"])]
    #[case(&["--max-outgoing-dependencies", "1.5"])]
    #[case(&["--max-outgoing-dependencies", "18446744073709551616"])]
    #[case(&["--max-outgoing-dependencies"])]
    #[case(&["--max-outgoing-dependencies", "1", "--outgoing-dependencies-mode", "unknown"])]
    fn test_invalid_arguments(#[case] arguments: &[&str]) {
        let replay_arguments = [env!("CARGO_PKG_NAME"), "--from-scip", "input.scip"];
        let system_under_test = Cli::try_parse_from(replay_arguments.into_iter().chain(arguments.iter().copied()));

        let error = system_under_test.err();
        let error = assert_some!(error, "CLI accepted invalid or conflicting arguments");
        let diagnostic = error.to_string();
        assert_ne!(error.kind(), ErrorKind::DisplayHelp, "CLI displayed help instead of rejecting arguments");
        assert!(!diagnostic.is_empty(), "CLI omitted invalid-argument diagnostics");
    }

    #[rstest]
    #[case::disabled(
        &[],
        None, None, false,
    )]
    #[case::zero(
        &["--max-outgoing-dependencies", "0"],
        Some(0), None, false,
    )]
    #[case::files(
        &["--max-outgoing-dependencies", "5", "--outgoing-dependencies-mode", "files"],
        Some(5), Some(OutgoingDependencyMode::Files), false,
    )]
    #[case::folders(
        &["--max-outgoing-dependencies", "7", "--outgoing-dependencies-mode", "folders"],
        Some(7), Some(OutgoingDependencyMode::Folders), false,
    )]
    #[case::report(
        &["--max-outgoing-dependencies", "3", "--outgoing-dependencies-output", "dependencies.md"],
        Some(3), None, true,
    )]
    fn test_outgoing_dependency_arguments(
        #[case] arguments: &[&str],
        #[case] maximum: Option<usize>,
        #[case] mode: Option<OutgoingDependencyMode>,
        #[case] report: bool,
    ) {
        let system_under_test = Cli::try_parse_from(iter::once(env!("CARGO_PKG_NAME")).chain(arguments.iter().copied()));
        let system_under_test = assert_ok!(system_under_test, "CLI must accept outgoing dependency arguments: {arguments:?}");
        let expected = OutgoingDependencySettings {
            maximum, mode, output: report.then(|| CommandLine("dependencies.md".into())),
        };

        assert_eq!(system_under_test.options.outgoing_dependencies, expected);
    }

    #[rstest]
    #[case(&["--analyzer-threads", "0"])]
    #[case(&["--analysis-mode", "sequential"])]
    fn test_removed_analyzer_arguments(#[case] arguments: &[&str]) {
        let system_under_test = Cli::try_parse_from(iter::once(env!("CARGO_PKG_NAME")).chain(arguments.iter().copied()));

        let error = system_under_test.err();
        let error = assert_some!(error, "CLI accepted a removed analyzer option");
        assert_eq!(error.kind(), ErrorKind::UnknownArgument, "Removed analyzer options must be unknown arguments");
    }

    #[rstest]
    #[case::default(&[], false)]
    #[case::verbose(&["--verbose"], true)]
    #[case::short(&["-v"], true)]
    fn test_verbose_argument(#[case] arguments: &[&str], #[case] enabled: bool) {
        let system_under_test = Cli::try_parse_from(iter::once(env!("CARGO_PKG_NAME")).chain(arguments.iter().copied()));
        let system_under_test = assert_ok!(system_under_test, "CLI must accept supported verbosity arguments: {arguments:?}");

        let verbose = system_under_test.verbose();

        assert_eq!(verbose, enabled, "CLI verbosity must follow the supplied arguments");
    }

    #[test]
    fn test_cli_numeric_range_survives_merge() {
        let contents = "[graph]\ndepth = 1\n[cycles]\ncycle-limit = 1\n[outgoing-dependencies]\nmaximum = 1";
        let maximum = usize::MAX.to_string();
        let saved_index_path = saved_index_path_test_fixture().display().to_string();
        let fixture = SettingsMergeTestFixture::new(contents, &[
            "--from-scip", &saved_index_path, "--depth", &maximum, "--cycle-limit", &maximum,
            "--max-outgoing-dependencies", &maximum,
        ]);
        let fixture = assert_ok!(fixture, "CLI numbers must support the complete native usize range");

        let system_under_test = fixture.settings.merge(fixture.cli, fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Native CLI numbers must merge without narrowing their range");

        let options = &system_under_test.options;
        assert_eq!(options.graph.depth, Some(usize::MAX));
        assert_eq!(options.cycles.cycle_limit, Some(NonZeroUsize::MAX));
        assert_eq!(options.outgoing_dependencies.maximum, Some(usize::MAX));
        let loaded = LoadedSettings { cli: system_under_test, directory: PathBuf::new(), protected_inputs: Vec::new() };
        let command = loaded.into_command();
        assert_ok!(command, "Builder projection must not constrain CLI numbers to TOML's signed integer range");
    }

    #[cfg(unix)]
    fn non_utf8_path_test_fixture() -> PathBuf {
        use std::ffi::OsString;
        use std::os::unix::ffi::OsStringExt as _;

        PathBuf::from(OsString::from_vec(b"native-\xff".to_vec()))
    }

    #[cfg(windows)]
    fn non_utf8_path_test_fixture() -> PathBuf {
        use std::ffi::OsString;
        use std::os::windows::ffi::OsStringExt as _;

        PathBuf::from(OsString::from_wide(&[0x006e, 0xd800]))
    }

    #[cfg(any(unix, windows))]
    #[rstest]
    #[case::native_input(true)]
    #[case::native_output(false)]
    fn test_native_cli_paths_survive_merge_and_projection(#[case] native_input: bool) {
        use std::ffi::OsStr;

        let contents = b"[export]\noutput = 'file.dot'\n[analysis]\nfrom-scip = 'file.scip'";
        let fixture = SettingsDirectoryTestFixture::new(Some(contents));
        let fixture = assert_ok!(fixture, "Native path settings must be written");
        let input_path = if native_input {
            fixture.directory.path().join(non_utf8_path_test_fixture()).with_extension("scip")
        } else {
            saved_index_path_test_fixture()
        };
        let output_path = fixture.directory.path().join(non_utf8_path_test_fixture()).with_extension("dot");
        assert!(output_path.to_str().is_none(), "The fixture must exercise a genuinely non-UTF-8 output path");
        assert_eq!(input_path.to_str().is_none(), native_input, "Only the native-input case may use a non-UTF-8 input path");
        let arguments = [
            OsStr::new(env!("CARGO_PKG_NAME")), OsStr::new("--settings"), fixture.settings_path.as_os_str(),
            OsStr::new("--from-scip"), input_path.as_os_str(), OsStr::new("--output"), output_path.as_os_str(),
        ];
        let matches = Cli::command().try_get_matches_from(arguments);
        let matches = assert_ok!(matches, "Native path arguments must parse without UTF-8 conversion");
        let cli = Cli::from_arg_matches(&matches);
        let cli = assert_ok!(cli, "Native path matches must convert to CLI options without UTF-8 conversion");
        let system_under_test = LoadedSettings::load(cli, matches);
        let system_under_test = assert_ok!(system_under_test, "Native paths must override the file layer without serialization");

        let options = &system_under_test.cli.options;
        let paths = [options.analysis.from_scip.clone(), options.export.output.clone()];
        let paths = paths.map(|path| path.map(|path| path.resolve(&system_under_test.directory)));
        let expected = [Some(input_path), Some(output_path)];
        assert_eq!(paths, expected);
        let command = system_under_test.into_command();
        if native_input {
            let error = command.err();
            let error = assert_some!(error, "An absent native input path must fail during request construction");
            assert!(error.is::<io::Error>(), "Native input paths must reach file I/O without UTF-8 conversion: {error:#}");
        } else {
            assert_ok!(command, "Native output paths must survive builder projection without serialization or output I/O");
        }
    }

    #[rstest]
    #[case::defaults(false)]
    #[case::partial_overrides(true)]
    fn test_cli_settings_merge(#[case] overridden: bool) {
        let mut expected = Settings {
            graph: GraphSettings {
                root: Some("src".into()), depth: Some(3), include_self: Some(true), exclude_tests: Some(true),
                exclude: vec!["src/generated/**".into(), "**/tests/**".into()],
            },
            export: ExportSettings {
                format: Some(Format::Graphml), output: Some(SettingsFile("diagram.graphml".into())), group_folders: Some(true),
            },
            cycles: CycleSettings {
                highlight_cycles: Some(true), fail_on_cycles: Some(true), cycle_limit: NonZeroUsize::new(7),
                cycles_output: Some(SettingsFile("folders.md".into())),
                file_cycles_output: Some(SettingsFile("files.md".into())),
            },
            outgoing_dependencies: OutgoingDependencySettings {
                maximum: Some(9), mode: Some(OutgoingDependencyMode::Files), output: Some(SettingsFile("outgoing.md".into())),
            },
            analysis: AnalysisSettings {
                from_scip: Some(SettingsFile("input.scip".into())), save_scip: Some(SettingsFile("saved.scip".into())),
                config: Some(SettingsFile("analyzer.json".into())), verbose: Some(true),
            },
        };
        let arguments: &[&str] = if overridden {
            expected.graph.depth = Some(0);
            expected.export.output = Some(CommandLine("cli.dot".into()));
            expected.cycles.fail_on_cycles = Some(false);
            expected.outgoing_dependencies.mode = Some(OutgoingDependencyMode::Folders);
            expected.analysis.verbose = Some(false);
            &[
                "--depth", "0", "--output", "cli.dot", "--fail-on-cycles=false",
                "--outgoing-dependencies-mode", "folders", "--verbose=false",
            ]
        } else { &[] };
        let fixture = SettingsMergeTestFixture::new(CONTENTS_COMPLETE_SETTINGS_TEST_FIXTURE, arguments);
        let fixture = assert_ok!(fixture, "Complete settings and CLI arguments must parse before merging");

        let system_under_test = fixture.settings.merge(fixture.cli, fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "CLI options must merge independently per field");

        assert_eq!(system_under_test.path, Path::new("."), "Settings must not change the selected project");
        assert_eq!(system_under_test.options, expected, "Only explicitly supplied fields may override file settings");
    }

    #[test]
    fn test_explicit_cli_values_override_settings() {
        let fixture = SettingsMergeTestFixture::new(CONTENTS_COMPLETE_SETTINGS_TEST_FIXTURE, &[
            "--root", ".", "--depth", "0", "--include-self=false", "--exclude-tests=false",
            "--exclude", "cli/**", "--exclude", "**/*.generated.rs", "--format", "dot", "--output", "cli.dot",
            "--group-folders=false", "--highlight-cycles=false", "--fail-on-cycles=false", "--cycle-limit", "1",
            "--cycles-output", "cli-folders.md", "--file-cycles-output", "cli-files.md",
            "--max-outgoing-dependencies", "0", "--outgoing-dependencies-mode", "folders",
            "--outgoing-dependencies-output", "cli-outgoing.md", "--from-scip", "cli-input.scip",
            "--save-scip", "cli-saved.scip", "--config", "cli-analyzer.json", "--verbose=false",
        ]);
        let fixture = assert_ok!(fixture, "Explicit CLI values must parse before cross-field validation");

        let system_under_test = fixture.settings.merge(fixture.cli, fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Explicit CLI values must replace their file settings");

        let expected = Settings {
            graph: GraphSettings {
                root: Some(".".into()), depth: Some(0), include_self: Some(false), exclude_tests: Some(false),
                exclude: vec!["cli/**".into(), "**/*.generated.rs".into()],
            },
            export: ExportSettings {
                format: Some(Format::Dot), output: Some(CommandLine("cli.dot".into())), group_folders: Some(false),
            },
            cycles: CycleSettings {
                highlight_cycles: Some(false), fail_on_cycles: Some(false), cycle_limit: NonZeroUsize::new(1),
                cycles_output: Some(CommandLine("cli-folders.md".into())),
                file_cycles_output: Some(CommandLine("cli-files.md".into())),
            },
            outgoing_dependencies: OutgoingDependencySettings {
                maximum: Some(0), mode: Some(OutgoingDependencyMode::Folders),
                output: Some(CommandLine("cli-outgoing.md".into())),
            },
            analysis: AnalysisSettings {
                from_scip: Some(CommandLine("cli-input.scip".into())), save_scip: Some(CommandLine("cli-saved.scip".into())),
                config: Some(CommandLine("cli-analyzer.json".into())), verbose: Some(false),
            },
        };
        assert_eq!(system_under_test.options, expected, "Explicit CLI values, including false and zero, must override TOML");
    }

    #[rstest]
    #[case::absent(None, None)]
    #[case::bare(Some(""), Some(true))]
    #[case::true_value(Some("true"), Some(true))]
    #[case::false_value(Some("false"), Some(false))]
    fn test_boolean_setting_precedence(
        #[case] value: Option<&str>,
        #[case] explicit: Option<bool>,
        #[values(false, true)] configured: bool,
    ) {
        let contents = format!("
[graph]
include-self = {configured}
exclude-tests = {configured}
[export]
group-folders = {configured}
[cycles]
highlight-cycles = {configured}
fail-on-cycles = {configured}
[analysis]
verbose = {configured}
");
        let flags = [
            "--include-self", "--exclude-tests", "--group-folders", "--highlight-cycles", "--fail-on-cycles", "--verbose",
        ];
        let arguments: Vec<_> = value.into_iter().flat_map(|value| {
            flags.map(|flag| if value.is_empty() { flag.to_owned() } else { format!("{flag}={value}") })
        }).collect();
        let arguments: Vec<_> = arguments.iter().map(String::as_str).collect();
        let fixture = SettingsMergeTestFixture::new(&contents, &arguments);
        let fixture = assert_ok!(fixture, "Boolean settings and CLI flags must parse before merging");

        let fail_on_cycles = fixture.cli.options.cycles.fail_on_cycles;
        assert_eq!(fail_on_cycles, explicit, "CLI cycle-failure flags must preserve the supplied value");

        let system_under_test = fixture.settings.merge(fixture.cli, fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Boolean precedence must follow explicit CLI values");

        let enabled = Some(explicit.unwrap_or(configured));
        let expected = Settings {
            graph: GraphSettings { include_self: enabled, exclude_tests: enabled, ..GraphSettings::default() },
            export: ExportSettings { group_folders: enabled, ..ExportSettings::default() },
            cycles: CycleSettings { highlight_cycles: enabled, fail_on_cycles: enabled, ..CycleSettings::default() },
            analysis: AnalysisSettings { verbose: enabled, ..AnalysisSettings::default() },
            ..Settings::default()
        };
        assert_eq!(system_under_test.options, expected, "Boolean precedence must be consistent across all settings");
    }

    #[rstest]
    #[case::single(&["--exclude", "cli/**"], &["cli/**"])]
    #[case::duplicates(&["--exclude", "cli/**", "--exclude", "cli/**"], &["cli/**", "cli/**"])]
    fn test_exclusion_list_replacement(#[case] arguments: &[&str], #[case] expected: &[&str]) {
        let fixture = SettingsMergeTestFixture::new("[graph]\nexclude = ['file/**', '**/tests/**']", arguments);
        let fixture = assert_ok!(fixture, "Settings and CLI exclusion lists must parse before merging");

        let system_under_test = fixture.settings.merge(fixture.cli, fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Explicit CLI exclusions must replace the file list");

        let exclusions = system_under_test.options.graph.exclude;
        assert_eq!(exclusions, expected, "CLI exclusions must replace, not append to, the file list");
    }

    #[rstest]
    #[case::top_level("unknown = true")]
    #[case::unknown_section("[unknown]\nvalue = true")]
    #[case::graph_key("[graph]\nunknown = true")]
    #[case::export_key("[export]\nunknown = true")]
    #[case::cycles_key("[cycles]\nunknown = true")]
    #[case::outgoing_key("[outgoing-dependencies]\nunknown = true")]
    #[case::analysis_key("[analysis]\nunknown = true")]
    #[case::underscore_key("[graph]\ninclude_self = true")]
    #[case::outgoing_cli_key("[outgoing-dependencies]\nmax-outgoing-dependencies = 1")]
    #[case::graph_type("graph = false")]
    #[case::graph_array("graph = []")]
    #[case::graph_positional("graph = ['src']")]
    #[case::export_type("export = []")]
    #[case::export_positional("export = ['dot']")]
    #[case::cycles_type("cycles = 1")]
    #[case::cycles_array("cycles = []")]
    #[case::cycles_positional("cycles = [true]")]
    #[case::outgoing_type("outgoing-dependencies = 'files'")]
    #[case::outgoing_array("outgoing-dependencies = []")]
    #[case::outgoing_positional("outgoing-dependencies = [0]")]
    #[case::analysis_type("analysis = true")]
    #[case::analysis_array("analysis = []")]
    #[case::analysis_positional("analysis = ['saved.scip']")]
    #[case::root_type("[graph]\nroot = 1")]
    #[case::root_date("[graph]\nroot = 2026-11-04")]
    #[case::output_datetime("[export]\noutput = 2026-11-04T12:30:00Z")]
    #[case::exclude_time("[graph]\nexclude = [12:30:00]")]
    #[case::format_table("[export]\nformat = { dot = {} }")]
    #[case::mode_table("[outgoing-dependencies]\nmode = { files = [] }")]
    #[case::depth_type("[graph]\ndepth = '3'")]
    #[case::depth_fraction("[graph]\ndepth = 1.5")]
    #[case::depth_negative("[graph]\ndepth = -1")]
    #[case::include_self_type("[graph]\ninclude-self = 'true'")]
    #[case::exclude_tests_type("[graph]\nexclude-tests = 1")]
    #[case::exclude_type("[graph]\nexclude = 'src/**'")]
    #[case::exclude_element_type("[graph]\nexclude = ['src/**', 1]")]
    #[case::format_type("[export]\nformat = false")]
    #[case::format_variant("[export]\nformat = 'unknown'")]
    #[case::format_case("[export]\nformat = 'DOT'")]
    #[case::output_type("[export]\noutput = false")]
    #[case::group_folders_type("[export]\ngroup-folders = 'false'")]
    #[case::highlight_type("[cycles]\nhighlight-cycles = 1")]
    #[case::fail_on_cycles_type("[cycles]\nfail-on-cycles = 'true'")]
    #[case::cycles_output_type("[cycles]\ncycles-output = []")]
    #[case::file_cycles_output_type("[cycles]\nfile-cycles-output = 1")]
    #[case::cycle_limit_type("[cycles]\ncycle-limit = '10'")]
    #[case::cycle_limit_fraction("[cycles]\ncycle-limit = 1.5")]
    #[case::cycle_limit_negative("[cycles]\ncycle-limit = -1")]
    #[case::cycle_limit_zero("[cycles]\ncycle-limit = 0")]
    #[case::maximum_type("[outgoing-dependencies]\nmaximum = '0'")]
    #[case::maximum_fraction("[outgoing-dependencies]\nmaximum = 1.5")]
    #[case::maximum_negative("[outgoing-dependencies]\nmaximum = -1")]
    #[case::maximum_overflow("[outgoing-dependencies]\nmaximum = 18446744073709551616")]
    #[case::mode_type("[outgoing-dependencies]\nmode = 1")]
    #[case::mode_variant("[outgoing-dependencies]\nmode = 'unknown'")]
    #[case::mode_case("[outgoing-dependencies]\nmode = 'FILES'")]
    #[case::outgoing_output_type("[outgoing-dependencies]\noutput = false")]
    #[case::from_scip_type("[analysis]\nfrom-scip = []")]
    #[case::save_scip_type("[analysis]\nsave-scip = 1")]
    #[case::config_type("[analysis]\nconfig = true")]
    #[case::verbose_type("[analysis]\nverbose = 'true'")]
    #[case::duplicate("[graph]\ndepth = 1\ndepth = 2")]
    #[case::syntax("[graph")]
    fn test_settings_validation(#[case] contents: &str) {
        let fixture = SettingsLoadingTestFixture::new(Some(contents.as_bytes()), &[]);
        let mut fixture = assert_ok!(fixture, "Invalid settings fixture must be prepared successfully");
        fixture.cli.settings = Some(fixture.settings_path.clone());

        let system_under_test = LoadedSettings::load(fixture.cli, fixture.matches).err();
        let error = assert_some!(system_under_test, "Malformed settings were accepted: {contents}");
        let cause = error.downcast_ref::<io::Error>();
        let diagnostic = error.to_string();
        let file_name = fixture.settings_path.display().to_string();

        assert!(cause.is_none(), "Validation failed for an unrelated filesystem reason: {error:#}");
        assert_contains!(diagnostic, &file_name, "Settings errors must identify their file: {diagnostic}");
    }

    #[rstest]
    #[case::number("[graph]\ndepth = 'invalid'", &["--depth", "0"])]
    #[case::boolean("[analysis]\nverbose = 'invalid'", &["--verbose=false"])]
    #[case::choice("[export]\nformat = 'invalid'", &["--format", "dot"])]
    #[case::list("[graph]\nexclude = 1", &["--exclude", "src/**"])]
    #[case::path_date("[export]\noutput = 2026-11-04", &["--output", "cli.dot"])]
    #[case::list_date("[graph]\nexclude = [2026-11-04]", &["--exclude", "cli/**"])]
    #[case::choice_table("[export]\nformat = { dot = {} }", &["--format", "dot"])]
    fn test_cli_overrides_do_not_hide_invalid_settings(#[case] contents: &str, #[case] arguments: &[&str]) {
        let fixture = SettingsLoadingTestFixture::new(Some(contents.as_bytes()), arguments);
        let mut fixture = assert_ok!(fixture, "Invalid settings and valid CLI overrides must be prepared");
        fixture.cli.settings = Some(fixture.settings_path);

        let system_under_test = LoadedSettings::load(fixture.cli, fixture.matches).err();
        let error = assert_some!(system_under_test, "An override must not conceal an invalid lower-priority value");
        let diagnostic = error.to_string();

        assert_contains!(diagnostic, "Cannot parse settings", "Unexpected loading failure: {error:#}");
    }

    #[test]
    fn test_empty_settings_sections() {
        let contents = "[graph]\n[export]\n[cycles]\n[outgoing-dependencies]\n[analysis]";
        let fixture = SettingsMergeTestFixture::new(contents, &[]);
        let fixture = assert_ok!(fixture, "Empty settings sections and default CLI arguments must parse");

        let system_under_test = fixture.settings.merge(fixture.cli, fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Empty sections must not introduce settings values");

        let expected = Settings::default();
        assert_eq!(system_under_test.options, expected, "Empty sections must not introduce any explicit settings");
    }

    #[rstest]
    #[case::directory(false)]
    #[case::manifest(true)]
    fn test_automatic_discovery_uses_selected_project(#[case] manifest: bool) {
        let fixture = SettingsDirectoryTestFixture::new(Some(b"[graph]\ndepth = 99"));
        let fixture = assert_ok!(fixture, "An ancestor settings file must be created");
        let selected = fixture.directory.path().join("selected");
        let created = fs::create_dir_all(&selected);
        assert_ok!(created, "The selected project directory must be created");
        let contents = "[graph]\ndepth = 2\n[export]\nformat = 'mermaid'\noutput = 'selected.md'";
        let settings_write = fs::write(selected.join(SETTINGS_FILE_NAME), contents);
        assert_ok!(settings_write, "The selected project's settings must be written");
        let project = if manifest {
            let project = selected.join("Cargo.toml");
            let manifest_write = fs::write(&project, "");
            assert_ok!(manifest_write, "An explicit manifest must exist before project-directory discovery");
            project
        } else {
            selected.clone()
        };
        let directory = project_directory(&selected);
        let directory = assert_ok!(directory, "The selected project must have a canonical directory");
        let cli_fixture = CliTestFixture::new(&["--from-scip", "saved.scip"]);
        let mut cli_fixture = assert_ok!(cli_fixture, "Replay arguments must parse independently of project discovery");
        cli_fixture.cli.path = project.clone();

        let system_under_test = LoadedSettings::load(cli_fixture.cli, cli_fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Selected project or manifest parent settings must load");

        let options = system_under_test.cli.options;
        let paths = [options.export.output, options.analysis.from_scip];
        let paths = paths.map(|path| path.map(|path| path.resolve(&system_under_test.directory)));
        let expected = [Some(directory.join("selected.md")), Some(PathBuf::from("saved.scip"))];
        assert_eq!(system_under_test.cli.path, project, "Discovery must not replace the CLI project selection");
        assert_eq!(options.graph.depth, Some(2), "Discovery read the ancestor or working-directory settings");
        assert_eq!(options.export.format, Some(Format::Mermaid), "Selected project settings did not load");
        assert_eq!(paths, expected, "Only paths from settings must resolve relative to the settings directory");
        assert_eq!(system_under_test.protected_inputs.len(), 1, "The discovered settings input must remain protected");
    }

    #[test]
    fn test_absent_automatic_settings_do_not_search_ancestors() {
        let fixture = SettingsDirectoryTestFixture::new(Some(b"this is malformed TOML"));
        let fixture = assert_ok!(fixture, "A malformed ancestor settings file must be written");
        let selected = fixture.directory.path().join("selected");
        let created = fs::create_dir_all(&selected);
        assert_ok!(created, "The selected directory without settings must be created");
        let cli_fixture = CliTestFixture::new(&["--from-scip", "saved.scip", "--depth", "0"]);
        let mut cli_fixture = assert_ok!(cli_fixture, "Replay with explicit CLI settings must parse");
        cli_fixture.cli.path = selected;

        let system_under_test = LoadedSettings::load(cli_fixture.cli, cli_fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Absent automatic settings must not read ancestor files");

        let options = system_under_test.cli.options;
        assert_eq!(options.graph.depth, Some(0));
        assert!(options.graph.root.is_none(), "Absent settings must not manufacture an explicit root");
        assert!(options.export.format.is_none(), "Absent settings must leave format defaulting to the command boundary");
        assert!(system_under_test.protected_inputs.is_empty(), "Absent settings must not introduce a protected input");
    }

    #[rstest]
    fn test_explicit_settings_override_discovery(#[values(false, true)] missing_project: bool) {
        let fixture = SettingsDirectoryTestFixture::new(Some(b"malformed automatic settings"));
        let fixture = assert_ok!(fixture, "Malformed automatic settings must exist before explicit selection");
        let directory = fixture.directory.path().join("configuration");
        let created = fs::create_dir_all(&directory);
        assert_ok!(created, "The explicitly selected settings directory must be created");
        let path = directory.join("custom.toml");
        let written = fs::write(&path, "[graph]\ndepth = 4\n[export]\noutput = 'custom.dot'");
        assert_ok!(written, "Explicit settings must be written separately from the automatic file");
        let cli_fixture = CliTestFixture::new(&[]);
        let mut cli_fixture = assert_ok!(cli_fixture, "Explicit settings selection must start with valid arguments");
        let project = fixture.directory.path();
        cli_fixture.cli.path = if missing_project { project.join("missing") } else { project.to_path_buf() };
        cli_fixture.cli.settings = Some(path);

        let system_under_test = LoadedSettings::load(cli_fixture.cli, cli_fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Explicit settings must bypass automatic discovery");

        let options = system_under_test.cli.options;
        let output = options.export.output.map(|file_path| file_path.resolve(&system_under_test.directory));
        let expected = Some(directory.join("custom.dot"));
        assert_eq!(options.graph.depth, Some(4), "Explicit settings were replaced by automatic configuration");
        assert_eq!(output, expected, "Explicit paths used the project directory");
        assert_eq!(system_under_test.protected_inputs.len(), 1, "Only the explicitly loaded settings file should be protected");
    }

    enum SettingsPathTestSource {
        RelativeFile,
        AbsoluteFile,
        CommandLine,
    }

    #[rstest]
    #[case::diagram(
        "export", "output", "--output",
        |cli: Cli| cli.options.export.output,
    )]
    #[case::folder_cycles(
        "cycles", "cycles-output", "--cycles-output",
        |cli: Cli| cli.options.cycles.cycles_output,
    )]
    #[case::file_cycles(
        "cycles", "file-cycles-output", "--file-cycles-output",
        |cli: Cli| cli.options.cycles.file_cycles_output,
    )]
    #[case::outgoing(
        "outgoing-dependencies", "output", "--outgoing-dependencies-output",
        |cli: Cli| cli.options.outgoing_dependencies.output,
    )]
    #[case::replay(
        "analysis", "from-scip", "--from-scip",
        |cli: Cli| cli.options.analysis.from_scip,
    )]
    #[case::saved_index(
        "analysis", "save-scip", "--save-scip",
        |cli: Cli| cli.options.analysis.save_scip,
    )]
    #[case::analyzer(
        "analysis", "config", "--config",
        |cli: Cli| cli.options.analysis.config,
    )]
    fn test_file_path_resolution_and_cli_precedence(
        #[case] section: &str,
        #[case] key: &str,
        #[case] argument: &str,
        #[case] selected_path: fn(Cli) -> Option<SettingsPath>,
        #[values(SettingsPathTestSource::RelativeFile, SettingsPathTestSource::AbsoluteFile, SettingsPathTestSource::CommandLine)]
        source: SettingsPathTestSource,
    ) {
        let fixture = SettingsDirectoryTestFixture::new(None);
        let fixture = assert_ok!(fixture, "File-path settings fixture directory must be created");
        let configured = match source {
            SettingsPathTestSource::AbsoluteFile => fixture.directory.path().join("absolute-file"),
            SettingsPathTestSource::RelativeFile | SettingsPathTestSource::CommandLine => {
                Path::new("nested").join("from-settings")
            }
        };
        let encoded = serde_json::to_string(&configured);
        let encoded = assert_ok!(encoded, "Fixture paths must be representable as escaped TOML strings");
        let contents = format!("[graph]\nroot = 'src'\nexclude = ['src/generated/**']\n[{section}]\n{key} = {encoded}");
        let written = fs::write(&fixture.settings_path, contents);
        assert_ok!(written, "File-path settings must be written before resolution");
        let arguments: &[&str] = match source {
            SettingsPathTestSource::CommandLine => &[argument, "from-cli"],
            SettingsPathTestSource::RelativeFile | SettingsPathTestSource::AbsoluteFile => &[],
        };
        let cli_fixture = CliTestFixture::new(arguments);
        let mut cli_fixture = assert_ok!(cli_fixture, "CLI file-path overrides must parse");
        cli_fixture.cli.path = fixture.directory.path().join("different-project");
        cli_fixture.cli.settings = Some(fixture.settings_path);

        let system_under_test = LoadedSettings::load(cli_fixture.cli, cli_fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Path resolution must not require referenced files to exist");

        let expected = match source {
            SettingsPathTestSource::RelativeFile => fixture.directory.path().join("nested").join("from-settings"),
            SettingsPathTestSource::AbsoluteFile => fixture.directory.path().join("absolute-file"),
            SettingsPathTestSource::CommandLine => PathBuf::from("from-cli"),
        };
        let graph = &system_under_test.cli.options.graph;
        let expected_graph = GraphSettings {
            root: Some("src".into()), exclude: vec!["src/generated/**".into()], ..GraphSettings::default()
        };
        assert_eq!(*graph, expected_graph, "Graph roots and exclusion globs must remain index-relative");
        let configured_path = selected_path(system_under_test.cli);
        let resolved_path = configured_path.map(|file_path| file_path.resolve(&system_under_test.directory));
        assert_eq!(resolved_path, Some(expected), "Wrong base for {section}.{key}");
    }

    #[rstest]
    #[case::missing_file(None, false)]
    #[case::malformed_file(Some(b"[graph".as_slice()), false)]
    #[case::non_utf8_file(Some(b"\xff".as_slice()), false)]
    #[case::missing_project(None, true)]
    fn test_no_settings_bypasses_loading(#[case] contents: Option<&[u8]>, #[case] missing_project: bool) {
        let arguments = ["--no-settings", "--depth", "0", "--format", "dot", "--from-scip", "saved.scip"];
        let fixture = SettingsLoadingTestFixture::new(contents, &arguments);
        let mut fixture = assert_ok!(fixture, "Settings bypass test fixture must be prepared");
        if missing_project {
            fixture.cli.path = fixture.directory.path().join("missing");
        }

        let system_under_test = LoadedSettings::load(fixture.cli, fixture.matches);
        let system_under_test = assert_ok!(system_under_test, "Bypass must skip settings discovery and loading");

        let options = system_under_test.cli.options;
        assert_eq!(options.graph.depth, Some(0), "Settings bypass lost explicit CLI values");
        assert_eq!(options.export.format, Some(Format::Dot));
        let from_scip = options.analysis.from_scip.map(|path| path.resolve(&system_under_test.directory));
        assert_eq!(from_scip.as_deref(), Some(Path::new("saved.scip")));
        assert!(system_under_test.protected_inputs.is_empty(), "Bypassed settings must not be retained as protected inputs");
    }

    #[rstest]
    fn test_missing_settings_file(#[values(false, true)] explicit: bool) {
        let fixture = SettingsLoadingTestFixture::new(None, &[]);
        let mut fixture = assert_ok!(fixture, "Missing-settings test fixture must be prepared");
        fixture.cli.settings = explicit.then(|| fixture.settings_path.clone());

        let system_under_test = LoadedSettings::load(fixture.cli, fixture.matches);

        if explicit {
            let error = system_under_test.err();
            let error = assert_some!(error, "Explicitly selected missing settings must fail");
            assert_missing_file_error(&error, &fixture.settings_path);
        } else {
            let loaded = assert_ok!(system_under_test, "Absent automatic settings must be optional");
            let expected = Settings::default();
            assert_eq!(loaded.cli.options, expected, "Absence changed CLI defaults");
            assert!(loaded.protected_inputs.is_empty(), "Missing automatic settings must not retain an input");
        }
    }

    #[rstest]
    #[case::automatic_directory(None, false)]
    #[case::explicit_directory(None, true)]
    #[case::automatic_malformed(Some(b"[graph".as_slice()), false)]
    #[case::automatic_non_utf8(Some(b"\xff".as_slice()), false)]
    #[case::explicit_non_utf8(Some(b"\xff".as_slice()), true)]
    fn test_settings_loading_errors(#[case] contents: Option<&[u8]>, #[case] explicit: bool) {
        let fixture = SettingsLoadingTestFixture::new(contents, &[]);
        let mut fixture = assert_ok!(fixture, "Settings loading-error test fixture must be prepared");
        if contents.is_none() {
            let created = fs::create_dir_all(&fixture.settings_path);
            assert_ok!(created, "Unreadable settings fixture must place a directory at the settings path");
        }
        fixture.cli.settings = explicit.then(|| fixture.settings_path.clone());

        let system_under_test = LoadedSettings::load(fixture.cli, fixture.matches).err();
        let error = assert_some!(system_under_test, "Malformed or unreadable settings must not be treated as absent");
        let diagnostic = error.to_string();

        assert_contains!(diagnostic, SETTINGS_FILE_NAME, "Settings errors must identify the failing file: {diagnostic}");
        if contents.is_none() {
            let cause = error.downcast_ref::<io::Error>();
            let cause = assert_some!(cause, "Unreadable settings must retain their I/O cause");
            assert_ne!(cause.kind(), io::ErrorKind::NotFound, "An existing unreadable settings path was treated as missing");
        }
    }

    #[rstest]
    #[case::empty("", &[], true)]
    #[case::file_threshold("[outgoing-dependencies]\nmaximum = 0", &["--outgoing-dependencies-mode", "files"], true)]
    #[case::file_mode("[outgoing-dependencies]\nmode = 'files'", &["--max-outgoing-dependencies", "0"], true)]
    #[case::file_report("[outgoing-dependencies]\noutput = 'outgoing.md'", &["--max-outgoing-dependencies", "0"], true)]
    #[case::cli_report("[outgoing-dependencies]\nmaximum = 0", &["--outgoing-dependencies-output", "cli.md"], true)]
    #[case::file_complete("[outgoing-dependencies]\nmaximum = 0\nmode = 'folders'\noutput = 'outgoing.md'", &[], true)]
    #[case::missing_file_threshold("[outgoing-dependencies]\nmode = 'files'", &[], false)]
    #[case::missing_report_threshold("[outgoing-dependencies]\noutput = 'outgoing.md'", &[], false)]
    #[case::missing_cli_threshold("", &["--outgoing-dependencies-mode", "files"], false)]
    fn test_outgoing_validation_after_merge(#[case] contents: &str, #[case] arguments: &[&str], #[case] valid: bool) {
        let saved_index_path = saved_index_path_test_fixture().display().to_string();
        let replay_arguments = ["--from-scip", &saved_index_path];
        let arguments: Vec<_> = replay_arguments.into_iter().chain(arguments.iter().copied()).collect();
        let fixture = SettingsLoadingTestFixture::new(Some(contents.as_bytes()), &arguments);
        let mut fixture = assert_ok!(fixture, "Outgoing merge-validation test fixture must be prepared");
        fixture.cli.settings = Some(fixture.settings_path);
        let loaded = LoadedSettings::load(fixture.cli, fixture.matches);
        let loaded = assert_ok!(loaded, "Outgoing settings must load before cross-field validation");

        let system_under_test = loaded.into_command();

        if valid {
            assert_ok!(system_under_test, "Outgoing requirements must be checked against the merged settings and CLI values");
        } else {
            let error = system_under_test.err();
            let error = assert_some!(error, "An outgoing mode or report without a merged threshold must fail");
            let diagnostic = error.to_string();
            assert_contains!(diagnostic, "--max-outgoing-dependencies", "Unexpected validation failure: {diagnostic}");
        }
    }

    #[test]
    fn test_missing_replay_into_command() {
        const INPUT_FILE_NAME: &str = "missing.scip";
        let contents = format!("[analysis]\nfrom-scip = '{INPUT_FILE_NAME}'");
        let fixture = SettingsLoadingTestFixture::new(Some(contents.as_bytes()), &[]);
        let fixture = assert_ok!(fixture, "Missing replay fixture must contain valid settings");
        let loaded = LoadedSettings::load(fixture.cli, fixture.matches);
        let loaded = assert_ok!(loaded, "Loading settings must not require the saved analysis to exist");

        let system_under_test = loaded.into_command();

        let error = system_under_test.err();
        let error = assert_some!(error, "Command construction must propagate a missing saved-analysis error");
        let directory = project_directory(fixture.directory.path());
        let directory = assert_ok!(directory, "Missing replay fixture must have a canonical directory");
        assert_missing_file_error(&error, &directory.join(INPUT_FILE_NAME));
    }

    #[rstest]
    #[case::file_replay(
        "[analysis]\nfrom-scip = 'saved.scip'", &[], None,
    )]
    #[case::cli_replay(
        "[graph]\ndepth = 0", &["--from-scip", &saved_index_path_test_fixture().display().to_string()], None,
    )]
    #[case::filtered_replay(
        "[analysis]\nfrom-scip = 'saved.scip'", &["--root", "src", "--exclude", "src/generated/**"], None,
    )]
    #[case::file_replay_cli_save(
        "[analysis]\nfrom-scip = 'saved.scip'", &["--save-scip", "new.scip"], Some("--save-scip"),
    )]
    #[case::file_save_cli_replay(
        "[analysis]\nsave-scip = 'new.scip'", &["--from-scip", "saved.scip"], Some("--save-scip"),
    )]
    #[case::file_replay_cli_config(
        "[analysis]\nfrom-scip = 'saved.scip'", &["--config", "analyzer.json"], Some("--config"),
    )]
    #[case::file_config_cli_replay(
        "[analysis]\nconfig = 'analyzer.json'", &["--from-scip", "saved.scip"], Some("--config"),
    )]
    #[case::file_replay_save(
        "[analysis]\nfrom-scip = 'saved.scip'\nsave-scip = 'new.scip'", &[], Some("--save-scip"),
    )]
    #[case::file_replay_config(
        "[analysis]\nfrom-scip = 'saved.scip'\nconfig = 'analyzer.json'", &[], Some("--config"),
    )]
    fn test_replay_validation_after_merge(
        #[case] contents: &str,
        #[case] arguments: &[&str],
        #[case] conflict: Option<&str>,
    ) {
        let fixture = SettingsLoadingTestFixture::new(Some(contents.as_bytes()), arguments);
        let fixture = assert_ok!(fixture, "Replay validation must have settings but no project manifest");
        let written = fs::copy(saved_index_path_test_fixture(), fixture.directory.path().join("saved.scip"));
        assert_ok!(written, "Valid saved analysis must be available beside the replay settings");
        let loaded = LoadedSettings::load(fixture.cli, fixture.matches);
        let loaded = assert_ok!(loaded, "Replay settings must load before conflicting options are validated");

        let system_under_test = loaded.into_command();

        if let Some(conflict) = conflict {
            let error = system_under_test.err();
            let error = assert_some!(error, "Merged replay options must reject {conflict}");
            let diagnostic = error.to_string();
            assert_contains!(diagnostic, conflict, "Replay failed for an unexpected reason: {diagnostic}");
        } else {
            assert_ok!(system_under_test, "Saved-index replay settings must load without a project manifest or source files");
        }
    }
}