Skip to content

Commit

Permalink
fix: relative path ignore patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
JMBeresford committed Feb 10, 2025
1 parent 357138c commit d1333c9
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 20 deletions.
48 changes: 41 additions & 7 deletions packages/service/src/grpc/library/content_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,24 @@ impl ContentResolver {
None => return true,
};

let rel_path = match path
.strip_prefix(&content_dir_path)
.unwrap_or(path)
.to_str()
{
let abs_path = match path.canonicalize() {
Ok(p) => p,
Err(why) => {
warn!("Could not canonicalize path: {:?}", why);
return true;
}
};

let abs_parent = content_dir_path
.parent()
.and_then(|p| p.canonicalize().ok());

let rel_path = match abs_parent {
Some(parent) => abs_path.strip_prefix(parent).unwrap_or(path),
None => path,
};

let rel_path = match rel_path.to_str() {
Some(rp) => rp,
None => return true,
};
Expand Down Expand Up @@ -140,17 +153,29 @@ mod tests {
}

#[tokio::test]
async fn ignores_nested_relative_path() {
async fn ignores_relative_path() {
let dir = tempfile::tempdir().unwrap();
let platform_dir = tempfile::TempDir::with_prefix_in("platform-", &dir).unwrap();
let _ignore_dir1 = tempfile::TempDir::with_prefix_in("ignore1-", &dir).unwrap();
let _ignore_dir2 = tempfile::TempDir::with_prefix_in("ignore2-", &dir).unwrap();

let game_file = tempfile::NamedTempFile::with_prefix_in("game-", &platform_dir).unwrap();
let _ignore_file =
tempfile::NamedTempFile::with_prefix_in("ignore-", &platform_dir).unwrap();

let cdir_relative_path = dir
.path()
.file_name()
.and_then(|ostr| ostr.to_str())
.unwrap();

let content_directory = ContentDirectory {
path: dir.path().to_str().unwrap().to_string(),
ignore_patterns: Some(IgnorePatterns {
patterns: vec!["platform-(.+)/ignore-".into()],
patterns: vec![
"platform-(.+)/ignore-".into(),
format!("{cdir_relative_path}/ignore"),
],
}),
storage_type: Some(StorageType::SingleFileGame.into()),
};
Expand All @@ -163,6 +188,15 @@ mod tests {
.map(|r| r.mock_resolve())
.collect::<Vec<_>>();

assert_eq!(resolved_platforms.len(), 1);
assert!(resolved_platforms.iter().any(|rp| rp.row.path
== platform_dir
.path()
.canonicalize()
.ok()
.and_then(|ref p| p.to_str().map(|s| s.to_string()))
.unwrap()));

let game_resolvers = resolved_platforms
.iter()
.flat_map(|pr| pr.get_game_resolvers())
Expand Down
11 changes: 6 additions & 5 deletions packages/service/src/grpc/library/game_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,19 @@ impl ResolvedGame {
.into_iter()
.filter_map(|entry| entry.ok())
.filter(|entry| {
let path = match entry.path().canonicalize() {
let path = entry.path();
let abs_path = match path.canonicalize() {
Ok(p) => p,
Err(why) => {
warn!("Could not canonicalize path: {:?}", why);
return false;
}
};

let rel_path = path
.strip_prefix(&content_dir_path)
.map(|p| p.to_path_buf())
.unwrap_or(path);
let rel_path = match content_dir_path.parent() {
Some(p) => abs_path.strip_prefix(p).unwrap_or(path),
None => path,
};

let rel_path = match rel_path.to_str() {
Some(p) => p,
Expand Down
28 changes: 20 additions & 8 deletions packages/service/src/grpc/library/platform_resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,31 @@ impl ResolvedPlatform {
})
.filter(|path| {
let content_dir = &self.content_resolver.content_directory;
let content_dir_path = content_dir.path.clone();
let rel_path = match path
.strip_prefix(&content_dir_path)
.unwrap_or(path)
.to_str()
{
Some(rp) => rp.to_string(),
let abs_path = match path.canonicalize() {
Ok(p) => p,
Err(why) => {
warn!("Could not canonicalize path: {:?}", why);
return true;
}
};

let abs_parent = PathBuf::from(&content_dir.path)
.parent()
.and_then(|p| p.canonicalize().ok());

let rel_path = match abs_parent {
Some(parent) => abs_path.strip_prefix(parent).unwrap_or(path),
None => path,
};

let rel_path = match rel_path.to_str() {
Some(rp) => rp,
None => return true,
};

match &self.content_resolver.ignore_regex_set {
Some(irs) => !irs.is_match(rel_path),
None => true,
Some(irs) => !irs.is_match(&rel_path),
}
})
.filter(|path| {
Expand Down

0 comments on commit d1333c9

Please sign in to comment.