From 9957ef06dc428bb86df963edef14b4b915a2a271 Mon Sep 17 00:00:00 2001 From: "Matthieu Baerts (NGI0)" Date: Tue, 9 Sep 2025 22:55:37 +0200 Subject: [PATCH] tests: python: add ruff When looking at 'ynl', 'ruff' found out at least one interesting error: one variable was used but not defined. It looks then interesting to add it. Its usage is very similar to pylint. Some says it can also be used instead of pylint, but I'm not experienced enough to judge. Note that by default, ruff will only check for "interesting" errors. Signed-off-by: Matthieu Baerts (NGI0) --- tests/patch/ruff/info.json | 3 ++ tests/patch/ruff/ruff.sh | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 tests/patch/ruff/info.json create mode 100755 tests/patch/ruff/ruff.sh diff --git a/tests/patch/ruff/info.json b/tests/patch/ruff/info.json new file mode 100644 index 0000000..03f4d40 --- /dev/null +++ b/tests/patch/ruff/info.json @@ -0,0 +1,3 @@ +{ + "run": ["ruff.sh"] +} diff --git a/tests/patch/ruff/ruff.sh b/tests/patch/ruff/ruff.sh new file mode 100755 index 0000000..fa25fde --- /dev/null +++ b/tests/patch/ruff/ruff.sh @@ -0,0 +1,60 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +HEAD=$(git rev-parse HEAD) +rc=0 + +pr() { + echo " ====== $* ======" | tee -a /dev/stderr +} + +# If it doesn't touch .py files, don't bother. Ignore deleted. +if ! git show --diff-filter=AM --pretty="" --name-only "${HEAD}" | grep -q -E "\.py$" +then + echo "No python scripts touched, skip" >&"$DESC_FD" + exit 0 +fi + +ruff --version || exit 1 + +tmpfile_o=$(mktemp) +tmpfile_n=$(mktemp) + +echo "Redirect to $tmpfile_o and $tmpfile_n" + +echo "Tree base:" +git log -1 --pretty='%h ("%s")' HEAD~ +echo "Now at:" +git log -1 --pretty='%h ("%s")' HEAD + +pr "Checking before the patch" +git checkout -q HEAD~ + +# Also ignore created, as not present in the parent commit +for f in $(git show --diff-filter=M --pretty="" --name-only "${HEAD}" | grep -E "\.py$"); do + ruff check --output-format pylint "$f" | tee -a "$tmpfile_o" +done + +incumbent=$(wc -l < "$tmpfile_o") + +pr "Checking the tree with the patch" +git checkout -q "$HEAD" + +for f in $(git show --diff-filter=AM --pretty="" --name-only "${HEAD}" | grep -E "\.py$"); do + ruff check --output-format pylint "$f" | tee -a "$tmpfile_n" +done + +current=$(wc -l < "$tmpfile_n") + +echo "Errors before: $incumbent ; this patch: $current" >&"$DESC_FD" + +if [ "$current" -gt "$incumbent" ]; then + echo "New errors added" 1>&2 + diff -U 0 "$tmpfile_o" "$tmpfile_n" 1>&2 + + rc=1 +fi + +rm "$tmpfile_o" "$tmpfile_n" + +exit $rc