test_style.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. (c) 2017 - Copyright Red Hat Inc
  5. Authors:
  6. Pierre-Yves Chibon <pingou@pingoured.fr>
  7. Tests for flake8 compliance of the code
  8. """
  9. from __future__ import unicode_literals, absolute_import
  10. import os
  11. import subprocess
  12. import sys
  13. import unittest
  14. import six
  15. REPO_PATH = os.path.abspath(
  16. os.path.join(os.path.dirname(__file__), "..", "pagure")
  17. )
  18. TESTS_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__)))
  19. class TestStyle(unittest.TestCase):
  20. """This test class contains tests pertaining to code style."""
  21. def test_code_with_flake8(self):
  22. """Enforce PEP-8 compliance on the codebase.
  23. This test runs flake8 on the code, and will fail if it returns a
  24. non-zero exit code.
  25. """
  26. # We ignore E712, which disallows non-identity comparisons with True and False
  27. # We ignore W503, which disallows line break before binary operator
  28. flake8_command = [
  29. sys.executable,
  30. "-m",
  31. "flake8",
  32. "--ignore=E712,W503,E203,E902",
  33. "--max-line-length=80",
  34. REPO_PATH,
  35. ]
  36. # check if we have an old flake8 or not
  37. import flake8
  38. flake8_v = flake8.__version__.split(".")
  39. for idx, val in enumerate(flake8_v):
  40. try:
  41. val = int(val)
  42. except ValueError:
  43. pass
  44. flake8_v[idx] = val
  45. old_flake = tuple(flake8_v) < (3, 0)
  46. if old_flake:
  47. raise unittest.SkipTest("Flake8 version too old to be useful")
  48. proc = subprocess.Popen(
  49. flake8_command, stdout=subprocess.PIPE, cwd=REPO_PATH
  50. )
  51. print(proc.communicate())
  52. self.assertEqual(proc.returncode, 0)
  53. @unittest.skipIf(
  54. not (six.PY3 and sys.version_info.minor >= 6),
  55. "Black is only available in python 3.6+",
  56. )
  57. def test_code_with_black(self):
  58. """Enforce black compliance on the codebase.
  59. This test runs black on the code, and will fail if it returns a
  60. non-zero exit code.
  61. """
  62. black_command = [
  63. sys.executable,
  64. "-m",
  65. "black",
  66. "-l",
  67. "79",
  68. "--check",
  69. "--diff",
  70. "--exclude",
  71. '"/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist)/"',
  72. REPO_PATH,
  73. TESTS_PATH,
  74. ]
  75. proc = subprocess.Popen(
  76. black_command,
  77. stdout=subprocess.PIPE,
  78. stderr=subprocess.PIPE,
  79. cwd=REPO_PATH,
  80. )
  81. stdout, stderr = proc.communicate()
  82. print("stdout: ")
  83. print(stdout.decode("utf-8"))
  84. print("stderr: ")
  85. print(stderr.decode("utf-8"))
  86. self.assertEqual(proc.returncode, 0)
  87. if __name__ == "__main__":
  88. unittest.main(verbosity=2)