diff options
author | fanquake <fanquake@gmail.com> | 2023-11-13 13:46:57 +0000 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2023-11-13 14:10:54 +0000 |
commit | 63423480723de8f4da67e9f4a715cca15498a4ca (patch) | |
tree | 9afc80788c7c0618d11b9a2cd68d33b3c52bd41e /test | |
parent | 29c2c903621f7daae26113dd2902c016b56929d4 (diff) | |
parent | bbbbdb0cd57d75a06357d2811363d30a498f4499 (diff) |
Merge bitcoin/bitcoin#28076: util: Replace std::filesystem with util/fs.h
bbbbdb0cd57d75a06357d2811363d30a498f4499 ci: Add filesystem lint check (MarcoFalke)
fada2f91108a56cc5c447bd6b6fac411e4d5cdca refactor: Replace <filesystem> with <util/fs.h> (MarcoFalke)
Pull request description:
Using `std::filesystem` is problematic:
* There is a `fs` namespace wrapper for it. So having two ways to achieve the same is confusing.
* Not using the `fs` wrapper is dangerous and buggy, because it disables known bugs by deleting problematic functions.
Fix all issues by removing use of it and adding a linter to avoid using it again in the future.
ACKs for top commit:
TheCharlatan:
ACK bbbbdb0cd57d75a06357d2811363d30a498f4499
fanquake:
ACK bbbbdb0cd57d75a06357d2811363d30a498f4499 🦀
Tree-SHA512: 0e2d49742b08eb2635e6fce41485277cb9c40fe20b81017c391d3472a43787db1278a236825714ca1e41c9d2f59913865cfb0c649e3c8ab1fb598c849f80c660
Diffstat (limited to 'test')
-rw-r--r-- | test/lint/README.md | 8 | ||||
-rw-r--r-- | test/lint/test_runner/Cargo.lock | 7 | ||||
-rw-r--r-- | test/lint/test_runner/Cargo.toml | 12 | ||||
-rw-r--r-- | test/lint/test_runner/src/main.rs | 77 |
4 files changed, 104 insertions, 0 deletions
diff --git a/test/lint/README.md b/test/lint/README.md index c0889b59af..484008298b 100644 --- a/test/lint/README.md +++ b/test/lint/README.md @@ -13,6 +13,14 @@ DOCKER_BUILDKIT=1 docker build -t bitcoin-linter --file "./ci/lint_imagefile" ./ Building the container can be done every time, because it is fast when the result is cached and it prevents issues when the image changes. +test runner +=========== + +To run the checks in the test runner outside the docker, use: + +```sh +( cd ./test/lint/test_runner/ && cargo fmt && cargo clippy && cargo run ) +``` check-doc.py ============ diff --git a/test/lint/test_runner/Cargo.lock b/test/lint/test_runner/Cargo.lock new file mode 100644 index 0000000000..ca83aa9331 --- /dev/null +++ b/test/lint/test_runner/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "test_runner" +version = "0.1.0" diff --git a/test/lint/test_runner/Cargo.toml b/test/lint/test_runner/Cargo.toml new file mode 100644 index 0000000000..053ce43d6c --- /dev/null +++ b/test/lint/test_runner/Cargo.toml @@ -0,0 +1,12 @@ +# Copyright (c) The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or https://opensource.org/license/mit/. + +[package] +name = "test_runner" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/test/lint/test_runner/src/main.rs b/test/lint/test_runner/src/main.rs new file mode 100644 index 0000000000..b7ec9ee3b2 --- /dev/null +++ b/test/lint/test_runner/src/main.rs @@ -0,0 +1,77 @@ +// Copyright (c) The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or https://opensource.org/license/mit/. + +use std::env; +use std::path::PathBuf; +use std::process::Command; +use std::process::ExitCode; + +use String as LintError; + +/// Return the git command +fn git() -> Command { + Command::new("git") +} + +/// Return stdout +fn check_output(cmd: &mut std::process::Command) -> Result<String, LintError> { + let out = cmd.output().expect("command error"); + if !out.status.success() { + return Err(String::from_utf8_lossy(&out.stderr).to_string()); + } + Ok(String::from_utf8(out.stdout) + .map_err(|e| format!("{e}"))? + .trim() + .to_string()) +} + +/// Return the git root as utf8, or panic +fn get_git_root() -> String { + check_output(git().args(["rev-parse", "--show-toplevel"])).unwrap() +} + +fn lint_std_filesystem() -> Result<(), LintError> { + let found = git() + .args([ + "grep", + "std::filesystem", + "--", + "./src/", + ":(exclude)src/util/fs.h", + ]) + .status() + .expect("command error") + .success(); + if found { + Err(r#" +^^^ +Direct use of std::filesystem may be dangerous and buggy. Please include <util/fs.h> and use the +fs:: namespace, which has unsafe filesystem functions marked as deleted. + "# + .to_string()) + } else { + Ok(()) + } +} + +fn main() -> ExitCode { + let test_list = [("std::filesystem check", lint_std_filesystem)]; + + let git_root = PathBuf::from(get_git_root()); + + let mut test_failed = false; + for (lint_name, lint_fn) in test_list { + // chdir to root before each lint test + env::set_current_dir(&git_root).unwrap(); + if let Err(err) = lint_fn() { + println!("{err}\n^---- Failure generated from {lint_name}!"); + test_failed = true; + } + } + if test_failed { + ExitCode::FAILURE + } else { + ExitCode::SUCCESS + } +} |