123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- (c) 2017 - Copyright Red Hat Inc
- Authors:
- Pierre-Yves Chibon <pingou@pingoured.fr>
- Tests for flake8 compliance of the code
- """
- from __future__ import unicode_literals, absolute_import
- import os
- import subprocess
- import sys
- import unittest
- import six
- REPO_PATH = os.path.abspath(
- os.path.join(os.path.dirname(__file__), "..", "pagure")
- )
- TESTS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__)))
- class TestStyle(unittest.TestCase):
- """This test class contains tests pertaining to code style."""
- def test_code_with_flake8(self):
- """Enforce PEP-8 compliance on the codebase.
- This test runs flake8 on the code, and will fail if it returns a
- non-zero exit code.
- If flake8 is not installed, this test auto-skips.
- """
- try:
- import flake8
- except ImportError as e:
- raise unittest.SkipTest(
- "flake8 is not installed, skipping flake8 style check..."
- )
- # We ignore E712, which disallows non-identity comparisons with True and False
- # We ignore W503, which disallows line break before binary operator
- flake8_command = [
- sys.executable,
- "-m",
- "flake8",
- "--ignore=E712,W503,E203,E902,I201,I100",
- "--max-line-length=80",
- REPO_PATH,
- ]
- # check if we have an old flake8 or not
- import flake8
- flake8_v = flake8.__version__.split(".")
- for idx, val in enumerate(flake8_v):
- try:
- val = int(val)
- except ValueError:
- pass
- flake8_v[idx] = val
- old_flake = tuple(flake8_v) < (3, 0)
- if old_flake:
- raise unittest.SkipTest("Flake8 version too old to be useful")
- proc = subprocess.Popen(
- flake8_command, stdout=subprocess.PIPE, cwd=REPO_PATH
- )
- print(proc.communicate())
- self.assertEqual(proc.returncode, 0)
- @unittest.skipIf(
- not (six.PY3 and sys.version_info.minor >= 6),
- "Black is only available in python 3.6+",
- )
- def test_code_with_black(self):
- """Enforce black compliance on the codebase.
- This test runs black on the code, and will fail if it returns a
- non-zero exit code.
- If black is not installed, this test auto-skips.
- """
- try:
- import black
- except ImportError as e:
- raise unittest.SkipTest(
- "black is not installed, skipping black style check..."
- )
- black_command = [
- sys.executable,
- "-m",
- "black",
- "-l",
- "79",
- "--check",
- "--diff",
- "--exclude",
- '"/(.eggs|.git|.hg|.mypy_cache|.nox|.tox|.venv|_build|buck-out|build|dist)/"',
- "--target-version",
- "py39",
- REPO_PATH,
- TESTS_PATH,
- ]
- proc = subprocess.Popen(
- black_command,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- cwd=REPO_PATH,
- )
- stdout, stderr = proc.communicate()
- print("stdout: ")
- print(stdout.decode("utf-8"))
- print("stderr: ")
- print(stderr.decode("utf-8"))
- self.assertEqual(proc.returncode, 0)
- def test_code_with_isort(self):
- """Enforce isort compliance on the codebase.
- This test runs isort on the code, and will fail if it returns a
- non-zero exit code.
- If isort is not installed, this test auto-skips.
- """
- try:
- import isort
- except ImportError as e:
- raise unittest.SkipTest(
- "isort is not installed, skipping isort style check..."
- )
- # We ignore the hooks files that have a bunch of symlink
- isort_command = [
- sys.executable,
- "-m",
- "isort",
- "-v",
- "--profile",
- "black",
- "-s",
- os.path.join(REPO_PATH, "hooks/files"),
- "-l",
- "79",
- REPO_PATH,
- ]
- # check if we have an old isort or not
- import isort
- print(" ".join(isort_command))
- proc = subprocess.Popen(
- isort_command, stdout=subprocess.PIPE, cwd=REPO_PATH
- )
- stdout, stderr = proc.communicate()
- print("stdout: ")
- print(stdout.decode("utf-8")) if stdout else ""
- print("stderr: ")
- print(stderr.decode("utf-8")) if stderr else ""
- self.assertEqual(proc.returncode, 0)
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|