def test_fix_failing_check_no_changes(file_config_files, capfd): autofix_lib.fix( ( str(file_config_files.output_dir.join('repo1')), str(file_config_files.output_dir.join('repo2')), ), apply_fix=lower_case_f, check_fix=failing_check_fix, config=load_config(file_config_files.cfg), commit=autofix_lib.Commit('message!', 'test-branch', None), autofix_settings=autofix_lib.AutofixSettings( jobs=1, color=False, limit=None, dry_run=False, interactive=False, ), ) out, err = capfd.readouterr() assert 'nope!' in err assert out.count('Errored') == 2 # An error while checking should not allow the changes assert file_config_files.dir1.join('f').read() == 'OHAI\n' assert file_config_files.dir2.join('f').read() == 'OHELLO\n'
def test_fix_dry_run_no_change(file_config_files, capfd): autofix_lib.fix( ( str(file_config_files.output_dir.join('repo1')), str(file_config_files.output_dir.join('repo2')), ), apply_fix=lower_case_f, config=load_config(file_config_files.cfg), commit=autofix_lib.Commit('message!', 'test-branch', None), autofix_settings=autofix_lib.AutofixSettings( jobs=1, color=False, limit=None, dry_run=True, interactive=False, ), ) out, err = capfd.readouterr() assert err == '' assert 'Errored' not in out # Showed the diff of what would have happened assert '-OHAI\n+ohai\n' in out assert '-OHELLO\n+ohello\n' in out # Didn't actually perform any changes assert file_config_files.dir1.join('f').read() == 'OHAI\n' assert file_config_files.dir2.join('f').read() == 'OHELLO\n'
def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) autofix_lib.assert_importable('pre_commit', install='pre-commit') autofix_lib.require_version_gte('pre-commit', '1.7.0') repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg='Migrate to official pycqa/flake8 hooks repo', branch_name='pre-commit-flake8-migrate', ) with tmp_pre_commit_home(): autofix_lib.fix( repos, apply_fix=apply_fix, check_fix=check_fix, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) autofix_lib.assert_importable('pre_commit', install='pre-commit') # pre-commit 0.16.3: autoupdate maintains formatting better # pre-commit 0.17.0: race conditions in pre-commit install fixed # pre-commit 1.0.0: migrate_config(...) is called for autoupdate # pre-commit 1.0.1: exit code fix # pre-commit 1.7.0: sha -> rev autofix_lib.require_version_gte('pre-commit', '1.7.0') repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg='Run pre-commit autoupdate', branch_name='pre-commit-autoupdate', ) with tmp_pre_commit_home(): autofix_lib.fix( repos, apply_fix=apply_fix, check_fix=check_fix, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) autofix_lib.assert_importable('pre_commit', install='pre-commit') # pre-commit 1.0.0: introduces migrate-config # pre-commit 1.0.1: exit code fix # pre-commit 1.7.0: sha -> rev autofix_lib.require_version_gte('pre-commit', '1.7.0') repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg='Ran pre-commit migrate-config.', branch_name='pre-commit-migrate-config', ) with tmp_pre_commit_home(): autofix_lib.fix( repos, apply_fix=apply_fix, check_fix=check_fix, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) autofix_lib.assert_importable('pre_commit', install='pre-commit') autofix_lib.require_version_gte('pre-commit', '1.7.0') repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg='Migrate from autopep8-wrapper to mirrors-autopep8', branch_name='pre-commit-autopep8-migrate', ) with tmp_pre_commit_home(): autofix_lib.fix( repos, apply_fix=apply_fix, check_fix=check_fix, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) autofix_lib.assert_importable( 'setup_py_upgrade', install='setup-py-upgrade', ) autofix_lib.assert_importable('setup_cfg_fmt', install='setup-cfg-fmt') repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg='Migrate setup.py to setup.cfg declarative metadata', branch_name='setup-py-upgrade', ) autofix_lib.fix( repos, apply_fix=apply_fix, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def test_autofix_makes_commits(file_config_files, capfd): autofix_lib.fix( ( str(file_config_files.output_dir.join('repo1')), str(file_config_files.output_dir.join('repo2')), ), apply_fix=lower_case_f, config=load_config(file_config_files.cfg), commit=autofix_lib.Commit('message!', 'test-branch', 'A B <[email protected]>'), autofix_settings=autofix_lib.AutofixSettings( jobs=1, color=False, limit=None, dry_run=False, interactive=False, ), ) out, err = capfd.readouterr() assert err == '' assert 'Errored' not in out assert file_config_files.dir1.join('f').read() == 'ohai\n' assert file_config_files.dir2.join('f').read() == 'ohello\n' # The branch name should be what we specified last_commit_msg = subprocess.check_output(( 'git', '-C', file_config_files.dir1, 'log', '--format=%s', '--first-parent', '-1', )).strip().decode() potential_msgs = testing.git.merge_msgs('all-repos_autofix_test-branch') assert last_commit_msg in potential_msgs # We should see a commit from the autofix change we made commit = subprocess.check_output(( 'git', '-C', file_config_files.dir1, 'log', '--patch', '--grep', 'message!', '--format=%an %ae\n%B', )).decode() assert commit.startswith( 'A B [email protected]\n' 'message!\n' '\n' 'Committed via https://github.com/asottile/all-repos\n', ) assert commit.endswith('-OHAI\n+ohai\n')
def test_fix_non_default_branch(file_config_non_default): clone.main(('--config-filename', str(file_config_non_default.cfg))) autofix_lib.fix( (str(file_config_non_default.output_dir.join('repo1')), ), apply_fix=lower_case_f, config=load_config(file_config_non_default.cfg), commit=autofix_lib.Commit('message!', 'test-branch', 'A B <[email protected]>'), autofix_settings=autofix_lib.AutofixSettings( jobs=1, color=False, limit=None, dry_run=False, interactive=False, ), ) assert file_config_non_default.dir1.join('f').read() == 'ohai\n'
def main(argv=None): parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg='Update pre-commit cache directory.', branch_name='pre-commit-cache-dir', ) autofix_lib.fix( repos, apply_fix=apply_fix, config=config, commit=commit, autofix_settings=autofix_settings, )
def main(argv=None): parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg="Use and pin Black 20", branch_name="black20", ) autofix_lib.fix( repos, apply_fix=apply_fix, config=config, commit=commit, autofix_settings=autofix_settings, )
def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg="Cleanup Apache 2 licence templates, add ASL2 if absent", branch_name="cleanup-licence", ) autofix_lib.fix( repos, apply_fix=apply_fix, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg='Update azure-pipelines template repositories', branch_name='azure-pipelines-autoupdate', ) autofix_lib.fix( repos, apply_fix=apply_fix, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def main(argv: Optional[Sequence[str]] = None) -> int: parser = argparse.ArgumentParser() autofix_lib.add_fixer_args(parser) args = parser.parse_args(argv) repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg="Add Apache 2 licences", branch_name="add-licence-apache2", ) autofix_lib.fix( repos, apply_fix=apply_fix, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def test_fix_interactive(file_config_files, capfd, mock_input): mock_input.set_side_effect('y', 'n') autofix_lib.fix( ( str(file_config_files.output_dir.join('repo1')), str(file_config_files.output_dir.join('repo2')), ), apply_fix=lower_case_f, config=load_config(file_config_files.cfg), commit=autofix_lib.Commit('message!', 'test-branch', None), autofix_settings=autofix_lib.AutofixSettings( jobs=1, color=False, limit=None, dry_run=False, interactive=True, ), ) assert file_config_files.dir1.join('f').read() == 'ohai\n' assert file_config_files.dir2.join('f').read() == 'OHELLO\n'
def test_noop_does_not_commit(file_config_files, capfd): rev_before1 = testing.git.revparse(file_config_files.dir1) rev_before2 = testing.git.revparse(file_config_files.dir2) autofix_lib.fix( ( str(file_config_files.output_dir.join('repo1')), str(file_config_files.output_dir.join('repo2')), ), apply_fix=lambda: None, config=load_config(file_config_files.cfg), commit=autofix_lib.Commit('message!', 'test-branch', None), autofix_settings=autofix_lib.AutofixSettings( jobs=1, color=False, limit=None, dry_run=False, interactive=False, ), ) rev_after1 = testing.git.revparse(file_config_files.dir1) rev_after2 = testing.git.revparse(file_config_files.dir2) assert (rev_before1, rev_before2) == (rev_after1, rev_after2)
def test_fix_with_limit(file_config_files, capfd): autofix_lib.fix( ( str(file_config_files.output_dir.join('repo1')), str(file_config_files.output_dir.join('repo2')), ), apply_fix=lower_case_f, config=load_config(file_config_files.cfg), commit=autofix_lib.Commit('message!', 'test-branch', None), autofix_settings=autofix_lib.AutofixSettings( jobs=1, color=False, limit=1, dry_run=True, interactive=False, ), ) out, err = capfd.readouterr() assert err == '' assert 'Errored' not in out # Should still see the diff from the first repository assert '-OHAI\n+ohai\n' in out assert '-OHELLO\n+ohello\n' not in out
def main(argv: Sequence[str] | None = None) -> int: parser = argparse.ArgumentParser( description='Interactively apply a manual change across repos.', usage='%(prog)s [options]', ) autofix_lib.add_fixer_args(parser) parser.add_argument( '--branch-name', default='all-repos-manual', help='override the autofixer branch name (default `%(default)s`).', ) parser.add_argument( '--commit-msg', required=True, help='set the autofixer commit message.', ) args = parser.parse_args(argv) # force interactive args.interactive = True repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=find_repos, msg=args.commit_msg, branch_name=args.branch_name, ) autofix_lib.fix( repos, apply_fix=autofix_lib.shell, config=config, commit=commit, autofix_settings=autofix_settings, ) return 0
def main(argv=None): parser = argparse.ArgumentParser( description=( 'Similar to a distributed ' '`git ls-files -z -- FILENAMES | xargs -0 sed -i EXPRESSION`.'), usage='%(prog)s [options] EXPRESSION FILENAMES', ) autofix_lib.add_fixer_args(parser) parser.add_argument( '-r', '--regexp-extended', action='store_true', help='use extended regular expressions in the script.', ) parser.add_argument( '--branch-name', default='all-repos-sed', help='override the autofixer branch name (default `%(default)s`).', ) parser.add_argument( '--commit-msg', help=( 'override the autofixer commit message. (default ' '`git ls-files -z -- FILENAMES | xargs -0 sed -i ... EXPRESSION`).' ), ) parser.add_argument( 'expression', help='sed program. For example: `s/hi/hello/g`.', ) parser.add_argument( 'filenames', help='filenames glob (passed to `git ls-files`).', ) args = parser.parse_args(argv) dash_r = ('-r', ) if args.regexp_extended else () sed_cmd = ('sed', '-i', *dash_r, args.expression) ls_files_cmd = ('git', 'ls-files', '-z', '--', args.filenames) msg = f'{_quote_cmd(ls_files_cmd)} | xargs -0 {_quote_cmd(sed_cmd)}' msg = args.commit_msg or msg repos, config, commit, autofix_settings = autofix_lib.from_cli( args, find_repos=functools.partial(find_repos, ls_files_cmd=ls_files_cmd), msg=msg, branch_name=args.branch_name, ) autofix_lib.fix( repos, apply_fix=functools.partial( apply_fix, ls_files_cmd=ls_files_cmd, sed_cmd=sed_cmd, ), config=config, commit=commit, autofix_settings=autofix_settings, )