def in_conflicting_submodule(tempdir_factory): git_dir_1 = git_dir(tempdir_factory) git_dir_2 = git_dir(tempdir_factory) git_commit(msg=in_conflicting_submodule.__name__, cwd=git_dir_2) cmd_output('git', 'submodule', 'add', git_dir_2, 'sub', cwd=git_dir_1) with cwd(os.path.join(git_dir_1, 'sub')): _make_conflict() yield
def in_conflicting_submodule(tempdir_factory): git_dir_1 = git_dir(tempdir_factory) git_dir_2 = git_dir(tempdir_factory) with cwd(git_dir_2): cmd_output('git', 'commit', '--allow-empty', '-m', 'init!') with cwd(git_dir_1): cmd_output('git', 'submodule', 'add', git_dir_2, 'sub') with cwd(os.path.join(git_dir_1, 'sub')): _make_conflict() yield
def test_install_hooks_directory_not_present(tmpdir_factory): path = git_dir(tmpdir_factory) # Simulate some git clients which don't make .git/hooks #234 shutil.rmtree(os.path.join(path, '.git', 'hooks')) runner = Runner(path) install(runner) assert os.path.exists(runner.pre_commit_path)
def test_clone_shallow_failure_fallback_to_complete( store, tempdir_factory, log_info_mock, ): path = git_dir(tempdir_factory) with cwd(path): git_commit() rev = git.head_rev(path) git_commit() # Force shallow clone failure def fake_shallow_clone(self, *args, **kwargs): raise CalledProcessError(None, None, None) store._shallow_clone = fake_shallow_clone ret = store.clone(path, rev) # 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 rev we specified assert git.head_rev(ret) == rev # Assert there's an entry in the sqlite db for this assert store.select_all_repos() == [(path, rev, ret)]
def test_install_pre_commit(tempdir_factory): path = git_dir(tempdir_factory) runner = Runner(path, C.CONFIG_FILE) ret = install(runner) assert ret == 0 assert os.path.exists(runner.pre_commit_path) pre_commit_contents = io.open(runner.pre_commit_path).read() pre_commit_script = resource_filename('hook-tmpl') expected_contents = io.open(pre_commit_script).read().format( sys_executable=pipes.quote(sys.executable), hook_type='pre-commit', hook_specific='', config_file=runner.config_file, skip_on_missing_conf='false', ) assert pre_commit_contents == expected_contents assert os.access(runner.pre_commit_path, os.X_OK) ret = install(runner, hook_type='pre-push') assert ret == 0 assert os.path.exists(runner.pre_push_path) pre_push_contents = io.open(runner.pre_push_path).read() pre_push_tmpl = resource_filename('pre-push-tmpl') pre_push_template_contents = io.open(pre_push_tmpl).read() expected_contents = io.open(pre_commit_script).read().format( sys_executable=pipes.quote(sys.executable), hook_type='pre-push', hook_specific=pre_push_template_contents, config_file=runner.config_file, skip_on_missing_conf='false', ) assert pre_push_contents == expected_contents
def test_install_pre_commit(tmpdir_factory): path = git_dir(tmpdir_factory) runner = Runner(path) ret = install(runner) assert ret == 0 assert os.path.exists(runner.pre_commit_path) pre_commit_contents = io.open(runner.pre_commit_path).read() pre_commit_script = resource_filename('hook-tmpl') expected_contents = io.open(pre_commit_script).read().format( sys_executable=sys.executable, hook_type='pre-commit', pre_push='' ) assert pre_commit_contents == expected_contents assert os.access(runner.pre_commit_path, os.X_OK) ret = install(runner, hook_type='pre-push') assert ret == 0 assert os.path.exists(runner.pre_push_path) pre_push_contents = io.open(runner.pre_push_path).read() pre_push_tmpl = resource_filename('pre-push-tmpl') pre_push_template_contents = io.open(pre_push_tmpl).read() expected_contents = io.open(pre_commit_script).read().format( sys_executable=sys.executable, hook_type='pre-push', pre_push=pre_push_template_contents, ) assert pre_push_contents == expected_contents
def test_get_root_deeper(tempdir_factory): path = git_dir(tempdir_factory) foo_path = os.path.join(path, "foo") os.mkdir(foo_path) with cwd(foo_path): assert git.get_root() == path
def test_get_root_deeper(tmpdir_factory): path = git_dir(tmpdir_factory) foo_path = os.path.join(path, 'foo') os.mkdir(foo_path) with local.cwd(foo_path): assert git.get_root() == path
def test_get_root_deeper(tempdir_factory): path = git_dir(tempdir_factory) foo_path = os.path.join(path, 'foo') os.mkdir(foo_path) with cwd(foo_path): assert os.path.normcase(git.get_root()) == os.path.normcase(path)
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 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)
def test_local_hooks(tempdir_factory, mock_out_store_directory): config = OrderedDict(( ('repo', 'local'), ( 'hooks', ( OrderedDict(( ('id', 'arg-per-line'), ('name', 'Args per line hook'), ('entry', 'bin/hook.sh'), ('language', 'script'), ('files', ''), ('args', ['hello', 'world']), )), OrderedDict(( ('id', 'do_not_commit'), ('name', 'Block if "DO NOT COMMIT" is found'), ('entry', 'DO NOT COMMIT'), ('language', 'pcre'), ('files', '^(.*)$'), )), ), ), )) git_path = git_dir(tempdir_factory) add_config_to_repo(git_path, config) runner = Runner(git_path, C.CONFIG_FILE) assert len(runner.repositories) == 1 assert len(runner.repositories[0].hooks) == 2
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 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_local_hooks_alt_config(tempdir_factory, mock_out_store_directory): config = OrderedDict(( ('repo', 'local'), ('hooks', (OrderedDict(( ('id', 'arg-per-line'), ('name', 'Args per line hook'), ('entry', 'bin/hook.sh'), ('language', 'script'), ('files', ''), ('args', ['hello', 'world']), )), OrderedDict(( ('id', 'ugly-format-json'), ('name', 'Ugly format json'), ('entry', 'ugly-format-json'), ('language', 'python'), ('files', ''), )), OrderedDict(( ('id', 'do_not_commit'), ('name', 'Block if "DO NOT COMMIT" is found'), ('entry', 'DO NOT COMMIT'), ('language', 'pcre'), ('files', '^(.*)$'), )))) )) git_path = git_dir(tempdir_factory) alt_config_file = 'alternate_config.yaml' add_config_to_repo(git_path, config, config_file=alt_config_file) runner = Runner(git_path, alt_config_file) assert len(runner.repositories) == 1 assert len(runner.repositories[0].hooks) == 3
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_pcre_hook_matching(tempdir_factory, store): path = git_dir(tempdir_factory) with cwd(path): with io.open("herp", "w") as herp: herp.write("\nherpfoo'bard\n") with io.open("derp", "w") as derp: derp.write("[INFO] information yo\n") _test_hook_repo( tempdir_factory, store, "pcre_hooks_repo", "regex-with-quotes", ["herp", "derp"], b"herp:2:herpfoo'bard\n", expected_return_code=123, ) _test_hook_repo( tempdir_factory, store, "pcre_hooks_repo", "other-regex", ["herp", "derp"], b"derp:1:[INFO] information yo\n", expected_return_code=123, )
def prepare_commit_msg_repo(tempdir_factory): path = git_dir(tempdir_factory) script_name = 'add_sign_off.sh' config = { 'repo': 'local', 'hooks': [{ 'id': 'add-signoff', 'name': 'Add "Signed off by:"', 'entry': './{}'.format(script_name), 'language': 'script', 'stages': ['prepare-commit-msg'], }], } write_config(path, config) with cwd(path): with io.open(script_name, 'w') as script_file: script_file.write( '#!/usr/bin/env bash\n' 'set -eu\n' 'echo "\nSigned off by: " >> "$1"\n', ) make_executable(script_name) cmd_output('git', 'add', '.') git_commit(msg=prepare_commit_msg_repo.__name__) yield path
def submodule_with_commits(tmpdir_factory): path = git_dir(tmpdir_factory) with local.cwd(path): local['git']('commit', '--allow-empty', '-m', 'foo') sha1 = local['git']('rev-parse', 'HEAD').strip() local['git']('commit', '--allow-empty', '-m', 'bar') sha2 = local['git']('rev-parse', 'HEAD').strip() yield auto_namedtuple(path=path, sha1=sha1, sha2=sha2)
def test_get_staged_files_deleted(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path): open('test', 'a').close() cmd_output('git', 'add', 'test') cmd_output('git', 'commit', '-m', 'foo', '--allow-empty') cmd_output('git', 'rm', '--cached', 'test') assert git.get_staged_files() == []
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)
def foo_staged(tmpdir_factory): path = git_dir(tmpdir_factory) with local.cwd(path): with io.open('foo', 'w') as foo_file: foo_file.write(FOO_CONTENTS) local['git']('add', 'foo') foo_filename = os.path.join(path, 'foo') yield auto_namedtuple(path=path, foo_filename=foo_filename)
def test_uninstall(tmpdir_factory): path = git_dir(tmpdir_factory) runner = Runner(path) assert not os.path.exists(runner.pre_commit_path) install(runner) assert os.path.exists(runner.pre_commit_path) uninstall(runner) assert not os.path.exists(runner.pre_commit_path)
def test_install_hooks_dead_symlink( tempdir_factory, ): # pragma: no cover (non-windows) path = git_dir(tempdir_factory) os.symlink('/fake/baz', os.path.join(path, '.git', 'hooks', 'pre-commit')) runner = Runner(path) install(runner) assert os.path.exists(runner.pre_commit_path)
def submodule_with_commits(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path): cmd_output('git', 'commit', '--allow-empty', '-m', 'foo') sha1 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip() cmd_output('git', 'commit', '--allow-empty', '-m', 'bar') sha2 = cmd_output('git', 'rev-parse', 'HEAD')[1].strip() yield auto_namedtuple(path=path, sha1=sha1, sha2=sha2)
def test_cwd_of_hook(tmpdir_factory, store): # Note: this doubles as a test for `system` hooks path = git_dir(tmpdir_factory) with cwd(path): _test_hook_repo( tmpdir_factory, store, 'prints_cwd_repo', 'prints_cwd', ['-L'], _norm_pwd(path) + '\n', )
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 test_autoupdate_local_hooks(tmpdir_factory): git_path = git_dir(tmpdir_factory) config = config_with_local_hooks() path = add_config_to_repo(git_path, config) runner = Runner(path) assert autoupdate(runner) == 0 new_config_writen = load_config(runner.config_file_path) assert len(new_config_writen) == 1 assert new_config_writen[0] == config
def test_install_hooks_directory_not_present(tempdir_factory): path = git_dir(tempdir_factory) # Simulate some git clients which don't make .git/hooks #234 hooks = os.path.join(path, '.git', 'hooks') if os.path.exists(hooks): # pragma: no cover (latest git) shutil.rmtree(hooks) runner = Runner(path) install(runner) assert os.path.exists(runner.pre_commit_path)
def test_autoupdate_local_hooks(tempdir_factory): git_path = git_dir(tempdir_factory) config = config_with_local_hooks() path = add_config_to_repo(git_path, config) runner = Runner(path, C.CONFIG_FILE) assert autoupdate(runner, tags_only=False) == 0 new_config_writen = load_config(runner.config_file_path) assert len(new_config_writen['repos']) == 1 assert new_config_writen['repos'][0] == config
def _run_try_repo(tempdir_factory, **kwargs): repo = make_repo(tempdir_factory, 'modified_file_returns_zero_repo') with cwd(git_dir(tempdir_factory)): open('test-file', 'a').close() cmd_output('git', 'add', '.') assert not try_repo(try_repo_opts(repo, **kwargs))
def test_get_root_at_root(tmpdir_factory): path = git_dir(tmpdir_factory) with cwd(path): assert git.get_root() == path
def _run_try_repo(tempdir_factory, **kwargs): repo = make_repo(tempdir_factory, 'modified_file_returns_zero_repo') with cwd(git_dir(tempdir_factory)): _add_test_file() assert not try_repo(try_repo_opts(repo, **kwargs))
def test_install_refuses_core_hookspath(tempdir_factory, store): path = git_dir(tempdir_factory) with cwd(path): cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks') runner = Runner(path, C.CONFIG_FILE) assert install(runner, store)
def test_create_sets_correct_directory(tmpdir_factory): path = git_dir(tmpdir_factory) with cwd(path): runner = Runner.create() assert runner.git_root == path assert os.getcwd() == path
def test_is_not_in_merge_conflict(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path): assert git.is_in_merge_conflict() is False
def test_create_sets_correct_directory(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path): runner = Runner.create(C.CONFIG_FILE) assert os.path.normcase(runner.git_root) == os.path.normcase(path) assert os.path.normcase(os.getcwd()) == os.path.normcase(path)
def test_get_root_at_root(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path): assert os.path.normcase(git.get_root()) == os.path.normcase(path)
def test_uninstall_does_not_blow_up_when_not_there(tempdir_factory): path = git_dir(tempdir_factory) runner = Runner(path, C.CONFIG_FILE) ret = uninstall(runner) assert ret == 0