Skip to content

Commit

Permalink
chore: add some utils for testing (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante authored May 10, 2024
1 parent bac2b39 commit 9c63d9a
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/auth/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
pub fn get_config_var(var_name: &str) -> Result<String> {
if let Ok(value) = env::var(var_name) {
return Ok(value);
}
Expand Down
7 changes: 6 additions & 1 deletion crates/core/src/pattern_compiler/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,13 @@ impl PatternBuilder {
self,
file_ranges: Option<Vec<FileRange>>,
injected_limit: Option<usize>,
auto_wrap: bool,
) -> Result<CompilationResult> {
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,
Expand Down
13 changes: 12 additions & 1 deletion crates/core/src/pattern_compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Problem> {
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)]
Expand Down
40 changes: 39 additions & 1 deletion crates/core/src/problem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Tree>,
) -> (State<MarzanoQueryContext>, MarzanoContext<'a>) {
let file_registry: FileRegistry<MarzanoQueryContext> = 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,
Expand Down
12 changes: 12 additions & 0 deletions crates/grit-util/src/analysis_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ pub struct AnalysisLog {

pub struct AnalysisLogs(Vec<AnalysisLog>);

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<AnalysisLog> {
self.0
Expand Down
2 changes: 1 addition & 1 deletion crates/util/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<LanguageModelAPI>,
Expand Down
4 changes: 2 additions & 2 deletions crates/wasm-bindings/src/match_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<RichFile> = paths
.into_iter()
.zip(contents)
Expand Down

0 comments on commit 9c63d9a

Please sign in to comment.