lint_py.rs raw
1 // Copyright (c) The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://opensource.org/license/mit/.
4
5 use std::io::ErrorKind;
6 use std::process::Command;
7
8 use crate::util::{check_output, get_pathspecs_default_excludes, git, LintResult};
9
10 pub fn lint_py_lint() -> LintResult {
11 let bin_name = "ruff";
12 let files = check_output(
13 git()
14 .args(["ls-files", "--", "*.py"])
15 .args(get_pathspecs_default_excludes()),
16 )?;
17
18 let mut cmd = Command::new(bin_name);
19 cmd.arg("check").args(files.lines());
20
21 match cmd.status() {
22 Ok(status) if status.success() => Ok(()),
23 Ok(_) => Err(format!("`{bin_name}` found errors!")),
24 Err(e) if e.kind() == ErrorKind::NotFound => {
25 println!("`{bin_name}` was not found in $PATH, skipping those checks.");
26 Ok(())
27 }
28 Err(e) => Err(format!("Error running `{bin_name}`: {e}")),
29 }
30 }
31
32 pub fn lint_rmtree() -> LintResult {
33 let found = git()
34 .args([
35 "grep",
36 "--line-number",
37 "rmtree",
38 "--",
39 "test/functional/",
40 ":(exclude)test/functional/test_framework/test_framework.py",
41 ])
42 .status()
43 .expect("command error")
44 .success();
45 if found {
46 Err(r#"
47 Use of shutil.rmtree() is dangerous and should be avoided. If it
48 is really required for the test, use self.cleanup_folder(_).
49 "#
50 .trim()
51 .to_string())
52 } else {
53 Ok(())
54 }
55 }
56