test_style.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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
  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. proc = subprocess.Popen(
  31. flake8_command,
  32. stdout=subprocess.PIPE,
  33. cwd=REPO_PATH,
  34. )
  35. print(proc.communicate())
  36. self.assertEqual(proc.returncode, 0)
  37. @unittest.skipIf(
  38. not (six.PY3 and sys.version_info.minor >=6),
  39. "Black is only available in python 3.6+")
  40. def test_code_with_black(self):
  41. """Enforce black compliance on the codebase.
  42. This test runs black on the code, and will fail if it returns a
  43. non-zero exit code.
  44. """
  45. black_command = [
  46. sys.executable, '-m', 'black', '-l', '79', '--check', REPO_PATH
  47. ]
  48. proc = subprocess.Popen(
  49. black_command,
  50. stdout=subprocess.PIPE,
  51. stderr=subprocess.PIPE,
  52. cwd=REPO_PATH,
  53. )
  54. stdout, stderr = proc.communicate()
  55. print('stdout: ')
  56. print(stdout.decode('utf-8'))
  57. print('stderr: ')
  58. print(stderr.decode('utf-8'))
  59. self.assertEqual(proc.returncode, 0)
  60. if __name__ == '__main__':
  61. unittest.main(verbosity=2)