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;
6use crate::{
7    apis::ManagedApis,
8    cmd::{
9        check::check_impl,
10        dispatch::{BlessedSourceArgs, GeneratedSourceArgs},
11    },
12    environment::{Environment, GeneratedSource},
13    output::OutputOpts,
14};
15use camino::Utf8PathBuf;
16
17/// Check that a set of APIs is up-to-date.
18///
19/// This is meant to be called within a test.
20pub fn check_apis_up_to_date(
21    env: &Environment,
22    apis: &ManagedApis,
23) -> Result<CheckResult, anyhow::Error> {
24    check_apis_impl(env, apis, None)
25}
26
27/// Check that a set of APIs is up-to-date, loading generated documents from
28/// the given directory instead of generating them from the API definitions.
29pub fn check_apis_with_generated_from_dir(
30    env: &Environment,
31    apis: &ManagedApis,
32    generated_from_dir: Utf8PathBuf,
33) -> Result<CheckResult, anyhow::Error> {
34    check_apis_impl(env, apis, Some(generated_from_dir))
35}
36
37fn check_apis_impl(
38    env: &Environment,
39    apis: &ManagedApis,
40    generated_from_dir: Option<Utf8PathBuf>,
41) -> Result<CheckResult, anyhow::Error> {
42    // env.resolve(None) assumes that env.default_openapi_dir is where the
43    // OpenAPI documents live and doesn't need a further override. (If a custom
44    // directory is desired, it can always be passed in via `env`.)
45    let env = env.resolve(None)?;
46
47    let blessed_source =
48        BlessedSourceArgs { blessed_from_git: None, blessed_from_dir: None }
49            .to_blessed_source(&env)?;
50    let generated_source =
51        GeneratedSource::from(GeneratedSourceArgs { generated_from_dir });
52    let output = OutputOpts { color: clap::ColorChoice::Auto };
53
54    check_impl(apis, &env, &blessed_source, &generated_source, &output)
55}