def test_no_editor(monkeypatch, capture_commands, result): commands = [] capture_commands(commands) monkeypatch.delenv('EDITOR', raising=False) editor.edit_issues(result) assert commands == []
def test_vim(monkeypatch, capture_commands, result): commands = [] capture_commands(commands) monkeypatch.setenv('EDITOR', 'vim') editor.edit_issues(result) assert len(commands) == 1 assert commands[0][0] == 'vim'
def test_no_issues(monkeypatch, capture_commands, result): commands = [] capture_commands(commands) monkeypatch.setenv('EDITOR', 'generic') result.issues = {} editor.edit_issues(result) assert commands == []
def run(paths, linters, formats, outgoing, workdir, edit, setup=False, list_linters=False, num_procs=None, **lintargs): from mozlint import LintRoller, formatters from mozlint.editor import edit_issues if list_linters: lint_paths = find_linters(linters) print("Available linters: {}".format( [os.path.splitext(os.path.basename(l))[0] for l in lint_paths])) return 0 lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # Always run bootstrapping, but return early if --setup was passed in. ret = lint.setup() if setup: return ret # run all linters result = lint.roll(paths, outgoing=outgoing, workdir=workdir, num_procs=num_procs) if edit and result.issues: edit_issues(result) result = lint.roll(result.issues.keys(), num_procs=num_procs) for formatter_name, path in formats: formatter = formatters.get(formatter_name) out = formatter(result) if out: fh = open(path, 'w') if path else sys.stdout if not path and fh.encoding == 'ascii': # If sys.stdout.encoding is ascii, printing output will fail # due to the stylish formatter's use of unicode characters. # Ideally the user should fix their environment by setting # `LC_ALL=C.UTF-8` or similar. But this is a common enough # problem that we help them out a little here by manually # encoding and writing to the stdout buffer directly. out += '\n' fh.buffer.write(out.encode('utf-8', errors='replace')) fh.buffer.flush() else: print(out, file=fh) return result.returncode
def test_generic(monkeypatch, capture_commands, result): commands = [] capture_commands(commands) monkeypatch.setenv('EDITOR', 'generic') editor.edit_issues(result) assert len(commands) == len(result.issues) assert all(c[0] == 'generic' for c in commands) assert all('foo.py' in c for c in commands)
def test_generic(monkeypatch, capture_commands, result): commands = [] capture_commands(commands) monkeypatch.setenv("EDITOR", "generic") editor.edit_issues(result) assert len(commands) == len(result.issues) assert all(c[0] == "generic" for c in commands) assert all("foo.py" in c for c in commands)
def run(paths, linters, formats, outgoing, workdir, edit, setup=False, list_linters=False, **lintargs): from mozlint import LintRoller, formatters from mozlint.editor import edit_issues if list_linters: lint_paths = find_linters(linters) print("Available linters: {}".format( [os.path.splitext(os.path.basename(l))[0] for l in lint_paths])) return 0 lint = LintRoller(**lintargs) lint.read(find_linters(linters)) # Always run bootstrapping, but return early if --setup was passed in. ret = lint.setup() if setup: return ret # run all linters result = lint.roll(paths, outgoing=outgoing, workdir=workdir) if edit and result.issues: edit_issues(result) result = lint.roll(result.issues.keys()) for formatter_name, path in formats: formatter = formatters.get(formatter_name) out = formatter(result) if sys.version_info[0] == 2: # Encode output with 'replace' to avoid UnicodeEncodeErrors on # environments that aren't using utf-8. out = formatter(result).encode(sys.stdout.encoding or 'ascii', 'replace') if out: output_file = open(path, 'w') if path else sys.stdout print(out, file=output_file) return result.returncode
def run( paths, linters, formats, outgoing, workdir, rev, edit, check_exclude_list, setup=False, list_linters=False, num_procs=None, virtualenv_manager=None, **lintargs ): from mozlint import LintRoller, formatters from mozlint.editor import edit_issues lintargs["config_paths"] = [ os.path.join(lintargs["root"], p) for p in lintargs["config_paths"] ] # Always perform exhaustive linting for exclude list paths lintargs["use_filters"] = lintargs["use_filters"] and not check_exclude_list if list_linters: lint_paths = find_linters(lintargs["config_paths"], linters) linters = [ os.path.splitext(os.path.basename(l))[0] for l in lint_paths["lint_paths"] ] print("\n".join(sorted(linters))) return 0 lint = LintRoller(**lintargs) linters_info = find_linters(lintargs["config_paths"], linters) result = None try: lint.read(linters_info["lint_paths"]) if check_exclude_list: if len(lint.linters) > 1: print("error: specify a single linter to check with `-l/--linter`") return 1 paths = lint.linters[0]["local_exclude"] if ( not linters and not paths and os.getcwd() == lint.root and not (outgoing or workdir) ): print( "warning: linting the entire repo takes a long time, using --outgoing and " "--workdir instead. If you want to lint the entire repo, run `./mach lint .`" ) # Setting the default values outgoing = "default" workdir = "all" # Always run bootstrapping, but return early if --setup was passed in. ret = lint.setup(virtualenv_manager=virtualenv_manager) if setup: return ret if linters_info["linters_not_found"] != []: raise NoValidLinter # run all linters result = lint.roll( paths, outgoing=outgoing, workdir=workdir, rev=rev, num_procs=num_procs ) except NoValidLinter as e: result = lint.result print(str(e)) if edit and result.issues: edit_issues(result) result = lint.roll(result.issues.keys(), num_procs=num_procs) for every in linters_info["linters_not_found"]: result.failed_setup.add(every) if check_exclude_list: # Get and display all those paths in the exclude list which are # now green and can be safely removed from the list out = get_exclude_list_output(result, paths) print(out, file=sys.stdout) return result.returncode for formatter_name, path in formats: formatter = formatters.get(formatter_name) out = formatter(result) if out: fh = open(path, "w") if path else sys.stdout if not path and fh.encoding == "ascii": # If sys.stdout.encoding is ascii, printing output will fail # due to the stylish formatter's use of unicode characters. # Ideally the user should fix their environment by setting # `LC_ALL=C.UTF-8` or similar. But this is a common enough # problem that we help them out a little here by manually # encoding and writing to the stdout buffer directly. out += "\n" fh.buffer.write(out.encode("utf-8", errors="replace")) fh.buffer.flush() else: print(out, file=fh) return result.returncode
def run( paths, linters, formats, outgoing, workdir, rev, edit, setup=False, list_linters=False, num_procs=None, virtualenv_manager=None, **lintargs ): from mozlint import LintRoller, formatters from mozlint.editor import edit_issues lintargs["config_paths"] = [ os.path.join(lintargs["root"], p) for p in lintargs["config_paths"] ] if list_linters: lint_paths = find_linters(lintargs["config_paths"], linters) linters = [ os.path.splitext(os.path.basename(l))[0] for l in lint_paths["lint_paths"] ] print("\n".join(sorted(linters))) return 0 lint = LintRoller(**lintargs) linters_info = find_linters(lintargs["config_paths"], linters) result = None try: lint.read(linters_info["lint_paths"]) # Always run bootstrapping, but return early if --setup was passed in. ret = lint.setup(virtualenv_manager=virtualenv_manager) if setup: return ret if linters_info["linters_not_found"] != []: raise NoValidLinter # run all linters result = lint.roll( paths, outgoing=outgoing, workdir=workdir, rev=rev, num_procs=num_procs ) except NoValidLinter as e: result = lint.result print(str(e)) if edit and result.issues: edit_issues(result) result = lint.roll(result.issues.keys(), num_procs=num_procs) for every in linters_info["linters_not_found"]: result.failed_setup.add(every) for formatter_name, path in formats: formatter = formatters.get(formatter_name) out = formatter(result) if out: fh = open(path, "w") if path else sys.stdout if not path and fh.encoding == "ascii": # If sys.stdout.encoding is ascii, printing output will fail # due to the stylish formatter's use of unicode characters. # Ideally the user should fix their environment by setting # `LC_ALL=C.UTF-8` or similar. But this is a common enough # problem that we help them out a little here by manually # encoding and writing to the stdout buffer directly. out += "\n" fh.buffer.write(out.encode("utf-8", errors="replace")) fh.buffer.flush() else: print(out, file=fh) return result.returncode