Skip to main content

dropshot_api_manager/
test_util.rs

1// Copyright 2026 Oxide Computer Company
2
3//! Test utilities for the Dropshot API manager.
4
5pub use crate::output::CheckResult;
6#[doc(hidden)]
7pub use crate::resolved::{ProblemKind, ProblemSummary};
8use crate::{
9    apis::ManagedApis,
10    cmd::{
11        check::check_impl_with_summaries,
12        dispatch::{BlessedSourceArgs, GeneratedSourceArgs},
13    },
14    environment::{Environment, GeneratedSource},
15    output::OutputOpts,
16    resolved,
17};
18use camino::Utf8PathBuf;
19
20/// Check that a set of APIs is up-to-date.
21///
22/// This is meant to be called within a test.
23pub fn check_apis_up_to_date(
24    env: &Environment,
25    apis: &ManagedApis,
26) -> Result<CheckResult, anyhow::Error> {
27    let (result, _summaries) = check_apis_with_summaries(env, apis)?;
28    Ok(result)
29}
30
31/// Check that a set of APIs is up-to-date, loading generated documents from
32/// the given directory instead of generating them from the API definitions.
33pub fn check_apis_with_generated_from_dir(
34    env: &Environment,
35    apis: &ManagedApis,
36    generated_from_dir: Utf8PathBuf,
37) -> Result<CheckResult, anyhow::Error> {
38    let (result, _summaries) =
39        check_apis_with_generated_from_dir_and_summaries(
40            env,
41            apis,
42            generated_from_dir,
43        )?;
44    Ok(result)
45}
46
47/// Like [`check_apis_up_to_date`], but also returns the list of problem
48/// summaries for detailed assertions in tests.
49#[doc(hidden)]
50pub fn check_apis_with_summaries(
51    env: &Environment,
52    apis: &ManagedApis,
53) -> Result<(CheckResult, Vec<resolved::ProblemSummary>), anyhow::Error> {
54    let env = resolve_env(env)?;
55    let (blessed_source, generated_source, output) =
56        default_sources(&env, None)?;
57    check_impl_with_summaries(
58        apis,
59        &env,
60        &blessed_source,
61        &generated_source,
62        &output,
63    )
64}
65
66/// Like [`check_apis_with_generated_from_dir`], but also returns the list
67/// of problem summaries for detailed assertions in tests.
68#[doc(hidden)]
69pub fn check_apis_with_generated_from_dir_and_summaries(
70    env: &Environment,
71    apis: &ManagedApis,
72    generated_from_dir: Utf8PathBuf,
73) -> Result<(CheckResult, Vec<resolved::ProblemSummary>), anyhow::Error> {
74    let env = resolve_env(env)?;
75    let (blessed_source, generated_source, output) =
76        default_sources(&env, Some(generated_from_dir))?;
77    check_impl_with_summaries(
78        apis,
79        &env,
80        &blessed_source,
81        &generated_source,
82        &output,
83    )
84}
85
86fn resolve_env(
87    env: &Environment,
88) -> Result<crate::environment::ResolvedEnv, anyhow::Error> {
89    // env.resolve(None) assumes that env.default_openapi_dir is where the
90    // OpenAPI documents live and doesn't need a further override. (If a custom
91    // directory is desired, it can always be passed in via `env`.)
92    env.resolve(None)
93}
94
95fn default_sources(
96    env: &crate::environment::ResolvedEnv,
97    generated_from_dir: Option<Utf8PathBuf>,
98) -> Result<
99    (crate::environment::BlessedSource, GeneratedSource, OutputOpts),
100    anyhow::Error,
101> {
102    let blessed_source = BlessedSourceArgs {
103        blessed_from_vcs: None,
104        blessed_from_vcs_path: None,
105        blessed_from_dir: None,
106    }
107    .to_blessed_source(env)?;
108    let generated_source =
109        GeneratedSource::from(GeneratedSourceArgs { generated_from_dir });
110    let output = OutputOpts { color: clap::ColorChoice::Auto };
111    Ok((blessed_source, generated_source, output))
112}