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
use std::io;
use std::io::Write;
use std::path::Path;
use std::process::ExitCode;

use anyhow::Error;
use clap::ArgMatches;
use clap::CommandFactory as _;
use clap::FromArgMatches as _;
use source_mesh_core::backend::contains_project_file;

use settings::Cli;
use settings::Command;
use settings::LoadedSettings;

mod settings;

#[must_use]
const fn should_display_help(has_arguments: bool, has_project_file: bool, has_settings: bool) -> bool {
    !has_arguments && !has_project_file && !has_settings
}

#[must_use]
fn error_message(error: &Error, verbose: bool) -> String {
    if verbose {
        format!("{error:#}")
    } else {
        error.to_string()
    }
}

fn report_error(message: &str, standard_error: &mut dyn Write) -> ExitCode {
    let _: io::Result<()> = writeln!(standard_error, "error: {message}");
    ExitCode::FAILURE
}

fn load_command(matches: ArgMatches, has_arguments: bool, has_project_file: bool) -> Result<Option<Command>, String> {
    let cli = Cli::from_arg_matches(&matches).map_err(|error| error.to_string())?;
    let cli_verbose = cli.verbose();
    let settings = LoadedSettings::load(cli, matches).map_err(|error| error_message(&error, cli_verbose))?;
    if should_display_help(has_arguments, has_project_file, settings.has_settings()) {
        return Ok(None);
    }
    let effective_verbose = settings.verbose();
    settings.into_command().map(Some).map_err(|error| error_message(&error, effective_verbose))
}

fn main() -> ExitCode {
    let matches = Cli::command().get_matches();
    let has_arguments = std::env::args_os().nth(1).is_some();
    let has_project_file = contains_project_file(Path::new("."));
    let command = match load_command(matches, has_arguments, has_project_file) {
        Ok(Some(command)) => command,
        Ok(None) => {
            if let Err(error) = Cli::command().print_help() {
                return report_error(&error.to_string(), &mut io::stderr().lock());
            }
            return ExitCode::SUCCESS;
        }
        Err(message) => return report_error(&message, &mut io::stderr().lock()),
    };
    if let Err(error) = command.run(&mut io::stdout().lock()) {
        if error.downcast_ref::<io::Error>().is_some_and(|source| source.kind() == io::ErrorKind::BrokenPipe) {
            return ExitCode::SUCCESS;
        }
        return report_error(&error_message(&error, command.verbose()), &mut io::stderr().lock());
    }
    ExitCode::SUCCESS
}

#[cfg(test)]
mod cli_tests {
    use std::process::ExitCode;

    use anyhow::Error;
    use assertables::assert_contains;
    use claims::assert_some;
    use clap::Parser as _;
    use clap::error::ErrorKind;
    use pretty_assertions::assert_eq;
    use rstest::rstest;

    use super::error_message;
    use super::report_error;
    use super::settings::BOOLEAN_OPTIONS_DEFAULT_HELP;
    use super::settings::Cli;
    use super::should_display_help;

    #[rstest]
    #[case::no_arguments_without_project(false, false, false, true)]
    #[case::arguments_without_project(true, false, false, false)]
    #[case::no_arguments_with_project(false, true, false, false)]
    #[case::arguments_with_project(true, true, false, false)]
    #[case::settings_without_arguments_or_project(false, false, true, false)]
    #[case::settings_with_arguments(true, false, true, false)]
    #[case::settings_with_project(false, true, true, false)]
    #[case::settings_with_arguments_and_project(true, true, true, false)]
    fn test_help_display_conditions(
        #[case] has_arguments: bool,
        #[case] has_project_file: bool,
        #[case] has_settings: bool,
        #[case] expected: bool,
    ) {
        let system_under_test = should_display_help(has_arguments, has_project_file, has_settings);

        assert_eq!(system_under_test, expected, "CLI help display policy changed");
    }

