示例#1
0
def cloned(tmpdir):
    src = tmpdir.join('repo')
    init_repo(src)

    dest = tmpdir.join('dest')
    subprocess.check_call(('git', 'clone', src, dest))
    subprocess.check_call((
        'git',
        '-C',
        dest,
        'checkout',
        'origin/master',
        '-b',
        'feature',
    ))
    subprocess.check_call((
        'git',
        '-C',
        dest,
        'commit',
        '--allow-empty',
        '-m',
        'This is a commit message\n\nHere is some more information!',
    ))
    return auto_namedtuple(src=src, dest=dest)
def fake_github_repo(tmpdir):
    # hax: make the repo end with :repo/slug so it "looks" like a github repo
    src = tmpdir.join('repo:user/slug')
    init_repo(src)

    dest = tmpdir.join('dest')
    subprocess.check_call(('git', 'clone', src, dest))
    subprocess.check_call((
        'git',
        '-C',
        dest,
        'checkout',
        'origin/master',
        '-b',
        'feature',
    ))
    subprocess.check_call((
        'git',
        '-C',
        dest,
        'commit',
        '--allow-empty',
        '-m',
        'This is a commit message\n\nHere is some more information!',
    ))
    settings = github_pull_request.Settings(api_key='fake', username='******')
    return auto_namedtuple(src=src, dest=dest, settings=settings)
示例#3
0
def _get_opts(
        all_files=False,
        files=(),
        color=False,
        verbose=False,
        hook=None,
        no_stash=False,
        origin='',
        source='',
        allow_unstaged_config=False,
        hook_stage='commit',
        show_diff_on_failure=False,
):
    # These are mutually exclusive
    assert not (all_files and files)
    return auto_namedtuple(
        all_files=all_files,
        files=files,
        color=color,
        verbose=verbose,
        hook=hook,
        no_stash=no_stash,
        origin=origin,
        source=source,
        allow_unstaged_config=allow_unstaged_config,
        hook_stage=hook_stage,
        show_diff_on_failure=show_diff_on_failure,
    )
def img_staged(tempdir_factory):
    path = git_dir(tempdir_factory)
    with cwd(path):
        img_filename = os.path.join(path, 'img.jpg')
        shutil.copy(get_resource_path('img1.jpg'), img_filename)
        cmd_output('git', 'add', 'img.jpg')
        yield auto_namedtuple(path=path, img_filename=img_filename)
示例#5
0
def run_opts(
        all_files=False,
        files=(),
        color=False,
        verbose=False,
        hook=None,
        origin='',
        source='',
        remote_name='',
        remote_url='',
        hook_stage='commit',
        show_diff_on_failure=False,
        commit_msg_filename='',
):
    # These are mutually exclusive
    assert not (all_files and files)
    return auto_namedtuple(
        all_files=all_files,
        files=files,
        color=color,
        verbose=verbose,
        hook=hook,
        origin=origin,
        source=source,
        remote_name=remote_name,
        remote_url=remote_url,
        hook_stage=hook_stage,
        show_diff_on_failure=show_diff_on_failure,
        commit_msg_filename=commit_msg_filename,
    )
示例#6
0
def file_config(tmpdir):
    dir1 = tmpdir.join('1')
    dir2 = tmpdir.join('2')
    rev1 = init_repo(dir1)
    rev2 = init_repo(dir2)

    repos_json = tmpdir.join('repos.json')
    repos_json.write(json.dumps({'repo1': str(dir1), 'repo2': str(dir2)}))

    cfg = tmpdir.join('config.json')
    cfg.write(
        json.dumps({
            'output_dir': 'output',
            'source': 'all_repos.source.json_file',
            'source_settings': {
                'filename': str(repos_json)
            },
            'push': 'all_repos.push.merge_to_master',
            'push_settings': {},
        }))
    cfg.chmod(0o600)
    return auto_namedtuple(
        output_dir=tmpdir.join('output'),
        cfg=cfg,
        repos_json=repos_json,
        dir1=dir1,
        dir2=dir2,
        rev1=rev1,
        rev2=rev2,
    )
