Skip to content

Commit

Permalink
chore(ci): add mocked backend binary to improve `compile_success_empt…
Browse files Browse the repository at this point in the history
…y` tests (noir-lang#2554)
  • Loading branch information
TomAFrench authored Sep 4, 2023
1 parent 99cf768 commit 1bd7111
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 11 deletions.
50 changes: 50 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 7 additions & 9 deletions crates/acvm_backend_barretenberg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 }
Expand All @@ -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)
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/nargo_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -66,6 +67,7 @@ pprof = { version = "0.12", features = [
"criterion",
] }
iai = "0.1.1"
test-binary = "3.0.1"

[[bench]]
name = "criterion"
Expand Down
12 changes: 10 additions & 2 deletions crates/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
7 changes: 7 additions & 0 deletions crates/nargo_cli/test-binaries/mock_backend/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions crates/nargo_cli/test-binaries/mock_backend/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
10 changes: 10 additions & 0 deletions crates/nargo_cli/test-binaries/mock_backend/src/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
2 changes: 2 additions & 0 deletions crates/nargo_cli/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

0 comments on commit 1bd7111

Please sign in to comment.