Skip to content

Commit bbbbdb0

Browse files
author
MarcoFalke
committed
ci: Add filesystem lint check
1 parent fada2f9 commit bbbbdb0

File tree

8 files changed

+120
-0
lines changed

8 files changed

+120
-0
lines changed

.cirrus.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ task:
8484
memory: 1G
8585
# For faster CI feedback, immediately schedule the linters
8686
<< : *CREDITS_TEMPLATE
87+
test_runner_cache:
88+
folder: "/lint_test_runner"
89+
fingerprint_script: echo $CIRRUS_TASK_NAME $(git rev-parse HEAD:test/lint/test_runner)
8790
python_cache:
8891
folder: "/python_build"
8992
fingerprint_script: cat .python-version /etc/os-release

ci/lint/04_install.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ export PATH="${PYTHON_PATH}/bin:${PATH}"
3333
command -v python3
3434
python3 --version
3535

36+
export LINT_RUNNER_PATH="/lint_test_runner"
37+
if [ ! -d "${LINT_RUNNER_PATH}" ]; then
38+
${CI_RETRY_EXE} apt-get install -y cargo
39+
(
40+
cd ./test/lint/test_runner || exit 1
41+
cargo build
42+
mkdir -p "${LINT_RUNNER_PATH}"
43+
mv target/debug/test_runner "${LINT_RUNNER_PATH}"
44+
)
45+
fi
46+
3647
${CI_RETRY_EXE} pip3 install \
3748
codespell==2.2.5 \
3849
flake8==6.1.0 \

ci/lint/06_script.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ test/lint/git-subtree-check.sh src/secp256k1
3030
test/lint/git-subtree-check.sh src/minisketch
3131
test/lint/git-subtree-check.sh src/leveldb
3232
test/lint/git-subtree-check.sh src/crc32c
33+
RUST_BACKTRACE=1 "${LINT_RUNNER_PATH}/test_runner"
3334
test/lint/check-doc.py
3435
test/lint/all-lint.py
3536

ci/lint/container-entrypoint.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export LC_ALL=C
1111
git config --global --add safe.directory /bitcoin
1212

1313
export PATH="/python_build/bin:${PATH}"
14+
export LINT_RUNNER_PATH="/lint_test_runner"
1415

1516
if [ -z "$1" ]; then
1617
LOCAL_BRANCH=1 bash -ic "./ci/lint/06_script.sh"

test/lint/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ docker run --rm -v $(pwd):/bitcoin -it bitcoin-linter
1515
After building the container once, you can simply run the last command any time you
1616
want to lint.
1717

18+
test runner
19+
===========
20+
21+
To run the checks in the test runner outside the docker, use:
22+
23+
```sh
24+
( cd ./test/lint/test_runner/ && cargo fmt && cargo clippy && cargo run )
25+
```
1826

1927
check-doc.py
2028
============

test/lint/test_runner/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/lint/test_runner/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
[package]
6+
name = "test_runner"
7+
version = "0.1.0"
8+
edition = "2021"
9+
10+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
11+
12+
[dependencies]

test/lint/test_runner/src/main.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)