示例#7
0
def fake_bitbucket_repo(tmpdir):
    # hax: make the repo end with proj/slug so it "looks" like a bitbucket repo
    src = tmpdir.join('proj/slug')
    init_repo(src)

    dest = tmpdir.join('dest')
    subprocess.check_call(('git', 'clone', src, dest))
    subprocess.check_call((
        'git',
        '-C',
        dest,
        'checkout',
        'origin/master',
        '-b',
        'feature',
    ))
    subprocess.check_call((
        'git',
        '-C',
        dest,
        'commit',
        '--allow-empty',
        '-m',
        'This is a commit message\n\nHere is some more information!',
    ))
    settings = Settings('user', 'token', 'bitbucket.domain.com')
    return auto_namedtuple(src=src, dest=dest, settings=settings)
示例#8
0
def fake_autoupdatable(tmpdir):
    repo = tmpdir.join('autoupdatable')
    update_repo = tmpdir.join('autoupdatable_clone')
    git.init_repo(repo)

    def init(contents):
        repo.join('.pre-commit-config.yaml').write(contents)
        git.commit(repo)
        subprocess.check_call(('git', 'clone', repo, update_repo))

    def fake_autoupdate():
        with open('.pre-commit-config.yaml') as f:
            contents = f.read()
        with open('.pre-commit-config.yaml', 'w') as f:
            f.write(contents.replace('rev: v1.4.0-1', 'rev: v2.0.0'))

    with mock.patch.object(
            pre_commit_autopep8_migrate,
            'autoupdate',
            fake_autoupdate,
    ):
        with mock.patch.object(pre_commit_autopep8_migrate, 'check_fix'):
            yield auto_namedtuple(
                init=init,
                repo=repo,
                update_repo=update_repo,
            )
def fake_github_repo_fork(tmpdir, fake_github_repo):
    fork = tmpdir.join('repo:u2/slug')
    subprocess.check_call(('git', 'clone', fake_github_repo.src, fork))

    settings = fake_github_repo.settings._replace(fork=True, username='******')
    dct = dict(fake_github_repo._asdict(), settings=settings, fork=fork)
    return auto_namedtuple(**dct)
示例#10
0
def run_opts(
    all_files=False,
    files=(),
    color=False,
    verbose=False,
    hook=None,
    from_ref='',
    to_ref='',
    remote_name='',
    remote_url='',
    hook_stage='commit',
    show_diff_on_failure=False,
    commit_msg_filename='',
    checkout_type='',
):
    # These are mutually exclusive
    assert not (all_files and files)
    return auto_namedtuple(
        all_files=all_files,
        files=files,
        color=color,
        verbose=verbose,
        hook=hook,
        from_ref=from_ref,
        to_ref=to_ref,
        remote_name=remote_name,
        remote_url=remote_url,
        hook_stage=hook_stage,
        show_diff_on_failure=show_diff_on_failure,
        commit_msg_filename=commit_msg_filename,
        checkout_type=checkout_type,
    )
def foo_staged(tempdir_factory):
    path = git_dir(tempdir_factory)
    with cwd(path):
        with io.open('foo', 'w') as foo_file:
            foo_file.write(FOO_CONTENTS)
        cmd_output('git', 'add', 'foo')
        foo_filename = os.path.join(path, 'foo')
        yield auto_namedtuple(path=path, foo_filename=foo_filename)
def submodule_with_commits(tempdir_factory):
    path = git_dir(tempdir_factory)
    with cwd(path):
        cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
        rev1 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
        cmd_output('git', 'commit', '--allow-empty', '-m', 'bar')
        rev2 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
        yield auto_namedtuple(path=path, rev1=rev1, rev2=rev2)
示例#13
0
def submodule_with_commits(tempdir_factory):
    path = git_dir(tempdir_factory)
    with cwd(path):
        git_commit()
        rev1 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
        git_commit()
        rev2 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
        yield auto_namedtuple(path=path, rev1=rev1, rev2=rev2)
