Skip to content

Commit

Permalink
ci: Add filesystem lint check
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoFalke committed Sep 14, 2023
1 parent fada2f9 commit bbbbdb0
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ task:
memory: 1G
# For faster CI feedback, immediately schedule the linters
<< : *CREDITS_TEMPLATE
test_runner_cache:
folder: "/lint_test_runner"
fingerprint_script: echo $CIRRUS_TASK_NAME $(git rev-parse HEAD:test/lint/test_runner)
python_cache:
folder: "/python_build"
fingerprint_script: cat .python-version /etc/os-release
Expand Down
11 changes: 11 additions & 0 deletions ci/lint/04_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export PATH="${PYTHON_PATH}/bin:${PATH}"
command -v python3
python3 --version

export LINT_RUNNER_PATH="/lint_test_runner"
if [ ! -d "${LINT_RUNNER_PATH}" ]; then
${CI_RETRY_EXE} apt-get install -y cargo
(
cd ./test/lint/test_runner || exit 1
cargo build
mkdir -p "${LINT_RUNNER_PATH}"
mv target/debug/test_runner "${LINT_RUNNER_PATH}"
)
fi

${CI_RETRY_EXE} pip3 install \
codespell==2.2.5 \
flake8==6.1.0 \
Expand Down
1 change: 1 addition & 0 deletions ci/lint/06_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ test/lint/git-subtree-check.sh src/secp256k1
test/lint/git-subtree-check.sh src/minisketch
test/lint/git-subtree-check.sh src/leveldb
test/lint/git-subtree-check.sh src/crc32c
RUST_BACKTRACE=1 "${LINT_RUNNER_PATH}/test_runner"
test/lint/check-doc.py
test/lint/all-lint.py

Expand Down
1 change: 1 addition & 0 deletions ci/lint/container-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export LC_ALL=C
git config --global --add safe.directory /bitcoin

export PATH="/python_build/bin:${PATH}"
export LINT_RUNNER_PATH="/lint_test_runner"

if [ -z "$1" ]; then
LOCAL_BRANCH=1 bash -ic "./ci/lint/06_script.sh"
Expand Down
8 changes: 8 additions & 0 deletions test/lint/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ docker run --rm -v $(pwd):/bitcoin -it bitcoin-linter
After building the container once, you can simply run the last command any time you
want to lint.

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
============
Expand Down
7 changes: 7 additions & 0 deletions test/lint/test_runner/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions test/lint/test_runner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
77 changes: 77 additions & 0 deletions test/lint/test_runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit bbbbdb0

Please sign in to comment.