Find user configuration even if there’s no project directory (#216)

This commit is contained in:
Anders Kaseorg 2022-09-16 20:47:15 -07:00 committed by GitHub
parent 9bdb922c75
commit b8f878df5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 21 deletions

View File

@ -10,8 +10,8 @@ use crate::checks::CheckCode;
use crate::fs;
pub fn load_config(paths: &[PathBuf]) -> Config {
match find_project_root(paths) {
Some(project_root) => match find_pyproject_toml(&project_root) {
let project_root = find_project_root(paths);
match find_pyproject_toml(project_root.as_deref()) {
Some(path) => {
debug!("Found pyproject.toml at: {:?}", path);
match parse_pyproject_toml(&path) {
@ -27,8 +27,6 @@ pub fn load_config(paths: &[PathBuf]) -> Config {
}
}
None => Default::default(),
},
None => Default::default(),
}
}
@ -57,11 +55,14 @@ fn parse_pyproject_toml(path: &Path) -> Result<PyProject> {
toml::from_str(&contents).map_err(|e| e.into())
}
fn find_pyproject_toml(path: &Path) -> Option<PathBuf> {
fn find_pyproject_toml(path: Option<&Path>) -> Option<PathBuf> {
if let Some(path) = path {
let path_pyproject_toml = path.join("pyproject.toml");
if path_pyproject_toml.is_file() {
return Some(path_pyproject_toml);
}
}
find_user_pyproject_toml()
}
@ -252,7 +253,8 @@ other-attribute = 1
.expect("Unable to find project root.");
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(Some(&project_root)).expect("Unable to find pyproject.toml.");
assert_eq!(path, cwd.join("resources/test/fixtures/pyproject.toml"));
let pyproject = parse_pyproject_toml(&path)?;