use std::path::{Path, PathBuf};
pub const DISCOVERY_ORDER: &[&str] = &[
"Flockfile.toml",
"Flockfile.yaml",
"Flockfile.yml",
"Flockfile.json",
"Flockfile.json5",
"flockfile.toml",
"flockfile.yaml",
"flockfile.yml",
"flockfile.json",
"flockfile.json5",
];
#[must_use]
pub fn discover(dir: &Path) -> Option<PathBuf> {
DISCOVERY_ORDER
.iter()
.map(|name| dir.join(name))
.find(|p| p.is_file())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::flockfile::format::FlockFormat;
#[test]
fn discover_prefers_toml_then_capitalized() {
let dir = tempfile::tempdir().unwrap();
std::fs::write(dir.path().join("flockfile.json"), "{}").unwrap();
std::fs::write(dir.path().join("Flockfile.yaml"), "").unwrap();
assert_eq!(
discover(dir.path()),
Some(dir.path().join("Flockfile.yaml"))
);
std::fs::write(dir.path().join("Flockfile.toml"), "").unwrap();
assert_eq!(
discover(dir.path()),
Some(dir.path().join("Flockfile.toml"))
);
}
#[test]
fn discovery_never_names_a_js_file_and_stays_ten_names() {
assert_eq!(DISCOVERY_ORDER.len(), 10);
for name in DISCOVERY_ORDER {
assert!(
!name.ends_with(".js"),
"{name} would let `shep start` execute a repo's JavaScript"
);
assert!(FlockFormat::from_path(Path::new(name)).is_some());
}
}
}