Skip to main content

solcompat_core/
report.rs

1//! Report finalization, suppression policy, and semantic verdict selection.
2
3use crate::{CheckResult, Counts, Outcome, Report, Severity, Suppression};
4use anyhow::{ensure, Result};
5
6pub(crate) fn apply_suppressions(
7    results: &mut [CheckResult],
8    suppressions: &[Suppression],
9) -> Result<()> {
10    for suppression in suppressions {
11        let matches: Vec<_> = results
12            .iter()
13            .enumerate()
14            .filter(|(_, result)| {
15                result.rule_id == suppression.rule && result.subject == suppression.subject
16            })
17            .map(|(i, _)| i)
18            .collect();
19        ensure!(
20            !matches.is_empty(),
21            "suppression does not match an evaluated rule/subject: {} {}",
22            suppression.rule,
23            suppression.subject
24        );
25        ensure!(matches.len()==1,"suppression is ambiguous for rule/subject: {} {}; use a unique read/component identity",suppression.rule,suppression.subject);
26        let result = &mut results[matches[0]];
27        ensure!(
28            result.suppression.is_none(),
29            "duplicate suppression for {}",
30            suppression.subject
31        );
32        ensure!(
33            matches!(result.outcome, Outcome::Finding | Outcome::Unknown),
34            "suppression must target a finding or unknown: {}",
35            suppression.subject
36        );
37        result.suppression = Some(suppression.clone());
38    }
39    Ok(())
40}
41
42impl Report {
43    /// Sort findings, recalculate counts, and apply the configured exit policy.
44    /// Suppressed results are counted separately and do not affect the exit code.
45    pub fn finish(&mut self) {
46        self.results
47            .sort_by(|a, b| (&a.subject, &a.rule_id).cmp(&(&b.subject, &b.rule_id)));
48        self.counts = Counts::default();
49        self.exit_code = 0;
50        for result in &self.results {
51            if result.suppression.is_some() {
52                self.counts.suppressed += 1;
53                continue;
54            }
55            match result.outcome {
56                Outcome::Pass => self.counts.passed += 1,
57                Outcome::Unknown => {
58                    self.counts.unknown += 1;
59                    if self.policy.deny_unknown {
60                        self.exit_code = 1;
61                    }
62                }
63                Outcome::NotApplicable => self.counts.not_applicable += 1,
64                Outcome::Skipped => {
65                    self.counts.skipped += 1;
66                    if self.policy.deny_unknown {
67                        self.exit_code = 1;
68                    }
69                }
70                Outcome::Finding => match result.severity {
71                    Some(Severity::Error) => {
72                        self.counts.errors += 1;
73                        self.exit_code = 1;
74                    }
75                    Some(Severity::Warning) => {
76                        self.counts.warnings += 1;
77                        if self.policy.deny_warnings {
78                            self.exit_code = 1;
79                        }
80                    }
81                    _ => self.counts.info += 1,
82                },
83            }
84        }
85    }
86}
87
88/// Semantic verdict used by every terminal renderer. This is not a new wire field.
89#[derive(Clone, Copy, Debug, PartialEq, Eq)]
90pub enum Verdict {
91    Inventory,
92    BreakingChanges,
93    UpgradeReview,
94    Incompatible,
95    InputPolicyFailure,
96    Incomplete,
97    WarningPolicyFailure,
98    Warnings,
99    Notes,
100    Suppressions,
101    NoUpgradeAdvice,
102    NoChecks,
103    Compatible,
104}
105
106impl Report {
107    /// Select the existing verdict using finalized counts and report policy.
108    /// Preserve priority between upgrade advice, errors, unknowns, and warnings.
109    pub fn verdict(&self) -> Verdict {
110        if self.command == "inspect" {
111            return Verdict::Inventory;
112        }
113        let visible = self
114            .results
115            .iter()
116            .any(|result| result.outcome != Outcome::NotApplicable);
117        if self.command == "upgrade" && self.counts.errors > 0 {
118            Verdict::BreakingChanges
119        } else if self.command == "upgrade" && self.counts.warnings > 0 {
120            Verdict::UpgradeReview
121        } else if self.counts.errors > 0 {
122            Verdict::Incompatible
123        } else if (self.counts.unknown > 0 || self.counts.skipped > 0) && self.policy.deny_unknown {
124            Verdict::InputPolicyFailure
125        } else if self.counts.unknown > 0 || self.counts.skipped > 0 {
126            Verdict::Incomplete
127        } else if self.counts.warnings > 0 && self.policy.deny_warnings {
128            Verdict::WarningPolicyFailure
129        } else if self.counts.warnings > 0 {
130            Verdict::Warnings
131        } else if self.counts.info > 0 {
132            Verdict::Notes
133        } else if self.counts.suppressed > 0 {
134            Verdict::Suppressions
135        } else if !visible && self.command == "upgrade" {
136            Verdict::NoUpgradeAdvice
137        } else if !visible {
138            Verdict::NoChecks
139        } else {
140            Verdict::Compatible
141        }
142    }
143}
144
145#[cfg(test)]
146mod tests {
147    use super::*;
148    use crate::{DatasetIdentity, Policy, Project, Verdict};
149
150    fn finding(outcome: Outcome, severity: Option<Severity>) -> CheckResult {
151        CheckResult {
152            rule_id: "SC201".into(),
153            rule_name: "test".into(),
154            rule_revision: 1,
155            subject: "client/read".into(),
156            operation: "getBlock".into(),
157            conditional: false,
158            outcome,
159            severity,
160            title: "test".into(),
161            summary: String::new(),
162            explanation: String::new(),
163            evidence: vec![],
164            sources: vec![],
165            remediation: None,
166            suppression: None,
167        }
168    }
169    fn report(results: Vec<CheckResult>, policy: crate::Policy) -> Report {
170        Report {
171            schema_version: 1,
172            command: "check".into(),
173            analysis_mode: "static".into(),
174            target: "local".into(),
175            target_digest: None,
176            dataset: DatasetIdentity {
177                revision: "test".into(),
178                digest: "test".into(),
179            },
180            project: Project {
181                name: "test".into(),
182                clients: vec![],
183                programs: vec![],
184                idls: vec![],
185                artifacts: vec![],
186                tools: vec![],
187            },
188            results,
189            counts: Counts::default(),
190            policy,
191            exit_code: 0,
192        }
193    }
194
195    #[test]
196    fn outcome_policy_table_preserves_verdicts_counts_and_exits() {
197        for (outcome, severity, default, strict, default_exit, strict_exit) in [
198            (
199                Outcome::Pass,
200                None,
201                Verdict::Compatible,
202                Verdict::Compatible,
203                0,
204                0,
205            ),
206            (
207                Outcome::Finding,
208                Some(Severity::Error),
209                Verdict::Incompatible,
210                Verdict::Incompatible,
211                1,
212                1,
213            ),
214            (
215                Outcome::Finding,
216                Some(Severity::Warning),
217                Verdict::Warnings,
218                Verdict::WarningPolicyFailure,
219                0,
220                1,
221            ),
222            (
223                Outcome::Finding,
224                Some(Severity::Info),
225                Verdict::Notes,
226                Verdict::Notes,
227                0,
228                0,
229            ),
230            (Outcome::Finding, None, Verdict::Notes, Verdict::Notes, 0, 0),
231            (
232                Outcome::Unknown,
233                None,
234                Verdict::Incomplete,
235                Verdict::InputPolicyFailure,
236                0,
237                1,
238            ),
239            (
240                Outcome::NotApplicable,
241                None,
242                Verdict::NoChecks,
243                Verdict::NoChecks,
244                0,
245                0,
246            ),
247            // Skipped checks fail a strict completeness policy just like unknowns.
248            (
249                Outcome::Skipped,
250                None,
251                Verdict::Incomplete,
252                Verdict::InputPolicyFailure,
253                0,
254                1,
255            ),
256        ] {
257            for (policy, expected, exit) in [
258                (Policy::default(), default, default_exit),
259                (
260                    Policy {
261                        deny_warnings: true,
262                        deny_unknown: true,
263                    },
264                    strict,
265                    strict_exit,
266                ),
267            ] {
268                let mut value = report(vec![finding(outcome, severity)], policy);
269                value.finish();
270                assert_eq!(value.verdict(), expected);
271                assert_eq!(value.exit_code, exit);
272                let c = &value.counts;
273                assert_eq!(
274                    c.passed
275                        + c.errors
276                        + c.warnings
277                        + c.info
278                        + c.unknown
279                        + c.not_applicable
280                        + c.skipped
281                        + c.suppressed,
282                    1
283                );
284                let counts = value.counts.clone();
285                value.finish();
286                assert_eq!(value.counts, counts);
287            }
288        }
289    }
290
291    #[test]
292    fn suppression_rejects_ambiguous_matches_and_invalid_targets() {
293        let mut results = vec![
294            finding(Outcome::Finding, Some(Severity::Error)),
295            finding(Outcome::Finding, Some(Severity::Warning)),
296        ];
297        let suppression = Suppression {
298            rule: "SC201".into(),
299            subject: "client/read".into(),
300            reason: "accepted".into(),
301        };
302        assert!(
303            apply_suppressions(&mut results, std::slice::from_ref(&suppression))
304                .unwrap_err()
305                .to_string()
306                .contains("ambiguous")
307        );
308        assert!(results.iter().all(|result| result.suppression.is_none()));
309        results[1].subject = "other/read".into();
310        apply_suppressions(&mut results, std::slice::from_ref(&suppression)).unwrap();
311        assert!(results[0].suppression.is_some());
312        assert!(results[1].suppression.is_none());
313        assert!(apply_suppressions(&mut results, &[suppression])
314            .unwrap_err()
315            .to_string()
316            .contains("duplicate suppression"));
317        let mut value = report(results, Policy::default());
318        value.finish();
319        assert_eq!(value.counts.suppressed, 1);
320        assert_eq!(value.counts.warnings, 1);
321        assert_eq!(value.counts.errors, 0);
322        assert_eq!(value.verdict(), Verdict::Warnings);
323        value.results.remove(1);
324        value.finish();
325        assert_eq!(value.verdict(), Verdict::Suppressions);
326        let mut passing = vec![finding(Outcome::Pass, None)];
327        let suppression = Suppression {
328            rule: "SC201".into(),
329            subject: "client/read".into(),
330            reason: "accepted".into(),
331        };
332        assert!(apply_suppressions(&mut passing, &[suppression]).is_err());
333    }
334
335    #[test]
336    fn upgrade_and_inventory_keep_their_existing_verdict_priority() {
337        let mut value = report(
338            vec![
339                finding(Outcome::Unknown, None),
340                finding(Outcome::Finding, Some(Severity::Warning)),
341            ],
342            Policy {
343                deny_warnings: true,
344                deny_unknown: true,
345            },
346        );
347        value.finish();
348        assert_eq!(value.verdict(), Verdict::InputPolicyFailure);
349        value.command = "upgrade".into();
350        assert_eq!(value.verdict(), Verdict::UpgradeReview);
351        value
352            .results
353            .push(finding(Outcome::Finding, Some(Severity::Error)));
354        value.finish();
355        assert_eq!(value.verdict(), Verdict::BreakingChanges);
356        value.command = "inspect".into();
357        assert_eq!(value.verdict(), Verdict::Inventory);
358        let mut empty = report(
359            vec![finding(Outcome::NotApplicable, None)],
360            Policy::default(),
361        );
362        empty.command = "upgrade".into();
363        empty.finish();
364        assert_eq!(empty.verdict(), Verdict::NoUpgradeAdvice);
365    }
366}