Skip to content

Commit c2c13df

Browse files
bors[bot]Atul9
andauthored
18: Add fmt tidy test r=Veykril a=Atul9 Co-authored-by: Atul Bhosale <atul1bhosale@gmail.com>
2 parents c243a10 + 4c4e75f commit c2c13df

File tree

1 file changed

+46
-0
lines changed
  • src/tools/rust-analyzer/lib/smol_str/tests

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use std::{
2+
env,
3+
path::{Path, PathBuf},
4+
process::{Command, Stdio},
5+
};
6+
7+
fn project_root() -> PathBuf {
8+
PathBuf::from(
9+
env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| env!("CARGO_MANIFEST_DIR").to_owned()),
10+
)
11+
}
12+
13+
fn run(cmd: &str, dir: impl AsRef<Path>) -> Result<(), ()> {
14+
let mut args: Vec<_> = cmd.split_whitespace().collect();
15+
let bin = args.remove(0);
16+
println!("> {}", cmd);
17+
let output = Command::new(bin)
18+
.args(args)
19+
.current_dir(dir)
20+
.stdin(Stdio::null())
21+
.stdout(Stdio::piped())
22+
.stderr(Stdio::inherit())
23+
.output()
24+
.map_err(drop)?;
25+
if output.status.success() {
26+
Ok(())
27+
} else {
28+
let stdout = String::from_utf8(output.stdout).map_err(drop)?;
29+
print!("{}", stdout);
30+
Err(())
31+
}
32+
}
33+
34+
#[test]
35+
fn check_code_formatting() {
36+
let dir = project_root();
37+
if run("rustfmt +stable --version", &dir).is_err() {
38+
panic!(
39+
"failed to run rustfmt from toolchain 'stable'; \
40+
please run `rustup component add rustfmt --toolchain stable` to install it.",
41+
);
42+
}
43+
if run("cargo +stable fmt -- --check", &dir).is_err() {
44+
panic!("code is not properly formatted; please format the code by running `cargo fmt`")
45+
}
46+
}

0 commit comments

Comments
 (0)