-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test missing OpType definitions against pytket
- Loading branch information
Showing
8 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
name: Detect missing operation definitions | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
workflow_dispatch: {} | ||
schedule: | ||
# 04:00 daily | ||
- cron: '0 4 * * *' | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
SCCACHE_GHA_ENABLED: "true" | ||
RUSTC_WRAPPER: "sccache" | ||
|
||
jobs: | ||
missing-optypes: | ||
name: Check for missing op type definitions | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: mozilla-actions/sccache-action@v0.0.4 | ||
- name: Install stable toolchain | ||
uses: dtolnay/rust-toolchain@stable | ||
with: | ||
components: rustfmt, clippy | ||
- name: Install poetry | ||
run: pipx install poetry | ||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: '3.12' | ||
cache: "poetry" | ||
- name: Update the project dependencies | ||
run: poetry -C tests update | ||
- name: Run the missing op types test | ||
run: poetry -C tests run -- cargo test --test integration -- --ignored missing_optypes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
Cargo.lock | ||
tests/poetry.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# List the available commands | ||
help: | ||
@just --list --justfile {{justfile()}} | ||
|
||
# Run all the rust tests | ||
test: | ||
cargo test --all-features | ||
|
||
# Auto-fix all clippy warnings | ||
fix: | ||
cargo clippy --all-targets --all-features --workspace --fix --allow-staged | ||
|
||
# Check for missing optypes | ||
check-optypes: | ||
poetry -C tests run -- cargo test --test integration -- --ignored missing_optypes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! Integration tests. | ||
pub mod missing_optypes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
//! Integration test to detect missing optypes in the optype module. | ||
//! | ||
//! Requires the `pyo3` feature to be enabled. | ||
use std::str::FromStr; | ||
|
||
use pyo3::prelude::*; | ||
use pyo3::types::PyDict; | ||
use tket_json_rs::OpType; | ||
|
||
#[test] | ||
#[ignore = "Requires a python environment with `pytket` installed."] | ||
fn missing_optypes() -> PyResult<()> { | ||
println!("Checking missing optypes"); | ||
|
||
pyo3::prepare_freethreaded_python(); | ||
Python::with_gil(|py| { | ||
let Ok(pytket) = PyModule::import_bound(py, "pytket") else { | ||
panic!("Failed to import `pytket`. Make sure the python library is installed."); | ||
}; | ||
let py_enum = pytket.getattr("OpType")?; | ||
let py_members = py_enum.getattr("__members__")?; | ||
let py_members = py_members.downcast::<PyDict>()?; | ||
|
||
let missing: Vec<String> = py_members | ||
.into_iter() | ||
.filter_map(|(name, _class)| { | ||
let name = name.extract::<String>().unwrap(); | ||
match OpType::from_str(&name) { | ||
Err(_) => Some(name), | ||
Ok(_) => None, | ||
} | ||
}) | ||
.collect(); | ||
|
||
if !missing.is_empty() { | ||
let msg = "\nMissing optypes in `tket_json_rs`:\n".to_string(); | ||
let msg = missing | ||
.into_iter() | ||
.fold(msg, |msg, s| msg + " - " + &s + "\n"); | ||
let msg = | ||
msg + "Please add them to the `OpType` enum in `tket_json_rs/src/optype.rs`.\n"; | ||
panic!("{msg}"); | ||
} | ||
|
||
Ok(()) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[tool.poetry] | ||
description = "tket-json-rs integration tests." | ||
name = "tket-json-rs-tests" | ||
version = "0.0.0" | ||
authors = ["TKET development team <tket-support@cambridgequantum.com>"] | ||
package-mode = false | ||
|
||
[tool.poetry.group.main.dependencies] | ||
python = "^3.10" | ||
# Always use the latest pytket version | ||
pytket = "*" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pytest = "^8.1.1" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |