|
| 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::env; |
| 6 | +use std::path::PathBuf; |
| 7 | +use std::process::Command; |
| 8 | +use std::process::ExitCode; |
| 9 | + |
| 10 | +use String as LintError; |
| 11 | + |
| 12 | +/// Return the git command |
| 13 | +fn git() -> Command { |
| 14 | + Command::new("git") |
| 15 | +} |
| 16 | + |
| 17 | +/// Return stdout |
| 18 | +fn check_output(cmd: &mut std::process::Command) -> Result<String, LintError> { |
| 19 | + let out = cmd.output().expect("command error"); |
| 20 | + if !out.status.success() { |
| 21 | + return Err(String::from_utf8_lossy(&out.stderr).to_string()); |
| 22 | + } |
| 23 | + Ok(String::from_utf8(out.stdout) |
| 24 | + .map_err(|e| format!("{e}"))? |
| 25 | + .trim() |
| 26 | + .to_string()) |
| 27 | +} |
| 28 | + |
| 29 | +/// Return the git root as utf8, or panic |
| 30 | +fn get_git_root() -> String { |
| 31 | + check_output(git().args(["rev-parse", "--show-toplevel"])).unwrap() |
| 32 | +} |
| 33 | + |
| 34 | +fn lint_std_filesystem() -> Result<(), LintError> { |
| 35 | + let found = git() |
| 36 | + .args([ |
| 37 | + "grep", |
| 38 | + "std::filesystem", |
| 39 | + "--", |
| 40 | + "./src/", |
| 41 | + ":(exclude)src/util/fs.h", |
| 42 | + ]) |
| 43 | + .status() |
| 44 | + .expect("command error") |
| 45 | + .success(); |
| 46 | + if found { |
| 47 | + Err(r#" |
| 48 | +^^^ |
| 49 | +Direct use of std::filesystem may be dangerous and buggy. Please include <util/fs.h> and use the |
| 50 | +fs:: namespace, which has unsafe filesystem functions marked as deleted. |
| 51 | + "# |
| 52 | + .to_string()) |
| 53 | + } else { |
| 54 | + Ok(()) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn main() -> ExitCode { |
| 59 | + let test_list = [("std::filesystem check", lint_std_filesystem)]; |
| 60 | + |
| 61 | + let git_root = PathBuf::from(get_git_root()); |
| 62 | + |
| 63 | + let mut test_failed = false; |
| 64 | + for (lint_name, lint_fn) in test_list { |
| 65 | + // chdir to root before each lint test |
| 66 | + env::set_current_dir(&git_root).unwrap(); |
| 67 | + if let Err(err) = lint_fn() { |
| 68 | + println!("{err}\n^---- Failure generated from {lint_name}!"); |
| 69 | + test_failed = true; |
| 70 | + } |
| 71 | + } |
| 72 | + if test_failed { |
| 73 | + ExitCode::FAILURE |
| 74 | + } else { |
| 75 | + ExitCode::SUCCESS |
| 76 | + } |
| 77 | +} |
0 commit comments