Skip to content

test: Check hugr json serializations against the schema (again) #2216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci-rs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ env:
CI: true # insta snapshots behave differently on ci
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
HUGR_TEST_SCHEMA: "1"
# different strings for install action and feature name
# adapted from https://github.com/TheDan64/inkwell/blob/master/.github/workflows/test.yml
LLVM_VERSION: "14.0"
Expand Down
11 changes: 11 additions & 0 deletions hugr-core/src/envelope/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ pub(super) fn to_json_writer<'h>(
modules: hugrs.into_iter().map(HugrSer).collect(),
extensions: extensions.iter().map(std::convert::AsRef::as_ref).collect(),
};

// Validate the hugr serializations against the schema.
#[cfg(all(test, not(miri)))]
if std::env::var("HUGR_TEST_SCHEMA").is_ok_and(|x| !x.is_empty()) {
use crate::hugr::serialize::test::check_hugr_serialization_schema;

for hugr in &pkg_ser.modules {
check_hugr_serialization_schema(hugr.0);
}
}

serde_json::to_writer(writer, &pkg_ser)?;
Ok(())
}
Expand Down
12 changes: 12 additions & 0 deletions hugr-core/src/hugr/serialize/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@ fn ser_roundtrip_check_schema<TSer: Serialize, TDeser: serde::de::DeserializeOwn
serde_json::from_value(val).unwrap()
}

/// Serialize a Hugr and check that it is valid against the schema.
///
/// # Panics
///
/// Panics if the serialization fails or if the schema validation fails.
pub(crate) fn check_hugr_serialization_schema(hugr: &Hugr) {
let schemas = get_schemas(true);
let hugr_ser = HugrSer(hugr);
let val = serde_json::to_value(hugr_ser).unwrap();
NamedSchema::check_schemas(&val, schemas);
}

/// Serialize and deserialize a HUGR, and check that the result is the same as the original.
///
/// If `check_schema` is true, checks the serialized json against the in-tree schema.
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ setup:

# Run the pre-commit checks.
check:
uv run pre-commit run --all-files
HUGR_TEST_SCHEMA=1 uv run pre-commit run --all-files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels quite heavyweight for a pre-commit, but I don't really object

Copy link
Collaborator Author

@aborgna-q aborgna-q May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just the rule ran on just check. pre-commit has its own config that doesn't set the envvar.


# Run all the tests.
test: test-rust test-python
Expand All @@ -20,7 +20,7 @@ test-rust *TEST_ARGS:
@# built into a binary build (without using `maturin`)
@#
@# This feature list should be kept in sync with the `hugr-py/pyproject.toml`
cargo test \
HUGR_TEST_SCHEMA=1 cargo test \
--workspace \
--exclude 'hugr-py' \
--features 'hugr/declarative hugr/llvm hugr/llvm-test hugr/zstd' {{TEST_ARGS}}
Expand Down
Loading