def test_img_conflict(img_staged, cmd_runner):
    """Admittedly, this shouldn't happen, but just in case."""
    shutil.copy(get_resource_path('img2.jpg'), img_staged.img_filename)

    _test_img_state(img_staged, 'img2.jpg', 'AM')

    with staged_files_only(cmd_runner):
        _test_img_state(img_staged)
        shutil.copy(get_resource_path('img3.jpg'), img_staged.img_filename)
        _test_img_state(img_staged, 'img3.jpg', 'AM')

    _test_img_state(img_staged, 'img2.jpg', 'AM')
def img_staged(tmpdir_factory):
    path = git_dir(tmpdir_factory)
    with local.cwd(path):
        img_filename = os.path.join(path, 'img.jpg')
        shutil.copy(get_resource_path('img1.jpg'), img_filename)
        local['git']('add', 'img.jpg')
        yield auto_namedtuple(path=path, img_filename=img_filename)
Beispiel #3
0
def make_repo(tmpdir_factory, repo_source):
    path = git_dir(tmpdir_factory)
    copy_tree_to_path(get_resource_path(repo_source), path)
    with local.cwd(path):
        git('add', '.')
        git('commit', '-m', 'Add hooks')
    return path
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)
def _test_img_state(path, expected_file='img1.jpg', status='A'):
    assert os.path.exists(path.img_filename)
    with io.open(path.img_filename, 'rb') as f1:
        with io.open(get_resource_path(expected_file), 'rb') as f2:
            assert f1.read() == f2.read()
    actual_status = get_short_git_status()['img.jpg']
    assert status == actual_status
Beispiel #6
0
def make_repo(tempdir_factory, repo_source):
    path = git_dir(tempdir_factory)
    copy_tree_to_path(get_resource_path(repo_source), path)
    with cwd(path):
        cmd_output('git', 'add', '.')
        cmd_output('git', 'commit', '-m', 'Add hooks')
    return path
def test_detect_aws_credentials(filename, expected_retval):
    # with a valid credentials file
    ret = main((
        get_resource_path(filename),
        '--credentials-file',
        'testing/resources/aws_config_with_multiple_sections.ini',
    ))
    assert ret == expected_retval
def test_img_something_unstaged(img_staged, cmd_runner):
    shutil.copy(get_resource_path('img2.jpg'), img_staged.img_filename)

    _test_img_state(img_staged, 'img2.jpg', 'AM')

    with staged_files_only(cmd_runner):
        _test_img_state(img_staged)

    _test_img_state(img_staged, 'img2.jpg', 'AM')
def test_detect_aws_credentials(filename, expected_retval):
    """Test if getting configured AWS secrets from files to be checked in works."""

    # with a valid credentials file
    ret = main((
        get_resource_path(filename),
        "--credentials-file=testing/resources/aws_config_with_multiple_sections.ini",
    ))
    assert ret == expected_retval
def test_non_existent_credentials_with_allow_flag(mock_secrets_env, mock_secrets_file):
    """Test behavior with no configured AWS secrets and flag to allow when missing."""
    mock_secrets_env.return_value = set()
    mock_secrets_file.return_value = set()
    ret = main((
        get_resource_path('aws_config_without_secrets.ini'),
        "--credentials-file=testing/resources/credentailsfilethatdoesntexist",
        "--allow-missing-credentials",
    ))
    assert ret == 0
def test_non_existent_credentials_with_allow_flag(
        mock_secrets_env, mock_secrets_file,
):
    mock_secrets_env.return_value = set()
    mock_secrets_file.return_value = set()
    ret = main((
        get_resource_path('aws_config_without_secrets.ini'),
        '--credentials-file=testing/resources/credentailsfilethatdoesntexist',
        '--allow-missing-credentials',
    ))
    assert ret == 0
def test_non_existent_credentials(capsys):
    # with a non-existent credentials file
    ret = main(
        (get_resource_path("with_secrets.txt"), "--credentials-file=testing/resources/credentailsfilethatdoesntexist")
    )
    assert ret == 2
    out, _ = capsys.readouterr()
    assert out == (
        "No aws keys were configured at "
        "testing/resources/credentailsfilethatdoesntexist\n"
        "Configure them with --credentials-file\n"
    )
Beispiel #13
0
def hook_disappearing_repo(tmpdir_factory):
    path = make_repo(tmpdir_factory, 'python_hooks_repo')
    original_sha = get_head_sha(path)

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

    yield auto_namedtuple(path=path, original_sha=original_sha)
Beispiel #14
0
def test_local_python_repo(store):
    # Make a "local" hooks repo that just installs our other hooks repo
    repo_path = get_resource_path('python_hooks_repo')
    manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE))
    hooks = [
        dict(hook, additional_dependencies=[repo_path]) for hook in manifest
    ]
    config = {'repo': 'local', 'hooks': hooks}
    repo = Repository.create(config, store)
    (_, hook), = repo.hooks
    ret = repo.run_hook(hook, ('filename',))
    assert ret[0] == 0
    assert ret[1].replace(b'\r\n', b'\n') == b"['filename']\nHello World\n"
def test_autofix_pretty_format_json(tmpdir):
    srcfile = tmpdir.join('to_be_json_formatted.json')
    with io.open(get_resource_path('not_pretty_formatted_json.json')) as f:
        srcfile.write_text(f.read(), 'UTF-8')

    # now launch the autofix on that file
    ret = pretty_format_json(['--autofix', srcfile.strpath])
    # it should have formatted it
    assert ret == 1

    # file was formatted (shouldn't trigger linter again)
    ret = pretty_format_json([srcfile.strpath])
    assert ret == 0
Beispiel #16
0
def test_failing_file():
    ret = check_ast([get_resource_path('cannot_parse_ast.notpy')])
    assert ret == 1
def test_check_yaml(filename, expected_retval):
    ret = check_yaml([get_resource_path(filename)])
    assert ret == expected_retval
def test_pretty_format_json(filename, expected_retval):
    ret = pretty_format_json([get_resource_path(filename)])
    assert ret == expected_retval
def test_badfile_pretty_format_json():
    ret = pretty_format_json([get_resource_path('ok_yaml.yaml')])
    assert ret == 1
Beispiel #20
0
def make_repo(tempdir_factory, repo_source):
    path = git_dir(tempdir_factory)
    copy_tree_to_path(get_resource_path(repo_source), path)
    cmd_output('git', 'add', '.', cwd=path)
    git_commit(msg=make_repo.__name__, cwd=path)
    return path
Beispiel #21
0
def test_main(capsys, filename, expected_retval):
    ret = main([get_resource_path(filename)])
    assert ret == expected_retval
    if expected_retval == 1:
        stdout, _ = capsys.readouterr()
        assert filename in stdout
def test_ignores_binary_files():
    shutil.copy(get_resource_path('img1.jpg'), 'f1')
    assert main(['f1']) == 0
Beispiel #23
0
def test_ignores_binary_files():
    shutil.copy(get_resource_path('img1.jpg'), 'f1')
    assert detect_merge_conflict(['f1']) == 0
import pytest

from pre_commit.clientlib.validate_manifest import additional_manifest_check
from pre_commit.clientlib.validate_manifest import InvalidManifestError
from pre_commit.clientlib.validate_manifest import MANIFEST_JSON_SCHEMA
from pre_commit.clientlib.validate_manifest import run
from testing.util import get_resource_path
from testing.util import is_valid_according_to_schema


@pytest.mark.parametrize(
    ('input', 'expected_output'),
    (
        (['.pre-commit-hooks.yaml'], 0),
        (['non_existent_file.yaml'], 1),
        ([get_resource_path('valid_yaml_but_invalid_manifest.yaml')], 1),
        ([get_resource_path('non_parseable_yaml_file.notyaml')], 1),
    ),
)
def test_run(input, expected_output):
    assert run(input) == expected_output


def test_additional_manifest_check_raises_for_bad_language():
    with pytest.raises(InvalidManifestError):
        additional_manifest_check([{'id': 'foo', 'language': 'not valid'}])


@pytest.mark.parametrize(
    'obj',
    (