2016-03-16 21:55:21 +03:00
|
|
|
# This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
# License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2017-08-29 21:50:33 +03:00
|
|
|
import mozunit
|
2016-09-09 23:20:09 +03:00
|
|
|
import pytest
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
from mozlint import ResultContainer
|
2016-03-16 21:55:21 +03:00
|
|
|
from mozlint.errors import LintersNotConfigured, LintException
|
|
|
|
|
|
|
|
|
|
|
|
here = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
|
|
|
2017-06-02 16:49:26 +03:00
|
|
|
linters = ('string.yml', 'regex.yml', 'external.yml')
|
2016-03-16 21:55:21 +03:00
|
|
|
|
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
def test_roll_no_linters_configured(lint, files):
|
|
|
|
with pytest.raises(LintersNotConfigured):
|
|
|
|
lint.roll(files)
|
2016-03-16 21:55:21 +03:00
|
|
|
|
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
def test_roll_successful(lint, linters, files):
|
|
|
|
lint.read(linters)
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
result = lint.roll(files)
|
|
|
|
assert len(result) == 1
|
2016-11-14 19:56:46 +03:00
|
|
|
assert lint.failed == []
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
path = result.keys()[0]
|
|
|
|
assert os.path.basename(path) == 'foobar.js'
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
errors = result[path]
|
|
|
|
assert isinstance(errors, list)
|
|
|
|
assert len(errors) == 6
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
container = errors[0]
|
|
|
|
assert isinstance(container, ResultContainer)
|
|
|
|
assert container.rule == 'no-foobar'
|
2016-03-16 21:55:21 +03:00
|
|
|
|
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
def test_roll_catch_exception(lint, lintdir, files):
|
2017-06-02 16:49:26 +03:00
|
|
|
lint.read(os.path.join(lintdir, 'raises.yml'))
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
# suppress printed traceback from test output
|
|
|
|
old_stderr = sys.stderr
|
|
|
|
sys.stderr = open(os.devnull, 'w')
|
|
|
|
with pytest.raises(LintException):
|
|
|
|
lint.roll(files)
|
|
|
|
sys.stderr = old_stderr
|
2016-03-16 21:55:21 +03:00
|
|
|
|
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
def test_roll_with_excluded_path(lint, linters, files):
|
|
|
|
lint.lintargs.update({'exclude': ['**/foobar.js']})
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
lint.read(linters)
|
|
|
|
result = lint.roll(files)
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
assert len(result) == 0
|
2016-11-14 19:56:46 +03:00
|
|
|
assert lint.failed == []
|
2016-03-16 21:55:21 +03:00
|
|
|
|
2016-09-09 23:20:09 +03:00
|
|
|
|
|
|
|
def test_roll_with_invalid_extension(lint, lintdir, filedir):
|
2017-06-02 16:49:26 +03:00
|
|
|
lint.read(os.path.join(lintdir, 'external.yml'))
|
2016-09-09 23:20:09 +03:00
|
|
|
result = lint.roll(os.path.join(filedir, 'foobar.py'))
|
|
|
|
assert len(result) == 0
|
2016-11-14 19:56:46 +03:00
|
|
|
assert lint.failed == []
|
2016-10-13 23:23:13 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_roll_with_failure_code(lint, lintdir, files):
|
2017-06-02 16:49:26 +03:00
|
|
|
lint.read(os.path.join(lintdir, 'badreturncode.yml'))
|
2016-10-13 23:23:13 +03:00
|
|
|
|
2016-11-14 19:56:46 +03:00
|
|
|
assert lint.failed is None
|
2016-10-13 23:23:13 +03:00
|
|
|
result = lint.roll(files)
|
|
|
|
assert len(result) == 0
|
2016-11-14 19:56:46 +03:00
|
|
|
assert lint.failed == ['BadReturnCodeLinter']
|
2016-08-09 23:24:04 +03:00
|
|
|
|
2016-03-16 21:55:21 +03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-08-29 21:50:33 +03:00
|
|
|
mozunit.main()
|