Skip to content

Commit c6293c9

Browse files
jfecherkevaundray
andauthored
fix(nargo): nargo test now only runs test functions defined in the current module (#805)
* Fix #759 * Change unwrap to expect --------- Co-authored-by: kevaundray <kevtheappdev@gmail.com>
1 parent 4dbb278 commit c6293c9

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

crates/noirc_driver/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,10 @@ impl Driver {
205205
/// will return all functions marked with #[test].
206206
pub fn get_all_test_functions_in_crate_matching(&self, pattern: &str) -> Vec<FuncId> {
207207
let interner = &self.context.def_interner;
208-
interner
209-
.get_all_test_functions()
208+
self.context
209+
.def_map(LOCAL_CRATE)
210+
.expect("The local crate should be analyzed already")
211+
.get_all_test_functions(interner)
210212
.filter_map(|id| interner.function_name(&id).contains(pattern).then_some(id))
211213
.collect()
212214
}

crates/noirc_frontend/src/hir/def_map/mod.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use crate::graph::CrateId;
22
use crate::hir::def_collector::dc_crate::DefCollector;
33
use crate::hir::Context;
4-
use crate::node_interner::FuncId;
4+
use crate::node_interner::{FuncId, NodeInterner};
55
use crate::parser::{parse_program, ParsedModule};
6+
use crate::token::Attribute;
67
use arena::{Arena, Index};
78
use fm::{FileId, FileManager};
89
use noirc_errors::FileDiagnostic;
@@ -112,6 +113,18 @@ impl CrateDefMap {
112113
pub fn module_file_id(&self, module_id: LocalModuleId) -> FileId {
113114
self.modules[module_id.0].origin.file_id()
114115
}
116+
117+
/// Go through all modules in this crate, and find all functions in
118+
/// each module with the #[test] attribute
119+
pub fn get_all_test_functions<'a>(
120+
&'a self,
121+
interner: &'a NodeInterner,
122+
) -> impl Iterator<Item = FuncId> + 'a {
123+
self.modules.iter().flat_map(|(_, module)| {
124+
let functions = module.scope.values().values().filter_map(|(id, _)| id.as_function());
125+
functions.filter(|id| interner.function_meta(id).attributes == Some(Attribute::Test))
126+
})
127+
}
115128
}
116129

117130
/// Given a FileId, fetch the File, from the FileManager and parse it's content

crates/noirc_frontend/src/node_interner.rs

-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::hir_def::{
1818
function::{FuncMeta, HirFunction},
1919
stmt::HirStatement,
2020
};
21-
use crate::token::Attribute;
2221
use crate::{Shared, TypeBinding, TypeBindings, TypeVariableId};
2322

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

579-
pub fn get_all_test_functions(&self) -> impl Iterator<Item = FuncId> + '_ {
580-
self.func_meta.iter().filter_map(|(id, meta)| {
581-
let is_test = meta.attributes.as_ref()? == &Attribute::Test;
582-
is_test.then_some(*id)
583-
})
584-
}
585-
586578
/// Add a method to a type.
587579
/// This will panic for non-struct types currently as we do not support methods
588580
/// for primitives. We could allow this in the future however.

0 commit comments

Comments
 (0)