Skip to content
Open
Changes from all 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
37 changes: 36 additions & 1 deletion sway-utils/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn find_nested_dir_with_file(starter_path: &Path, file_name: &str) -> Option
} else {
starter_path.parent()?
};
WalkDir::new(starter_path).into_iter().find_map(|e| {
WalkDir::new(starter_dir).into_iter().find_map(|e| {
let entry = e.ok()?;
if entry.path() != starter_dir.join(file_name) && entry.file_name() == OsStr::new(file_name)
{
Expand Down Expand Up @@ -117,3 +117,38 @@ where
}
})
}

#[cfg(test)]
mod tests {
use super::*;
use std::{
fs::{self, File},
process,
time::{SystemTime, UNIX_EPOCH},
};

fn test_dir(name: &str) -> PathBuf {
let nanos = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system time should be after UNIX_EPOCH")
.as_nanos();
std::env::temp_dir().join(format!("sway-utils-{name}-{}-{nanos}", process::id()))
}

#[test]
fn finds_nested_dir_when_starter_path_is_a_file() {
let root = test_dir("finds-nested-dir-from-file");
let nested_dir = root.join("nested");
fs::create_dir_all(&nested_dir).expect("failed to create nested dir");

let file_name = "Forc.toml";
let starter_file = root.join(file_name);
File::create(&starter_file).expect("failed to create root manifest");
File::create(nested_dir.join(file_name)).expect("failed to create nested manifest");

let nested = find_nested_dir_with_file(&starter_file, file_name);

fs::remove_dir_all(&root).expect("failed to remove test dir");
assert_eq!(nested, Some(nested_dir));
}
}