test_formatters.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # This Source Code Form is subject to the terms of the Mozilla Public
  2. # License, v. 2.0. If a copy of the MPL was not distributed with this
  3. # file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. from __future__ import unicode_literals
  5. import json
  6. import sys
  7. from collections import defaultdict
  8. import pytest
  9. from mozlint import ResultContainer
  10. from mozlint import formatters
  11. @pytest.fixture
  12. def results(scope='module'):
  13. containers = (
  14. ResultContainer(
  15. linter='foo',
  16. path='a/b/c.txt',
  17. message="oh no foo",
  18. lineno=1,
  19. ),
  20. ResultContainer(
  21. linter='bar',
  22. path='d/e/f.txt',
  23. message="oh no bar",
  24. hint="try baz instead",
  25. level='warning',
  26. lineno=4,
  27. column=2,
  28. rule="bar-not-allowed",
  29. ),
  30. ResultContainer(
  31. linter='baz',
  32. path='a/b/c.txt',
  33. message="oh no baz",
  34. lineno=4,
  35. source="if baz:",
  36. ),
  37. )
  38. results = defaultdict(list)
  39. for c in containers:
  40. results[c.path].append(c)
  41. return results
  42. def test_stylish_formatter(results):
  43. expected = """
  44. a/b/c.txt
  45. 1 error oh no foo (foo)
  46. 4 error oh no baz (baz)
  47. d/e/f.txt
  48. 4:2 warning oh no bar bar-not-allowed (bar)
  49. \u2716 3 problems (2 errors, 1 warning)
  50. """.strip()
  51. fmt = formatters.get('stylish', disable_colors=True)
  52. assert expected == fmt(results)
  53. def test_treeherder_formatter(results):
  54. expected = """
  55. TEST-UNEXPECTED-ERROR | a/b/c.txt:1 | oh no foo (foo)
  56. TEST-UNEXPECTED-ERROR | a/b/c.txt:4 | oh no baz (baz)
  57. TEST-UNEXPECTED-WARNING | d/e/f.txt:4:2 | oh no bar (bar-not-allowed)
  58. """.strip()
  59. fmt = formatters.get('treeherder')
  60. assert expected == fmt(results)
  61. def test_json_formatter(results):
  62. fmt = formatters.get('json')
  63. formatted = json.loads(fmt(results))
  64. assert set(formatted.keys()) == set(results.keys())
  65. slots = ResultContainer.__slots__
  66. for errors in formatted.values():
  67. for err in errors:
  68. assert all(s in err for s in slots)
  69. if __name__ == '__main__':
  70. sys.exit(pytest.main(['--verbose', __file__]))