Skip to content

Commit e90dc84

Browse files
committed
test: Check hugr json serializations against the schema (again)
1 parent 0280eb2 commit e90dc84

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

.github/workflows/ci-rs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ env:
1919
CI: true # insta snapshots behave differently on ci
2020
SCCACHE_GHA_ENABLED: "true"
2121
RUSTC_WRAPPER: "sccache"
22+
HUGR_TEST_SCHEMA: "1"
2223
# different strings for install action and feature name
2324
# adapted from https://github.com/TheDan64/inkwell/blob/master/.github/workflows/test.yml
2425
LLVM_VERSION: "14.0"

hugr-core/src/envelope/package_json.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ pub(super) fn to_json_writer<'h>(
5151
modules: hugrs.into_iter().map(HugrSer).collect(),
5252
extensions: extensions.iter().map(std::convert::AsRef::as_ref).collect(),
5353
};
54+
55+
// Validate the hugr serializations against the schema.
56+
#[cfg(all(test, not(miri)))]
57+
if std::env::var("HUGR_TEST_SCHEMA").is_ok_and(|x| !x.is_empty()) {
58+
use crate::hugr::serialize::test::check_hugr_serialization_schema;
59+
60+
for hugr in &pkg_ser.modules {
61+
check_hugr_serialization_schema(hugr.0);
62+
}
63+
}
64+
5465
serde_json::to_writer(writer, &pkg_ser)?;
5566
Ok(())
5667
}

hugr-core/src/hugr/serialize/test.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,18 @@ fn ser_roundtrip_check_schema<TSer: Serialize, TDeser: serde::de::DeserializeOwn
175175
serde_json::from_value(val).unwrap()
176176
}
177177

178+
/// Serialize a Hugr and check that it is valid against the schema.
179+
///
180+
/// # Panics
181+
///
182+
/// Panics if the serialization fails or if the schema validation fails.
183+
pub(crate) fn check_hugr_serialization_schema(hugr: &Hugr) {
184+
let schemas = get_schemas(true);
185+
let hugr_ser = HugrSer(hugr);
186+
let val = serde_json::to_value(hugr_ser).unwrap();
187+
NamedSchema::check_schemas(&val, schemas);
188+
}
189+
178190
/// Serialize and deserialize a HUGR, and check that the result is the same as the original.
179191
///
180192
/// If `check_schema` is true, checks the serialized json against the in-tree schema.

justfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ setup:
1010

1111
# Run the pre-commit checks.
1212
check:
13-
uv run pre-commit run --all-files
13+
HUGR_TEST_SCHEMA=1 uv run pre-commit run --all-files
1414

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

0 commit comments

Comments
 (0)