[PULL 06/15] tests/functional: Add a generic test that checks the files with pylint

Thomas Huth posted 15 patches 3 weeks, 6 days ago
Maintainers: John Snow <jsnow@redhat.com>, Peter Maydell <peter.maydell@linaro.org>, Mauro Carvalho Chehab <mchehab+huawei@kernel.org>, Halil Pasic <pasic@linux.ibm.com>, Christian Borntraeger <borntraeger@linux.ibm.com>, Eric Farman <farman@linux.ibm.com>, Matthew Rosato <mjrosato@linux.ibm.com>, Thomas Huth <thuth@redhat.com>, Richard Henderson <richard.henderson@linaro.org>, Ilya Leoshkevich <iii@linux.ibm.com>, David Hildenbrand <david@kernel.org>, Farhan Ali <alifm@linux.ibm.com>, Paolo Bonzini <pbonzini@redhat.com>, Eric Auger <eric.auger@redhat.com>, "Alex Bennée" <alex.bennee@linaro.org>, Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp>, Dmitry Osipenko <dmitry.osipenko@collabora.com>, "Philippe Mathieu-Daudé" <philmd@linaro.org>, "Daniel P. Berrangé" <berrange@redhat.com>, Aurelien Jarno <aurelien@aurel32.net>, Nicholas Piggin <npiggin@gmail.com>, Harsh Prateek Bora <harshpb@linux.ibm.com>
[PULL 06/15] tests/functional: Add a generic test that checks the files with pylint
Posted by Thomas Huth 3 weeks, 6 days ago
From: Thomas Huth <thuth@redhat.com>

To avoid that new pylint-related warnings get committed, let's check
the files with pylint during each run (similar to what we are doing
for the iotests already).

Message-Id: <20251119082636.43286-16-thuth@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 MAINTAINERS                              |  1 +
 tests/functional/generic/meson.build     |  1 +
 tests/functional/generic/test_linters.py | 41 ++++++++++++++++++++++++
 3 files changed, 43 insertions(+)
 create mode 100755 tests/functional/generic/test_linters.py

diff --git a/MAINTAINERS b/MAINTAINERS
index a6168cb46f7..6dfd9b27c20 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -4461,6 +4461,7 @@ F: docs/devel/testing/functional.rst
 F: scripts/clean_functional_cache.py
 F: tests/functional/meson.build
 F: tests/functional/generic/meson.build
+F: tests/functional/generic/test_linters.py
 F: tests/functional/pylintrc
 F: tests/functional/qemu_test/
 
diff --git a/tests/functional/generic/meson.build b/tests/functional/generic/meson.build
index 013cc96fbf8..09763c5d229 100644
--- a/tests/functional/generic/meson.build
+++ b/tests/functional/generic/meson.build
@@ -3,6 +3,7 @@
 tests_generic_system = [
   'empty_cpu_model',
   'info_usernet',
+  'linters',
   'version',
   'vnc',
 ]
diff --git a/tests/functional/generic/test_linters.py b/tests/functional/generic/test_linters.py
new file mode 100755
index 00000000000..b5b90fcf7a3
--- /dev/null
+++ b/tests/functional/generic/test_linters.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+#
+'''Python linter tests'''
+
+import os
+
+from pathlib import Path
+from qemu_test import QemuBaseTest, skipIfMissingImports
+
+
+class LinterTest(QemuBaseTest):
+    '''
+    Run python linters on the test *.py files
+    '''
+
+    @skipIfMissingImports("pylint")
+    def test_pylint(self):
+        '''Check source files with pylint'''
+        from pylint.lint import Run as pylint_run
+        from pylint.reporters.collecting_reporter import CollectingReporter
+        srcdir = os.path.join(Path(__file__).parent.parent, self.arch)
+        rcfile = os.path.join(Path(__file__).parent.parent, "pylintrc")
+        self.log.info('Checking files in %s with pylint', srcdir)
+        reporter = CollectingReporter()
+        pylint_run(["--rcfile", rcfile, srcdir], reporter=reporter, exit=False)
+        if reporter.messages:
+            fmt = '"{path}:{line}: {msg_id}: {msg} ({symbol})"'
+            for msg in reporter.messages:
+                if msg.category == "error":
+                    self.log.error(msg.format(fmt))
+                elif msg.category == "warning":
+                    self.log.warning(msg.format(fmt))
+                else:
+                    self.log.info(msg.format(fmt))
+            self.fail("Pylint failed, see base.log for details.")
+
+
+if __name__ == '__main__':
+    QemuBaseTest.main()
-- 
2.52.0