-
Notifications
You must be signed in to change notification settings - Fork 499
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ignore file name case while searching for justfile (#436)
- Loading branch information
Showing
8 changed files
with
302 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
use crate::common::*; | ||
use std::fs; | ||
use std::path::{Path, PathBuf}; | ||
|
||
const FILENAME: &str = "justfile"; | ||
|
||
pub fn justfile(directory: &Path) -> Result<PathBuf, SearchError> { | ||
let mut candidates = Vec::new(); | ||
let dir = fs::read_dir(directory).map_err(|io_error| SearchError::Io { | ||
io_error, | ||
directory: directory.to_owned(), | ||
})?; | ||
for entry in dir { | ||
let entry = entry.map_err(|io_error| SearchError::Io { | ||
io_error, | ||
directory: directory.to_owned(), | ||
})?; | ||
if let Some(name) = entry.file_name().to_str() { | ||
if name.eq_ignore_ascii_case(FILENAME) { | ||
candidates.push(entry.path()); | ||
} | ||
} | ||
} | ||
if candidates.len() == 1 { | ||
Ok(candidates.pop().unwrap()) | ||
} else if candidates.len() > 1 { | ||
Err(SearchError::MultipleCandidates { candidates }) | ||
} else if let Some(parent_dir) = directory.parent() { | ||
justfile(parent_dir) | ||
} else { | ||
Err(SearchError::NotFound) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::fs; | ||
use tempdir::TempDir; | ||
|
||
#[test] | ||
fn not_found() { | ||
let tmp = TempDir::new("just-test-justfile-search") | ||
.expect("test justfile search: failed to create temporary directory"); | ||
match search::justfile(tmp.path()) { | ||
Err(SearchError::NotFound) => { | ||
assert!(true); | ||
} | ||
_ => panic!("No justfile found error was expected"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn multiple_candidates() { | ||
let tmp = TempDir::new("just-test-justfile-search") | ||
.expect("test justfile search: failed to create temporary directory"); | ||
let mut path = tmp.path().to_path_buf(); | ||
path.push(FILENAME); | ||
fs::write(&path, "default:\n\techo ok").unwrap(); | ||
path.pop(); | ||
path.push(FILENAME.to_uppercase()); | ||
if let Ok(_) = fs::File::open(path.as_path()) { | ||
// We are in case-insensitive file system | ||
return; | ||
} | ||
fs::write(&path, "default:\n\techo ok").unwrap(); | ||
path.pop(); | ||
match search::justfile(path.as_path()) { | ||
Err(SearchError::MultipleCandidates { .. }) => { | ||
assert!(true); | ||
} | ||
_ => panic!("Multiple candidates error was expected"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn found() { | ||
let tmp = TempDir::new("just-test-justfile-search") | ||
.expect("test justfile search: failed to create temporary directory"); | ||
let mut path = tmp.path().to_path_buf(); | ||
path.push(FILENAME); | ||
fs::write(&path, "default:\n\techo ok").unwrap(); | ||
path.pop(); | ||
match search::justfile(path.as_path()) { | ||
Ok(_path) => { | ||
assert!(true); | ||
} | ||
_ => panic!("No errors were expected"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn found_spongebob_case() { | ||
let tmp = TempDir::new("just-test-justfile-search") | ||
.expect("test justfile search: failed to create temporary directory"); | ||
let mut path = tmp.path().to_path_buf(); | ||
let spongebob_case = FILENAME | ||
.chars() | ||
.enumerate() | ||
.map(|(i, c)| { | ||
if i % 2 == 0 { | ||
c.to_ascii_uppercase() | ||
} else { | ||
c | ||
} | ||
}) | ||
.collect::<String>(); | ||
path.push(spongebob_case); | ||
fs::write(&path, "default:\n\techo ok").unwrap(); | ||
path.pop(); | ||
match search::justfile(path.as_path()) { | ||
Ok(_path) => { | ||
assert!(true); | ||
} | ||
_ => panic!("No errors were expected"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn found_from_inner_dir() { | ||
let tmp = TempDir::new("just-test-justfile-search") | ||
.expect("test justfile search: failed to create temporary directory"); | ||
let mut path = tmp.path().to_path_buf(); | ||
path.push(FILENAME); | ||
fs::write(&path, "default:\n\techo ok").unwrap(); | ||
path.pop(); | ||
path.push("a"); | ||
fs::create_dir(&path).expect("test justfile search: failed to create intermediary directory"); | ||
path.push("b"); | ||
fs::create_dir(&path).expect("test justfile search: failed to create intermediary directory"); | ||
match search::justfile(path.as_path()) { | ||
Ok(_path) => { | ||
assert!(true); | ||
} | ||
_ => panic!("No errors were expected"), | ||
} | ||
} | ||
|
||
#[test] | ||
fn found_and_stopped_at_first_justfile() { | ||
let tmp = TempDir::new("just-test-justfile-search") | ||
.expect("test justfile search: failed to create temporary directory"); | ||
let mut path = tmp.path().to_path_buf(); | ||
path.push(FILENAME); | ||
fs::write(&path, "default:\n\techo ok").unwrap(); | ||
path.pop(); | ||
path.push("a"); | ||
fs::create_dir(&path).expect("test justfile search: failed to create intermediary directory"); | ||
path.push(FILENAME); | ||
fs::write(&path, "default:\n\techo ok").unwrap(); | ||
path.pop(); | ||
path.push("b"); | ||
fs::create_dir(&path).expect("test justfile search: failed to create intermediary directory"); | ||
match search::justfile(path.as_path()) { | ||
Ok(found_path) => { | ||
path.pop(); | ||
path.push(FILENAME); | ||
assert_eq!(found_path, path); | ||
} | ||
_ => panic!("No errors were expected"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::{fmt, io, path::PathBuf}; | ||
|
||
use crate::misc::And; | ||
|
||
pub enum SearchError { | ||
MultipleCandidates { | ||
candidates: Vec<PathBuf>, | ||
}, | ||
Io { | ||
directory: PathBuf, | ||
io_error: io::Error, | ||
}, | ||
NotFound, | ||
} | ||
|
||
impl fmt::Display for SearchError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
match self { | ||
SearchError::Io { | ||
directory, | ||
io_error, | ||
} => write!( | ||
f, | ||
"I/O error reading directory `{}`: {}", | ||
directory.display(), | ||
io_error | ||
), | ||
SearchError::MultipleCandidates { candidates } => write!( | ||
f, | ||
"Multiple candidate justfiles found in `{}`: {}", | ||
candidates[0].parent().unwrap().display(), | ||
And( | ||
&candidates | ||
.iter() | ||
.map(|candidate| format!("`{}`", candidate.file_name().unwrap().to_string_lossy())) | ||
.collect::<Vec<String>>() | ||
), | ||
), | ||
SearchError::NotFound => write!(f, "No justfile found"), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn multiple_candidates_formatting() { | ||
let error = SearchError::MultipleCandidates { | ||
candidates: vec![ | ||
PathBuf::from("/foo/justfile"), | ||
PathBuf::from("/foo/JUSTFILE"), | ||
], | ||
}; | ||
|
||
assert_eq!( | ||
error.to_string(), | ||
"Multiple candidate justfiles found in `/foo`: `justfile` and `JUSTFILE`" | ||
) | ||
} | ||
} |
Oops, something went wrong.