test_style.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, '-m', 'flake8', '--ignore=E712,W503,E203',
  30. REPO_PATH
  31. ]
  32. # check if we have an old flake8 or not
  33. import flake8
  34. flake8_v = flake8.__version__.split('.')
  35. for idx, val in enumerate(flake8_v):
  36. try:
  37. val = int(val)
  38. except ValueError:
  39. pass
  40. flake8_v[idx] = val
  41. old_flake = tuple(flake8_v) < (3, 0)
  42. if old_flake:
  43. raise unittest.SkipTest("Flake8 version too old to be useful")
  44. proc = subprocess.Popen(
  45. flake8_command,
  46. stdout=subprocess.PIPE,
  47. cwd=REPO_PATH,
  48. )
  49. print(proc.communicate())
  50. self.assertEqual(proc.returncode, 0)
  51. @unittest.skipIf(
  52. not (six.PY3 and sys.version_info.minor >=6),
  53. "Black is only available in python 3.6+")
  54. def test_code_with_black(self):
  55. """Enforce black compliance on the codebase.
  56. This test runs black on the code, and will fail if it returns a
  57. non-zero exit code.
  58. """
  59. black_command = [
  60. sys.executable,
  61. "-m",
  62. "black",
  63. "-l",
  64. "79",
  65. "--check",
  66. REPO_PATH,
  67. TESTS_PATH,
  68. ]
  69. proc = subprocess.Popen(
  70. black_command,
  71. stdout=subprocess.PIPE,
  72. stderr=subprocess.PIPE,
  73. cwd=REPO_PATH,
  74. )
  75. stdout, stderr = proc.communicate()
  76. print('stdout: ')
  77. print(stdout.decode('utf-8'))
  78. print('stderr: ')
  79. print(stderr.decode('utf-8'))
  80. self.assertEqual(proc.returncode, 0)
  81. if __name__ == '__main__':
  82. unittest.main(verbosity=2)