| 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 |
|
| 5 |
import os |
| 6 |
|
| 7 |
import mozunit |
| 8 |
import pytest |
| 9 |
|
| 10 |
from mozlint.errors import LinterNotFound, LinterParseError |
| 11 |
from mozlint.parser import Parser |
| 12 |
|
| 13 |
here = os.path.abspath(os.path.dirname(__file__)) |
| 14 |
|
| 15 |
|
| 16 |
@pytest.fixture(scope="module") |
| 17 |
def parse(lintdir): |
| 18 |
parser = Parser(here) |
| 19 |
|
| 20 |
def _parse(name): |
| 21 |
path = os.path.join(lintdir, name) |
| 22 |
return parser(path) |
| 23 |
|
| 24 |
return _parse |
| 25 |
|
| 26 |
|
| 27 |
def test_parse_valid_linter(parse): |
| 28 |
lintobj = parse("string.yml") |
| 29 |
assert isinstance(lintobj, list) |
| 30 |
assert len(lintobj) == 1 |
| 31 |
|
| 32 |
lintobj = lintobj[0] |
| 33 |
assert isinstance(lintobj, dict) |
| 34 |
assert "name" in lintobj |
| 35 |
assert "description" in lintobj |
| 36 |
assert "type" in lintobj |
| 37 |
assert "payload" in lintobj |
| 38 |
assert "extensions" in lintobj |
| 39 |
assert "include" in lintobj |
| 40 |
assert lintobj["include"] == ["."] |
| 41 |
assert set(lintobj["extensions"]) == set(["js"]) |
| 42 |
|
| 43 |
|
| 44 |
def test_parser_valid_multiple(parse): |
| 45 |
lintobj = parse("multiple.yml") |
| 46 |
assert isinstance(lintobj, list) |
| 47 |
assert len(lintobj) == 2 |
| 48 |
|
| 49 |
assert lintobj[0]["name"] == "StringLinter" |
| 50 |
assert lintobj[1]["name"] == "RegexLinter" |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.parametrize( |
| 54 |
"linter", |
| 55 |
[ |
| 56 |
"invalid_type.yml", |
| 57 |
"invalid_extension.ym", |
| 58 |
"invalid_include.yml", |
| 59 |
"invalid_include_with_glob.yml", |
| 60 |
"invalid_exclude.yml", |
| 61 |
"invalid_support_files.yml", |
| 62 |
"invalid_ext_and_exclude_ext.yml", |
| 63 |
"missing_attrs.yml", |
| 64 |
"missing_definition.yml", |
| 65 |
"non_existing_include.yml", |
| 66 |
"non_existing_exclude.yml", |
| 67 |
"non_existing_support_files.yml", |
| 68 |
], |
| 69 |
) |
| 70 |
def test_parse_invalid_linter(parse, linter): |
| 71 |
with pytest.raises(LinterParseError): |
| 72 |
parse(linter) |
| 73 |
|
| 74 |
|
| 75 |
def test_parse_non_existent_linter(parse): |
| 76 |
with pytest.raises(LinterNotFound): |
| 77 |
parse("missing_file.lint") |
| 78 |
|
| 79 |
|
| 80 |
if __name__ == "__main__": |
| 81 |
mozunit.main() |