source-mesh 1.2.0

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
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 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!(help.contains("--group-folders"), "CLI help omitted folder grouping");
        assert!(help.contains(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::process::ExitCode;

    use anyhow::Result;
    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;

    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]) -> Result<(TempDir, ArgMatches)> {
        let fixture = tempdir()?;
        if let Some(contents) = contents {
            fs::write(fixture.path().join(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)?;
        Ok((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 = automatic_settings_test_fixture(contents, &[]);
        let (fixture, matches) = assert_ok!(fixture, "Automatic settings fixture must prepare its invocation directory");
        let index = acyclic_index_fixture();
        let index = assert_ok!(index, "No-argument replay must have valid saved analysis");
        let index_write = fs::write(fixture.path().join("input.scip"), index);
        assert_ok!(index_write, "Saved analysis must be written beside automatic settings");
        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!(!has_project_file, "The replay fixture must not contain a Cargo manifest");
        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 = automatic_settings_test_fixture(Some(contents), &[]);
        let (fixture, matches) = assert_ok!(fixture, "Invalid automatic settings must be written before startup");
        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!(!has_project_file, "Invalid settings must be tested without a Cargo manifest");
        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!(diagnostic.contains(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 = automatic_settings_test_fixture(Some(&contents), argument.as_slice());
        let (fixture, matches) = assert_ok!(fixture, "Validation diagnostics require valid TOML with an invalid exclusion glob");
        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 = tempdir();
        let fixture = assert_ok!(fixture, "Missing-settings diagnostics require a temporary directory");
        let settings_path = fixture.path().join("missing.toml");
        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 = tempdir();
        let fixture = assert_ok!(fixture, "Invalid-input fixture must have a temporary directory");
        let index_path = fixture.path().join("input.scip");
        let diagram_path = fixture.path().join("diagram.dot");
        let index = if malformed_index {
            b"this is not protobuf".to_vec()
        } else {
            let index = acyclic_index_fixture();
            assert_ok!(index, "Outside-root scenario must start with a valid serialized acyclic index")
        };
        let index_write_result = fs::write(&index_path, index);
        assert_ok!(index_write_result, "Invalid-input scenario must write its SCIP fixture");
        let diagram_write_result = fs::write(&diagram_path, CONTENTS_OUTPUT_EXISTING);
        assert_ok!(diagram_write_result, "Existing diagram must be written before testing invalid input");
        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");
    }

    fn acyclic_index_fixture() -> Result<Vec<u8>> {
        Ok(Index {
            documents: vec![Document { relative_path: "src/item.rs".into(), ..Document::default() }],
            ..Index::default()
        }.write_to_bytes()?)
    }

    fn cyclic_index_fixture() -> Result<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();
        Ok(Index { documents, ..Index::default() }.write_to_bytes()?)
    }

    #[rstest]
    #[case::folder_report(None)]
    #[case::file_report(Some(0))]
    fn test_cycle_limit_output_preservation(#[case] depth: Option<usize>, #[values(false, true)] diagram_to_file: bool) {
        const CONTENTS_OUTPUT_EXISTING: &str = "previous result";
        let fixture = tempdir();
        let fixture = assert_ok!(fixture, "Cycle-limit fixture must have a temporary directory");
        let index_path = fixture.path().join("input.scip");
        let diagram_path = fixture.path().join("diagram.dot");
        let folder_report_path = fixture.path().join("folders.md");
        let file_report_path = fixture.path().join("files.md");
        let index = cyclic_index_fixture();
        let index = assert_ok!(index, "Cycle-limit scenario must have a valid serialized cyclic index");
        let index_write_result = fs::write(&index_path, index);
        assert_ok!(index_write_result, "Cycle-limit scenario must write its SCIP fixture");
        for path in [&diagram_path, &folder_report_path, &file_report_path] {
            let output_write_result = fs::write(path, CONTENTS_OUTPUT_EXISTING);
            assert_ok!(output_write_result, "Cycle-limit scenario must create existing output at {}", path.display());
        }
        let depth_argument = depth.map(|depth| depth.to_string());
        let mut arguments = vec![
            OsStr::new(env!("CARGO_PKG_NAME")),
            OsStr::new("--from-scip"), index_path.as_os_str(),
            OsStr::new("--cycle-limit"), OsStr::new("1"),
            OsStr::new("--cycles-output"), folder_report_path.as_os_str(),
            OsStr::new("--file-cycles-output"), file_report_path.as_os_str(),
        ];
        if let Some(depth_argument) = depth_argument.as_deref() {
            arguments.extend([OsStr::new("--depth"), OsStr::new(depth_argument)]);
        }
        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 cycle-limit and report-output arguments");
        let mut output = Vec::new();

        let error = run(system_under_test, &mut output).err();
        let error = assert_some!(error, "Command accepted more cycles than the limit");
        let message = error.to_string();
        let preserved = [&diagram_path, &folder_report_path, &file_report_path].map(|path| {
            let preserved = fs::read_to_string(path);
            assert_ok!(preserved, "Cycle-limit failure must leave {} readable", path.display())
        });

        assert!(message.contains("Cycle count exceeds"), "Failure must be due to the cycle limit: {error:#}");
        assert!(output.is_empty(), "Command wrote to stdout before checking the cycle limit");
        assert_eq!(preserved, [CONTENTS_OUTPUT_EXISTING; 3], "Cycle limit error overwrote an output");
    }

    #[rstest]
    #[case::report_cycles(false)]
    #[case::cycle_gate(true)]
    fn test_cyclic_graph_outputs(#[case] fail_on_cycles: bool, #[values(false, true)] diagram_to_file: bool) {
        let fixture = tempdir();
        let fixture = assert_ok!(fixture, "Cyclic-output fixture must have a temporary directory");
        let index_path = fixture.path().join("input.scip");
        let diagram_path = fixture.path().join("diagram.graphml");
        let folder_report_path = fixture.path().join("folders.md");
        let file_report_path = fixture.path().join("files.md");
        let index = cyclic_index_fixture();
        let index = assert_ok!(index, "Cyclic-output scenario must have a valid serialized index");
        let index_write_result = fs::write(&index_path, index);
        assert_ok!(index_write_result, "Cyclic-output scenario must write its SCIP fixture");
        let cycle_gate_argument = format!("--fail-on-cycles={fail_on_cycles}");
        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("--highlight-cycles"), OsStr::new(&cycle_gate_argument),
            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, "CLI must accept GraphML highlighting and cycle reports");
        let mut output = Vec::new();

        let command_result = run(system_under_test, &mut output);
        let standard_output_is_empty = output.is_empty();
        let diagram = if diagram_to_file {
            let diagram = fs::read_to_string(&diagram_path);
            assert_ok!(diagram, "Cyclic graph command must produce a readable diagram file")
        } else {
            let diagram = String::from_utf8(output);
            assert_ok!(diagram, "Cyclic graph command must emit a UTF-8 diagram on stdout")
        };
        let folder_report = fs::read_to_string(&folder_report_path);
        let folder_report = assert_ok!(folder_report, "Cyclic graph command must produce a readable folder report");
        let file_report = fs::read_to_string(&file_report_path);
        let file_report = assert_ok!(file_report, "Cyclic graph command must produce a readable file report");

        assert_eq!(command_result.is_err(), fail_on_cycles, "Fail on cycles only when requested: {command_result:?}");
        if let Err(error) = command_result {
            let message = error.to_string();
            assert!(message.contains("Dependency cycles found:"), "Failure must be due to cycles: {error:#}");
        }
        assert_eq!(standard_output_is_empty, diagram_to_file, "Diagram output did not follow the selected destination");
        assert!(diagram.contains("cycle_group"), "Cyclic graph did not produce a highlighted diagram");
        assert!(folder_report.contains("## C1 ("), "Cyclic graph did not produce a folder cycle report");
        assert!(file_report.contains("## C1 ("), "Cyclic graph did not produce a file cycle report");
    }

    #[rstest]
    fn test_outgoing_dependency_failure_outputs(
        #[values("files", "folders")] mode: &str,
        #[values(false, true)] diagram_to_file: bool,
    ) {
        let fixture = tempdir();
        let fixture = assert_ok!(fixture, "Outgoing dependency fixture must have a temporary directory");
        let index_path = fixture.path().join("input.scip");
        let diagram_path = fixture.path().join("diagram.dot");
        let report_path = fixture.path().join("outgoing.md");
        let index = cyclic_index_fixture();
        let index = assert_ok!(index, "Outgoing dependency scenario must have a valid serialized index");
        let written = fs::write(&index_path, index);
        assert_ok!(written, "Outgoing dependency scenario must write its SCIP fixture");
        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!(diagnostic.contains(&expected_check), "Diagnostic must identify the check");
        assert!(report.contains(&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 = tempdir();
        let fixture = assert_ok!(fixture, "Settings protection fixture directory must be created");
        let settings_path = fixture.path().join("settings.toml");
        let settings_write = fs::write(&settings_path, CONTENTS_SETTINGS);
        assert_ok!(settings_write, "Settings must be written before testing output protection");
        let index_path = fixture.path().join("input.scip");
        let index = acyclic_index_fixture();
        let index = assert_ok!(index, "Settings protection requires valid analysis");
        let index_write = fs::write(&index_path, index);
        assert_ok!(index_write, "Settings protection must write its saved analysis");
        let output_path = if alias {
            let path = fixture.path().join("alias.toml");
            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!(message.contains("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 = tempdir();
        let fixture = assert_ok!(fixture, "Invalid-settings fixture directory must be created");
        let settings_path = fixture.path().join("settings.toml");
        let settings_write = fs::write(&settings_path, settings);
        assert_ok!(settings_write, "Invalid settings fixture must be written");
        let output_path = fixture.path().join("diagram.dot");
        let output_write = fs::write(&output_path, CONTENTS_EXISTING);
        assert_ok!(output_write, "Existing output must be written before loading invalid settings");
        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!(message.contains(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 = tempdir();
        let fixture = assert_ok!(fixture, "Acyclic cycle-gate fixture must have a temporary directory");
        let index_path = fixture.path().join("input.scip");
        let index = acyclic_index_fixture();
        let index = assert_ok!(index, "Cycle-gate scenario must have a valid serialized acyclic index");
        let index_write_result = fs::write(&index_path, index);
        assert_ok!(index_write_result, "Acyclic cycle-gate scenario must write its SCIP 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");
    }
}