unsafe-budget 0.5.0

keeps the unsafety demons out. an unsafe code budget gate for CI pipelines.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
use crate::analyzer::process::{self, Run};
use crate::analyzer::{aggregate_units, Analyzer, AnalyzerInfo};
use crate::error::{Error, Result};
use crate::model::{ScanOpts, ScanResult, Scope, Totals, UnitKind};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;

const PLUGIN_PREFIX: &str = "unsafe-budget-plugin-";

/// external plugin analyzer.
pub struct PluginAnalyzer {
    pub id: String,
    pub language: String,
    pub path: PathBuf,
}

impl Analyzer for PluginAnalyzer {
    fn id(&self) -> &str {
        &self.id
    }

    fn language(&self) -> &str {
        &self.language
    }

    fn run(&self, opts: &ScanOpts) -> Result<ScanResult> {
        run_plugin(&self.id, &self.path, opts)
    }
}

/// discover plugin executables on PATH.
///
/// `timeout` (seconds) bounds each plugin's `--info` probe; `None` leaves the
/// probe unbounded.
pub fn discover_plugins(timeout: Option<u64>) -> Vec<AnalyzerInfo> {
    let path_var = match std::env::var("PATH") {
        Ok(p) => p,
        Err(_) => return Vec::new(),
    };

    let mut plugins = Vec::new();

    for dir in std::env::split_paths(&path_var) {
        if let Ok(entries) = std::fs::read_dir(&dir) {
            for entry in entries.flatten() {
                let path = entry.path();
                if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
                    if name.starts_with(PLUGIN_PREFIX) && is_executable(&path) {
                        let id = name.strip_prefix(PLUGIN_PREFIX).unwrap_or(name);
                        let language = probe_plugin_language(&path, timeout)
                            .unwrap_or_else(|| "unknown".into());
                        plugins.push(AnalyzerInfo {
                            id: id.into(),
                            language,
                            builtin: false,
                            path: Some(path),
                        });
                    }
                }
            }
        }
    }

    plugins.sort_by(|a, b| a.id.cmp(&b.id));
    plugins.dedup_by(|a, b| a.id == b.id);
    plugins
}

/// check if a path is executable.
#[cfg(unix)]
fn is_executable(path: &Path) -> bool {
    use std::os::unix::fs::PermissionsExt;
    path.is_file()
        && std::fs::metadata(path)
            .map(|m| m.permissions().mode() & 0o111 != 0)
            .unwrap_or(false)
}

#[cfg(not(unix))]
fn is_executable(path: &Path) -> bool {
    path.is_file()
}

/// try to get plugin language by running with --info.
///
/// `timeout` (seconds) bounds the probe; `None` leaves it unbounded. A probe
/// that fails to run, exits non-zero, or exceeds the deadline yields `None`,
/// so a hung plugin falls back to an "unknown" language instead of blocking
/// discovery.
fn probe_plugin_language(path: &Path, timeout: Option<u64>) -> Option<String> {
    let mut cmd = Command::new(path);
    cmd.arg("--info");

    let output = match process::run_process(&mut cmd, timeout.map(Duration::from_secs)).ok()? {
        Run::Completed(out) => out,
        Run::TimedOut => return None,
    };

    if !output.status.success() {
        return None;
    }

    #[derive(serde::Deserialize)]
    struct PluginInfo {
        language: String,
    }

    let info: PluginInfo = serde_json::from_slice(&output.stdout).ok()?;
    Some(info.language)
}

