1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
# 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 platform
import signal
import subprocess
import sys
import time
from itertools import chain
import mozunit
import pytest
from mozlint.errors import LintersNotConfigured, NoValidLinter
from mozlint.result import Issue, ResultSummary
from mozlint.roller import LintRoller
here = os.path.abspath(os.path.dirname(__file__))
def test_roll_no_linters_configured(lint, files):
with pytest.raises(LintersNotConfigured):
lint.roll(files)
def test_roll_successful(lint, linters, files):
lint.read(linters("string", "regex", "external"))
result = lint.roll(files)
assert len(result.issues) == 1
assert result.failed == set([])
path = list(result.issues.keys())[0]
assert os.path.basename(path) == "foobar.js"
errors = result.issues[path]
assert isinstance(errors, list)
assert len(errors) == 6
container = errors[0]
assert isinstance(container, Issue)
assert container.rule == "no-foobar"
def test_roll_from_subdir(lint, linters):
lint.read(linters("string", "regex", "external"))
oldcwd = os.getcwd()
try:
os.chdir(os.path.join(lint.root, "files"))
# Path relative to cwd works
result = lint.roll("foobar.js")
assert len(result.issues) == 1
assert len(result.failed) == 0
assert result.returncode == 1
# Path relative to root doesn't work
result = lint.roll(os.path.join("files", "foobar.js"))
assert len(result.issues) == 0
assert len(result.failed) == 0
assert result.returncode == 0
# Paths from vcs are always joined to root instead of cwd
lint.mock_vcs([os.path.join("files", "foobar.js")])
result = lint.roll(outgoing=True)
assert len(result.issues) == 1
assert len(result.failed) == 0
assert result.returncode == 1
result = lint.roll(workdir=True)
assert len(result.issues) == 1
assert len(result.failed) == 0
assert result.returncode == 1
result = lint.roll(rev='not public() and keyword("dummy revset expression")')
assert len(result.issues) == 1
assert len(result.failed) == 0
assert result.returncode == 1
finally:
os.chdir(oldcwd)
def test_roll_catch_exception(lint, linters, files, capfd):
lint.read(linters("raises"))
lint.roll(files) # assert not raises
out, err = capfd.readouterr()
assert "LintException" in err
def test_roll_with_global_excluded_path(lint, linters, files):
lint.exclude = ["**/foobar.js"]
lint.read(linters("string", "regex", "external"))
result = lint.roll(files)
assert len(result.issues) == 0
assert result.failed == set([])
def test_roll_with_local_excluded_path(lint, linters, files):
lint.read(linters("excludes"))
result = lint.roll(files)
assert "**/foobar.js" in lint.linters[0]["local_exclude"]
assert len(result.issues) == 0
assert result.failed == set([])
def test_roll_with_no_files_to_lint(lint, linters, capfd):
lint.read(linters("string", "regex", "external"))
lint.mock_vcs([])
result = lint.roll([], workdir=True)
assert isinstance(result, ResultSummary)
assert len(result.issues) == 0
assert len(result.failed) == 0
out, err = capfd.readouterr()
assert "WARNING: no files linted" in err
def test_roll_with_invalid_extension(lint, linters, filedir):
lint.read(linters("external"))
result = lint.roll(os.path.join(filedir, "foobar.py"))
assert len(result.issues) == 0
assert result.failed == set([])
def test_roll_with_failure_code(lint, linters, files):
lint.read(linters("badreturncode"))
result = lint.roll(files, num_procs=1)
assert len(result.issues) == 0
assert result.failed == set(["BadReturnCodeLinter"])
def test_roll_warnings(lint, linters, files):
lint.read(linters("warning"))
result = lint.roll(files)
assert len(result.issues) == 0
assert result.total_issues == 0
assert len(result.suppressed_warnings) == 1
assert result.total_suppressed_warnings == 2
lint.lintargs["show_warnings"] = True
result = lint.roll(files)
assert len(result.issues) == 1
assert result.total_issues == 2
assert len(result.suppressed_warnings) == 0
assert result.total_suppressed_warnings == 0
def test_roll_code_review(monkeypatch, linters, files):
monkeypatch.setenv("CODE_REVIEW", "1")
lint = LintRoller(root=here, show_warnings=False)
lint.read(linters("warning"))
result = lint.roll(files)
assert len(result.issues) == 1
assert result.total_issues == 2
assert len(result.suppressed_warnings) == 0
assert result.total_suppressed_warnings == 0
assert result.returncode == 1
def test_roll_code_review_warnings_disabled(monkeypatch, linters, files):
monkeypatch.setenv("CODE_REVIEW", "1")
lint = LintRoller(root=here, show_warnings=False)
lint.read(linters("warning_no_code_review"))
result = lint.roll(files)
assert len(result.issues) == 0
assert result.total_issues == 0
assert lint.result.fail_on_warnings is True
assert len(result.suppressed_warnings) == 1
assert result.total_suppressed_warnings == 2
assert result.returncode == 0
def test_roll_code_review_warnings_soft(linters, files):
lint = LintRoller(root=here, show_warnings="soft")
lint.read(linters("warning_no_code_review"))
result = lint.roll(files)
assert len(result.issues) == 1
assert result.total_issues == 2
assert lint.result.fail_on_warnings is False
assert len(result.suppressed_warnings) == 0
assert result.total_suppressed_warnings == 0
assert result.returncode == 0
def fake_run_worker(config, paths, **lintargs):
result = ResultSummary(lintargs["root"])
result.issues["count"].append(1)
return result
@pytest.mark.skipif(
platform.system() == "Windows",
reason="monkeypatch issues with multiprocessing on Windows",
)
@pytest.mark.parametrize("num_procs", [1, 4, 8, 16])
def test_number_of_jobs(monkeypatch, lint, linters, files, num_procs):
monkeypatch.setattr(sys.modules[lint.__module__], "_run_worker", fake_run_worker)
linters = linters("string", "regex", "external")
lint.read(linters)
num_jobs = len(lint.roll(files, num_procs=num_procs).issues["count"])
if len(files) >= num_procs:
assert num_jobs == num_procs * len(linters)
else:
assert num_jobs == len(files) * len(linters)
@pytest.mark.skipif(
platform.system() == "Windows",
reason="monkeypatch issues with multiprocessing on Windows",
)
@pytest.mark.parametrize("max_paths,expected_jobs", [(1, 12), (4, 6), (16, 6)])
def test_max_paths_per_job(monkeypatch, lint, linters, files, max_paths, expected_jobs):
monkeypatch.setattr(sys.modules[lint.__module__], "_run_worker", fake_run_worker)
files = files[:4]
assert len(files) == 4
linters = linters("string", "regex", "external")[:3]
assert len(linters) == 3
lint.MAX_PATHS_PER_JOB = max_paths
lint.read(linters)
num_jobs = len(lint.roll(files, num_procs=2).issues["count"])
assert num_jobs == expected_jobs
@pytest.mark.skipif(
platform.system() == "Windows",
reason="monkeypatch issues with multiprocessing on Windows",
)
@pytest.mark.parametrize("num_procs", [1, 4, 8, 16])
def test_number_of_jobs_global(monkeypatch, lint, linters, files, num_procs):
monkeypatch.setattr(sys.modules[lint.__module__], "_run_worker", fake_run_worker)
linters = linters("global")
lint.read(linters)
num_jobs = len(lint.roll(files, num_procs=num_procs).issues["count"])
assert num_jobs == 1
@pytest.mark.skipif(
platform.system() == "Windows",
reason="monkeypatch issues with multiprocessing on Windows",
)
@pytest.mark.parametrize("max_paths", [1, 4, 16])
def test_max_paths_per_job_global(monkeypatch, lint, linters, files, max_paths):
monkeypatch.setattr(sys.modules[lint.__module__], "_run_worker", fake_run_worker)
files = files[:4]
assert len(files) == 4
linters = linters("global")[:1]
assert len(linters) == 1
lint.MAX_PATHS_PER_JOB = max_paths
lint.read(linters)
num_jobs = len(lint.roll(files, num_procs=2).issues["count"])
assert num_jobs == 1
@pytest.mark.skipif(
platform.system() == "Windows",
reason="signal.CTRL_C_EVENT isn't causing a KeyboardInterrupt on Windows",
)
def test_keyboard_interrupt():
# We use two linters so we'll have two jobs. One (string.yml) will complete
# quickly. The other (slow.yml) will run slowly. This way the first worker
# will be be stuck blocking on the ProcessPoolExecutor._call_queue when the
# signal arrives and the other still be doing work.
cmd = [sys.executable, "runcli.py", "-l=string", "-l=slow", "files/foobar.js"]
env = os.environ.copy()
env["PYTHONPATH"] = os.pathsep.join(sys.path)
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
cwd=here,
env=env,
universal_newlines=True,
)
time.sleep(1)
proc.send_signal(signal.SIGINT)
out = proc.communicate()[0]
print(out)
assert "WARNING: \nnot all files were linted" in out
assert "2 problems" in out
assert "Traceback" not in out
def test_support_files(lint, linters, filedir, monkeypatch, files):
jobs = []
# Replace the original _generate_jobs with a new one that simply
# adds jobs to a list (and then doesn't return anything).
orig_generate_jobs = lint._generate_jobs
def fake_generate_jobs(*args, **kwargs):
jobs.extend([job[1] for job in orig_generate_jobs(*args, **kwargs)])
return []
monkeypatch.setattr(lint, "_generate_jobs", fake_generate_jobs)
linter_path = linters("support_files")[0]
lint.read(linter_path)
lint.root = filedir
# Modified support files only lint entire root if --outgoing or --workdir
# are used.
path = os.path.join(filedir, "foobar.js")
vcs_path = os.path.join(filedir, "foobar.py")
lint.mock_vcs([vcs_path])
lint.roll(path)
actual_files = sorted(chain(*jobs))
assert actual_files == [path]
expected_files = sorted(files)
jobs = []
lint.roll(path, workdir=True)
actual_files = sorted(chain(*jobs))
assert actual_files == expected_files
jobs = []
lint.roll(path, outgoing=True)
actual_files = sorted(chain(*jobs))
assert actual_files == expected_files
jobs = []
lint.roll(path, rev='draft() and keyword("dummy revset expression")')
actual_files = sorted(chain(*jobs))
assert actual_files == expected_files
# Lint config file is implicitly added as a support file
lint.mock_vcs([linter_path])
jobs = []
lint.roll(path, outgoing=True, workdir=True)
actual_files = sorted(chain(*jobs))
assert actual_files == expected_files
# Avoid linting the entire root when `--fix` is passed.
lint.mock_vcs([vcs_path])
lint.lintargs["fix"] = True
jobs = []
lint.roll(path, outgoing=True)
actual_files = sorted(chain(*jobs))
assert actual_files == sorted([path, vcs_path]), (
"`--fix` with `--outgoing` on a `support-files` change should "
"avoid linting the entire root."
)
jobs = []
lint.roll(path, workdir=True)
actual_files = sorted(chain(*jobs))
assert actual_files == sorted([path, vcs_path]), (
"`--fix` with `--workdir` on a `support-files` change should "
"avoid linting the entire root."
)
jobs = []
lint.roll(path, rev='draft() and keyword("dummy revset expression")')
actual_files = sorted(chain(*jobs))
assert actual_files == sorted([path, vcs_path]), (
"`--fix` with `--rev` on a `support-files` change should "
"avoid linting the entire root."
)
def test_setup(lint, linters, filedir, capfd):
with pytest.raises(NoValidLinter):
lint.setup()
lint.read(linters("setup", "setupfailed", "setupraised", "setupskipped"))
ret = lint.setup()
assert ret == 1
out, err = capfd.readouterr()
assert "oh no setup failed" in err
assert "ERROR: problem with lint setup, skipping" in err
assert lint.result.failed_setup == set(["SetupFailedLinter", "SetupRaisedLinter"])
def test_setup_all_skipped(lint, linters, filedir, capfd):
lint.read(linters("setupskipped"))
ret = lint.setup()
assert ret == 1
out, err = capfd.readouterr()
assert "ERROR: all linters were skipped due to setup, nothing to do!" in err
assert lint.result.failed_setup == set()
if __name__ == "__main__":
mozunit.main()
|