    #[test]
    fn test_folder_grouping_help() {
        let system_under_test = Cli::try_parse_from([env!("CARGO_PKG_NAME"), "--help"]).err();
        let error = assert_some!(system_under_test, "CLI did not return help");
        let help = error.to_string();

        assert_eq!(error.kind(), ErrorKind::DisplayHelp, "CLI rejected the help argument");
        assert_contains!(help, "--group-folders", "CLI help omitted folder grouping");
        assert_contains!(help, BOOLEAN_OPTIONS_DEFAULT_HELP, "CLI help omitted boolean option defaults");
    }

    #[rstest]
    fn test_error_detail_visibility(#[values(false, true)] verbose: bool) {
        const MESSAGE_FAILURE: &str = "Cannot resolve dependency definitions";
        const MESSAGE_DIAGNOSTIC: &str = "Internal analyzer diagnostic";
        let fixture = Error::msg(MESSAGE_DIAGNOSTIC).context(MESSAGE_FAILURE);

        let system_under_test = error_message(&fixture, verbose);

        assert!(system_under_test.starts_with(MESSAGE_FAILURE), "The failure reason is missing");
        assert_eq!(system_under_test.contains(MESSAGE_DIAGNOSTIC), verbose, "Diagnostic details must require verbose output");
    }

    #[test]
    fn test_error_report() {
        let mut fixture = Vec::new();

        let system_under_test = report_error("Cannot analyze project", &mut fixture);

        assert_eq!(system_under_test, ExitCode::FAILURE, "Reporting an error must return the failure exit code");
        assert_eq!(fixture, b"error: Cannot analyze project\n", "Error reporting must write the complete diagnostic");
    }

    #[rstest]
    #[case::unavailable(0, b"")]
    #[case::partial(3, b"err")]
    fn test_error_report_write_failure(#[case] capacity: usize, #[case] expected: &[u8]) {
        let mut fixture = vec![0; capacity];
        let mut standard_error = fixture.as_mut_slice();

        let system_under_test = report_error("Cannot analyze project", &mut standard_error);

        assert_eq!(system_under_test, ExitCode::FAILURE, "Unavailable stderr must preserve the failure exit code");
        assert_eq!(fixture, expected, "Error reporting must retain the bytes written before stderr became unavailable");
    }
}

#[cfg(test)]
mod command_tests {
    use std::ffi::OsStr;
    use std::fs;
    use std::path::Path;
    use std::path::PathBuf;
    use std::process::ExitCode;

    use anyhow::Result;
    use assertables::assert_contains;
    use claims::assert_ok;
    use claims::assert_some;
    use clap::ArgMatches;
    use clap::CommandFactory as _;
    use clap::FromArgMatches as _;
    use pretty_assertions::assert_eq;
    use protobuf::Enum as _;
    use protobuf::Message as _;
    use rstest::rstest;
    use scip::types::Document;
    use scip::types::Index;
    use scip::types::Occurrence;
    use scip::types::SymbolRole;
    use source_mesh_core::backend::contains_project_file;
    use tempfile::TempDir;
    use tempfile::tempdir;

    use super::error_message;
    use super::load_command;
    use super::report_error;
    use super::settings::Cli;
    use super::settings::LoadedSettings;
    use super::settings::SETTINGS_FILE_NAME;

    struct CommandTestFixture {
        directory: TempDir,
    }

    impl CommandTestFixture {
        #[must_use]
        fn new() -> Self {
            let directory = tempdir();
            let directory = assert_ok!(directory, "Command fixture directory must be created");
            Self { directory }
        }

        #[must_use]
        fn path(&self) -> &Path {
            self.directory.path()
        }

        fn make_file(&self, path: impl AsRef<Path>, contents: Option<impl AsRef<[u8]>>) -> PathBuf {
            let path = self.path().join(path);
            if let Some(contents) = contents {
                let written = fs::write(&path, contents);
                assert_ok!(written, "Command fixture file must be written at {}", path.display());
            }
            path
        }
    }

    mod fixture_tests {
        use std::fs;
        use std::io::ErrorKind;

        use claims::assert_err;
        use claims::assert_ok;
        use pretty_assertions::assert_eq;
        use rstest::rstest;

        use super::CommandTestFixture;