/// build a [`Command`] for an external plugin, setting the shared flags and
/// environment variables derived from [`ScanOpts`].
fn build_plugin_cmd(path: &Path, opts: &ScanOpts) -> Command {
    let mut cmd = Command::new(path);
    cmd.arg("--format").arg("json");

    cmd.env(
        "UNSAFE_BUDGET_WORKSPACE_ONLY",
        opts.workspace_only.to_string(),
    );
    cmd.env("UNSAFE_BUDGET_INCLUDE_DEPS", opts.include_deps.to_string());
    cmd.env("UNSAFE_BUDGET_ALL_FEATURES", opts.all_features.to_string());
    cmd.env(
        "UNSAFE_BUDGET_NO_DEFAULT_FEATURES",
        opts.no_default_features.to_string(),
    );
    cmd.env("UNSAFE_BUDGET_ALL_TARGETS", opts.all_targets.to_string());

    if !opts.features.is_empty() {
        cmd.env("UNSAFE_BUDGET_FEATURES", opts.features.join(","));
    }
    if !opts.targets.is_empty() {
        cmd.env("UNSAFE_BUDGET_TARGETS", opts.targets.join(","));
    }
    if let Some(ref manifest) = opts.manifest_path {
        cmd.env("UNSAFE_BUDGET_MANIFEST_PATH", manifest);
    }

    cmd
}

/// run an external plugin, parse its output, and enforce the host contract.
pub fn run_plugin(id: &str, path: &Path, opts: &ScanOpts) -> Result<ScanResult> {
    let mut cmd = build_plugin_cmd(path, opts);
    let timeout_secs = opts.plugin_timeout_secs;

    match process::run_process(&mut cmd, timeout_secs.map(Duration::from_secs))? {
        Run::Completed(out) => {
            parse_plugin_output(id, path, out.status, &out.stdout, &out.stderr, opts)
        }
        Run::TimedOut => Err(Error::Plugin(format!(
            "plugin {} timed out after {}s",
            path.display(),
            timeout_secs.unwrap_or_default()
        ))),
    }
}

/// parse a completed plugin's output into a sanitized [`ScanResult`].
fn parse_plugin_output(
    id: &str,
    path: &std::path::Path,
    status: std::process::ExitStatus,
    stdout: &[u8],
    stderr: &[u8],
    opts: &ScanOpts,
) -> Result<ScanResult> {
    if !status.success() {
        let stderr_str = String::from_utf8_lossy(stderr);
        return Err(Error::Plugin(format!(
            "plugin {} exited with {}: {}",
            path.display(),
            status,
            stderr_str
        )));
    }

    let result: ScanResult = serde_json::from_slice(stdout).map_err(|e| {
        Error::Plugin(format!(
            "failed to parse plugin output from {}: {}",
            path.display(),
            e
        ))
    })?;

    Ok(sanitize_plugin_result(id, opts, result))
}

