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

fix(nargo): nargo test now only runs test functions defined in the current module #805

Merged
merged 3 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions crates/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,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")
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
.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::CollectedErrors;
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 @@ -569,11 +568,4 @@ impl NodeInterner {
pub fn take_delayed_type_check_functions(&mut self) -> Vec<TypeCheckFn> {
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)
})
}
}