        #[rstest]
        #[case::empty(b"", b"")]
        #[case::text(b"fixture contents", b"fixture contents")]
        #[case::binary(b"\0\xffSCIP", b"\0\xffSCIP")]
        fn test_make_file_contents(#[case] contents: &[u8], #[case] expected: &[u8]) {
            let system_under_test = CommandTestFixture::new();

            let path = system_under_test.make_file("input.dat", Some(contents));

            let actual = fs::read(path);
            let actual = assert_ok!(actual, "The created fixture file must be readable");
            assert_eq!(actual, expected, "Fixture files must preserve all explicitly supplied contents");
        }

        #[rstest]
        #[case::missing(false)]
        #[case::existing(true)]
        fn test_make_file_without_contents(#[case] existing: bool) {
            const CONTENTS_EXISTING: &[u8] = b"existing fixture contents";
            let system_under_test = CommandTestFixture::new();
            let file_to_make = "input.dat";
            system_under_test.make_file(file_to_make, existing.then_some(CONTENTS_EXISTING));

            let path = system_under_test.make_file(file_to_make, None::<&[u8]>);
            let actual = fs::read(path);

            if existing {
                let actual = assert_ok!(actual, "An absent content argument must leave the existing file readable");
                assert_eq!(actual, CONTENTS_EXISTING, "An absent content argument must not modify the existing file");
            } else {
                let error = assert_err!(actual, "An absent content argument must not create the file");
                assert_eq!(error.kind(), ErrorKind::NotFound, "The fixture file must remain absent");
            }
        }
    }

    fn run(matches: ArgMatches, output: &mut dyn std::io::Write) -> Result<()> {
        let cli = Cli::from_arg_matches(&matches)?;
        LoadedSettings::load(cli, matches)?.into_command()?.run(output)
    }

    fn automatic_settings_test_fixture(contents: Option<&str>, arguments: &[&str]) -> (CommandTestFixture, ArgMatches) {
        let fixture = CommandTestFixture::new();
        fixture.make_file(SETTINGS_FILE_NAME, contents);
        /* Select the invocation directory without changing the working directory shared by parallel tests. */
        let project_arguments = [OsStr::new(env!("CARGO_PKG_NAME")), fixture.path().as_os_str()];
        let arguments = project_arguments.into_iter().chain(arguments.iter().map(OsStr::new));
        let matches = Cli::command().try_get_matches_from(arguments);
        let matches = assert_ok!(matches, "Automatic settings fixture arguments must parse");
        (fixture, matches)
    }

    #[rstest]
    #[case::absent(None, true)]
    #[case::replay(Some("[analysis]\nfrom-scip = 'input.scip'"), false)]
    fn test_no_arguments_settings_discovery(#[case] contents: Option<&str>, #[case] display_help: bool) {
        let (fixture, matches) = automatic_settings_test_fixture(contents, &[]);
        fixture.make_file("input.scip", Some(acyclic_index_fixture()));
        let has_project_file = contains_project_file(fixture.path());

        let system_under_test = load_command(matches, false, has_project_file);
        let system_under_test = assert_ok!(system_under_test, "Automatic settings discovery must precede no-argument help");

        assert_eq!(system_under_test.is_none(), display_help, "Loaded replay settings must prevent automatic help");
        if let Some(command) = system_under_test {
            let mut output = Vec::new();
            let result = command.run(&mut output);

            assert_ok!(result, "Settings-only replay must run without a Cargo manifest");
            assert!(!output.is_empty(), "Settings-only replay must produce a diagram rather than skip execution");
        }
    }

    #[rstest]
    #[case::malformed("[analysis")]
    #[case::unknown_key("[analysis]\nverbsoe = true")]
    fn test_no_arguments_settings_errors(#[case] contents: &str) {
        let (fixture, matches) = automatic_settings_test_fixture(Some(contents), &[]);
        let has_project_file = contains_project_file(fixture.path());
        let mut diagnostic = Vec::new();

        let message = load_command(matches, false, has_project_file).err();
        let message = assert_some!(message, "Invalid automatic settings must fail instead of selecting help");
        let system_under_test = report_error(&message, &mut diagnostic);
        let diagnostic = String::from_utf8(diagnostic);
        let diagnostic = assert_ok!(diagnostic, "Automatic-settings diagnostics must be UTF-8");

        assert_eq!(system_under_test, ExitCode::FAILURE, "Malformed automatic settings must fail the invocation");
        assert!(diagnostic.starts_with("error: Cannot parse settings"), "Startup must report the settings parse error");
        assert_contains!(diagnostic, SETTINGS_FILE_NAME, "The diagnostic must identify the automatic settings file");
    }

