Move project_root function to clippy_dev/src/lib.rs
This allows us to use the method in both `fmt.rs` and `lib.rs` in multiple places. The downside is that we panic inside the method now, instead of using the error handling in `fmt.rs`. We may want to centralize the error handling for clippy_dev at some point, though.
This commit is contained in:
parent
4d1a11d354
commit
3036a2c30e
@ -1,7 +1,8 @@
|
||||
use clippy_dev::clippy_project_root;
|
||||
use shell_escape::escape;
|
||||
use std::ffi::OsStr;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::Path;
|
||||
use std::process::{self, Command};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
@ -9,7 +10,6 @@ use walkdir::WalkDir;
|
||||
pub enum CliError {
|
||||
CommandFailed(String),
|
||||
IoError(io::Error),
|
||||
ProjectRootNotFound,
|
||||
RustfmtNotInstalled,
|
||||
WalkDirError(walkdir::Error),
|
||||
}
|
||||
@ -35,7 +35,7 @@ pub fn run(check: bool, verbose: bool) {
|
||||
fn try_run(context: &FmtContext) -> Result<bool, CliError> {
|
||||
let mut success = true;
|
||||
|
||||
let project_root = project_root()?;
|
||||
let project_root = clippy_project_root();
|
||||
|
||||
rustfmt_test(context)?;
|
||||
|
||||
@ -69,9 +69,6 @@ pub fn run(check: bool, verbose: bool) {
|
||||
CliError::IoError(err) => {
|
||||
eprintln!("error: {}", err);
|
||||
},
|
||||
CliError::ProjectRootNotFound => {
|
||||
eprintln!("error: Can't determine root of project. Please run inside a Clippy working dir.");
|
||||
},
|
||||
CliError::RustfmtNotInstalled => {
|
||||
eprintln!("error: rustfmt nightly is not installed.");
|
||||
},
|
||||
@ -176,22 +173,3 @@ fn rustfmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> {
|
||||
}
|
||||
Ok(success)
|
||||
}
|
||||
|
||||
fn project_root() -> Result<PathBuf, CliError> {
|
||||
let current_dir = std::env::current_dir()?;
|
||||
for path in current_dir.ancestors() {
|
||||
let result = std::fs::read_to_string(path.join("Cargo.toml"));
|
||||
if let Err(err) = &result {
|
||||
if err.kind() == io::ErrorKind::NotFound {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let content = result?;
|
||||
if content.contains("[package]\nname = \"clippy\"") {
|
||||
return Ok(path.to_path_buf());
|
||||
}
|
||||
}
|
||||
|
||||
Err(CliError::ProjectRootNotFound)
|
||||
}
|
||||
|
@ -206,7 +206,7 @@ fn parse_contents(content: &str, filename: &str) -> impl Iterator<Item = Lint> {
|
||||
fn lint_files() -> impl Iterator<Item = walkdir::DirEntry> {
|
||||
// We use `WalkDir` instead of `fs::read_dir` here in order to recurse into subdirectories.
|
||||
// Otherwise we would not collect all the lints, for example in `clippy_lints/src/methods/`.
|
||||
let path = clippy_project_dir().join("clippy_lints/src");
|
||||
let path = clippy_project_root().join("clippy_lints/src");
|
||||
WalkDir::new(path)
|
||||
.into_iter()
|
||||
.filter_map(std::result::Result::ok)
|
||||
@ -237,7 +237,7 @@ pub fn replace_region_in_file<F>(
|
||||
where
|
||||
F: Fn() -> Vec<String>,
|
||||
{
|
||||
let path = clippy_project_dir().join(path);
|
||||
let path = clippy_project_root().join(path);
|
||||
let mut f = fs::File::open(&path).expect(&format!("File not found: {}", path.to_string_lossy()));
|
||||
let mut contents = String::new();
|
||||
f.read_to_string(&mut contents)
|
||||
@ -322,9 +322,22 @@ where
|
||||
}
|
||||
|
||||
/// Returns the path to the Clippy project directory
|
||||
fn clippy_project_dir() -> PathBuf {
|
||||
let clippy_dev_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
|
||||
clippy_dev_dir.parent().unwrap().to_path_buf()
|
||||
pub fn clippy_project_root() -> PathBuf {
|
||||
let current_dir = std::env::current_dir().unwrap();
|
||||
for path in current_dir.ancestors() {
|
||||
let result = std::fs::read_to_string(path.join("Cargo.toml"));
|
||||
if let Err(err) = &result {
|
||||
if err.kind() == std::io::ErrorKind::NotFound {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
let content = result.unwrap();
|
||||
if content.contains("[package]\nname = \"clippy\"") {
|
||||
return path.to_path_buf();
|
||||
}
|
||||
}
|
||||
panic!("error: Can't determine root of project. Please run inside a Clippy working dir.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user