From 9c63d9aefed6c96b9188f654ab5f93ad8e6c7e37 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Fri, 10 May 2024 10:03:28 -0700 Subject: [PATCH] chore: add some utils for testing (#322) --- crates/auth/src/testing.rs | 2 +- crates/core/src/pattern_compiler/builder.rs | 7 +++- crates/core/src/pattern_compiler/compiler.rs | 13 ++++++- crates/core/src/problem.rs | 40 +++++++++++++++++++- crates/grit-util/src/analysis_logs.rs | 12 ++++++ crates/util/src/runtime.rs | 2 +- crates/wasm-bindings/src/match_pattern.rs | 4 +- 7 files changed, 73 insertions(+), 7 deletions(-) diff --git a/crates/auth/src/testing.rs b/crates/auth/src/testing.rs index 30fa105a2..2e59e809d 100644 --- a/crates/auth/src/testing.rs +++ b/crates/auth/src/testing.rs @@ -12,7 +12,7 @@ use crate::{ /// Attempts to read a variable, first from the environment, then from Doppler. /// /// If neither source has the variable, an error is returned. -fn get_config_var(var_name: &str) -> Result { +pub fn get_config_var(var_name: &str) -> Result { if let Ok(value) = env::var(var_name) { return Ok(value); } diff --git a/crates/core/src/pattern_compiler/builder.rs b/crates/core/src/pattern_compiler/builder.rs index 6a55bf467..80d30ad35 100644 --- a/crates/core/src/pattern_compiler/builder.rs +++ b/crates/core/src/pattern_compiler/builder.rs @@ -315,8 +315,13 @@ impl PatternBuilder { self, file_ranges: Option>, injected_limit: Option, + auto_wrap: bool, ) -> Result { - let target_builder = self.auto_wrap(file_ranges, injected_limit)?; + let target_builder = if auto_wrap { + self.auto_wrap(file_ranges, injected_limit)? + } else { + self + }; let problem = Problem::new( target_builder.tree, target_builder.pattern, diff --git a/crates/core/src/pattern_compiler/compiler.rs b/crates/core/src/pattern_compiler/compiler.rs index 476a96c0a..e730297ab 100644 --- a/crates/core/src/pattern_compiler/compiler.rs +++ b/crates/core/src/pattern_compiler/compiler.rs @@ -592,7 +592,18 @@ pub fn src_to_problem_libs( let src_tree = parser.parse_file(&src, Some(Path::new(DEFAULT_FILE_NAME)))?; let lang = TargetLanguage::from_tree(&src_tree).unwrap_or(default_lang); let builder = PatternBuilder::start(src, libs, lang, name, &mut parser, custom_built_ins)?; - builder.compile(file_ranges, injected_limit) + builder.compile(file_ranges, injected_limit, true) +} + +/// Only use this for testing +pub fn src_to_problem(src: String, default_lang: TargetLanguage) -> Result { + let mut parser = MarzanoGritParser::new()?; + let src_tree = parser.parse_file(&src, Some(Path::new(DEFAULT_FILE_NAME)))?; + let lang = TargetLanguage::from_tree(&src_tree).unwrap_or(default_lang); + let libs = BTreeMap::new(); + let builder = PatternBuilder::start(src, &libs, lang, None, &mut parser, None)?; + let CompilationResult { problem, .. } = builder.compile(None, None, false)?; + Ok(problem) } #[derive(Debug, Default)] diff --git a/crates/core/src/problem.rs b/crates/core/src/problem.rs index 4a2f65bda..2e63548c0 100644 --- a/crates/core/src/problem.rs +++ b/crates/core/src/problem.rs @@ -19,7 +19,7 @@ use grit_pattern_matcher::{ PredicateDefinition, ResolvedPattern, State, VariableContent, }, }; -use grit_util::VariableMatch; +use grit_util::{VariableMatch}; use im::vector; use log::error; use marzano_language::{language::Tree, target_language::TargetLanguage}; @@ -385,6 +385,44 @@ impl Problem { } } + /// Construct a context, only for testing + pub fn get_context<'a>( + &'a self, + context: &'a ExecutionContext, + owned_files: &'a FileOwners, + ) -> (State, MarzanoContext<'a>) { + let file_registry: FileRegistry = FileRegistry::new_from_paths(vec![]); + + let bindings = self + .variables + .locations + .iter() + .map(|scope| { + vector![scope + .iter() + .map(|s| Box::new(VariableContent::new(s.name.clone()))) + .collect()] + }) + .collect(); + let state = State::new(bindings, file_registry); + + ( + state, + MarzanoContext::new( + &self.pattern_definitions, + &self.predicate_definitions, + &self.function_definitions, + &self.foreign_function_definitions, + vec![], + owned_files, + &self.built_ins, + &self.language, + context, + self.name.clone(), + ), + ) + } + fn execute<'a>( &self, binding: FilePattern, diff --git a/crates/grit-util/src/analysis_logs.rs b/crates/grit-util/src/analysis_logs.rs index ac7b716b4..867ead9e5 100644 --- a/crates/grit-util/src/analysis_logs.rs +++ b/crates/grit-util/src/analysis_logs.rs @@ -31,6 +31,18 @@ pub struct AnalysisLog { pub struct AnalysisLogs(Vec); +impl AnalysisLogs { + pub fn new() -> Self { + Self(Vec::new()) + } +} + +impl Default for AnalysisLogs { + fn default() -> Self { + Self::new() + } +} + impl AnalysisLogs { pub fn logs(self) -> Vec { self.0 diff --git a/crates/util/src/runtime.rs b/crates/util/src/runtime.rs index 4760783a4..111c49bbc 100644 --- a/crates/util/src/runtime.rs +++ b/crates/util/src/runtime.rs @@ -13,7 +13,7 @@ use tokio::runtime::Handle; * Nothing in the compiler should depend on ExecutionContext. * In theory, it should be possible to take a compiled pattern and run it with different execution contexts. */ -#[cfg(feature = "network_requests")] +#[cfg(any(test, feature = "network_requests"))] #[derive(Clone, Debug)] pub struct ExecutionContext { llm_api: Option, diff --git a/crates/wasm-bindings/src/match_pattern.rs b/crates/wasm-bindings/src/match_pattern.rs index 6dd673066..03f468c55 100644 --- a/crates/wasm-bindings/src/match_pattern.rs +++ b/crates/wasm-bindings/src/match_pattern.rs @@ -106,7 +106,7 @@ pub async fn parse_input_files_internal( parser, injected_builtins, )?; - match builder.compile(None, None) { + match builder.compile(None, None, true) { Ok(c) => { let warning_logs = c .compilation_warnings @@ -224,7 +224,7 @@ async fn match_pattern_internal( let builder = PatternBuilder::start(pattern, &libs, lang, None, parser, injected_builtins)?; let CompilationResult { problem: pattern, .. - } = builder.compile(None, None)?; + } = builder.compile(None, None, true)?; let files: Vec = paths .into_iter() .zip(contents)