Blame tests/test_style.py

Pierre-Yves Chibon 393f31
#!/usr/bin/env python
Pierre-Yves Chibon 393f31
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 393f31
Pierre-Yves Chibon 393f31
"""
Pierre-Yves Chibon 393f31
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon 393f31
Pierre-Yves Chibon 393f31
 Authors:
Pierre-Yves Chibon 393f31
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 393f31
Pierre-Yves Chibon 393f31
Tests for flake8 compliance of the code
Pierre-Yves Chibon 393f31
Pierre-Yves Chibon 393f31
"""
Pierre-Yves Chibon 393f31
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon bd1fbf
import os
Pierre-Yves Chibon bd1fbf
import subprocess
Shengjing Zhu 77a68b
import sys
Pierre-Yves Chibon bd1fbf
import unittest
Pierre-Yves Chibon bd1fbf
Pierre-Yves Chibon d5959a
import six
Pierre-Yves Chibon bd1fbf
Pierre-Yves Chibon bd1fbf
REPO_PATH = os.path.abspath(
Pierre-Yves Chibon 8f8cbb
    os.path.join(os.path.dirname(__file__), "..", "pagure")
Pierre-Yves Chibon 8f8cbb
)
Pierre-Yves Chibon 8f8cbb
TESTS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__)))
Pierre-Yves Chibon bd1fbf
Pierre-Yves Chibon bd1fbf
Pierre-Yves Chibon bd1fbf
class TestStyle(unittest.TestCase):
Pierre-Yves Chibon bd1fbf
    """This test class contains tests pertaining to code style."""
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon bd1fbf
    def test_code_with_flake8(self):
Pierre-Yves Chibon bd1fbf
        """Enforce PEP-8 compliance on the codebase.
Pierre-Yves Chibon bd1fbf
Pierre-Yves Chibon 140059
        This test runs flake8 on the code, and will fail if it returns a
Pierre-Yves Chibon 140059
        non-zero exit code.
Pierre-Yves Chibon bd1fbf
        """
Pierre-Yves Chibon bd1fbf
        # We ignore E712, which disallows non-identity comparisons with True and False
Pierre-Yves Chibon d5959a
        # We ignore W503, which disallows line break before binary operator
Pierre-Yves Chibon d28de1
        flake8_command = [
Pierre-Yves Chibon 73d120
            sys.executable,
Pierre-Yves Chibon 73d120
            "-m",
Pierre-Yves Chibon 73d120
            "flake8",
Pierre-Yves Chibon 73d120
            "--ignore=E712,W503,E203",
Pierre-Yves Chibon 73d120
            REPO_PATH,
Pierre-Yves Chibon 140059
        ]
Pierre-Yves Chibon 671c79
Pierre-Yves Chibon 671c79
        # check if we have an old flake8 or not
Pierre-Yves Chibon 671c79
        import flake8
Pierre-Yves Chibon 73d120
Pierre-Yves Chibon 73d120
        flake8_v = flake8.__version__.split(".")
Pierre-Yves Chibon 671c79
        for idx, val in enumerate(flake8_v):
Pierre-Yves Chibon 671c79
            try:
Pierre-Yves Chibon 671c79
                val = int(val)
Pierre-Yves Chibon 671c79
            except ValueError:
Pierre-Yves Chibon 671c79
                pass
Pierre-Yves Chibon 671c79
            flake8_v[idx] = val
Pierre-Yves Chibon 671c79
        old_flake = tuple(flake8_v) < (3, 0)
Pierre-Yves Chibon 671c79
Pierre-Yves Chibon 671c79
        if old_flake:
Pierre-Yves Chibon 671c79
            raise unittest.SkipTest("Flake8 version too old to be useful")
Pierre-Yves Chibon 671c79
Pierre-Yves Chibon 140059
        proc = subprocess.Popen(
Pierre-Yves Chibon 73d120
            flake8_command, stdout=subprocess.PIPE, cwd=REPO_PATH
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon b18561
        print(proc.communicate())
Pierre-Yves Chibon bd1fbf
Pierre-Yves Chibon b18561
        self.assertEqual(proc.returncode, 0)
Pierre-Yves Chibon bd1fbf
Pierre-Yves Chibon d5959a
    @unittest.skipIf(
Pierre-Yves Chibon 73d120
        not (six.PY3 and sys.version_info.minor >= 6),
Pierre-Yves Chibon 73d120
        "Black is only available in python 3.6+",
Pierre-Yves Chibon 73d120
    )
Pierre-Yves Chibon d5959a
    def test_code_with_black(self):
Pierre-Yves Chibon d5959a
        """Enforce black compliance on the codebase.
Pierre-Yves Chibon d5959a
Pierre-Yves Chibon 140059
        This test runs black on the code, and will fail if it returns a
Pierre-Yves Chibon 140059
        non-zero exit code.
Pierre-Yves Chibon d5959a
        """
Pierre-Yves Chibon 140059
        black_command = [
Pierre-Yves Chibon 8f8cbb
            sys.executable,
Pierre-Yves Chibon 8f8cbb
            "-m",
Pierre-Yves Chibon 8f8cbb
            "black",
Pierre-Yves Chibon 8f8cbb
            "-l",
Pierre-Yves Chibon 8f8cbb
            "79",
Pierre-Yves Chibon 8f8cbb
            "--check",
Pierre-Yves Chibon 8f8cbb
            REPO_PATH,
Pierre-Yves Chibon 8f8cbb
            TESTS_PATH,
Pierre-Yves Chibon 140059
        ]
Pierre-Yves Chibon d5959a
        proc = subprocess.Popen(
Pierre-Yves Chibon 140059
            black_command,
Pierre-Yves Chibon 140059
            stdout=subprocess.PIPE,
Pierre-Yves Chibon 140059
            stderr=subprocess.PIPE,
Pierre-Yves Chibon 140059
            cwd=REPO_PATH,
Pierre-Yves Chibon 140059
        )
Pierre-Yves Chibon d5959a
        stdout, stderr = proc.communicate()
Pierre-Yves Chibon 73d120
        print("stdout: ")
Pierre-Yves Chibon 73d120
        print(stdout.decode("utf-8"))
Pierre-Yves Chibon 73d120
        print("stderr: ")
Pierre-Yves Chibon 73d120
        print(stderr.decode("utf-8"))
Pierre-Yves Chibon d5959a
Pierre-Yves Chibon d5959a
        self.assertEqual(proc.returncode, 0)
Pierre-Yves Chibon d5959a
Pierre-Yves Chibon bd1fbf
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon bd1fbf
    unittest.main(verbosity=2)