test_roller.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. import os
  5. import sys
  6. import pytest
  7. from mozlint import ResultContainer
  8. from mozlint.errors import LintersNotConfigured, LintException
  9. here = os.path.abspath(os.path.dirname(__file__))
  10. linters = ('string.lint', 'regex.lint', 'external.lint')
  11. def test_roll_no_linters_configured(lint, files):
  12. with pytest.raises(LintersNotConfigured):
  13. lint.roll(files)
  14. def test_roll_successful(lint, linters, files):
  15. lint.read(linters)
  16. result = lint.roll(files)
  17. assert len(result) == 1
  18. assert lint.return_code == 1
  19. path = result.keys()[0]
  20. assert os.path.basename(path) == 'foobar.js'
  21. errors = result[path]
  22. assert isinstance(errors, list)
  23. assert len(errors) == 6
  24. container = errors[0]
  25. assert isinstance(container, ResultContainer)
  26. assert container.rule == 'no-foobar'
  27. def test_roll_catch_exception(lint, lintdir, files):
  28. lint.read(os.path.join(lintdir, 'raises.lint'))
  29. # suppress printed traceback from test output
  30. old_stderr = sys.stderr
  31. sys.stderr = open(os.devnull, 'w')
  32. with pytest.raises(LintException):
  33. lint.roll(files)
  34. sys.stderr = old_stderr
  35. def test_roll_with_excluded_path(lint, linters, files):
  36. lint.lintargs.update({'exclude': ['**/foobar.js']})
  37. lint.read(linters)
  38. result = lint.roll(files)
  39. assert len(result) == 0
  40. assert lint.return_code == 0
  41. def test_roll_with_invalid_extension(lint, lintdir, filedir):
  42. lint.read(os.path.join(lintdir, 'external.lint'))
  43. result = lint.roll(os.path.join(filedir, 'foobar.py'))
  44. assert len(result) == 0
  45. assert lint.return_code == 0
  46. def test_roll_with_failure_code(lint, lintdir, files):
  47. lint.read(os.path.join(lintdir, 'badreturncode.lint'))
  48. assert lint.return_code is None
  49. result = lint.roll(files)
  50. assert len(result) == 0
  51. assert lint.return_code == 1
  52. if __name__ == '__main__':
  53. sys.exit(pytest.main(['--verbose', __file__]))