From 1bd7111819fe442c7b15ec45dd05f88c6ad18737 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Mon, 4 Sep 2023 19:30:36 +0100 Subject: [PATCH] chore(ci): add mocked backend binary to improve `compile_success_empty` tests (#2554) --- Cargo.lock | 50 +++++++++++++++++++ crates/acvm_backend_barretenberg/src/lib.rs | 16 +++--- crates/nargo_cli/Cargo.toml | 2 + crates/nargo_cli/build.rs | 12 ++++- .../test-binaries/mock_backend/Cargo.lock | 7 +++ .../test-binaries/mock_backend/Cargo.toml | 10 ++++ .../test-binaries/mock_backend/src/main.rs | 10 ++++ crates/nargo_cli/tests/execute.rs | 2 + 8 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 crates/nargo_cli/test-binaries/mock_backend/Cargo.lock create mode 100644 crates/nargo_cli/test-binaries/mock_backend/Cargo.toml create mode 100644 crates/nargo_cli/test-binaries/mock_backend/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 76a9d98e23c..a2d00ee6cde 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -588,6 +588,38 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be" +[[package]] +name = "camino" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-platform" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cfa25e60aea747ec7e1124f238816749faa93759c6ff5b31f1ccdda137f4479" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.15.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eee4243f1f26fc7a42710e7439c149e2b10b05472f88090acce52632f231a73a" +dependencies = [ + "camino", + "cargo-platform", + "semver", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "cast" version = "0.3.0" @@ -2179,6 +2211,7 @@ dependencies = [ "color-eyre", "const_format", "criterion", + "dirs", "fm", "hex", "iai", @@ -2199,6 +2232,7 @@ dependencies = [ "serde_json", "tempdir", "termcolor", + "test-binary", "thiserror", "tokio", "tokio-util", @@ -3248,6 +3282,9 @@ name = "semver" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b0293b4b29daaf487284529cc2f5675b8e57c61f70167ba415a463651fd6a918" +dependencies = [ + "serde", +] [[package]] name = "serde" @@ -3687,6 +3724,19 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3369f5ac52d5eb6ab48c6b4ffdc8efbcad6b89c765749064ba298f2c68a16a76" +[[package]] +name = "test-binary" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bb28771e7854f02e5705f2a1b09451d932a273f5a4ec1c9fa4c65882b8b7b6ca" +dependencies = [ + "camino", + "cargo_metadata", + "once_cell", + "paste", + "thiserror", +] + [[package]] name = "thiserror" version = "1.0.43" diff --git a/crates/acvm_backend_barretenberg/src/lib.rs b/crates/acvm_backend_barretenberg/src/lib.rs index 9f327ad8398..77e028bf30d 100644 --- a/crates/acvm_backend_barretenberg/src/lib.rs +++ b/crates/acvm_backend_barretenberg/src/lib.rs @@ -17,7 +17,7 @@ pub fn backends_directory() -> PathBuf { #[cfg(test)] fn get_bb() -> Backend { - let bb = Backend::default(); + let bb = Backend::new("acvm-backend-barretenberg".to_string()); crate::assert_binary_exists(&bb); bb } @@ -36,12 +36,6 @@ pub struct Backend { name: String, } -impl Default for Backend { - fn default() -> Self { - Self { name: "acvm-backend-barretenberg".to_string() } - } -} - impl Backend { pub fn new(name: String) -> Backend { Backend { name } @@ -52,9 +46,13 @@ impl Backend { } fn binary_path(&self) -> PathBuf { - const BINARY_NAME: &str = "backend_binary"; + if let Some(binary_path) = std::env::var_os("NARGO_BACKEND_PATH") { + PathBuf::from(binary_path) + } else { + const BINARY_NAME: &str = "backend_binary"; - self.backend_directory().join(BINARY_NAME) + self.backend_directory().join(BINARY_NAME) + } } } diff --git a/crates/nargo_cli/Cargo.toml b/crates/nargo_cli/Cargo.toml index 466d34e1df1..c5bf646af18 100644 --- a/crates/nargo_cli/Cargo.toml +++ b/crates/nargo_cli/Cargo.toml @@ -54,6 +54,7 @@ tokio-util = { version = "0.7.8", features = ["compat"] } [dev-dependencies] tempdir = "0.3.7" +dirs.workspace = true assert_cmd = "2.0.8" assert_fs = "1.0.10" predicates = "2.1.5" @@ -66,6 +67,7 @@ pprof = { version = "0.12", features = [ "criterion", ] } iai = "0.1.1" +test-binary = "3.0.1" [[bench]] name = "criterion" diff --git a/crates/nargo_cli/build.rs b/crates/nargo_cli/build.rs index 42e0e318bfb..ad8988fba8e 100644 --- a/crates/nargo_cli/build.rs +++ b/crates/nargo_cli/build.rs @@ -104,15 +104,23 @@ fn generate_compile_success_empty_tests(test_file: &mut File, test_data_dir: &Pa r#" #[test] fn compile_success_empty_{test_name}() {{ - let test_program_dir = PathBuf::from("{test_dir}"); + + // We use a mocked backend for this test as we do not rely on the returned circuit size + // but we must call a backend as part of querying the number of opcodes in the circuit. + let test_program_dir = PathBuf::from("{test_dir}"); let mut cmd = Command::cargo_bin("nargo").unwrap(); + cmd.env("NARGO_BACKEND_PATH", path_to_mock_backend()); cmd.arg("--program-dir").arg(test_program_dir); cmd.arg("info"); cmd.arg("--json"); let output = cmd.output().expect("Failed to execute command"); - + + if !output.status.success() {{ + panic!("`nargo info` failed with: {{}}", String::from_utf8(output.stderr).unwrap()); + }} + // `compile_success_empty` tests should be able to compile down to an empty circuit. let json: serde_json::Value = serde_json::from_slice(&output.stdout).expect("JSON was not well-formatted"); let num_opcodes = &json["programs"][0]["acir_opcodes"]; diff --git a/crates/nargo_cli/test-binaries/mock_backend/Cargo.lock b/crates/nargo_cli/test-binaries/mock_backend/Cargo.lock new file mode 100644 index 00000000000..fd23780289a --- /dev/null +++ b/crates/nargo_cli/test-binaries/mock_backend/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "mock_backend" +version = "0.1.0" diff --git a/crates/nargo_cli/test-binaries/mock_backend/Cargo.toml b/crates/nargo_cli/test-binaries/mock_backend/Cargo.toml new file mode 100644 index 00000000000..25528c7a6af --- /dev/null +++ b/crates/nargo_cli/test-binaries/mock_backend/Cargo.toml @@ -0,0 +1,10 @@ +[workspace] + +[package] +name = "mock_backend" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/crates/nargo_cli/test-binaries/mock_backend/src/main.rs b/crates/nargo_cli/test-binaries/mock_backend/src/main.rs new file mode 100644 index 00000000000..3d2cd9f79b0 --- /dev/null +++ b/crates/nargo_cli/test-binaries/mock_backend/src/main.rs @@ -0,0 +1,10 @@ +#![forbid(unsafe_code)] +#![warn(unreachable_pub)] +#![warn(clippy::semicolon_if_nothing_returned)] +#![cfg_attr(not(test), warn(unused_crate_dependencies, unused_extern_crates))] + +use std::io::Write; + +fn main() { + std::io::stdout().write_all(&0u64.to_be_bytes()).unwrap(); +} diff --git a/crates/nargo_cli/tests/execute.rs b/crates/nargo_cli/tests/execute.rs index e53ad068c01..664e9ad6a39 100644 --- a/crates/nargo_cli/tests/execute.rs +++ b/crates/nargo_cli/tests/execute.rs @@ -13,6 +13,8 @@ mod tests { use super::*; + test_binary::build_test_binary_once!(mock_backend, "test-binaries"); + // include tests generated by `build.rs` include!(concat!(env!("OUT_DIR"), "/execute.rs")); }