    #[rstest]
    #[case::settings_quiet(false, None, false)]
    #[case::settings_verbose(true, None, true)]
    #[case::cli_quiet(true, Some("--verbose=false"), false)]
    #[case::cli_verbose(false, Some("--verbose"), true)]
    fn test_settings_validation_diagnostic_verbosity(
        #[case] configured: bool,
        #[case] argument: Option<&str>,
        #[case] verbose: bool,
    ) {
        let contents = format!("[graph]\nexclude = ['[']\n[analysis]\nverbose = {configured}");
        let (fixture, matches) = automatic_settings_test_fixture(Some(&contents), argument.as_slice());
        let has_project_file = contains_project_file(fixture.path());
        let mut diagnostic = Vec::new();

        let message = load_command(matches, argument.is_some(), has_project_file).err();
        let message = assert_some!(message, "An invalid exclusion glob must fail after settings merge");
        let system_under_test = report_error(&message, &mut diagnostic);
        let diagnostic = String::from_utf8(diagnostic);
        let diagnostic = assert_ok!(diagnostic, "Validation diagnostics must be UTF-8");

        assert_eq!(system_under_test, ExitCode::FAILURE, "Invalid merged exclusions must fail the invocation");
        assert!(diagnostic.starts_with("error: Invalid exclusion pattern"), "Validation must identify the invalid exclusion");
        assert_eq!(diagnostic.contains("unclosed character class"), verbose, "Diagnostics must use effective verbosity");
    }

    #[rstest]
    fn test_settings_load_diagnostic_verbosity(#[values(false, true)] verbose: bool) {
        let fixture = CommandTestFixture::new();
        let settings_path = fixture.make_file("missing.toml", None::<&str>);
        let verbosity_argument = format!("--verbose={verbose}");
        let matches = Cli::command().try_get_matches_from([
            OsStr::new(env!("CARGO_PKG_NAME")), OsStr::new("--settings"), settings_path.as_os_str(),
            OsStr::new(&verbosity_argument),
        ]);
        let matches = assert_ok!(matches, "CLI verbosity must parse before settings loading fails");
        let mut diagnostic = Vec::new();

        let message = load_command(matches, true, false).err();
        let message = assert_some!(message, "Explicitly selected missing settings must fail loading");
        let system_under_test = report_error(&message, &mut diagnostic);
        let diagnostic = String::from_utf8(diagnostic);
        let diagnostic = assert_ok!(diagnostic, "Settings-loading diagnostics must be UTF-8");

        assert_eq!(system_under_test, ExitCode::FAILURE, "Settings loading errors must fail the invocation");
        assert!(diagnostic.starts_with("error: Cannot load settings"), "Startup must report the settings loading error");
        assert_eq!(diagnostic.contains("Cannot open input file"), verbose, "Loading errors must use CLI verbosity");
    }

    #[rstest]
    #[case::malformed_index(true, Path::new("."))]
    #[case::outside_root(false, Path::new(".."))]
    fn test_invalid_input_output_preservation(#[case] malformed_index: bool, #[case] root: &Path) {
        const CONTENTS_OUTPUT_EXISTING: &str = "previous diagram";
        let fixture = CommandTestFixture::new();
        let index = if malformed_index { b"this is not protobuf".to_vec() } else { acyclic_index_fixture() };
        let index_path = fixture.make_file("input.scip", Some(index));
        let diagram_path = fixture.make_file("diagram.dot", Some(CONTENTS_OUTPUT_EXISTING));
        let system_under_test = Cli::command().try_get_matches_from([
            OsStr::new(env!("CARGO_PKG_NAME")),
            OsStr::new("--from-scip"), index_path.as_os_str(),
            OsStr::new("--root"), root.as_os_str(),
            OsStr::new("--output"), diagram_path.as_os_str(),
        ]);
        let system_under_test = assert_ok!(system_under_test, "Invalid-input fixture arguments must parse");
        let mut output = Vec::new();

        let error = run(system_under_test, &mut output).err();
        let error = assert_some!(error, "Command accepted an invalid index or subtree");
        let message = error.to_string();
        let preserved = fs::read_to_string(&diagram_path);
        let preserved = assert_ok!(preserved, "Existing diagram must remain readable after invalid input");

        assert!(!message.is_empty(), "Command omitted invalid-input diagnostics");
        assert!(output.is_empty(), "Command wrote to stdout for invalid input");
        assert_eq!(preserved, CONTENTS_OUTPUT_EXISTING, "Command overwrote the existing diagram");
    }

