aboutsummaryrefslogtreecommitdiff
path: root/tests/qemu-iotests/297
blob: ee78a627359dd02e33f97fc8e26ae1d451453788 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python3
# group: meta
#
# 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 subprocess
import sys
from typing import List

import iotests
import linters


# 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:
        linters.run_linter(linter, ['--version'], suppress_output=True)
    except subprocess.CalledProcessError:
        iotests.case_notrun(f"'{linter}' not found")
        return False
    return True


def test_pylint(files: List[str]) -> None:
    print('=== pylint ===')
    sys.stdout.flush()

    if not check_linter('pylint'):
        return

    linters.run_linter('pylint', files)


def test_mypy(files: List[str]) -> None:
    print('=== mypy ===')
    sys.stdout.flush()

    if not check_linter('mypy'):
        return

    env = os.environ.copy()
    env['MYPYPATH'] = env['PYTHONPATH']

    linters.run_linter('mypy', files, env=env, suppress_output=True)


def main() -> None:
    files = linters.get_test_files()

    iotests.logger.debug('Files to be checked:')
    iotests.logger.debug(', '.join(sorted(files)))

    for test in (test_pylint, test_mypy):
        try:
            test(files)
        except subprocess.CalledProcessError as exc:
            # Linter failure will be caught by diffing the IO.
            if exc.output:
                print(exc.output)


iotests.script_main(main)