def test_clone(store, tmpdir_factory, log_info_mock): path = git_dir(tmpdir_factory) with local.cwd(path): local['git']('commit', '--allow-empty', '-m', 'foo') sha = get_head_sha(path) local['git']('commit', '--allow-empty', '-m', 'bar') ret = store.clone(path, sha) # Should have printed some stuff log_info_mock.assert_called_with('This may take a few minutes...') # Should return a directory inside of the store assert os.path.exists(ret) assert ret.startswith(store.directory) # Directory should start with `repo` _, dirname = os.path.split(ret) assert dirname.startswith('repo') # Should be checked out to the sha we specified assert get_head_sha(ret) == sha # Assert that we made a symlink from the sha to the repo sha_path = os.path.join(store.directory, sha + '_' + hex_md5(path)) assert os.path.exists(sha_path) assert os.path.islink(sha_path) assert os.readlink(sha_path) == ret
def test_clone(store, tempdir_factory, log_info_mock): path = git_dir(tempdir_factory) with cwd(path): cmd_output('git', 'commit', '--allow-empty', '-m', 'foo') sha = get_head_sha(path) cmd_output('git', 'commit', '--allow-empty', '-m', 'bar') ret = store.clone(path, sha) # Should have printed some stuff assert log_info_mock.call_args_list[0][0][0].startswith( 'Initializing environment for ' ) # Should return a directory inside of the store assert os.path.exists(ret) assert ret.startswith(store.directory) # Directory should start with `repo` _, dirname = os.path.split(ret) assert dirname.startswith('repo') # Should be checked out to the sha we specified assert get_head_sha(ret) == sha # Assert there's an entry in the sqlite db for this with sqlite3.connect(store.db_path) as db: path, = db.execute( 'SELECT path from repos WHERE repo = ? and ref = ?', [path, sha], ).fetchone() assert path == ret
def test_clone(store, tmpdir_factory, log_info_mock): path = git_dir(tmpdir_factory) with cwd(path): cmd_output('git', 'commit', '--allow-empty', '-m', 'foo') sha = get_head_sha(path) cmd_output('git', 'commit', '--allow-empty', '-m', 'bar') ret = store.clone(path, sha) # Should have printed some stuff log_info_mock.assert_called_with('This may take a few minutes...') # Should return a directory inside of the store assert os.path.exists(ret) assert ret.startswith(store.directory) # Directory should start with `repo` _, dirname = os.path.split(ret) assert dirname.startswith('repo') # Should be checked out to the sha we specified assert get_head_sha(ret) == sha # Assert that we made a symlink from the sha to the repo sha_path = os.path.join(store.directory, sha + '_' + hex_md5(path)) assert os.path.exists(sha_path) assert os.path.islink(sha_path) assert os.readlink(sha_path) == ret
def test_autoupdate_old_revision_broken( tempdir_factory, in_tmpdir, mock_out_store_directory, ): """In $FUTURE_VERSION, hooks.yaml will no longer be supported. This asserts that when that day comes, pre-commit will be able to autoupdate despite not being able to read hooks.yaml in that repository. """ path = make_repo(tempdir_factory, 'python_hooks_repo') config = make_config_from_repo(path, check=False) with cwd(path): cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml') cmd_output('git', 'commit', '-m', 'simulate old repo') # Assume this is the revision the user's old repository was at rev = get_head_sha(path) cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE) cmd_output('git', 'commit', '-m', 'move hooks file') update_rev = get_head_sha(path) config['sha'] = rev write_config('.', config) before = open(C.CONFIG_FILE).read() ret = autoupdate(Runner('.', C.CONFIG_FILE), tags_only=False) after = open(C.CONFIG_FILE).read() assert ret == 0 assert before != after assert update_rev in after
def out_of_date_repo(tmpdir_factory): path = make_repo(tmpdir_factory, 'python_hooks_repo') original_sha = get_head_sha(path) # Make a commit with cwd(path): cmd_output('git', 'commit', '--allow-empty', '-m', 'foo') head_sha = get_head_sha(path) yield auto_namedtuple( path=path, original_sha=original_sha, head_sha=head_sha, )
def out_of_date_repo(tmpdir_factory): path = make_repo(tmpdir_factory, 'python_hooks_repo') original_sha = get_head_sha(path) # Make a commit with local.cwd(path): local['git']['commit', '--allow-empty', '-m', 'foo']() head_sha = get_head_sha(path) yield auto_namedtuple( path=path, original_sha=original_sha, head_sha=head_sha, )
def make_config_from_repo( repo_path, sha=None, hooks=None, check=True, legacy=False, ): filename = C.MANIFEST_FILE_LEGACY if legacy else C.MANIFEST_FILE manifest = load_manifest(os.path.join(repo_path, filename)) config = OrderedDict(( ('repo', repo_path), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']), )) for hook in manifest], ), )) if check: wrapped = validate([config], CONFIG_SCHEMA) config, = apply_defaults(wrapped, CONFIG_SCHEMA) return config else: return config
def test_make_archive(tempdir_factory): output_dir = tempdir_factory.get() git_path = git_dir(tempdir_factory) # Add a files to the git directory with cwd(git_path): open('foo', 'a').close() cmd_output('git', 'add', '.') cmd_output('git', 'commit', '-m', 'foo') # We'll use this sha head_sha = get_head_sha('.') # And check that this file doesn't exist open('bar', 'a').close() cmd_output('git', 'add', '.') cmd_output('git', 'commit', '-m', 'bar') # Do the thing archive_path = make_archives.make_archive( 'foo', git_path, head_sha, output_dir, ) assert archive_path == os.path.join(output_dir, 'foo.tar.gz') assert os.path.exists(archive_path) extract_dir = tempdir_factory.get() # Extract the tar with tarfile.open(archive_path) as tf: tf.extractall(extract_dir) # Verify the contents of the tar assert os.path.exists(os.path.join(extract_dir, 'foo')) assert os.path.exists(os.path.join(extract_dir, 'foo', 'foo')) assert not os.path.exists(os.path.join(extract_dir, 'foo', '.git')) assert not os.path.exists(os.path.join(extract_dir, 'foo', 'bar'))
def test_make_archive(tmpdir_factory): output_dir = tmpdir_factory.get() git_path = git_dir(tmpdir_factory) # Add a files to the git directory with local.cwd(git_path): local['touch']('foo') local['git']('add', '.') local['git']('commit', '-m', 'foo') # We'll use this sha head_sha = get_head_sha('.') # And check that this file doesn't exist local['touch']('bar') local['git']('add', '.') local['git']('commit', '-m', 'bar') # Do the thing archive_path = make_archives.make_archive( 'foo', git_path, head_sha, output_dir, ) assert archive_path == os.path.join(output_dir, 'foo.tar.gz') assert os.path.exists(archive_path) extract_dir = tmpdir_factory.get() # Extract the tar with tarfile_open(archive_path) as tf: tf.extractall(extract_dir) # Verify the contents of the tar assert os.path.exists(os.path.join(extract_dir, 'foo')) assert os.path.exists(os.path.join(extract_dir, 'foo', 'foo')) assert not os.path.exists(os.path.join(extract_dir, 'foo', '.git')) assert not os.path.exists(os.path.join(extract_dir, 'foo', 'bar'))
def test_default_python_language_version(store, tempdir_factory): path = make_repo(tempdir_factory, 'python_hooks_repo') repo_path = store.clone(path, get_head_sha(path)) manifest = Manifest(repo_path, path) # This assertion is difficult as it is version dependent, just assert # that it is *something* assert manifest.hooks['foo']['language_version'] != 'default'
def hook_disappearing_repo(tempdir_factory): path = make_repo(tempdir_factory, 'python_hooks_repo') original_sha = get_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)
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)
def test_legacy_manifest_warn(store, tempdir_factory, log_warning_mock): path = make_repo(tempdir_factory, 'legacy_hooks_yaml_repo') head_sha = get_head_sha(path) repo_path = store.clone(path, head_sha) Manifest(repo_path, path).manifest_contents # Should have printed a warning assert log_warning_mock.call_args_list[0][0][0] == ( '{} uses legacy hooks.yaml to provide hooks.\n' 'In newer versions, this file is called .pre-commit-hooks.yaml\n' 'This will work in this version of pre-commit but will be removed at ' 'a later time.\n' 'If `pre-commit autoupdate` does not silence this warning consider ' 'making an issue / pull request.'.format(path))
def test_legacy_manifest_warn(store, tempdir_factory, log_warning_mock): path = make_repo(tempdir_factory, 'legacy_hooks_yaml_repo') head_sha = get_head_sha(path) repo_path = store.clone(path, head_sha) Manifest(repo_path, path).manifest_contents # Should have printed a warning assert log_warning_mock.call_args_list[0][0][0] == ( '{} uses legacy hooks.yaml to provide hooks.\n' 'In newer versions, this file is called .pre-commit-hooks.yaml\n' 'This will work in this version of pre-commit but will be removed at ' 'a later time.\n' 'If `pre-commit autoupdate` does not silence this warning consider ' 'making an issue / pull request.'.format(path) )
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True): manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) config = OrderedDict(( ('repo', repo_path), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], ), )) if check: wrapped_config = apply_defaults([config], CONFIG_JSON_SCHEMA) validate_config_extra(wrapped_config) return wrapped_config[0] else: return config
def make_config_from_repo(repo_path, sha=None, hooks=None, check=True): manifest = load_manifest(os.path.join(repo_path, C.MANIFEST_FILE)) config = OrderedDict(( ('repo', 'file://{}'.format(repo_path)), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], ), )) if check: wrapped = validate({'repos': [config]}, CONFIG_SCHEMA) wrapped = apply_defaults(wrapped, CONFIG_SCHEMA) config, = wrapped['repos'] return config else: return config
def make_config_from_repo( repo_path, sha=None, hooks=None, check=True, legacy=False, ): filename = C.MANIFEST_FILE_LEGACY if legacy else C.MANIFEST_FILE manifest = load_manifest(os.path.join(repo_path, filename)) config = OrderedDict(( ('repo', repo_path), ('sha', sha or get_head_sha(repo_path)), ( 'hooks', hooks or [OrderedDict((('id', hook['id']),)) for hook in manifest], ), )) if check: wrapped = validate([config], CONFIG_SCHEMA) config, = apply_defaults(wrapped, CONFIG_SCHEMA) return config else: return config
def manifest(store, tempdir_factory): path = make_repo(tempdir_factory, 'script_hooks_repo') head_sha = get_head_sha(path) repo_path = store.clone(path, head_sha) yield Manifest(repo_path, path)
def manifest(store, tmpdir_factory): path = make_repo(tmpdir_factory, 'script_hooks_repo') head_sha = get_head_sha(path) repo_path_getter = store.get_repo_path_getter(path, head_sha) yield Manifest(repo_path_getter)