    #[must_use]
    fn acyclic_index_fixture() -> Vec<u8> {
        let index = Index {
            documents: vec![Document { relative_path: "src/item.rs".into(), ..Document::default() }],
            ..Index::default()
        }.write_to_bytes();
        assert_ok!(index, "Failed to serialize the acyclic index fixture")
    }

    #[must_use]
    fn cyclic_index_fixture() -> Vec<u8> {
        let documents = [
            ("first/item.rs", &["second/item.rs"][..]),
            ("second/item.rs", &["first/item.rs", "third/item.rs"]),
            ("third/item.rs", &["second/item.rs"]),
        ].into_iter().map(|(path, references)| {
            let mut occurrences = vec![Occurrence {
                symbol: path.into(),
                symbol_roles: SymbolRole::Definition.value(),
                ..Occurrence::default()
            }];
            occurrences.extend(references.iter().map(|&target| Occurrence { symbol: target.into(), ..Occurrence::default() }));
            Document { relative_path: path.into(), occurrences, ..Document::default() }
        }).collect();
        let index = Index { documents, ..Index::default() }.write_to_bytes();
        assert_ok!(index, "Failed to serialize the cyclic index fixture")
    }

    #[rstest]
    fn test_cycle_gate_failure_outputs(#[values(false, true)] diagram_to_file: bool) {
        const CONTENTS_OUTPUT_EXISTING: &str = "previous result";
        let fixture = CommandTestFixture::new();
        let index_path = fixture.make_file("input.scip", Some(cyclic_index_fixture()));
        let diagram_path = fixture.make_file("diagram.graphml", Some(CONTENTS_OUTPUT_EXISTING));
        let folder_report_path = fixture.make_file("folders.md", Some(CONTENTS_OUTPUT_EXISTING));
        let file_report_path = fixture.make_file("files.md", Some(CONTENTS_OUTPUT_EXISTING));

        let mut arguments = vec![
            OsStr::new(env!("CARGO_PKG_NAME")),
            OsStr::new("--from-scip"), index_path.as_os_str(),
            OsStr::new("--format"), OsStr::new("graphml"),
            OsStr::new("--no-settings"), OsStr::new("--cycle-limit"), OsStr::new("1"),
            OsStr::new("--highlight-cycles"), OsStr::new("--fail-on-cycles"),
            OsStr::new("--cycles-output"), folder_report_path.as_os_str(),
            OsStr::new("--file-cycles-output"), file_report_path.as_os_str(),
        ];
        if diagram_to_file {
            arguments.extend([OsStr::new("--output"), diagram_path.as_os_str()]);
        }
        let system_under_test = Cli::command().try_get_matches_from(arguments);
        let system_under_test = assert_ok!(system_under_test, "Failed to parse GraphML highlighting and cycle-report arguments");
        let mut output = Vec::new();

        let command_result = run(system_under_test, &mut output);
        assert_eq!(output.is_empty(), diagram_to_file, "Diagram output did not follow the selected destination");
        let diagram_file = fs::read_to_string(&diagram_path);
        let diagram_file = assert_ok!(diagram_file, "Failed to read the diagram file");
        let diagram = if diagram_to_file {
            diagram_file
        } else {
            assert_eq!(diagram_file, CONTENTS_OUTPUT_EXISTING, "Unrequested diagram file contents changed");
            let diagram = String::from_utf8(output);
            assert_ok!(diagram, "Failed to decode the stdout diagram as UTF-8")
        };
        let folder_report = fs::read_to_string(&folder_report_path);
        let folder_report = assert_ok!(folder_report, "Failed to read the folder cycle report");
        let file_report = fs::read_to_string(&file_report_path);
        let file_report = assert_ok!(file_report, "Failed to read the file cycle report");

        assert!(command_result.is_err(), "Command unexpectedly succeeded with the cycle gate enabled");
        assert_contains!(diagram, "cycle_group", "Diagram is missing the cycle_group marker");
        assert_eq!(folder_report.lines().filter(|line| line.starts_with("- ")).count(), 1);
        assert_eq!(file_report.lines().filter(|line| line.starts_with("- ")).count(), 1);
    }

