Skip to main content

rto_exec/
adapter.rs

1//! Per-analyzer adapters: native analyzer output in, a [`NormalizedReport`] out.
2//!
3//! An adapter is the **only** analyzer-specific code in this crate. It knows one
4//! tool's native JSON, how to name that tool's findings so they are recognisable
5//! across runs, and which argv produces that JSON. Everything downstream — the
6//! validation, the identity keys, the ordering, the store — is shared.
7//!
8//! # Why this is the seam, and not the runner
9//!
10//! ADR-0012 requires that "a finding is the same artifact whether it was produced
11//! locally in a sandbox or ingested from a CI report". The cheap way to satisfy
12//! that is to write the conversion twice and add a test comparing the two. This
13//! crate does the other thing: **there is one conversion**, and both paths call
14//! it. A subprocess run captures the analyzer's stdout and hands those bytes to
15//! the adapter; `roteiro security ingest` reads a file of the same native bytes
16//! and hands them to the same adapter. Equality of the resulting [`Finding`]s is
17//! therefore a property of the code, and the tests that assert it are guarding
18//! against a future refactor rather than establishing the invariant.
19//!
20//! [`Finding`]: rto_graph::Finding
21//!
22//! # Adding an analyzer needs no migration
23//!
24//! [`rto_graph::FindingKey`] is `finding:<analyzer>:<that analyzer's own ordered
25//! identity components>`. An adapter chooses the recipe; the schema never learns
26//! what the components mean. So a new analyzer is a new file in `adapters/`, an
27//! entry in [`ADAPTERS`], and nothing else — no schema change, no migration.
28//!
29//! @rto:0012
30//! @rto:0014
31//! @rto:0018
32
33use rto_graph::SourceIdentity;
34
35use crate::guidance::{Guidance, Line};
36use crate::ingest::NormalizedReport;
37use crate::runner::ExecError;
38use crate::snippet::SnippetSource;
39
40pub mod cargo_audit;
41pub mod clippy;
42pub mod osv_scanner;
43pub mod semgrep;
44
45/// Everything an adapter may need that is *not* in the analyzer's own output.
46///
47/// Native analyzer output is missing things the evidence chain requires — no
48/// mainstream analyzer stamps its report with the wall-clock window it ran in,
49/// and `cargo audit` does not even record its own version. Rather than let an
50/// adapter invent them, the caller supplies what it actually knows, and an
51/// adapter that has nothing better says so ([`UNKNOWN_VERSION`]).
52#[derive(Clone)]
53pub struct NativeContext<'a> {
54    /// When the run started, RFC 3339 UTC. A subprocess run measures it; an
55    /// ingest of a report file uses the file's modification time, which is the
56    /// only timestamp evidence a bare report carries.
57    pub started_at: String,
58    /// When the run ended, RFC 3339 UTC.
59    pub ended_at: String,
60    /// The analyzer's version, where the caller learned it out of band (a
61    /// subprocess run asks the binary). `None` leaves the adapter to use
62    /// whatever the report itself carries.
63    pub analyzer_version: Option<String>,
64    /// The analyzer's process exit status, where the caller observed it.
65    pub exit_status: i32,
66    /// The source identity the run was against. Some identity recipes need it —
67    /// `cargo-audit` keys findings by lockfile blob, so a finding stays distinct
68    /// when the lockfile changes underneath the same advisory.
69    pub source: &'a SourceIdentity,
70    /// Digest of the rule set the analyzer ran with, where one applies.
71    pub rules_digest: Option<String>,
72    /// The pinned advisory database the caller provisioned, where one applies.
73    ///
74    /// A fallback, not an override: an adapter prefers what the analyzer's own
75    /// report says about the database it consulted, and uses this only when the
76    /// report says nothing. `cargo audit` says nothing whenever it is pointed at
77    /// a database with `--db`, which is every pinned run — so without this, the
78    /// reproducible configuration would be the one with no staleness evidence.
79    pub advisory_db: Option<rto_graph::AdvisoryDb>,
80    /// The checkout the report describes, where the caller knows it.
81    ///
82    /// Only an adapter whose analyzer reports **absolute** paths needs this, and
83    /// `osv-scanner` is that adapter: it returns a full filesystem path for every
84    /// manifest even when it is told to scan `.`. Without the worktree there is
85    /// nothing to relativise against, so an absolute path would be stored
86    /// verbatim — user-identifying data in a persisted finding key, and a key
87    /// that differs between two machines running the identical scan.
88    ///
89    /// `None` is the honest answer for a report about a tree this checkout does
90    /// not have; an adapter must then say the location is unknown rather than
91    /// guess at one.
92    pub worktree: Option<&'a std::path::Path>,
93    /// Where to read the source a finding points at, for identity recipes that
94    /// include a snippet hash.
95    ///
96    /// It is here rather than inside an adapter because the *caller* knows which
97    /// checkout the report describes, and because both execution paths must read
98    /// the same one — that is what makes a subprocess run and an ingest of its
99    /// output produce identical finding keys.
100    pub snippets: &'a dyn SnippetSource,
101}
102
103// Hand-written because `&dyn SnippetSource` is not `Debug` and does not need to
104// be: what a debug print of a context should show is the evidence it carries,
105// not the identity of the thing that reads files.
106impl std::fmt::Debug for NativeContext<'_> {
107    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108        f.debug_struct("NativeContext")
109            .field("started_at", &self.started_at)
110            .field("ended_at", &self.ended_at)
111            .field("analyzer_version", &self.analyzer_version)
112            .field("exit_status", &self.exit_status)
113            .field("source", self.source)
114            .field("rules_digest", &self.rules_digest)
115            .field("advisory_db", &self.advisory_db)
116            .field("worktree", &self.worktree)
117            .finish_non_exhaustive()
118    }
119}
120
121impl NativeContext<'_> {
122    /// The version to record: what the caller learned, else what the report
123    /// carried, else [`UNKNOWN_VERSION`].
124    ///
125    /// Never empty — [`crate::IngestRunner`] refuses a report that cannot say
126    /// what version produced it, and "unknown" is a truthful answer where an
127    /// empty string is a missing one.
128    #[must_use]
129    pub fn version_or(&self, from_report: Option<&str>) -> String {
130        self.analyzer_version
131            .as_deref()
132            .or(from_report)
133            .map(str::trim)
134            .filter(|v| !v.is_empty())
135            .unwrap_or(UNKNOWN_VERSION)
136            .to_owned()
137    }
138}
139
140/// Recorded as an analyzer's version when neither the caller nor the report
141/// knows it — which is the ordinary case for a `cargo audit` report ingested
142/// from CI, since its JSON has no version field.
143pub const UNKNOWN_VERSION: &str = "unknown";
144
145/// How an analyzer is invoked as a child process.
146///
147/// Returned by [`Adapter::command`] and consumed by the subprocess runner, so
148/// the argv lives beside the parser that understands its output rather than in
149/// the runner, which knows no analyzer.
150#[derive(Debug, Clone, PartialEq, Eq)]
151pub struct Invocation {
152    /// The program to execute, looked up on `PATH` unless it is a path.
153    pub program: String,
154    /// Its arguments, in order.
155    pub args: Vec<String>,
156    /// Exit statuses that mean "the analyzer ran and produced a report".
157    ///
158    /// Analyzers overload the exit status: `semgrep` exits `1` when it found
159    /// something, `cargo audit` exits `1` on a vulnerability. Treating non-zero
160    /// as failure would discard exactly the runs that matter, so each adapter
161    /// declares which statuses carry a usable report and every other status is a
162    /// hard failure.
163    pub success_statuses: Vec<i32>,
164}
165
166/// How to obtain one program from [`Adapter::host_programs`], for the refusal
167/// that discovers it is absent.
168///
169/// The counterpart of the refusal rule in `docs/REVIEW_CHECKLIST.md`: a refusal
170/// names the way forward, and *this* is the way forward for the one obstacle
171/// Roteiro will never clear on the reader's behalf. It is keyed **by program and
172/// not by analyzer** because a single analyzer's programs are obtained
173/// differently — `cargo-audit` needs `cargo` from rustup *and* `cargo-audit`
174/// from crates.io, and one hint covering both would be right about at most one
175/// of them. That is the "right *kind* of way forward" check, which is the one
176/// that has shipped wrong here before.
177///
178/// # What may go in one, and what may not
179///
180/// - **The command is upstream's, verbatim, or there is none.** Every command
181///   below was read off the tool's own install page at the time it was written,
182///   not recalled. Where upstream documents no single command — `osv-scanner`
183///   offers eight platform-specific ones and ranks none — the hint says so and
184///   gives the page. A plausible command that fails is worse than a URL.
185/// - **Never the reader's package manager.** No `brew`, no `apt`, unless
186///   upstream itself names one as *the* way. A canonical ecosystem command is
187///   portable and checkable; a package-manager guess is wrong for most readers.
188/// - **Always the upstream page.** A URL ages better than a command line, so
189///   even a hint with a good command carries the page that would correct it.
190/// - **Saying how is not doing it.** Nothing reads a hint and runs it. Roteiro
191///   installs no analyzer (ADR-0014), and a refusal that quietly installed one
192///   is the silent downgrade ADR-0019 §6 and ADR-0020 §6 forbid.
193#[derive(Debug, Clone, Copy, PartialEq, Eq)]
194pub struct InstallHint {
195    /// The program this obtains — an entry in the same adapter's
196    /// [`Adapter::host_programs`], which is what
197    /// `tests::every_host_program_has_an_install_hint` pairs them by.
198    pub program: &'static str,
199    /// What to tell the reader, rendered by [`Guidance`] so a message built from
200    /// it cannot lose its own indentation. See [`crate::guidance`].
201    pub guidance: Guidance,
202}
203
204/// Obtaining `cargo` or `rustc`: the toolchain itself, not a tool installed with
205/// it.
206///
207/// Shared by the two adapters that shell out to cargo, so the answer to "how do
208/// I get cargo" cannot drift into two answers. **Deliberately no command:**
209/// rustup's installer is a different shell line on every host, and printing one
210/// of them is exactly the platform guess the refusals checklist forbids. Its
211/// front page picks the right one, which is why the page *is* the answer here.
212///
213/// So this hint has a `Note` and no [`Line::Command`], which is what "no
214/// command" has to mean if the sentence above is to be true of the code under
215/// it. It read `Line::Command("https://rustup.rs")` for one revision — a URL
216/// promoted into the slot a command would have occupied, three lines under a
217/// comment denying there was one. See [`URL_PREFIX`].
218pub const RUST_TOOLCHAIN: Guidance = Guidance::new(&[
219    Line::Note(&[
220        "Roteiro does not install toolchains. Install Rust — rustup's front page",
221        "selects the right installer for this host, so there is nothing to paste",
222        "here.",
223    ]),
224    Line::Note(&["Upstream: https://rustup.rs"]),
225]);
226
227/// How every install hint introduces its upstream page.
228///
229/// One convention, named once, because there were two. Three hints carried the
230/// URL as a `Note` reading `Upstream: …` and two promoted it into a
231/// [`Line::Command`] — and both of the two were the hints whose prose said they
232/// had *no* command, so the odd rendering and the contradicted comment were the
233/// same mistake seen from either end.
234///
235/// The `Note` is the right side of that split. [`Line::Command`] renders one
236/// step further in, as the thing to copy and run; a page is a thing to *read*,
237/// and the label is what says which of the two a reader is looking at. Reserving
238/// the command slot for commands is also what lets a hint say "there is nothing
239/// to paste here" and be visibly telling the truth.
240///
241/// `tests::every_hint_renders_its_upstream_page_the_same_way` holds it, so a
242/// fifth adapter cannot introduce a third convention.
243pub const URL_PREFIX: &str = "Upstream: ";
244
245/// One analyzer's native output format and invocation.
246pub trait Adapter: Sync + std::fmt::Debug {
247    /// The analyzer id — the value that appears in every layer key and finding
248    /// key this adapter produces.
249    fn analyzer(&self) -> &'static str;
250
251    /// A one-line description of what it looks for, for `roteiro security
252    /// status` and `--help`.
253    fn summary(&self) -> &'static str;
254
255    /// The languages this adapter produces findings for, as the coverage matrix
256    /// in ADR-0018 states them. Reported by the CLI so the claim is inspectable
257    /// rather than only documented.
258    fn languages(&self) -> &'static [&'static str];
259
260    /// Which pinned assets the analyzer needs before it can run offline (see
261    /// [`crate::assets`]). An empty slice means it needs none.
262    fn asset_ids(&self) -> &'static [&'static str];
263
264    /// Which programs must be on `PATH` for this analyzer to run **on this host**,
265    /// in the order a reader would install them.
266    ///
267    /// The counterpart of [`Adapter::asset_ids`], and the reason both exist:
268    /// asset ids are what Roteiro *provisions*, and these are what it
269    /// deliberately **never installs** (ADR-0014). `roteiro security status`
270    /// reports the two separately because their remedies differ — `prefetch` for
271    /// the first, an install the host owner performs for the second — and
272    /// collapsing them into one word is issue #464.
273    ///
274    /// # Why this is declared and not read off [`Adapter::command`]
275    ///
276    /// Because [`Invocation::program`] is not always the thing to look for.
277    /// `cargo-audit`'s program is `cargo`, and `cargo audit` dispatches to a
278    /// separate `cargo-audit` binary on `PATH` — so probing `Invocation::program`
279    /// would find `cargo` on any Rust developer's machine and report *ready* in
280    /// precisely the commonest failure, `cargo` installed and `cargo-audit` not.
281    /// That is the defect #464 is about, reintroduced one level down. An adapter
282    /// therefore states its own requirement.
283    ///
284    /// Empty means the analyzer needs nothing on `PATH`.
285    fn host_programs(&self) -> &'static [&'static str];
286
287    /// How to obtain each of [`Adapter::host_programs`], one hint per program.
288    ///
289    /// Required rather than defaulted, and that is the whole point of it being
290    /// on the trait: a fifth analyzer cannot compile without answering, so it
291    /// cannot ship a refusal that names the obstacle and trails off. The
292    /// *pairing* is what
293    /// `tests::every_host_program_has_an_install_hint` checks — a hint for
294    /// some other program would satisfy the compiler and not the reader.
295    ///
296    /// Order and duplication do not matter; [`install_hint`] looks up by
297    /// program. Empty is correct only for an analyzer that needs nothing on
298    /// `PATH`, which no shipped adapter is.
299    fn install_hints(&self) -> &'static [InstallHint];
300
301    /// The argv that makes the analyzer emit the native format
302    /// [`Adapter::normalize`] parses, with egress configured off.
303    ///
304    /// `assets` maps an id from [`Adapter::asset_ids`] to the verified local
305    /// path it was provisioned to.
306    fn command(&self, assets: &AssetPaths<'_>) -> Invocation;
307
308    /// Parse native output into a normalized report.
309    ///
310    /// # Errors
311    /// Returns [`ExecError::MalformedReport`] when the bytes are not this
312    /// analyzer's format, or [`ExecError::Json`] when they are not JSON at all.
313    /// A partially-parsed report is never returned: either the whole thing
314    /// converts or the run fails.
315    fn normalize(
316        &self,
317        native: &[u8],
318        ctx: &NativeContext<'_>,
319    ) -> Result<NormalizedReport, ExecError>;
320}
321
322/// Verified local paths of an analyzer's provisioned assets, keyed by asset id.
323#[derive(Debug, Clone, Copy, Default)]
324pub struct AssetPaths<'a> {
325    entries: &'a [(&'a str, std::path::PathBuf)],
326}
327
328impl<'a> AssetPaths<'a> {
329    /// Wrap a resolved id → path list.
330    #[must_use]
331    pub fn new(entries: &'a [(&'a str, std::path::PathBuf)]) -> Self {
332        Self { entries }
333    }
334
335    /// The path provisioned for `id`, or `None` if it was not resolved.
336    ///
337    /// An adapter that asked for an asset in [`Adapter::asset_ids`] will always
338    /// find it here, because the runner refuses to start otherwise — see
339    /// [`ExecError::AssetsUnavailableOffline`].
340    #[must_use]
341    pub fn get(&self, id: &str) -> Option<&std::path::Path> {
342        self.entries
343            .iter()
344            .find(|(key, _)| *key == id)
345            .map(|(_, path)| path.as_path())
346    }
347
348    /// The path provisioned for `id` as a string, or an empty string. Adapters
349    /// build argv from this; an unresolved asset cannot reach here.
350    #[must_use]
351    pub fn arg(&self, id: &str) -> String {
352        self.get(id)
353            .map(|p| p.to_string_lossy().into_owned())
354            .unwrap_or_default()
355    }
356}
357
358/// Every analyzer whose findings this build can **store**.
359///
360/// Ingest consults this table, so a report from any of them can be read in from
361/// CI whether or not this build can *execute* the analyzer — which is the whole
362/// point of ADR-0014's "ingest is always available".
363///
364/// # `clippy` is an adapter and is deliberately not here
365///
366/// [`clippy::Clippy`] implements the trait above and is reached only by
367/// `roteiro lint`, which reports and stores nothing. Membership of this table is
368/// what makes an analyzer storable — it is how `ingest` resolves `--analyzer`,
369/// and everything it resolves ends at
370/// [`rto_graph::Store::replace_findings_layer`]. Leaving a linter out is
371/// therefore the mechanism, not a note: there is no `--analyzer clippy` to
372/// accept and no layer key for two runs at different toolchains to collide over.
373/// ADR-0020 v1.1 is the decision, and [`clippy`]'s module documentation is the
374/// reasoning. **Adding it here would silently make lint output an artifact.**
375pub static ADAPTERS: &[&dyn Adapter] = &[
376    &semgrep::Semgrep,
377    &cargo_audit::CargoAudit,
378    &osv_scanner::OsvScanner,
379];
380
381/// Every analyzer `roteiro lint` can run.
382///
383/// A separate list from [`known_analyzers`], which answers a different question
384/// — *what can be stored* — and would name `semgrep` and `cargo-audit` here,
385/// sending a caller off to ask for a lint from an analyzer that files layers.
386/// It sits beside [`ADAPTERS`] rather than in [`crate::lint`] so that the two
387/// lists are read together: they are the same shape and deliberately disjoint,
388/// and a name that drifted into both would make a lint storable by accident.
389///
390/// Ungated, unlike the linter itself, for [`crate::lint_grant`]'s reason: what
391/// `roteiro lint` *could* run is a question a build that cannot run it still has
392/// to answer, and `roteiro security prefetch --analyzer clippy` is one of the
393/// callers that asks.
394pub const LINT_ANALYZERS: &[&str] = &[clippy::ANALYZER];
395
396/// The adapters behind [`LINT_ANALYZERS`].
397///
398/// [`LINT_ANALYZERS`] names them and this one *is* them, because the callers
399/// differ: `prefetch` and the CLI want ids, and anything asking how to obtain a
400/// linter's binary wants the adapter. Kept in step by
401/// `tests::the_lint_tables_name_the_same_analyzers` rather than derived from
402/// each other, since neither can be `const`-derived from the other and a silent
403/// divergence would cost a linter its install hint.
404static LINT_ADAPTERS: &[&dyn Adapter] = &[&clippy::Clippy];
405
406/// The adapter for `analyzer`, or `None` if this build has none.
407///
408/// Storable analyzers only, deliberately: this is what `ingest` resolves
409/// `--analyzer` through, so answering for `clippy` here would make lint output
410/// storable — see [`ADAPTERS`]. Use [`every_adapter`] to ask a question that is
411/// about the tool rather than about the store.
412#[must_use]
413pub fn adapter_for(analyzer: &str) -> Option<&'static dyn Adapter> {
414    ADAPTERS.iter().copied().find(|a| a.analyzer() == analyzer)
415}
416
417/// Every adapter this build has, storable or not.
418///
419/// The set an install hint has to exist for, which is a wider set than
420/// [`ADAPTERS`]: `roteiro lint` reaches the same subprocess machinery, so a
421/// missing `cargo-clippy` produces the same refusal as a missing `semgrep` and
422/// deserves the same answer. Kept distinct from [`adapter_for`] so that widening
423/// *this* can never widen what `ingest` accepts.
424pub fn every_adapter() -> impl Iterator<Item = &'static dyn Adapter> {
425    ADAPTERS.iter().chain(LINT_ADAPTERS).copied()
426}
427
428/// How to obtain `program`, as declared by the adapter that needs it.
429///
430/// `None` for a program no adapter declares — which a refusal must then print
431/// without an install clause rather than with a guessed one. It cannot happen
432/// for a program reached through [`Adapter::command`], because
433/// `tests::every_host_program_has_an_install_hint` pairs the two lists and
434/// `tests::every_invoked_program_is_declared_on_path` ties the invocation to
435/// them.
436#[must_use]
437pub fn install_hint(program: &str) -> Option<Guidance> {
438    every_adapter()
439        .flat_map(Adapter::install_hints)
440        .find(|hint| hint.program == program)
441        .map(|hint| hint.guidance)
442}
443
444/// Every analyzer id this build can normalise, sorted — for error messages that
445/// tell a caller what it *could* have asked for.
446#[must_use]
447pub fn known_analyzers() -> Vec<&'static str> {
448    let mut ids: Vec<&'static str> = ADAPTERS.iter().map(|a| a.analyzer()).collect();
449    ids.sort_unstable();
450    ids
451}
452
453/// Recorded in place of a snippet hash when the source could not be read — an
454/// ingested report about a tree this checkout does not have.
455///
456/// A named marker rather than a hash of the empty string, so a reader of a
457/// finding key can tell "the code was empty" from "the code was unavailable".
458pub const NO_SNIPPET: &str = "no-snippet";
459
460/// Short SHA-256 prefix of a snippet, used by identity recipes that need to
461/// notice that the *code* at a location changed even though the location did
462/// not.
463///
464/// Sixteen hex characters is 64 bits — far more than enough to keep two
465/// snippets at the same rule and offset distinct, and short enough that a
466/// rendered key stays readable in a terminal. Leading and trailing whitespace is
467/// stripped first, so a reformat that only moved indentation is not a new
468/// finding.
469#[must_use]
470pub fn snippet_hash(snippet: &str) -> String {
471    crate::sha256_hex(snippet.trim().as_bytes())[..16].to_owned()
472}
473
474/// [`snippet_hash`] of what `snippets` holds for the span, or [`NO_SNIPPET`].
475#[must_use]
476pub fn snippet_hash_at(snippets: &dyn SnippetSource, path: &str, start: u32, end: u32) -> String {
477    snippets
478        .snippet(path, start, end)
479        .map_or_else(|| NO_SNIPPET.to_owned(), |text| snippet_hash(&text))
480}
481
482#[cfg(test)]
483mod tests {
484    use super::{
485        Adapter as _, AssetPaths, Guidance, LINT_ANALYZERS, Line, NO_SNIPPET, NativeContext,
486        UNKNOWN_VERSION, URL_PREFIX, adapter_for, every_adapter, install_hint, known_analyzers,
487        snippet_hash, snippet_hash_at,
488    };
489    use rto_graph::SourceIdentity;
490
491    fn ctx(version: Option<&str>) -> NativeContext<'static> {
492        static SOURCE: std::sync::LazyLock<SourceIdentity> =
493            std::sync::LazyLock::new(SourceIdentity::default);
494        NativeContext {
495            started_at: "2026-08-15T09:00:00Z".to_owned(),
496            ended_at: "2026-08-15T09:00:04Z".to_owned(),
497            analyzer_version: version.map(str::to_owned),
498            exit_status: 0,
499            source: &SOURCE,
500            rules_digest: None,
501            advisory_db: None,
502            worktree: None,
503            snippets: &crate::snippet::NoSnippets,
504        }
505    }
506
507    #[test]
508    fn the_registry_answers_for_every_analyzer_it_lists() {
509        for id in known_analyzers() {
510            assert_eq!(adapter_for(id).expect("registered").analyzer(), id);
511        }
512        assert!(adapter_for("no-such-analyzer").is_none());
513    }
514
515    /// The guard issue #430 asks for, and the reason the hint lives on the trait
516    /// rather than in a table beside it.
517    ///
518    /// What cannot be tested offline is that a command *works* — this machine
519    /// may have no network and certainly should not install anything to find
520    /// out. What can be tested is that none is **missing**, and missing is the
521    /// failure that shipped: a refusal that names the obstacle and stops. So a
522    /// fifth analyzer that declares a program and no hint for it fails here,
523    /// rather than reaching a reader as a message that trails off.
524    ///
525    /// Paired **by program**, not counted: an adapter with two programs and two
526    /// hints for one of them would satisfy a count and leave the other reader
527    /// with nothing.
528    #[test]
529    fn every_host_program_has_an_install_hint() {
530        for adapter in every_adapter() {
531            for program in adapter.host_programs() {
532                let hint = adapter
533                    .install_hints()
534                    .iter()
535                    .find(|hint| hint.program == *program);
536                assert!(
537                    hint.is_some(),
538                    "{} needs `{program}` on PATH and says nothing about how to get it — \
539                     see `Adapter::install_hints`",
540                    adapter.analyzer()
541                );
542            }
543            for hint in adapter.install_hints() {
544                assert!(
545                    adapter.host_programs().contains(&hint.program),
546                    "{} hints at installing `{}`, which it does not need on PATH — a hint \
547                     for a program no refusal names is one nobody reads",
548                    adapter.analyzer(),
549                    hint.program
550                );
551            }
552        }
553    }
554
555    /// A hint is a *way forward*, so this asserts the shape that makes it one.
556    ///
557    /// `Guidance` checks its own prose whenever it renders ([`crate::guidance`]),
558    /// which covers the collapsed-continuation defect. What it cannot know is
559    /// that a hint about obtaining a program must carry the upstream page —
560    /// #430's durability rule, because a URL ages better than a command line —
561    /// and must not print a package manager that upstream did not name, which is
562    /// the platform guess `docs/REVIEW_CHECKLIST.md` forbids.
563    #[test]
564    fn every_install_hint_carries_upstream_and_guesses_no_package_manager() {
565        for adapter in every_adapter() {
566            for hint in adapter.install_hints() {
567                let rendered = hint.guidance.to_string();
568                assert!(
569                    rendered.contains("https://"),
570                    "the hint for `{}` names no upstream page",
571                    hint.program
572                );
573                // Not a blanket ban on the words: an analyzer whose upstream
574                // *does* name one as canonical would state so here and this
575                // would have to be argued with. None of the shipped four does,
576                // and every one of them has an ecosystem command or a page
577                // instead.
578                for guess in ["brew ", "apt ", "apt-get ", "yum ", "dnf ", "choco "] {
579                    assert!(
580                        !rendered.contains(guess),
581                        "the hint for `{}` reaches for `{guess}`, which guesses the \
582                         reader's platform",
583                        hint.program
584                    );
585                }
586            }
587        }
588    }
589
590    /// One convention for the upstream page, asserted on the [`Line`]s rather
591    /// than on rendered text.
592    ///
593    /// Two of the five hints used to promote the URL into a [`Line::Command`],
594    /// and both were the hints whose prose said they had *no* command — so the
595    /// inconsistent rendering and the contradicted comment were one mistake, and
596    /// a reviewer met it as the comment. The convention is now: the page is a
597    /// `Note` beginning [`URL_PREFIX`], and the command slot holds commands.
598    ///
599    /// Structural, because that is what a fifth adapter would evade. A test on
600    /// rendered text would pass on a hint that put the URL anywhere at all — it
601    /// is the *shape* that has drifted here, not the presence of the string, and
602    /// `every_install_hint_carries_upstream_and_guesses_no_package_manager`
603    /// already checks the presence.
604    #[test]
605    fn every_hint_renders_its_upstream_page_the_same_way() {
606        for adapter in every_adapter() {
607            for hint in adapter.install_hints() {
608                let pages: Vec<&str> = hint
609                    .guidance
610                    .lines()
611                    .iter()
612                    .filter_map(|line| match line {
613                        Line::Note(fragments) => fragments
614                            .iter()
615                            .copied()
616                            .find(|f| f.starts_with(URL_PREFIX)),
617                        Line::Command(_) => None,
618                    })
619                    .collect();
620                assert_eq!(
621                    pages.len(),
622                    1,
623                    "the hint for `{}` must introduce its upstream page exactly once, as a \
624                     note beginning {URL_PREFIX:?} — found {pages:?}",
625                    hint.program
626                );
627
628                // The other half, and the one that caught the real defect: a URL
629                // in the command slot. `Line::Command` renders one step further
630                // in as the thing to copy and run, so a page there reads as a
631                // command — and in both hints where it happened, the prose three
632                // lines above said there was no command at all.
633                for line in hint.guidance.lines() {
634                    if let Line::Command(command) = line {
635                        assert!(
636                            !command.contains("://"),
637                            "the hint for `{}` puts a URL in the command slot ({command:?}) — \
638                             a page is read, not run; introduce it with {URL_PREFIX:?}",
639                            hint.program
640                        );
641                    }
642                }
643            }
644        }
645    }
646
647    /// Two adapters needing the same program must answer the same way.
648    ///
649    /// `cargo` is needed by `cargo-audit` and by `clippy`, and
650    /// [`install_hint`] resolves by program alone — so it returns whichever is
651    /// found first, and the two disagreeing would make the message depend on
652    /// table order. They share [`RUST_TOOLCHAIN`] for that reason, and this is
653    /// what says so.
654    #[test]
655    fn a_program_two_adapters_need_is_obtained_one_way() {
656        let mut seen: Vec<(&str, Guidance)> = Vec::new();
657        for adapter in every_adapter() {
658            for hint in adapter.install_hints() {
659                if let Some((_, first)) = seen.iter().find(|(name, _)| *name == hint.program) {
660                    assert_eq!(
661                        *first, hint.guidance,
662                        "`{}` is obtained two different ways depending on which adapter \
663                         asked — `install_hint` resolves by program, so one of them would \
664                         never be printed",
665                        hint.program
666                    );
667                } else {
668                    seen.push((hint.program, hint.guidance));
669                }
670            }
671        }
672    }
673
674    /// The program a refusal actually names is the one an invocation runs, so
675    /// that is the one that must resolve to a hint.
676    ///
677    /// [`every_host_program_has_an_install_hint`] pairs the two *declared*
678    /// lists; this ties them to the third thing, which is what
679    /// `SubprocessError::BinaryNotFound` looks up. `cargo-audit` is why it is a
680    /// separate assertion: its invocation is `cargo`, so a hint table covering
681    /// only `cargo-audit` would pass the pairing and still leave the commonest
682    /// refusal without an answer.
683    #[test]
684    fn every_invoked_program_is_declared_on_path() {
685        let empty = AssetPaths::default();
686        for adapter in every_adapter() {
687            let program = adapter.command(&empty).program;
688            assert!(
689                adapter.host_programs().contains(&program.as_str()),
690                "{} invokes `{program}`, which it does not declare in `host_programs` — \
691                 a refusal naming it would find no install hint",
692                adapter.analyzer()
693            );
694            assert!(
695                install_hint(&program).is_some(),
696                "`{program}` is invoked and has no install hint"
697            );
698        }
699    }
700
701    /// [`LINT_ANALYZERS`] and `LINT_ADAPTERS` are two spellings of one list, and
702    /// this is what keeps them one. A linter present in the first and absent
703    /// from the second would lose its install hint silently — the refusal would
704    /// still print, just without the half that says what to do.
705    #[test]
706    fn the_lint_tables_name_the_same_analyzers() {
707        let from_adapters: Vec<&str> = super::LINT_ADAPTERS.iter().map(|a| a.analyzer()).collect();
708        assert_eq!(from_adapters, LINT_ANALYZERS.to_vec());
709    }
710
711    /// The lookup is by program and answers for every shipped one, including the
712    /// two that are toolchain components rather than analyzers.
713    #[test]
714    fn the_lookup_answers_for_a_known_program_and_not_an_unknown_one() {
715        for program in [
716            "semgrep",
717            "cargo-audit",
718            "osv-scanner",
719            "cargo",
720            "cargo-clippy",
721        ] {
722            assert!(install_hint(program).is_some(), "no hint for `{program}`");
723        }
724        assert!(install_hint("no-such-binary").is_none());
725    }
726
727    /// The two hints that could most easily become the same wrong answer.
728    ///
729    /// A reader missing `cargo-audit` has cargo already; a reader missing
730    /// `cargo` has neither. Collapsing them onto one hint would send the first
731    /// to an installer they do not need, which is the "wrong *kind* of way
732    /// forward" the refusals checklist says has cost an hour here before. This
733    /// asserts the distinction survives, and asserts it on rendered text because
734    /// that is what the reader gets.
735    #[test]
736    fn the_toolchain_and_the_subcommand_are_obtained_differently() {
737        let toolchain = install_hint("cargo").expect("cargo").to_string();
738        let subcommand = install_hint("cargo-audit")
739            .expect("cargo-audit")
740            .to_string();
741        assert!(toolchain.contains("https://rustup.rs"), "{toolchain}");
742        assert!(
743            !toolchain.contains("cargo install cargo-audit"),
744            "{toolchain}"
745        );
746        assert!(
747            subcommand.contains("cargo install cargo-audit"),
748            "{subcommand}"
749        );
750        assert!(!subcommand.contains("https://rustup.rs"), "{subcommand}");
751        // Verified against upstream's README, which documents no `--locked`.
752        assert!(!subcommand.contains("--locked"), "{subcommand}");
753    }
754
755    /// The analyzer with no honest single command says so, rather than reaching
756    /// for one of the eight platform-specific ones upstream lists.
757    #[test]
758    fn an_analyzer_without_one_canonical_command_says_so() {
759        let hint = install_hint("osv-scanner")
760            .expect("osv-scanner")
761            .to_string();
762        assert!(
763            hint.contains("https://google.github.io/osv-scanner/installation/"),
764            "{hint}"
765        );
766        assert!(hint.contains("no single install command"), "{hint}");
767    }
768
769    /// Every registered analyzer is one whose findings are **stored**, so each
770    /// one must have a pinned rule set or database to decide the answer. A
771    /// linter has neither — its rules are the toolchain — which is why clippy
772    /// has an adapter and no registry entry, and why this asserts the property
773    /// rather than the name: a future storable analyzer with no asset would fail
774    /// here and have to argue its case.
775    #[test]
776    fn every_storable_analyzer_pins_what_decides_its_answer() {
777        for id in known_analyzers() {
778            let adapter = adapter_for(id).expect("registered");
779            assert!(
780                !adapter.asset_ids().is_empty(),
781                "{id} is stored but pins nothing that decides its findings"
782            );
783        }
784        assert!(
785            super::clippy::Clippy.asset_ids().is_empty(),
786            "a linter has no pinned rule set — that is why it is not stored"
787        );
788    }
789
790    /// Every shipped adapter claims at least one language and a summary, because
791    /// `roteiro security status` prints the coverage matrix from this table —
792    /// an adapter that claims nothing would silently shrink the reported
793    /// coverage.
794    #[test]
795    fn every_adapter_states_its_coverage() {
796        for id in known_analyzers() {
797            let adapter = adapter_for(id).expect("registered");
798            assert!(!adapter.languages().is_empty(), "{id} claims no language");
799            assert!(!adapter.summary().is_empty(), "{id} has no summary");
800        }
801    }
802
803    #[test]
804    fn a_version_is_taken_from_the_caller_then_the_report_then_unknown() {
805        assert_eq!(ctx(Some("1.2.3")).version_or(Some("0.0.1")), "1.2.3");
806        assert_eq!(ctx(None).version_or(Some("0.0.1")), "0.0.1");
807        assert_eq!(ctx(None).version_or(None), UNKNOWN_VERSION);
808        // Whitespace is not a version: an all-blank field would be refused
809        // downstream as missing evidence, so it is treated as absent here.
810        assert_eq!(ctx(Some("  ")).version_or(None), UNKNOWN_VERSION);
811    }
812
813    #[test]
814    fn snippet_hashes_are_short_stable_and_whitespace_insensitive() {
815        let hash = snippet_hash("eval(user_input)");
816        assert_eq!(hash.len(), 16);
817        assert_eq!(hash, snippet_hash("  eval(user_input)\n"));
818        assert_ne!(hash, snippet_hash("eval(other_input)"));
819    }
820
821    /// A report about a tree this checkout does not have still yields a
822    /// well-formed identity, and one that says why it is weaker.
823    #[test]
824    fn an_unavailable_snippet_is_named_not_hashed_as_empty() {
825        let hash = snippet_hash_at(&crate::snippet::NoSnippets, "a.py", 0, 4);
826        assert_eq!(hash, NO_SNIPPET);
827        assert_ne!(hash, snippet_hash(""));
828    }
829
830    #[test]
831    fn asset_paths_resolve_only_what_was_provisioned() {
832        let entries = [("semgrep-rules", std::path::PathBuf::from("/cache/r.yaml"))];
833        let paths = AssetPaths::new(&entries);
834        assert_eq!(paths.arg("semgrep-rules"), "/cache/r.yaml");
835        assert!(paths.get("advisory-db").is_none());
836        assert!(paths.arg("advisory-db").is_empty());
837    }
838}