Skip to content

Commit

Permalink
Speed up the schema definition tests in CI. (#146)
Browse files Browse the repository at this point in the history
### 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.
  • Loading branch information
SamirTalwar authored Nov 6, 2023
1 parent 8602578 commit d2b901d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/check-format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions .github/workflows/schema-definitions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 17 additions & 18 deletions crates/documentation/openapi/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::<RawConfiguration>();
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::<RawConfiguration>()
}

#[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(())
}
}

0 comments on commit d2b901d

Please sign in to comment.