    #[rstest]
    fn test_outgoing_dependency_failure_outputs(
        #[values("files", "folders")] mode: &str,
        #[values(false, true)] diagram_to_file: bool,
    ) {
        let fixture = CommandTestFixture::new();
        let index_path = fixture.make_file("input.scip", Some(cyclic_index_fixture()));
        let diagram_path = fixture.make_file("diagram.dot", None::<&str>);
        let report_path = fixture.make_file("outgoing.md", None::<&str>);
        let mut arguments = vec![
            OsStr::new(env!("CARGO_PKG_NAME")),
            OsStr::new("--from-scip"), index_path.as_os_str(),
            OsStr::new("--max-outgoing-dependencies"), OsStr::new("1"),
            OsStr::new("--outgoing-dependencies-mode"), OsStr::new(mode),
            OsStr::new("--outgoing-dependencies-output"), report_path.as_os_str(),
        ];
        if diagram_to_file {
            arguments.extend([OsStr::new("--output"), diagram_path.as_os_str()]);
        }
        let system_under_test = Cli::command().try_get_matches_from(arguments);
        let system_under_test = assert_ok!(system_under_test, "CLI must accept outgoing dependency check and report options");
        let mut output = Vec::new();
        let mut diagnostic = Vec::new();

        let error = run(system_under_test, &mut output).err();
        let error = assert_some!(error, "Two outgoing targets must exceed a maximum of one");
        let status = report_error(&error_message(&error, false), &mut diagnostic);
        let diagnostic = String::from_utf8(diagnostic);
        let diagnostic = assert_ok!(diagnostic, "Dependency diagnostic must be UTF-8");
        let report = fs::read_to_string(report_path);
        let report = assert_ok!(report, "The violation report must be written before command failure");
        let source = if mode == "files" { Path::new("second").join("item.rs") } else { "second".into() };
        let source = source.to_string_lossy();
        let expected_violation = format!("- {source:?}: 2 outgoing dependencies (maximum: 1)");
        let expected_check = format!("mode: {mode}, maximum: 1");
        let expected_mode = format!("Mode: {mode}");
        let violations: Vec<_> = diagnostic.lines().filter(|line| line.starts_with("- ")).collect();
        let report_violation_count = report.lines().filter(|line| line.starts_with("- ")).count();

        assert_eq!(status, ExitCode::FAILURE, "A threshold violation must fail the command");
        assert_eq!(violations, [expected_violation], "Normal stderr must list exactly the violating source and count");
        assert!(diagnostic.starts_with("error: "), "CLI must emit the failure on the error stream");
        assert_contains!(diagnostic, &expected_check, "Diagnostic must identify the check");
        assert_contains!(report, &expected_mode, "Report must use the requested dependency mode");
        assert_eq!(report_violation_count, 1, "Report must list the violation");
        assert_eq!(output.is_empty(), diagram_to_file, "Diagnostics must not replace or contaminate the diagram");
        if diagram_to_file {
            let diagram = fs::read(diagram_path);
            let diagram = assert_ok!(diagram, "Diagram must be written before threshold failure");
            assert!(!diagram.is_empty(), "Threshold failure must preserve the requested diagram");
        }
    }

