From d2b901d62f478959ac16e35d79306644d6294bc2 Mon Sep 17 00:00:00 2001 From: Samir Talwar Date: Mon, 6 Nov 2023 09:23:58 +0100 Subject: [PATCH] Speed up the schema definition tests in CI. (#146) ### What The OpenAPI schema definition tests can take several minutes to run, for two reasons: 1. The tests call `cargo run`, which can trigger recompilation of all dependencies if they haven't been built for this specific target before. 2. On CI, the tests were running in "debug" mode, which means they couldn't make use of the shared build cache, and were taking a few minutes as opposed to, well, less time than that. ### How I have made sure the tests run in release mode on CI (and added `cargo nextest` so things are consistent with the other test jobs). I rewrote the schema definition generator to expose a helper function which can be called directly by tests, rather than using `cargo` to rebuild and rerun the program. I also removed the Rust cache used by the formatting job because it doesn't actually compile code, which can mean that it ends up writing an empty cache, forcing all other jobs to recompile. --- .github/workflows/check-format.yaml | 4 --- .github/workflows/schema-definitions.yaml | 7 +++-- crates/documentation/openapi/src/main.rs | 35 +++++++++++------------ 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/.github/workflows/check-format.yaml b/.github/workflows/check-format.yaml index c371591c4..3fd647227 100644 --- a/.github/workflows/check-format.yaml +++ b/.github/workflows/check-format.yaml @@ -17,10 +17,6 @@ jobs: run: | rustup show - - uses: Swatinem/rust-cache@v2 - with: - shared-key: "build" # share the cache across jobs - - name: check formatting run: | cargo fmt --all --check diff --git a/.github/workflows/schema-definitions.yaml b/.github/workflows/schema-definitions.yaml index a1c67196d..9b2f77e45 100644 --- a/.github/workflows/schema-definitions.yaml +++ b/.github/workflows/schema-definitions.yaml @@ -13,11 +13,14 @@ jobs: - uses: actions/checkout@v4 - name: install tools - run: rustup show + run: | + rustup show + # install cargo-nextest + curl -LsSf https://get.nexte.st/latest/linux | tar zxf - -C ${CARGO_HOME:-~/.cargo}/bin - uses: Swatinem/rust-cache@v2 with: shared-key: "build" # share the cache across jobs - name: OpenAPI Definitions - run: cargo test --bin openapi-generator + run: cargo nextest run --no-fail-fast --release --bin openapi-generator diff --git a/crates/documentation/openapi/src/main.rs b/crates/documentation/openapi/src/main.rs index 73d97c355..7b5e5135e 100644 --- a/crates/documentation/openapi/src/main.rs +++ b/crates/documentation/openapi/src/main.rs @@ -1,31 +1,30 @@ +use std::io; + use ndc_postgres::configuration::RawConfiguration; use schemars::{gen::SchemaSettings, schema::RootSchema}; -fn main() { - let schema: RootSchema = SchemaSettings::openapi3() - .into_generator() - .into_root_schema_for::(); +fn main() -> io::Result<()> { + let schema = generate_schema(); + serde_json::to_writer_pretty(io::stdout(), &schema)?; + Ok(()) +} - println!("{}", serde_json::to_string_pretty(&schema).unwrap()); +fn generate_schema() -> RootSchema { + SchemaSettings::openapi3() + .into_generator() + .into_root_schema_for::() } #[cfg(test)] mod tests { - use std::process::Command; + use super::*; #[test] - fn main() { - let command_output = Command::new("cargo") - .arg("run") - .arg("--bin") - .arg("openapi-generator") - .output() - .expect("Failed to run schema generation") - .stdout; - - let generated_schema: String = - String::from_utf8(command_output).expect("Failed to read snapshot"); + fn main() -> io::Result<()> { + let generated_schema = generate_schema(); + let generated_schema_json = serde_json::to_string_pretty(&generated_schema)?; - insta::assert_snapshot!(generated_schema); + insta::assert_snapshot!(generated_schema_json); + Ok(()) } }