[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 11/22] iotests: split linters.py out from 297
From: |
John Snow |
Subject: |
[PULL 11/22] iotests: split linters.py out from 297 |
Date: |
Mon, 1 Nov 2021 13:29:55 -0400 |
Now, 297 is just the iotests-specific incantations and linters.py is as
minimal as I can think to make it. The only remaining element in here
that ought to be configuration and not code is the list of skip files,
but they're still numerous enough that repeating them for mypy and
pylint configurations both would be ... a hassle.
Signed-off-by: John Snow <jsnow@redhat.com>
Reviewed-by: Hanna Reitz <hreitz@redhat.com>
Message-id: 20211019144918.3159078-12-jsnow@redhat.com
Signed-off-by: John Snow <jsnow@redhat.com>
---
tests/qemu-iotests/297 | 72 +++++----------------------------
tests/qemu-iotests/linters.py | 76 +++++++++++++++++++++++++++++++++++
2 files changed, 87 insertions(+), 61 deletions(-)
create mode 100644 tests/qemu-iotests/linters.py
diff --git a/tests/qemu-iotests/297 b/tests/qemu-iotests/297
index b7d9d6077b3..ee78a627359 100755
--- a/tests/qemu-iotests/297
+++ b/tests/qemu-iotests/297
@@ -17,74 +17,24 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
-import re
import subprocess
import sys
-from typing import List, Mapping, Optional
+from typing import List
import iotests
+import linters
-# TODO: Empty this list!
-SKIP_FILES = (
- '030', '040', '041', '044', '045', '055', '056', '057', '065', '093',
- '096', '118', '124', '132', '136', '139', '147', '148', '149',
- '151', '152', '155', '163', '165', '194', '196', '202',
- '203', '205', '206', '207', '208', '210', '211', '212', '213', '216',
- '218', '219', '224', '228', '234', '235', '236', '237', '238',
- '240', '242', '245', '246', '248', '255', '256', '257', '258', '260',
- '262', '264', '266', '274', '277', '280', '281', '295', '296', '298',
- '299', '302', '303', '304', '307',
- 'nbd-fault-injector.py', 'qcow2.py', 'qcow2_format.py', 'qed.py'
-)
-
-
-def is_python_file(filename):
- if not os.path.isfile(filename):
- return False
-
- if filename.endswith('.py'):
- return True
-
- with open(filename, encoding='utf-8') as f:
- try:
- first_line = f.readline()
- return re.match('^#!.*python', first_line) is not None
- except UnicodeDecodeError: # Ignore binary files
- return False
-
-
-def get_test_files() -> List[str]:
- named_tests = [f'tests/{entry}' for entry in os.listdir('tests')]
- check_tests = set(os.listdir('.') + named_tests) - set(SKIP_FILES)
- return list(filter(is_python_file, check_tests))
-
-
-def run_linter(
- tool: str,
- args: List[str],
- env: Optional[Mapping[str, str]] = None,
- suppress_output: bool = False,
-) -> None:
- """
- Run a python-based linting tool.
-
- :param suppress_output: If True, suppress all stdout/stderr output.
- :raise CalledProcessError: If the linter process exits with failure.
- """
- subprocess.run(
- ('python3', '-m', tool, *args),
- env=env,
- check=True,
- stdout=subprocess.PIPE if suppress_output else None,
- stderr=subprocess.STDOUT if suppress_output else None,
- universal_newlines=True,
- )
+# Looking for something?
+#
+# List of files to exclude from linting: linters.py
+# mypy configuration: mypy.ini
+# pylint configuration: pylintrc
def check_linter(linter: str) -> bool:
try:
- run_linter(linter, ['--version'], suppress_output=True)
+ linters.run_linter(linter, ['--version'], suppress_output=True)
except subprocess.CalledProcessError:
iotests.case_notrun(f"'{linter}' not found")
return False
@@ -98,7 +48,7 @@ def test_pylint(files: List[str]) -> None:
if not check_linter('pylint'):
return
- run_linter('pylint', files)
+ linters.run_linter('pylint', files)
def test_mypy(files: List[str]) -> None:
@@ -111,11 +61,11 @@ def test_mypy(files: List[str]) -> None:
env = os.environ.copy()
env['MYPYPATH'] = env['PYTHONPATH']
- run_linter('mypy', files, env=env, suppress_output=True)
+ linters.run_linter('mypy', files, env=env, suppress_output=True)
def main() -> None:
- files = get_test_files()
+ files = linters.get_test_files()
iotests.logger.debug('Files to be checked:')
iotests.logger.debug(', '.join(sorted(files)))
diff --git a/tests/qemu-iotests/linters.py b/tests/qemu-iotests/linters.py
new file mode 100644
index 00000000000..c515c7afe36
--- /dev/null
+++ b/tests/qemu-iotests/linters.py
@@ -0,0 +1,76 @@
+# Copyright (C) 2020 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import os
+import re
+import subprocess
+from typing import List, Mapping, Optional
+
+
+# TODO: Empty this list!
+SKIP_FILES = (
+ '030', '040', '041', '044', '045', '055', '056', '057', '065', '093',
+ '096', '118', '124', '132', '136', '139', '147', '148', '149',
+ '151', '152', '155', '163', '165', '194', '196', '202',
+ '203', '205', '206', '207', '208', '210', '211', '212', '213', '216',
+ '218', '219', '224', '228', '234', '235', '236', '237', '238',
+ '240', '242', '245', '246', '248', '255', '256', '257', '258', '260',
+ '262', '264', '266', '274', '277', '280', '281', '295', '296', '298',
+ '299', '302', '303', '304', '307',
+ 'nbd-fault-injector.py', 'qcow2.py', 'qcow2_format.py', 'qed.py'
+)
+
+
+def is_python_file(filename):
+ if not os.path.isfile(filename):
+ return False
+
+ if filename.endswith('.py'):
+ return True
+
+ with open(filename, encoding='utf-8') as f:
+ try:
+ first_line = f.readline()
+ return re.match('^#!.*python', first_line) is not None
+ except UnicodeDecodeError: # Ignore binary files
+ return False
+
+
+def get_test_files() -> List[str]:
+ named_tests = [f'tests/{entry}' for entry in os.listdir('tests')]
+ check_tests = set(os.listdir('.') + named_tests) - set(SKIP_FILES)
+ return list(filter(is_python_file, check_tests))
+
+
+def run_linter(
+ tool: str,
+ args: List[str],
+ env: Optional[Mapping[str, str]] = None,
+ suppress_output: bool = False,
+) -> None:
+ """
+ Run a python-based linting tool.
+
+ :param suppress_output: If True, suppress all stdout/stderr output.
+ :raise CalledProcessError: If the linter process exits with failure.
+ """
+ subprocess.run(
+ ('python3', '-m', tool, *args),
+ env=env,
+ check=True,
+ stdout=subprocess.PIPE if suppress_output else None,
+ stderr=subprocess.STDOUT if suppress_output else None,
+ universal_newlines=True,
+ )
--
2.31.1
- [PULL 01/22] iotests/297: Move pylint config into pylintrc, (continued)
- [PULL 01/22] iotests/297: Move pylint config into pylintrc, John Snow, 2021/11/01
- [PULL 02/22] iotests/297: Split mypy configuration out into mypy.ini, John Snow, 2021/11/01
- [PULL 03/22] iotests/297: Add get_files() function, John Snow, 2021/11/01
- [PULL 04/22] iotests/297: Create main() function, John Snow, 2021/11/01
- [PULL 05/22] iotests/297: Don't rely on distro-specific linter binaries, John Snow, 2021/11/01
- [PULL 06/22] iotests/297: Split run_linters apart into run_pylint and run_mypy, John Snow, 2021/11/01
- [PULL 07/22] iotests/297: refactor run_[mypy|pylint] as generic execution shim, John Snow, 2021/11/01
- [PULL 08/22] iotests/297: Change run_linter() to raise an exception on failure, John Snow, 2021/11/01
- [PULL 09/22] iotests/297: update tool availability checks, John Snow, 2021/11/01
- [PULL 10/22] iotests/297: split test into sub-cases, John Snow, 2021/11/01
- [PULL 11/22] iotests: split linters.py out from 297,
John Snow <=
- [PULL 12/22] iotests/linters: Add entry point for linting via Python CI, John Snow, 2021/11/01
- [PULL 13/22] iotests/linters: Add workaround for mypy bug #9852, John Snow, 2021/11/01
- [PULL 14/22] python: Add iotest linters to test suite, John Snow, 2021/11/01
- [PULL 15/22] python/machine: remove has_quit argument, John Snow, 2021/11/01
- [PULL 16/22] python/machine: Handle QMP errors on close more meticulously, John Snow, 2021/11/01
- [PULL 17/22] python/aqmp: Remove scary message, John Snow, 2021/11/01
- [PULL 18/22] iotests: Accommodate async QMP Exception classes, John Snow, 2021/11/01
- [PULL 19/22] iotests: Conditionally silence certain AQMP errors, John Snow, 2021/11/01
- [PULL 20/22] iotests/300: avoid abnormal shutdown race condition, John Snow, 2021/11/01
- [PULL 22/22] python, iotests: replace qmp with aqmp, John Snow, 2021/11/01