Skip to content
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

feat(nargo): Add nargo test command to run all unit tests #728

Merged
merged 15 commits into from
Feb 3, 2023
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ tempdir.workspace = true
thiserror.workspace = true

# Backends
aztec_backend = { optional = true, package = "barretenberg_static_lib", git = "https://github.com/noir-lang/aztec_backend", rev = "c673a14be33e445d98b35969716ac59c682036d6" }
aztec_wasm_backend = { optional = true, package = "barretenberg_wasm", git = "https://github.com/noir-lang/aztec_backend", rev = "c673a14be33e445d98b35969716ac59c682036d6" }
aztec_backend = { optional = true, package = "barretenberg_static_lib", git = "https://github.com/noir-lang/aztec_backend", rev = "b7349851d7afcffef4171e4d3a30b163ea37e579" }
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
aztec_wasm_backend = { optional = true, package = "barretenberg_wasm", git = "https://github.com/noir-lang/aztec_backend", rev = "b7349851d7afcffef4171e4d3a30b163ea37e579" }
marlin_arkworks_backend = { optional = true, git = "https://github.com/noir-lang/marlin_arkworks_backend", rev = "144378edad821bfaa52bf2cacca8ecc87514a4fc" }

[features]
Expand Down
13 changes: 6 additions & 7 deletions crates/nargo/src/cli/test_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ fn run_tests(test_name: &str, allow_warnings: bool) -> Result<(), CliError> {
match prove(test_name, test_function, &driver, allow_warnings) {
Ok(_) => println!("ok"),
Err(_) => {
// An error likely was printed in the meantime, so this is the start of a newline
println!("{test_name} failed");
// An error likely was printed in the meantime, so this is the start of a newline.
// Add an extra newline as well to help separate failing tests.
println!("{test_name} failed\n");
failing += 1;
}
}
Expand Down Expand Up @@ -69,11 +70,9 @@ fn prove(
let language = backend.np_language();
let program_dir = std::env::current_dir().unwrap();

let program =
driver.compile_no_check(language, false, allow_warnings, Some(main)).map_err(|_| {
// TODO: Add test name
CliError::Generic(format!("Test '{test_name}' failed to compile"))
})?;
let program = driver
.compile_no_check(language, false, allow_warnings, Some(main))
jfecher marked this conversation as resolved.
Show resolved Hide resolved
.map_err(|_| CliError::Generic(format!("Test '{test_name}' failed to compile")))?;

let solved_witness = parse_and_solve_witness(program_dir, &program)?;

Expand Down
7 changes: 7 additions & 0 deletions crates/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub enum ResolverError {
IntegerTooLarge { span: Span },
#[error("Closures cannot capture mutable variables")]
CapturedMutableVariable { span: Span },
#[error("Test functions are not allowed to have any parameters")]
TestFunctionHasParameters { span: Span },
}

impl ResolverError {
Expand Down Expand Up @@ -194,6 +196,11 @@ impl ResolverError {
"Mutable variable".into(),
span,
),
ResolverError::TestFunctionHasParameters { span } => Diagnostic::simple_error(
"Test functions cannot have any parameters".into(),
"Try removing the parameters or moving the test into a wrapper function".into(),
span,
),
}
}
}
7 changes: 7 additions & 0 deletions crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::hir_def::expr::{
HirIndexExpression, HirInfixExpression, HirLambda, HirLiteral, HirMemberAccess,
HirMethodCallExpression, HirPrefixExpression,
};
use crate::token::Attribute;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::rc::Rc;

Expand Down Expand Up @@ -491,6 +492,12 @@ impl<'a> Resolver<'a> {
self.push_err(ResolverError::NecessaryPub { ident: func.name_ident().clone() })
}

if attributes == Some(Attribute::Test) && !parameters.is_empty() {
self.push_err(ResolverError::TestFunctionHasParameters {
span: func.name_ident().span(),
})
}

let mut typ = Type::Function(parameter_types, return_type);

if !generics.is_empty() {
Expand Down