Skip to content

Commit 09bbb20

Browse files
matttbekuba-moo
authored andcommitted
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) <[email protected]>
1 parent 8e974af commit 09bbb20

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

tests/patch/ruff/info.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"run": ["ruff.sh"]
3+
}

tests/patch/ruff/ruff.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
HEAD=$(git rev-parse HEAD)
5+
rc=0
6+
7+
pr() {
8+
echo " ====== $* ======" | tee -a /dev/stderr
9+
}
10+
11+
# If it doesn't touch .py files, don't bother. Ignore deleted.
12+
if ! git show --diff-filter=AM --pretty="" --name-only "${HEAD}" | grep -q -E "\.py$"
13+
then
14+
echo "No python scripts touched, skip" >&"$DESC_FD"
15+
exit 0
16+
fi
17+
18+
ruff --version || exit 1
19+
20+
tmpfile_o=$(mktemp)
21+
tmpfile_n=$(mktemp)
22+
23+
echo "Redirect to $tmpfile_o and $tmpfile_n"
24+
25+
echo "Tree base:"
26+
git log -1 --pretty='%h ("%s")' HEAD~
27+
echo "Now at:"
28+
git log -1 --pretty='%h ("%s")' HEAD
29+
30+
pr "Checking before the patch"
31+
git checkout -q HEAD~
32+
33+
# Also ignore created, as not present in the parent commit
34+
for f in $(git show --diff-filter=M --pretty="" --name-only "${HEAD}" | grep -E "\.py$"); do
35+
ruff check --output-format pylint "$f" | tee -a "$tmpfile_o"
36+
done
37+
38+
incumbent=$(wc -l < "$tmpfile_o")
39+
40+
pr "Checking the tree with the patch"
41+
git checkout -q "$HEAD"
42+
43+
for f in $(git show --diff-filter=AM --pretty="" --name-only "${HEAD}" | grep -E "\.py$"); do
44+
ruff check --output-format pylint "$f" | tee -a "$tmpfile_n"
45+
done
46+
47+
current=$(wc -l < "$tmpfile_n")
48+
49+
echo "Errors before: $incumbent ; this patch: $current" >&"$DESC_FD"
50+
51+
if [ "$current" -gt "$incumbent" ]; then
52+
echo "New errors added" 1>&2
53+
diff -U 0 "$tmpfile_o" "$tmpfile_n" 1>&2
54+
55+
rc=1
56+
fi
57+
58+
rm "$tmpfile_o" "$tmpfile_n"
59+
60+
exit $rc

0 commit comments

Comments
 (0)