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

chore: add some utils for testing #322

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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 @@
PredicateDefinition, ResolvedPattern, State, VariableContent,
},
};
use grit_util::VariableMatch;
use grit_util::{AnalysisLogs, VariableMatch};

Check failure on line 22 in crates/core/src/problem.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `AnalysisLogs`

error: unused import: `AnalysisLogs` --> crates/core/src/problem.rs:22:17 | 22 | use grit_util::{AnalysisLogs, VariableMatch}; | ^^^^^^^^^^^^ | = note: `-D unused-imports` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_imports)]`
use im::vector;
use log::error;
use marzano_language::{language::Tree, target_language::TargetLanguage};
Expand Down Expand Up @@ -385,6 +385,44 @@
}
}

/// 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 mut state = State::new(bindings, file_registry);

Check failure on line 407 in crates/core/src/problem.rs

View workflow job for this annotation

GitHub Actions / clippy

variable does not need to be mutable

error: variable does not need to be mutable --> crates/core/src/problem.rs:407:13 | 407 | let mut state = State::new(bindings, file_registry); | ----^^^^^ | | | help: remove this `mut` | = note: `-D unused-mut` implied by `-D warnings` = help: to override `-D warnings` add `#[allow(unused_mut)]`

(
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
2 changes: 1 addition & 1 deletion 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
Loading