示例#14
0
def hook_disappearing(tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    original_rev = git.head_rev(path)

    with modify_manifest(path) as manifest:
        manifest[0]['id'] = 'bar'

    yield auto_namedtuple(path=path, original_rev=original_rev)
def repo(tmpdir):
    src_repo = tmpdir.join('hook_repo')
    init_repo(src_repo)
    write_file_commit(src_repo, 'azure-pipelines.yml', SAMPLE)

    update_repo = tmpdir.join('update_repo')
    subprocess.check_call(('git', 'clone', src_repo, update_repo))

    return auto_namedtuple(src_repo=src_repo, update_repo=update_repo)
示例#16
0
def repo_with_commits(tempdir_factory):
    path = git_dir(tempdir_factory)
    with cwd(path):
        open('foo', 'a+').close()
        cmd_output('git', 'add', 'foo')
        git_commit()
        rev1 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
        git_commit()
        rev2 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip()
        yield auto_namedtuple(path=path, rev1=rev1, rev2=rev2)
示例#17
0
def out_of_date_repo(tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    original_rev = git.head_rev(path)

    git_commit(cwd=path)
    head_rev = git.head_rev(path)

    yield auto_namedtuple(
        path=path, original_rev=original_rev, head_rev=head_rev,
    )
示例#18
0
def out_of_date_repo(tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    original_rev = git.head_rev(path)

    # Make a commit
    cmd_output('git', 'commit', '--allow-empty', '-m', 'foo', cwd=path)
    head_rev = git.head_rev(path)

    yield auto_namedtuple(
        path=path, original_rev=original_rev, head_rev=head_rev,
    )
示例#19
0
def hook_disappearing_repo(tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    original_rev = git.head_rev(path)

    shutil.copy(
        get_resource_path('manifest_without_foo.yaml'),
        os.path.join(path, C.MANIFEST_FILE),
    )
    cmd_output('git', 'add', '.', cwd=path)
    cmd_output('git', 'commit', '-m', 'Remove foo', cwd=path)

    yield auto_namedtuple(path=path, original_rev=original_rev)
示例#20
0
def out_of_date_repo(tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    original_sha = git.head_sha(path)

    # Make a commit
    with cwd(path):
        cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
    head_sha = git.head_sha(path)

    yield auto_namedtuple(
        path=path, original_sha=original_sha, head_sha=head_sha,
    )
def sub_staged(submodule_with_commits, tmpdir_factory):
    path = git_dir(tmpdir_factory)
    with cwd(path):
        cmd_output(
            'git', 'submodule', 'add', submodule_with_commits.path, 'sub',
        )
        checkout_submodule(submodule_with_commits.sha1)
        cmd_output('git', 'add', 'sub')
        yield auto_namedtuple(
            path=path,
            sub_path=os.path.join(path, 'sub'),
            submodule=submodule_with_commits,
        )
示例#22
0
def hook_disappearing_repo(tempdir_factory):
    path = make_repo(tempdir_factory, 'python_hooks_repo')
    original_sha = git.head_sha(path)

    with cwd(path):
        shutil.copy(
            get_resource_path('manifest_without_foo.yaml'),
            C.MANIFEST_FILE,
        )
        cmd_output('git', 'add', '.')
        cmd_output('git', 'commit', '-m', 'Remove foo')

    yield auto_namedtuple(path=path, original_sha=original_sha)
示例#23
0
def mock_commands():
    with mock.patch.object(main, 'autoupdate') as autoupdate_mock:
        with mock.patch.object(main, 'clean') as clean_mock:
            with mock.patch.object(main, 'install') as install_mock:
                with mock.patch.object(main, 'uninstall') as uninstall_mock:
                    with mock.patch.object(main, 'run') as run_mock:
                        yield auto_namedtuple(
                            autoupdate_mock=autoupdate_mock,
                            clean_mock=clean_mock,
                            install_mock=install_mock,
                            uninstall_mock=uninstall_mock,
                            run_mock=run_mock,
                        )
示例#24
0
def setup_py_repo(tmpdir):
    src_repo = tmpdir.join('hook_repo')
    init_repo(src_repo)
    src_repo.join('setup.cfg').write('[bdist_wheel]\nuniversal = true\n')
    write_file_commit(
        src_repo, 'setup.py',
        'from setuptools import setup\n'
        'setup(name="pkg", version="1.0")\n',
    )

    update_repo = tmpdir.join('update_repo')
    subprocess.check_call(('git', 'clone', src_repo, update_repo))

    return auto_namedtuple(src_repo=src_repo, update_repo=update_repo)
示例#25
0
def sub_staged(repo_with_commits, tempdir_factory):
    path = git_dir(tempdir_factory)
    with cwd(path):
        open('bar', 'a+').close()
        cmd_output('git', 'add', 'bar')
        git_commit()
        cmd_output(
            'git', 'submodule', 'add', repo_with_commits.path, 'sub',
        )
        checkout_submodule(repo_with_commits.rev1)
        cmd_output('git', 'add', 'sub')
        yield auto_namedtuple(
            path=path,
            sub_path=os.path.join(path, 'sub'),
            submodule=repo_with_commits,
        )
示例#26
0
def autoupdatable(tmpdir):
    hook_repo = tmpdir.join('hook_repo')
    init_repo(hook_repo)
    hook_repo.join('.pre-commit-hooks.yaml').write(
        '-   id: hook\n'
        '    name: Hook\n'
        '    entry: echo hi\n'
        '    language: system\n'
        '    types: [file]\n', )
    subprocess.check_call(('git', '-C', hook_repo, 'add', '.'))
    subprocess.check_call((
        'git',
        '-C',
        hook_repo,
        'commit',
        '-m',
        'add hook',
    ))
    rev = revparse(hook_repo)
    subprocess.check_call(('git', '-C', hook_repo, 'tag', 'v1'))

    consuming_repo = tmpdir.join('consuming')
    init_repo(consuming_repo)
    consuming_repo.join('.pre-commit-config.yaml').write(
        f'-   repo: {hook_repo}\n'
        f'    rev: {rev}\n'
        f'    hooks:\n'
        f'    -   id: hook\n', )
    subprocess.check_call(('git', '-C', consuming_repo, 'add', '.'))
    subprocess.check_call((
        'git',
        '-C',
        consuming_repo,
        'commit',
        '-m',
        'consume hook',
    ))

    update_repo = tmpdir.join('update_repo')
    subprocess.check_call(('git', 'clone', consuming_repo, update_repo))

    return auto_namedtuple(
        hook_repo=hook_repo,
        hook_repo_rev=rev,
        consuming_repo=consuming_repo,
        update_repo=update_repo,
    )
def fake_autoupdatable(tmpdir):
    repo = tmpdir.join('autoupdatable')
    update_repo = tmpdir.join('autoupdatable_clone')
    git.init_repo(repo)

    def init(contents):
        repo.join('.pre-commit-config.yaml').write(contents)
        git.commit(repo)
        subprocess.check_call(('git', 'clone', repo, update_repo))

    with mock.patch.object(_pre_commit_hook_migrate, 'autoupdate'):
        with mock.patch.object(pre_commit_flake8_migrate, 'check_fix'):
            yield auto_namedtuple(
                init=init,
                repo=repo,
                update_repo=update_repo,
            )
示例#28
0
def _get_opts(
        all_files=False,
        files=(),
        color=False,
        verbose=False,
        hook=None,
        no_stash=False,
):
    # These are mutually exclusive
    assert not (all_files and files)
    return auto_namedtuple(
        all_files=all_files,
        files=files,
        color=color,
        verbose=verbose,
        hook=hook,
        no_stash=no_stash,
    )
示例#29
0
def run_opts(
    all_files=False,
    files=(),
    color=False,
    verbose=False,
    hook=None,
    remote_branch='',
    local_branch='',
    from_ref='',
    to_ref='',
    remote_name='',
    remote_url='',
    hook_stage='commit',
    show_diff_on_failure=False,
    commit_msg_filename='',
    prepare_commit_message_source='',
    commit_object_name='',
    checkout_type='',
    is_squash_merge='',
    rewrite_command='',
):
    # These are mutually exclusive
    assert not (all_files and files)
    return auto_namedtuple(
        all_files=all_files,
        files=files,
        color=color,
        verbose=verbose,
        hook=hook,
        remote_branch=remote_branch,
        local_branch=local_branch,
        from_ref=from_ref,
        to_ref=to_ref,
        remote_name=remote_name,
        remote_url=remote_url,
        hook_stage=hook_stage,
        show_diff_on_failure=show_diff_on_failure,
        commit_msg_filename=commit_msg_filename,
        prepare_commit_message_source=prepare_commit_message_source,
        commit_object_name=commit_object_name,
        checkout_type=checkout_type,
        is_squash_merge=is_squash_merge,
        rewrite_command=rewrite_command,
    )
示例#30
0
def mock_commands():
    mcks = {fn: mock.patch.object(main, fn).start() for fn in FNS}
    ret = auto_namedtuple(**mcks)
    yield ret
    for mck in ret:
        mck.stop()