/// overwrite the host-authoritative fields of a plugin's self-reported result.
///
/// `analyzer_id` and `scope` are set to the host's values, dependency units are
/// filtered per `opts`, and `totals` are recomputed from the surviving units.
/// `tool_version`, `language`, and findings are kept as the plugin reported them.
fn sanitize_plugin_result(id: &str, opts: &ScanOpts, result: ScanResult) -> ScanResult {
    let counts: HashMap<String, (UnitKind, u64)> = result
        .units
        .into_iter()
        .map(|u| (u.name, (u.kind, u.unsafe_count)))
        .collect();
    let (units, details) = aggregate_units(counts, result.details, opts);
    let totals = Totals::from_units(&units);

    ScanResult {
        tool_version: result.tool_version,
        analyzer_id: id.to_string(),
        language: result.language,
        scope: Scope::from(opts),
        units,
        totals,
        details,
        parse_warnings: result.parse_warnings,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::analyzer::test_spawn_guard;
    use crate::model::{Occurrence, Unit};
    use std::fs;
    use std::os::unix::fs::PermissionsExt;
    use tempfile::TempDir;

    fn make_file(dir: &std::path::Path, name: &str, mode: u32) -> PathBuf {
        let p = dir.join(name);
        fs::write(&p, "").unwrap();
        fs::set_permissions(&p, fs::Permissions::from_mode(mode)).unwrap();
        p
    }

    fn make_script(dir: &std::path::Path, name: &str, body: &str) -> PathBuf {
        use std::io::Write;
        let p = dir.join(name);
        let mut f = fs::File::create(&p).unwrap();
        f.write_all(format!("#!/bin/sh\n{body}").as_bytes())
            .unwrap();
        f.sync_all().unwrap();
        drop(f);
        fs::set_permissions(&p, fs::Permissions::from_mode(0o755)).unwrap();
        p
    }

    // --- is_executable ---

    #[test]
    fn executable_file_returns_true() {
        let tmp = TempDir::new().unwrap();
        let p = make_file(tmp.path(), "exe", 0o755);
        assert!(is_executable(&p));
    }

    #[test]
    fn non_executable_file_returns_false() {
        let tmp = TempDir::new().unwrap();
        let p = make_file(tmp.path(), "noexe", 0o644);
        assert!(!is_executable(&p));
    }

    #[test]
    fn directory_returns_false() {
        let tmp = TempDir::new().unwrap();
        let dir = tmp.path().join("subdir");
        fs::create_dir(&dir).unwrap();
        fs::set_permissions(&dir, fs::Permissions::from_mode(0o755)).unwrap();
        assert!(!is_executable(&dir));
    }

    #[test]
    fn nonexistent_path_returns_false() {
        assert!(!is_executable(&PathBuf::from("/no/such/path")));
    }

    #[test]
    fn group_execute_only_returns_true() {
        let tmp = TempDir::new().unwrap();
        let p = make_file(tmp.path(), "gexe", 0o610);
        assert!(is_executable(&p));
    }

    #[test]
    fn other_execute_only_returns_true() {
        let tmp = TempDir::new().unwrap();
        let p = make_file(tmp.path(), "oexe", 0o601);
        assert!(is_executable(&p));
    }

    // --- probe_plugin_language ---

    #[test]
    fn probe_parses_language_from_info_json() {
        let _lock = test_spawn_guard();
        let tmp = TempDir::new().unwrap();
        let p = make_script(tmp.path(), "plugin", r#"echo '{"language":"python"}'"#);
        assert_eq!(probe_plugin_language(&p, None), Some("python".into()));
    }

    #[test]
    fn probe_returns_none_on_missing_field() {
        let _lock = test_spawn_guard();
        let tmp = TempDir::new().unwrap();
        let p = make_script(tmp.path(), "plugin", r#"echo '{"version":"1"}'"#);
        assert_eq!(probe_plugin_language(&p, None), None);
    }

    #[test]
    fn probe_returns_none_on_non_zero_exit() {
        let _lock = test_spawn_guard();
        let tmp = TempDir::new().unwrap();
        let p = make_script(tmp.path(), "plugin", "exit 1");
        assert_eq!(probe_plugin_language(&p, None), None);
    }

    #[test]
    fn probe_returns_none_for_nonexistent_binary() {
        let _lock = test_spawn_guard();
        assert_eq!(
            probe_plugin_language(&PathBuf::from("/no/such/binary"), None),
            None
        );
    }

    #[test]
    fn probe_times_out_slow_plugin() {
        // serialize spawns to avoid the ETXTBSY fork-race (see the run_plugin
        // timeout tests below).
        let _lock = test_spawn_guard();
        // `exec sleep` replaces the shell in place so the child *is* sleep;
        // killing it on timeout closes the pipes directly (no orphaned
        // grandchild holding them open).
        let tmp = TempDir::new().unwrap();
        let p = make_script(tmp.path(), "unsafe-budget-plugin-slow", "exec sleep 60\n");
        // the probe outlives the 1s deadline, so it is killed and reported as a
        // failed probe (None) rather than hanging for the full 60s.
        assert_eq!(probe_plugin_language(&p, Some(1)), None);
    }

    #[test]
    fn probe_fast_plugin_completes_within_timeout() {
        let _lock = test_spawn_guard();
        // a plugin that answers immediately finishes well inside the deadline,
        // so a set timeout does not interfere with a normal probe.
        let tmp = TempDir::new().unwrap();
        let p = make_script(
            tmp.path(),
            "unsafe-budget-plugin-fast",
            r#"echo '{"language":"python"}'"#,
        );
        assert_eq!(probe_plugin_language(&p, Some(10)), Some("python".into()));
    }

    // --- discover_plugins ---

    #[test]
    fn discover_finds_plugin_on_path() {
        let _lock = test_spawn_guard();
        let tmp = TempDir::new().unwrap();
        make_script(
            tmp.path(),
            "unsafe-budget-plugin-demo",
            r#"echo '{"language":"demo-lang"}'"#,
        );

        let old_path = std::env::var("PATH").unwrap_or_default();
        std::env::set_var("PATH", format!("{}:{old_path}", tmp.path().display()));

        let plugins = discover_plugins(None);

        std::env::set_var("PATH", &old_path);

        let found = plugins.iter().find(|p| p.id == "demo");
        assert!(found.is_some(), "plugin 'demo' should be discovered");
        let info = found.unwrap();
        assert_eq!(info.language, "demo-lang");
        assert!(!info.builtin);
        assert!(info.path.is_some());
    }

    #[test]
    fn discover_ignores_non_executable_plugin() {
        let _lock = test_spawn_guard();
        let tmp = TempDir::new().unwrap();
        make_file(tmp.path(), "unsafe-budget-plugin-noexe", 0o644);

        let old_path = std::env::var("PATH").unwrap_or_default();
        std::env::set_var("PATH", format!("{}:{old_path}", tmp.path().display()));

        let plugins = discover_plugins(None);

        std::env::set_var("PATH", &old_path);

        assert!(
            plugins.iter().all(|p| p.id != "noexe"),
            "non-executable file should not be discovered"
        );
    }

    #[test]
    fn discover_ignores_files_without_prefix() {
        let _lock = test_spawn_guard();
        let tmp = TempDir::new().unwrap();
        make_script(tmp.path(), "some-other-tool", r#"echo '{}'"#);

        let old_path = std::env::var("PATH").unwrap_or_default();
        std::env::set_var("PATH", format!("{}:{old_path}", tmp.path().display()));

        let plugins = discover_plugins(None);

        std::env::set_var("PATH", &old_path);

        assert!(
            plugins.iter().all(|p| p.id != "some-other-tool"),
            "files without the plugin prefix should be ignored"
        );
    }

    #[test]
    fn discover_falls_back_to_unknown_language() {
        let _lock = test_spawn_guard();
        let tmp = TempDir::new().unwrap();
        make_script(tmp.path(), "unsafe-budget-plugin-bad", "exit 1");

        let old_path = std::env::var("PATH").unwrap_or_default();
        std::env::set_var("PATH", format!("{}:{old_path}", tmp.path().display()));

        let plugins = discover_plugins(None);

        std::env::set_var("PATH", &old_path);

        let found = plugins.iter().find(|p| p.id == "bad");
        assert!(found.is_some());
        assert_eq!(found.unwrap().language, "unknown");
    }

    // --- run_plugin timeout ---

    #[test]
    fn run_plugin_times_out_slow_plugin() {
        // serialize with the other script-writing tests: the timeout path spawns
        // the child in its own process group (the fork path), and that fork must
        // not overlap another test's freshly-written-but-not-yet-exec'd plugin
        // file, or that test's exec races with our inherited write fd (ETXTBSY).
        let _lock = test_spawn_guard();
        // `exec sleep` replaces the shell in place, so the spawned child *is*
        // sleep: killing it on timeout closes the pipes directly, leaving no
        // orphaned grandchild holding them open (which would hang the drain
        // threads).
        let tmp = TempDir::new().unwrap();
        let p = make_script(tmp.path(), "unsafe-budget-plugin-slow", "exec sleep 60\n");
        let opts = ScanOpts {
            plugin_timeout_secs: Some(1),
            ..Default::default()
        };
        let err = run_plugin("slow", &p, &opts).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("timed out"),
            "expected timeout error, got: {msg}"
        );
    }

    #[test]
    fn run_plugin_allows_fast_plugin() {
        // serialize spawns to avoid the ETXTBSY fork-race (see the slow-plugin
        // test above).
        let _lock = test_spawn_guard();
        // the plugin exits quickly; the error is a JSON parse failure, not a
        // timeout, proving it ran to completion within the deadline.
        let tmp = TempDir::new().unwrap();
        let p = make_script(tmp.path(), "unsafe-budget-plugin-fast", "echo not-json\n");
        let opts = ScanOpts {
            plugin_timeout_secs: Some(10),
            ..Default::default()
        };
        let err = run_plugin("fast", &p, &opts).unwrap_err();
        let msg = err.to_string();
        assert!(
            msg.contains("failed to parse"),
            "expected parse error (not timeout), got: {msg}"
        );
    }

    #[test]
    fn discover_returns_sorted_and_deduped() {
        let _lock = test_spawn_guard();
        let tmp1 = TempDir::new().unwrap();
        let tmp2 = TempDir::new().unwrap();
        make_script(tmp1.path(), "unsafe-budget-plugin-zzz", "exit 1");
        make_script(tmp1.path(), "unsafe-budget-plugin-aaa", "exit 1");
        make_script(tmp2.path(), "unsafe-budget-plugin-aaa", "exit 1");

        let old_path = std::env::var("PATH").unwrap_or_default();
        std::env::set_var(
            "PATH",
            format!(
                "{}:{}:{old_path}",
                tmp1.path().display(),
                tmp2.path().display()
            ),
        );

        let plugins = discover_plugins(None);

        std::env::set_var("PATH", &old_path);

        let ids: Vec<&str> = plugins.iter().map(|p| p.id.as_str()).collect();
        let aaa_count = ids.iter().filter(|&&id| id == "aaa").count();
        assert_eq!(aaa_count, 1, "duplicates should be removed");

        if let Some(aaa_pos) = ids.iter().position(|&id| id == "aaa") {
            if let Some(zzz_pos) = ids.iter().position(|&id| id == "zzz") {
                assert!(aaa_pos < zzz_pos, "plugins should be sorted by id");
            }
        }
    }

    // --- sanitize_plugin_result ---

    fn host_scope() -> Scope {
        Scope {
            workspace_only: false,
            include_deps: true,
            features: vec![],
            all_features: false,
            no_default_features: false,
            all_targets: false,
            targets: vec![],
            manifest_path: None,
        }
    }

    fn plugin_scan_result(
        scope: Scope,
        units: Vec<Unit>,
        totals: Totals,
        details: Vec<Occurrence>,
    ) -> ScanResult {
        ScanResult {
            tool_version: "9.9.9".into(),
            analyzer_id: "plugin-self-reported".into(),
            language: "cpp".into(),
            scope,
            units,
            totals,
            details,
            parse_warnings: vec![],
        }
    }

    #[test]
    fn sanitize_drops_dep_units_when_deps_excluded() {
        let opts = ScanOpts {
            include_deps: false,
            ..Default::default()
        };
        let units = vec![
            Unit {
                name: "app".into(),
                kind: UnitKind::Workspace,
                unsafe_count: 3,
            },
            Unit {
                name: "libc".into(),
                kind: UnitKind::Dep,
                unsafe_count: 10,
            },
        ];
        let details = vec![
            Occurrence {
                unit: "app".into(),
                file: "src/lib.rs".into(),
                line: 1,
                col: 1,
                message: None,
            },
            Occurrence {
                unit: "libc".into(),
                file: "libc/lib.rs".into(),
                line: 2,
                col: 1,
                message: None,
            },
        ];
        let totals = Totals {
            workspace_unsafe: 3,
            deps_unsafe: 10,
            overall_unsafe: 13,
        };
        let out = sanitize_plugin_result(
            "plug",
            &opts,
            plugin_scan_result(host_scope(), units, totals, details),
        );

        assert_eq!(out.units.len(), 1);
        assert_eq!(out.units[0].name, "app");
        assert_eq!(out.totals.deps_unsafe, 0);
        assert_eq!(out.totals.overall_unsafe, 3);
        assert!(
            out.details.iter().all(|d| d.unit == "app"),
            "dependency occurrences must be dropped"
        );
    }

    #[test]
    fn sanitize_recomputes_totals_from_units() {
        let opts = ScanOpts {
            include_deps: true,
            ..Default::default()
        };
        let units = vec![
            Unit {
                name: "app".into(),
                kind: UnitKind::Workspace,
                unsafe_count: 4,
            },
            Unit {
                name: "dep".into(),
                kind: UnitKind::Dep,
                unsafe_count: 6,
            },
        ];
        let totals = Totals {
            workspace_unsafe: 999,
            deps_unsafe: 999,
            overall_unsafe: 999,
        };
        let out = sanitize_plugin_result(
            "plug",
            &opts,
            plugin_scan_result(host_scope(), units, totals, vec![]),
        );

        assert_eq!(out.totals.workspace_unsafe, 4);
        assert_eq!(out.totals.deps_unsafe, 6);
        assert_eq!(out.totals.overall_unsafe, 10);
    }

    #[test]
    fn sanitize_overrides_scope_with_host_scope() {
        let opts = ScanOpts {
            features: vec!["hostfeat".into()],
            ..Default::default()
        };
        let wrong = Scope {
            workspace_only: true,
            include_deps: false,
            features: vec!["pluginfeat".into()],
            all_features: true,
            no_default_features: true,
            all_targets: true,
            targets: vec!["wrong-target".into()],
            manifest_path: Some("/wrong/Cargo.toml".into()),
        };
        let out = sanitize_plugin_result(
            "plug",
            &opts,
            plugin_scan_result(wrong, vec![], Totals::default(), vec![]),
        );

        assert_eq!(out.scope, Scope::from(&opts));
        assert!(!out.scope.workspace_only);
        assert_eq!(out.scope.features, vec!["hostfeat"]);
    }

    #[test]
    fn sanitize_overrides_analyzer_id_with_host_id() {
        let opts = ScanOpts::default();
        let out = sanitize_plugin_result(
            "plug",
            &opts,
            plugin_scan_result(host_scope(), vec![], Totals::default(), vec![]),
        );
        assert_eq!(out.analyzer_id, "plug");
    }

    #[test]
    fn sanitize_preserves_plugin_tool_version_and_language() {
        let opts = ScanOpts::default();
        let out = sanitize_plugin_result(
            "plug",
            &opts,
            plugin_scan_result(host_scope(), vec![], Totals::default(), vec![]),
        );
        assert_eq!(out.tool_version, "9.9.9");
        assert_eq!(out.language, "cpp");
    }

    #[test]
    fn run_plugin_sanitizes_output_end_to_end() {
        let _lock = test_spawn_guard();
        let tmp = TempDir::new().unwrap();
        let body = r#"cat <<'EOF'
{"tool_version":"2.0.0","analyzer_id":"self","language":"cpp",
 "scope":{"workspace_only":true,"include_deps":false,"features":[],"all_targets":false,"targets":[]},
 "units":[{"name":"app","kind":"workspace","unsafe_count":2},{"name":"dep","kind":"dep","unsafe_count":9}],
 "totals":{"workspace_unsafe":100,"deps_unsafe":100,"overall_unsafe":200}}
EOF
"#;
        let p = make_script(tmp.path(), "unsafe-budget-plugin-e2e", body);
        let opts = ScanOpts {
            include_deps: false,
            ..Default::default()
        };
        let out = run_plugin("e2e", &p, &opts).unwrap();

        assert_eq!(out.analyzer_id, "e2e");
        assert_eq!(out.tool_version, "2.0.0");
        assert_eq!(out.units.len(), 1);
        assert_eq!(out.units[0].name, "app");
        assert_eq!(out.totals.overall_unsafe, 2);
        assert_eq!(out.scope, Scope::from(&opts));
    }
}