Files
firefox/python/mozlint/test/test_roller.py
T
Andrew Halberstadt b0ccc9546d Bug 1289805 - Resolve vcs arguments directly in LintRoller class, r=smacleod
Previously, vcs related stuff was resolved in the cli.py module. But it's possible
for consumers to bypass the cli and instantiate a LintRoller directly. In fact this
is what the mozlint tests do.

Now that we always try to find the vcs root, calling into vcs is no longer optional.
This patch moves the VCSFiles class to a new vcs.py module and makes LintRoller
responsible for instantiating it instead of cli.py.

MozReview-Commit-ID: 5yA3gDZ1UGM
2016-08-10 10:21:43 -04:00

76 lines
2.2 KiB
Python

# 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
from unittest import TestCase
from mozunit import main
from mozlint import LintRoller, ResultContainer
from mozlint.errors import LintersNotConfigured, LintException
here = os.path.abspath(os.path.dirname(__file__))
class TestLintRoller(TestCase):
def __init__(self, *args, **kwargs):
TestCase.__init__(self, *args, **kwargs)
filedir = os.path.join(here, 'files')
self.files = [os.path.join(filedir, f) for f in os.listdir(filedir)]
self.lintdir = os.path.join(here, 'linters')
names = ('string.lint', 'regex.lint', 'external.lint')
self.linters = [os.path.join(self.lintdir, n) for n in names]
def setUp(self):
TestCase.setUp(self)
self.lint = LintRoller()
def test_roll_no_linters_configured(self):
with self.assertRaises(LintersNotConfigured):
self.lint.roll(self.files)
def test_roll_successful(self):
self.lint.read(self.linters)
result = self.lint.roll(self.files)
self.assertEqual(len(result), 1)
path = result.keys()[0]
self.assertEqual(os.path.basename(path), 'foobar.js')
errors = result[path]
self.assertIsInstance(errors, list)
self.assertEqual(len(errors), 6)
container = errors[0]
self.assertIsInstance(container, ResultContainer)
self.assertEqual(container.rule, 'no-foobar')
def test_roll_catch_exception(self):
self.lint.read(os.path.join(self.lintdir, 'raises.lint'))
# suppress printed traceback from test output
old_stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
with self.assertRaises(LintException):
self.lint.roll(self.files)
sys.stderr = old_stderr
def test_roll_with_excluded_path(self):
self.lint.lintargs.update({'exclude': ['**/foobar.js']})
self.lint.read(self.linters)
result = self.lint.roll(self.files)
self.assertEqual(len(result), 0)
if __name__ == '__main__':
main()