def precommit(config=None, **kwargs): # pylint: disable=unused-argument check_black_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if len(files) == 0: ok('No staged files for black available.') return 0 arguments = ['black'] arguments.extend(get_black_arguments(config)) with stash_unstaged_changes(files): for f in files: try: args = arguments.copy() args.append(str(f.absolute_path())) subprocess.check_call(args) ok('Running black on {}'.format(str(f.path))) except subprocess.CalledProcessError as e: error('Running black on {}'.format(str(f.path))) raise e from None stage_files_from_status_list(files) return 0
def precommit(config=None, **kwargs): out('Running pylint pre-commit hook') check_pylint_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if not files: ok('No files to lint') return 0 with stash_unstaged_changes(files): args = ['pylint'] args.extend([str(f.absolute_path()) for f in files]) status = subprocess.call(args) str_files = ', '.join([str(f.path) for f in files]) if status: fail('Linting error(s) found in {}'.format(str_files)) else: ok('Linting {} was successful'.format(str_files)) return status
def precommit(config=None, **kwargs): # pylint: disable=unused-argument check_pylint_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if not files: ok('No staged files to lint.') return 0 arguments = get_pylint_arguments(config) with stash_unstaged_changes(files): ret = 0 for f in files: cmd = ['pylint'] cmd.extend(arguments) cmd.append(str(f.absolute_path())) try: subprocess.run(cmd, check=True, capture_output=True) except subprocess.CalledProcessError as e: ret = e.returncode error(f'Linting error(s) found in {str(f.path)}:') lint_errors = e.stdout.decode( encoding=sys.getdefaultencoding(), errors='replace').split('\n') # Skip the first line that only shows ******** Module blah for line in lint_errors[1:]: out(line) continue ok(f'Linting {str(f.path)} was successful.') return ret
def precommit(config=None, **kwargs): # pylint: disable=unused-argument check_pylint_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if not files: ok('No staged files to lint.') return 0 arguments = get_pylint_arguments(config) with stash_unstaged_changes(files): for f in files: cmd = ['pylint'] cmd.extend(arguments) cmd.append(str(f.absolute_path())) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) out_, _ = proc.communicate() if out_: out_ = out_.decode(encoding=sys.getdefaultencoding(), errors='replace').split('\n') for line in out_: out(line) if proc.returncode: error('Linting error(s) found in {}.'.format(str(f.path))) else: ok('Linting {} was successful.'.format(str(f.path))) return proc.returncode
def precommit(config=Union[None, Config], **kwargs) -> int: # pylint: disable=unused-argument # out('Running pytest pre-commit hook') check_pytest_installed() root_dir = get_source_from_config(config) view_mode = get_view_mode_from_config(config) cov_source = get_cov_source_from_config(config) cov_on_fail = get_cov_on_fail_from_config(config) cov_fail_under = get_cov_fail_under_from_config(config) cov_report_format = get_cov_report_format_from_config(config) call_str = [ "python", "-m", "pytest", "--tb=line", view_mode, "--rootdir", root_dir, "--cov", cov_source, "--cov-fail-under", str(cov_fail_under), "--cov-report", cov_report_format, cov_on_fail ] try: subprocess.check_call(call_str) ok('Running pytest on all test files') except subprocess.CalledProcessError as e: error('Running pytest get fail on some test cases') raise Exception("Some test cases got failed") return 0
def precommit(config=Union[None, Config], **kwargs) -> int: # pylint: disable=unused-argument out('Running autopep8 pre-commit hook') check_autopep8_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] ignore_errors = get_ignore_errors_from_config(config) max_line_length = get_default_line_length_from_config(config) experimental = get_experimental_features_from_config(config) if len(files) == 0: ok('No staged files for autopep8 available') return 0 call_str = [ 'autopep8', '-i', '-a', '-r', '--ignore', ",".join(ignore_errors), '--max-line-length', str(max_line_length) ] if experimental: call_str.append('--experimental') with stash_unstaged_changes(files): for f in files: try: subprocess.check_call(call_str + [str(f.absolute_path())]) ok('Running autopep8 on {}'.format(str(f.path))) except subprocess.CalledProcessError as e: error('Running autopep8 on {}'.format(str(f.path))) raise e stage_files_from_status_list(files) return 0
def precommit(config=None, **kwargs): # pylint: disable=unused-argument out('Running isort pre-commit hook') check_isort_installed() include = get_include_from_config(config) files = [f for f in get_staged_status() if match(f.path, include)] if len(files) == 0: ok('No staged files for isort available') return 0 with stash_unstaged_changes(files): for f in files: try: subprocess.check_call(['isort', '-q', str(f.absolute_path())]) ok('Running isort on {}'.format(str(f.path))) except subprocess.CalledProcessError as e: error('Running isort on {}'.format(str(f.path))) raise e stage_files_from_status_list(files) return 0
def test_ok(self): ok('foo bar') self.term.ok.assert_called_with('foo bar')