Skip to content

Commit

Permalink
thus the last test falls
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed May 6, 2024
1 parent 80c63ab commit 0aaa4d1
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 11 deletions.
2 changes: 2 additions & 0 deletions crates/cli_bin/tests/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,8 @@ fn test_absolute_path() -> Result<()> {
let file = dir.join("dir2/unique.js");
let content = std::fs::read_to_string(file)?;

println!("content: {:?}", content);

// Verify it contains dir2/unique.js
assert!(content.contains("dir2/unique.js"));

Expand Down
1 change: 1 addition & 0 deletions crates/core/src/built_in_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ fn resolve_path_fn<'a>(
let args = MarzanoResolvedPattern::from_patterns(args, state, context, logs)?;

let current_file = get_absolute_file_name(state, context.language())?;

let target_path = match &args[0] {
Some(resolved_pattern) => resolved_pattern.text(&state.files, context.language())?,
None => return Err(anyhow!("No path argument provided for resolve function")),
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/marzano_resolved_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ impl<'a> File<'a, MarzanoQueryContext> for MarzanoFile<'a> {
)))
}
Self::Ptr(ptr) => Ok(ResolvedPattern::from_path_binding(
files.get_file_name(*ptr),
files.get_absolute_path(*ptr)?,
)),
}
}
Expand Down
47 changes: 46 additions & 1 deletion crates/core/src/test_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use marzano_util::{
};
use serde::{Deserialize, Serialize};

use crate::api::MatchResult;
use crate::api::{MatchResult, Rewrite};

use self::{pattern_compiler::src_to_problem_libs, problem::Problem};
use anyhow::Result;
Expand Down Expand Up @@ -166,6 +166,51 @@ fn test_lazy_filename_variable() {
assert!(results.iter().any(|r| r.is_match()));
}

#[test]
fn test_absolute_path_resolution() {
let pattern_src = r#"
file(body=contains bubble `console.log($msg)` where {
$resolved = resolve($absolute_filename),
$msg => `Hello, from $resolved`
})
"#;
let libs = BTreeMap::new();

let matching_src = r#"
console.log("Hello, world!");
"#;

let pattern = src_to_problem_libs(
pattern_src.to_string(),
&libs,
TargetLanguage::default(),
None,
None,
None,
None,
)
.unwrap()
.problem;

// All together now
let test_files = vec![SyntheticFile::new(
"file/dir/target.js".to_owned(),
matching_src.to_owned(),
true,
)];
let results = run_on_test_files(&pattern, &test_files);
assert!(!results.iter().any(|r| r.is_error()));
let mut has_rewrite = false;
for r in results.iter() {
if let MatchResult::Rewrite(Rewrite { rewritten, .. }) = r {
let content = &rewritten.content;
assert!(content.contains("core/file/dir/target.js"));
has_rewrite = true;
}
}
assert!(has_rewrite);
}

#[test]
fn test_lazy_program_variable() {
let pattern_src = r#"
Expand Down
4 changes: 3 additions & 1 deletion crates/grit-pattern-matcher/src/pattern/file_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
state::State,
};
use crate::{
constants::{FILENAME_INDEX, GLOBAL_VARS_SCOPE_INDEX, PROGRAM_INDEX},
constants::{ABSOLUTE_PATH_INDEX, FILENAME_INDEX, GLOBAL_VARS_SCOPE_INDEX, PROGRAM_INDEX},
context::ExecContext,
};
use crate::{context::QueryContext, pattern::resolved_pattern::File};
Expand Down Expand Up @@ -56,6 +56,8 @@ impl<Q: QueryContext> Matcher<Q> for FilePattern<Q> {
Some(file.binding(&state.files));
state.bindings[GLOBAL_VARS_SCOPE_INDEX].back_mut().unwrap()[FILENAME_INDEX].value =
Some(file.name(&state.files));
state.bindings[GLOBAL_VARS_SCOPE_INDEX].back_mut().unwrap()[ABSOLUTE_PATH_INDEX].value =
Some(file.absolute_path(&state.files, context.language())?);

if !self
.body
Expand Down
2 changes: 0 additions & 2 deletions crates/grit-pattern-matcher/src/pattern/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ impl<Q: QueryContext> Matcher<Q> for Pattern<Q> {
if let Some(file) = binding.get_file() {
state.bindings[GLOBAL_VARS_SCOPE_INDEX].back_mut().unwrap()[FILENAME_INDEX].value =
Some(file.name(&state.files));
state.bindings[GLOBAL_VARS_SCOPE_INDEX].back_mut().unwrap()[ABSOLUTE_PATH_INDEX]
.value = Some(file.absolute_path(&state.files, context.language())?);
}

match self {
Expand Down
16 changes: 10 additions & 6 deletions crates/grit-pattern-matcher/src/pattern/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,17 @@ impl<'a, Q: QueryContext> FileRegistry<'a, Q> {
.expect("File path should exist for given file index.")
}

/// If you already have all the files loaded, immediately create a FileRegistry
pub fn new(files: Vec<&'a FileOwner<Q::Tree>>) -> Self {
Self {
version_count: files.iter().map(|_| 1).collect(),
file_paths: files.iter().map(|f| &f.name).collect(),
owners: files.into_iter().map(|f| vector![f]).collect(),
pub fn get_absolute_path(&self, pointer: FilePtr) -> Result<&'a PathBuf> {
let file_index = pointer.file as usize;
let version_index = pointer.version as usize;
if let Some(owners) = self.owners.get(file_index) {
if let Some(owner) = owners.get(version_index) {
return Ok(&owner.absolute_path);
}
}
Err(anyhow!(
"Absolute file path accessed before file was loaded."
))
}

/// If only the paths are available, create a FileRegistry with empty owners
Expand Down

0 comments on commit 0aaa4d1

Please sign in to comment.