test_style.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. class TestStyle(unittest.TestCase):
  18. """This test class contains tests pertaining to code style."""
  19. def test_code_with_flake8(self):
  20. """Enforce PEP-8 compliance on the codebase.
  21. This test runs flake8 on the code, and will fail if it returns a
  22. non-zero exit code.
  23. """
  24. # We ignore E712, which disallows non-identity comparisons with True and False
  25. # We ignore W503, which disallows line break before binary operator
  26. flake8_command = [
  27. sys.executable, '-m', 'flake8', '--ignore=E712,W503,E203',
  28. REPO_PATH
  29. ]
  30. # check if we have an old flake8 or not
  31. import flake8
  32. flake8_v = flake8.__version__.split('.')
  33. for idx, val in enumerate(flake8_v):
  34. try:
  35. val = int(val)
  36. except ValueError:
  37. pass
  38. flake8_v[idx] = val
  39. old_flake = tuple(flake8_v) < (3, 0)
  40. if old_flake:
  41. raise unittest.SkipTest("Flake8 version too old to be useful")
  42. proc = subprocess.Popen(
  43. flake8_command,
  44. stdout=subprocess.PIPE,
  45. cwd=REPO_PATH,
  46. )
  47. print(proc.communicate())
  48. self.assertEqual(proc.returncode, 0)
  49. @unittest.skipIf(
  50. not (six.PY3 and sys.version_info.minor >=6),
  51. "Black is only available in python 3.6+")
  52. def test_code_with_black(self):
  53. """Enforce black compliance on the codebase.
  54. This test runs black on the code, and will fail if it returns a
  55. non-zero exit code.
  56. """
  57. black_command = [
  58. sys.executable, '-m', 'black', '-l', '79', '--check', REPO_PATH
  59. ]
  60. proc = subprocess.Popen(
  61. black_command,
  62. stdout=subprocess.PIPE,
  63. stderr=subprocess.PIPE,
  64. cwd=REPO_PATH,
  65. )
  66. stdout, stderr = proc.communicate()
  67. print('stdout: ')
  68. print(stdout.decode('utf-8'))
  69. print('stderr: ')
  70. print(stderr.decode('utf-8'))
  71. self.assertEqual(proc.returncode, 0)
  72. if __name__ == '__main__':
  73. unittest.main(verbosity=2)