Fix find_project_root with relative paths (#214)

This commit is contained in:
Anders Kaseorg 2022-09-16 17:04:35 -07:00 committed by GitHub
parent 8e903153f6
commit edecc1bba6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 3 deletions

View File

@ -1,3 +1,4 @@
use std::env::current_dir;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::Result; use anyhow::Result;
@ -69,7 +70,10 @@ fn find_user_pyproject_toml() -> Option<PathBuf> {
} }
fn find_project_root(sources: &[PathBuf]) -> Option<PathBuf> { fn find_project_root(sources: &[PathBuf]) -> Option<PathBuf> {
if let Some(prefix) = common_path_all(sources.iter().map(PathBuf::as_path)) { let cwd = current_dir().unwrap_or_else(|_| ".".into());
// common_path doesn't work correctly with relative paths
let absolute_sources: Vec<PathBuf> = sources.iter().map(|source| cwd.join(source)).collect();
if let Some(prefix) = common_path_all(absolute_sources.iter().map(PathBuf::as_path)) {
for directory in prefix.ancestors() { for directory in prefix.ancestors() {
if directory.join(".git").is_dir() { if directory.join(".git").is_dir() {
return Some(directory.to_path_buf()); return Some(directory.to_path_buf());
@ -88,6 +92,7 @@ fn find_project_root(sources: &[PathBuf]) -> Option<PathBuf> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::env::current_dir;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::Result; use anyhow::Result;
@ -241,13 +246,14 @@ other-attribute = 1
#[test] #[test]
fn find_and_parse_pyproject_toml() -> Result<()> { fn find_and_parse_pyproject_toml() -> Result<()> {
let cwd = current_dir().unwrap_or_else(|_| ".".into());
let project_root = let project_root =
find_project_root(&[PathBuf::from("resources/test/fixtures/__init__.py")]) find_project_root(&[PathBuf::from("resources/test/fixtures/__init__.py")])
.expect("Unable to find project root."); .expect("Unable to find project root.");
assert_eq!(project_root, Path::new("resources/test/fixtures")); assert_eq!(project_root, cwd.join("resources/test/fixtures"));
let path = find_pyproject_toml(&project_root).expect("Unable to find pyproject.toml."); let path = find_pyproject_toml(&project_root).expect("Unable to find pyproject.toml.");
assert_eq!(path, Path::new("resources/test/fixtures/pyproject.toml")); assert_eq!(path, cwd.join("resources/test/fixtures/pyproject.toml"));
let pyproject = parse_pyproject_toml(&path)?; let pyproject = parse_pyproject_toml(&path)?;
let config = pyproject let config = pyproject