Skip to content

Commit

Permalink
fix(nargo): nargo test now only runs test functions defined in the …
Browse files Browse the repository at this point in the history
…current module (#805)

* Fix #759

* Change unwrap to expect

---------

Co-authored-by: kevaundray <[email protected]>
  • Loading branch information
jfecher and kevaundray authored Feb 11, 2023
1 parent 4dbb278 commit c6293c9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
6 changes: 4 additions & 2 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,10 @@ impl Driver {
/// will return all functions marked with #[test].
pub fn get_all_test_functions_in_crate_matching(&self, pattern: &str) -> Vec<FuncId> {
let interner = &self.context.def_interner;
interner
.get_all_test_functions()
self.context
.def_map(LOCAL_CRATE)
.expect("The local crate should be analyzed already")
.get_all_test_functions(interner)
.filter_map(|id| interner.function_name(&id).contains(pattern).then_some(id))
.collect()
}
Expand Down
15 changes: 14 additions & 1 deletion crates/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::graph::CrateId;
use crate::hir::def_collector::dc_crate::DefCollector;
use crate::hir::Context;
use crate::node_interner::FuncId;
use crate::node_interner::{FuncId, NodeInterner};
use crate::parser::{parse_program, ParsedModule};
use crate::token::Attribute;
use arena::{Arena, Index};
use fm::{FileId, FileManager};
use noirc_errors::FileDiagnostic;
Expand Down Expand Up @@ -112,6 +113,18 @@ impl CrateDefMap {
pub fn module_file_id(&self, module_id: LocalModuleId) -> FileId {
self.modules[module_id.0].origin.file_id()
}

/// Go through all modules in this crate, and find all functions in
/// each module with the #[test] attribute
pub fn get_all_test_functions<'a>(
&'a self,
interner: &'a NodeInterner,
) -> impl Iterator<Item = FuncId> + 'a {
self.modules.iter().flat_map(|(_, module)| {
let functions = module.scope.values().values().filter_map(|(id, _)| id.as_function());
functions.filter(|id| interner.function_meta(id).attributes == Some(Attribute::Test))
})
}
}

/// Given a FileId, fetch the File, from the FileManager and parse it's content
Expand Down
8 changes: 0 additions & 8 deletions crates/noirc_frontend/src/node_interner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::hir_def::{
function::{FuncMeta, HirFunction},
stmt::HirStatement,
};
use crate::token::Attribute;
use crate::{Shared, TypeBinding, TypeBindings, TypeVariableId};

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
Expand Down Expand Up @@ -576,13 +575,6 @@ impl NodeInterner {
std::mem::take(&mut self.delayed_type_checks)
}

pub fn get_all_test_functions(&self) -> impl Iterator<Item = FuncId> + '_ {
self.func_meta.iter().filter_map(|(id, meta)| {
let is_test = meta.attributes.as_ref()? == &Attribute::Test;
is_test.then_some(*id)
})
}

/// Add a method to a type.
/// This will panic for non-struct types currently as we do not support methods
/// for primitives. We could allow this in the future however.
Expand Down

0 comments on commit c6293c9

Please sign in to comment.