test: functional: drop rmtree usage and add lint check

`shutil.rmtree` is dangerous because it recursively deletes. There are
not likely to be any issues with it's current uses, but it is possible
that some of the assumptions being made now won't always be true, e.g.
about what some of the variables being passed to `rmtree` represent.

For some remaining uses of rmtree that can't be avoided for now, use
`cleanup_dir` which asserts that the recursively deleted folder is a
child of the the `tmpdir` of the test run. Otherwise,
`tempfile.TemporaryDirectory` should be used which does it's own
deleting on being garbage collected, or old fashioned unlinking and
rmdir in the case of directories with known contents.
This commit is contained in:
David Gumberg
2026-01-28 15:53:10 -08:00
parent 8bfb422de8
commit 0d1301b47a
16 changed files with 68 additions and 37 deletions

View File

@@ -73,3 +73,28 @@ pub fn lint_py_lint() -> LintResult {
Err(e) => Err(format!("Error running `{bin_name}`: {e}")),
}
}
pub fn lint_rmtree() -> LintResult {
let found = git()
.args([
"grep",
"--line-number",
"rmtree",
"--",
"test/functional/",
":(exclude)test/functional/test_framework/test_framework.py",
])
.status()
.expect("command error")
.success();
if found {
Err(r#"
Use of shutil.rmtree() is dangerous and should be avoided.
"#
.trim()
.to_string())
} else {
Ok(())
}
}

View File

@@ -17,7 +17,7 @@ use lint_cpp::{
lint_boost_assert, lint_includes_build_config, lint_remove_all, lint_rpc_assert, lint_std_filesystem,
};
use lint_docs::{lint_doc_args, lint_doc_release_note_snippets, lint_markdown};
use lint_py::lint_py_lint;
use lint_py::{lint_py_lint, lint_rmtree};
use lint_repo_hygiene::{lint_scripted_diff, lint_subtree};
use lint_text_format::{
lint_commit_msg, lint_tabs_whitespace, lint_trailing_newline, lint_trailing_whitespace,
@@ -52,6 +52,11 @@ fn get_linter_list() -> Vec<&'static Linter> {
name: "py_lint",
lint_fn: lint_py_lint,
},
&Linter {
description: "Check that shutil.rmtree is not used",
name: "rmtree",
lint_fn: lint_rmtree,
},
&Linter {
description: "Check that std::filesystem is not used directly",
name: "std_filesystem",