Fix find_root() search in case if .git isn't dir

This commit is contained in:
juliancoffee 2021-06-08 01:26:59 +03:00
parent 8f86f474e8
commit df2c3a4950
3 changed files with 8 additions and 23 deletions

View File

@ -128,15 +128,16 @@ impl Asset for DotVoxAsset {
const EXTENSION: &'static str = "vox";
}
fn find_root() -> Option<PathBuf> {
/// Return path to repository root by searching 10 directories back
pub fn find_root() -> Option<PathBuf> {
std::env::current_dir().map_or(None, |path| {
// If we are in the root, push path
if path.join(".git").is_dir() {
if path.join(".git").exists() {
return Some(path);
}
// Search .git directory in parent directries
for ancestor in path.ancestors().take(10) {
if ancestor.join(".git").is_dir() {
if ancestor.join(".git").exists() {
return Some(ancestor.to_path_buf());
}
}

View File

@ -31,7 +31,7 @@ fn main() {
.get_matches();
// Generate paths
let root = veloren_i18n::find_root().expect("Failed to find root of repository");
let root = common_assets::find_root().expect("Failed to find root of repository");
let asset_path = Path::new("assets/voxygen/i18n/");
if let Some(code) = matches.value_of("CODE") {

View File

@ -388,23 +388,6 @@ pub fn list_localizations() -> Vec<LanguageMetadata> {
/// Start hot reloading of i18n assets
pub fn start_hot_reloading() { assets::start_hot_reloading(); }
/// Return path to repository by searching 10 directories back
pub fn find_root() -> Option<PathBuf> {
std::env::current_dir().map_or(None, |path| {
// If we are in the root, push path
if path.join(".git").is_dir() {
return Some(path);
}
// Search .git directory in parent directries
for ancestor in path.ancestors().take(10) {
if ancestor.join(".git").is_dir() {
return Some(ancestor.to_path_buf());
}
}
None
})
}
/// List localization directories as a `PathBuf` vector
pub fn i18n_directories(i18n_dir: &Path) -> Vec<PathBuf> {
fs::read_dir(i18n_dir)
@ -416,6 +399,7 @@ pub fn i18n_directories(i18n_dir: &Path) -> Vec<PathBuf> {
#[cfg(test)]
mod tests {
use super::assets;
// Test that localization list is loaded (not empty)
#[test]
fn test_localization_list() {
@ -435,7 +419,7 @@ mod tests {
fn verify_all_localizations() {
// Generate paths
let i18n_asset_path = std::path::Path::new("assets/voxygen/i18n/");
let root_dir = super::find_root().expect("Failed to discover repository root");
let root_dir = assets::find_root().expect("Failed to discover repository root");
crate::verification::verify_all_localizations(&root_dir, &i18n_asset_path);
}
@ -447,7 +431,7 @@ mod tests {
let be_verbose = true;
// Generate paths
let i18n_asset_path = std::path::Path::new("assets/voxygen/i18n/");
let root_dir = super::find_root().expect("Failed to discover repository root");
let root_dir = assets::find_root().expect("Failed to discover repository root");
crate::analysis::test_all_localizations(&root_dir, &i18n_asset_path, be_verbose);
}
}