forked from rust-lang/rust-analyzer
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
18: Add fmt tidy test r=Veykril a=Atul9 Co-authored-by: Atul Bhosale <atul1bhosale@gmail.com>
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::{ | ||
env, | ||
path::{Path, PathBuf}, | ||
process::{Command, Stdio}, | ||
}; | ||
|
||
fn project_root() -> PathBuf { | ||
PathBuf::from( | ||
env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()), | ||
) | ||
} | ||
|
||
fn run(cmd: &str, dir: impl AsRef<Path>) -> Result<(), ()> { | ||
let mut args: Vec<_> = cmd.split_whitespace().collect(); | ||
let bin = args.remove(0); | ||
println!("> {}", cmd); | ||
let output = Command::new(bin) | ||
.args(args) | ||
.current_dir(dir) | ||
.stdin(Stdio::null()) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::inherit()) | ||
.output() | ||
.map_err(drop)?; | ||
if output.status.success() { | ||
Ok(()) | ||
} else { | ||
let stdout = String::from_utf8(output.stdout).map_err(drop)?; | ||
print!("{}", stdout); | ||
Err(()) | ||
} | ||
} | ||
|
||
#[test] | ||
fn check_code_formatting() { | ||
let dir = project_root(); | ||
if run("rustfmt +stable --version", &dir).is_err() { | ||
panic!( | ||
"failed to run rustfmt from toolchain 'stable'; \ | ||
please run `rustup component add rustfmt --toolchain stable` to install it.", | ||
); | ||
} | ||
if run("cargo +stable fmt -- --check", &dir).is_err() { | ||
panic!("code is not properly formatted; please format the code by running `cargo fmt`") | ||
} | ||
} |