    #[rstest]
    #[case::same_path(false)]
    #[case::hard_link(true)]
    fn test_settings_input_output_preservation(#[case] alias: bool) {
        const CONTENTS_SETTINGS: &str = "[export]\nformat = 'dot'\n";
        let fixture = CommandTestFixture::new();
        let settings_path = fixture.make_file("settings.toml", Some(CONTENTS_SETTINGS));
        let index_path = fixture.make_file("input.scip", Some(acyclic_index_fixture()));
        let output_path = if alias {
            let path = fixture.make_file("alias.toml", None::<&str>);
            let linked = fs::hard_link(&settings_path, &path);
            assert_ok!(linked, "Settings alias must identify the loaded file");
            path
        } else {
            settings_path.clone()
        };
        let system_under_test = Cli::command().try_get_matches_from([
            OsStr::new(env!("CARGO_PKG_NAME")),
            OsStr::new("--settings"), settings_path.as_os_str(),
            OsStr::new("--from-scip"), index_path.as_os_str(),
            OsStr::new("--output"), output_path.as_os_str(),
        ]);
        let system_under_test = assert_ok!(system_under_test, "Settings protection arguments must parse");
        let mut output = Vec::new();

        let error = run(system_under_test, &mut output).err();
        let error = assert_some!(error, "The command must not overwrite its TOML settings");
        let message = error.to_string();
        let preserved = fs::read_to_string(&settings_path);
        let preserved = assert_ok!(preserved, "Loaded settings must remain readable after output rejection");

        assert_contains!(message, "must not overwrite input", "Unexpected settings failure: {error:#}");
        assert_eq!(preserved, CONTENTS_SETTINGS, "The settings file changed");
        assert!(output.is_empty(), "An input conflict must be detected before writing the diagram");
    }

    #[rstest]
    #[case::unknown_key("[graph]\nexlcude = []", "exlcude")]
    #[case::invalid_glob("[graph]\nexclude = ['[']", "exclusion pattern")]
    #[case::missing_limit("[outgoing-dependencies]\nmode = 'files'", "require")]
    fn test_invalid_settings_output_preservation(#[case] settings: &str, #[case] diagnostic: &str) {
        const CONTENTS_EXISTING: &str = "previous diagram";
        let fixture = CommandTestFixture::new();
        let settings_path = fixture.make_file("settings.toml", Some(settings));
        let output_path = fixture.make_file("diagram.dot", Some(CONTENTS_EXISTING));
        let system_under_test = Cli::command().try_get_matches_from([
            OsStr::new(env!("CARGO_PKG_NAME")),
            OsStr::new("--settings"), settings_path.as_os_str(),
            OsStr::new("--from-scip"), OsStr::new("missing.scip"),
            OsStr::new("--output"), output_path.as_os_str(),
        ]);
        let system_under_test = assert_ok!(system_under_test, "Invalid-settings arguments must parse before validation");
        let mut output = Vec::new();

        let error = run(system_under_test, &mut output).err();
        let error = assert_some!(error, "The command accepted invalid settings");
        let message = error.to_string();
        let preserved = fs::read_to_string(&output_path);
        let preserved = assert_ok!(preserved, "Invalid settings must leave the output readable");

        assert_contains!(message, diagnostic, "Settings must fail before opening analysis: {error:#}");
        assert_eq!(preserved, CONTENTS_EXISTING, "Invalid settings changed an existing output");
        assert!(output.is_empty(), "Invalid settings must not produce a diagram");
    }

    #[test]
    fn test_acyclic_graph_cycle_gate() {
        let fixture = CommandTestFixture::new();
        let index_path = fixture.make_file("input.scip", Some(acyclic_index_fixture()));
        let system_under_test = Cli::command().try_get_matches_from([
            OsStr::new(env!("CARGO_PKG_NAME")),
            OsStr::new("--from-scip"), index_path.as_os_str(),
            OsStr::new("--root"), OsStr::new("src"),
            OsStr::new("--fail-on-cycles"),
        ]);
        let system_under_test = assert_ok!(system_under_test, "CLI must accept the acyclic replay and cycle-gate arguments");
        let mut output = Vec::new();

        let command_result = run(system_under_test, &mut output);

        assert_ok!(command_result, "Cycle gate must accept an acyclic graph");
        assert!(!output.is_empty(), "Cycle gate did not export the acyclic graph");
    }
}