| 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 mozpack.path as mozpath |
| 8 |
import mozunit |
| 9 |
import pytest |
| 10 |
|
| 11 |
from mozlint.result import Issue, ResultSummary |
| 12 |
|
| 13 |
|
| 14 |
@pytest.fixture |
| 15 |
def path(filedir): |
| 16 |
def _path(name): |
| 17 |
return mozpath.join(filedir, name) |
| 18 |
|
| 19 |
return _path |
| 20 |
|
| 21 |
|
| 22 |
@pytest.fixture( |
| 23 |
params=[ |
| 24 |
"external.yml", |
| 25 |
"global.yml", |
| 26 |
"regex.yml", |
| 27 |
"string.yml", |
| 28 |
"structured.yml", |
| 29 |
] |
| 30 |
) |
| 31 |
def linter(lintdir, request): |
| 32 |
return os.path.join(lintdir, request.param) |
| 33 |
|
| 34 |
|
| 35 |
def test_linter_types(lint, linter, files, path): |
| 36 |
lint.read(linter) |
| 37 |
result = lint.roll(files) |
| 38 |
assert isinstance(result, ResultSummary) |
| 39 |
assert isinstance(result.issues, dict) |
| 40 |
assert path("foobar.js") in result.issues |
| 41 |
assert path("no_foobar.js") not in result.issues |
| 42 |
|
| 43 |
issue = result.issues[path("foobar.js")][0] |
| 44 |
assert isinstance(issue, Issue) |
| 45 |
|
| 46 |
name = os.path.basename(linter).split(".")[0] |
| 47 |
assert issue.linter.lower().startswith(name) |
| 48 |
|
| 49 |
|
| 50 |
def test_linter_missing_files(lint, linter, filedir): |
| 51 |
# Missing files should be caught by `mozlint.cli`, so the only way they |
| 52 |
# could theoretically happen is if they show up from versioncontrol. So |
| 53 |
# let's just make sure they get ignored. |
| 54 |
lint.read(linter) |
| 55 |
files = [ |
| 56 |
os.path.join(filedir, "missing.js"), |
| 57 |
os.path.join(filedir, "missing.py"), |
| 58 |
] |
| 59 |
result = lint.roll(files) |
| 60 |
assert result.returncode == 0 |
| 61 |
|
| 62 |
lint.mock_vcs(files) |
| 63 |
result = lint.roll(outgoing=True) |
| 64 |
assert result.returncode == 0 |
| 65 |
|
| 66 |
|
| 67 |
def test_no_filter(lint, lintdir, files): |
| 68 |
lint.read(os.path.join(lintdir, "explicit_path.yml")) |
| 69 |
result = lint.roll(files) |
| 70 |
assert len(result.issues) == 0 |
| 71 |
|
| 72 |
lint.lintargs["use_filters"] = False |
| 73 |
result = lint.roll(files) |
| 74 |
assert len(result.issues) == 3 |
| 75 |
|
| 76 |
|
| 77 |
def test_global_skipped(lint, lintdir, files): |
| 78 |
lint.read(os.path.join(lintdir, "global_skipped.yml")) |
| 79 |
result = lint.roll(files) |
| 80 |
assert len(result.issues) == 0 |
| 81 |
|
| 82 |
|
| 83 |
if __name__ == "__main__": |
| 84 |
mozunit.main() |