def test_lots_of_files(mock_out_store_directory, tempdir_factory): # windows xargs seems to have a bug, here's a regression test for # our workaround git_path = make_consuming_repo(tempdir_factory, 'python_hooks_repo') with cwd(git_path): # Override files so we run against them with modify_config() as config: config[0]['hooks'][0]['files'] = '' # Write a crap ton of files for i in range(400): filename = '{0}{1}'.format('a' * 100, i) open(filename, 'w').close() cmd_output('bash', '-c', 'git add .') install(Runner(git_path)) # Don't want to write to home directory env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get()) cmd_output( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, env=env, )
def aliased_repo(tempdir_factory): git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(git_path): with modify_config() as config: config['repos'][0]['hooks'].append( {'id': 'bash_hook', 'alias': 'foo_bash'}, ) stage_a_file() yield git_path
def test_fail_fast(cap_out, store, repo_with_failing_hook): with modify_config() as config: # More than one hook config['fail_fast'] = True config['repos'][0]['hooks'] *= 2 stage_a_file() ret, printed = _do_run(cap_out, store, repo_with_failing_hook, run_opts()) # it should have only run one hook assert printed.count(b'Failing hook') == 1
def test_multiple_hooks_same_id(cap_out, store, repo_with_passing_hook): with cwd(repo_with_passing_hook): # Add bash hook on there again with modify_config() as config: config['repos'][0]['hooks'].append({'id': 'bash_hook'}) stage_a_file() ret, output = _do_run(cap_out, store, repo_with_passing_hook, run_opts()) assert ret == 0 assert output.count(b'Bash hook') == 2
def aliased_repo(tempdir_factory): git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(git_path): with modify_config() as config: config['repos'][0]['hooks'].append( { 'id': 'bash_hook', 'alias': 'foo_bash' }, ) stage_a_file() yield git_path
def test_always_run(cap_out, store, repo_with_passing_hook): with modify_config() as config: config['repos'][0]['hooks'][0]['always_run'] = True _test_run( cap_out, store, repo_with_passing_hook, {}, (b'Bash hook', b'Passed'), 0, stage=False, )
def test_multiple_hooks_same_id( repo_with_passing_hook, mock_out_store_directory, ): with cwd(repo_with_passing_hook): # Add bash hook on there again with modify_config() as config: config[0]['hooks'].append({'id': 'bash_hook'}) stage_a_file() ret, output = _do_run(repo_with_passing_hook, _get_opts()) assert ret == 0 assert output.count(b'Bash hook') == 2
def test_pass_filenames( cap_out, store, repo_with_passing_hook, pass_filenames, hook_args, expected_out, ): with modify_config() as config: config['repos'][0]['hooks'][0]['pass_filenames'] = pass_filenames config['repos'][0]['hooks'][0]['args'] = hook_args stage_a_file() ret, printed = _do_run( cap_out, store, repo_with_passing_hook, run_opts(verbose=True), ) assert expected_out + b'\nHello World' in printed assert (b'foo.py' in printed) == pass_filenames
def test_global_exclude(cap_out, tempdir_factory, mock_out_store_directory): git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(git_path): with modify_config() as config: config['exclude'] = '^foo.py$' open('foo.py', 'a').close() open('bar.py', 'a').close() cmd_output('git', 'add', '.') ret, printed = _do_run(cap_out, git_path, _get_opts(verbose=True)) assert ret == 0 # Does not contain foo.py since it was excluded expected = b'hookid: bash_hook\n\nbar.py\nHello World\n\n' assert printed.endswith(expected)
def test_global_exclude(cap_out, tempdir_factory, mock_out_store_directory): git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(git_path): with modify_config() as config: config['exclude'] = '^foo.py$' open('foo.py', 'a').close() open('bar.py', 'a').close() cmd_output('git', 'add', '.') ret, printed = _do_run(cap_out, git_path, run_opts(verbose=True)) assert ret == 0 # Does not contain foo.py since it was excluded expected = b'hookid: bash_hook\n\nbar.py\nHello World\n\n' assert printed.endswith(expected)
def test_hook_verbose_enabled(cap_out, store, repo_with_passing_hook): with modify_config() as config: config['repos'][0]['hooks'][0]['always_run'] = True config['repos'][0]['hooks'][0]['verbose'] = True _test_run( cap_out, store, repo_with_passing_hook, {}, (b'Hello World',), 0, stage=False, )
def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory): with cwd(repo_with_failing_hook): with modify_config() as config: config['repos'][0]['hooks'][0]['args'] = ['☃'] stage_a_file() install(C.CONFIG_FILE, store, hook_types=['pre-commit']) # Have to use subprocess because pytest monkeypatches sys.stdout _, out = git_commit( fn=cmd_output_mocked_pre_commit_home, tempdir_factory=tempdir_factory, retcode=None, ) assert 'UnicodeEncodeError' not in out # Doesn't actually happen, but a reasonable assertion assert 'UnicodeDecodeError' not in out
def test_stdout_write_bug_py26(repo_with_failing_hook, store, tempdir_factory): with cwd(repo_with_failing_hook): with modify_config() as config: config['repos'][0]['hooks'][0]['args'] = ['☃'] stage_a_file() install(C.CONFIG_FILE, store) # Have to use subprocess because pytest monkeypatches sys.stdout _, stdout, _ = git_commit( fn=cmd_output_mocked_pre_commit_home, # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, retcode=None, tempdir_factory=tempdir_factory, ) assert 'UnicodeEncodeError' not in stdout # Doesn't actually happen, but a reasonable assertion assert 'UnicodeDecodeError' not in stdout
def test_lots_of_files(store, tempdir_factory): # windows xargs seems to have a bug, here's a regression test for # our workaround git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(git_path): # Override files so we run against them with modify_config() as config: config['repos'][0]['hooks'][0]['files'] = '' # Write a crap ton of files for i in range(400): open(f'{"a" * 100}{i}', 'w').close() cmd_output('git', 'add', '.') install(C.CONFIG_FILE, store, hook_types=['pre-commit']) git_commit( fn=cmd_output_mocked_pre_commit_home, tempdir_factory=tempdir_factory, )
def test_stdout_write_bug_py26( repo_with_failing_hook, mock_out_store_directory, tempdir_factory, ): with cwd(repo_with_failing_hook): with modify_config() as config: config[0]['hooks'][0]['args'] = ['☃'] stage_a_file() install(Runner(repo_with_failing_hook, C.CONFIG_FILE)) # Have to use subprocess because pytest monkeypatches sys.stdout _, stdout, _ = cmd_output_mocked_pre_commit_home( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, retcode=None, tempdir_factory=tempdir_factory, ) assert 'UnicodeEncodeError' not in stdout # Doesn't actually happen, but a reasonable assertion assert 'UnicodeDecodeError' not in stdout
def test_gc_config_with_missing_hook( tempdir_factory, store, in_git_dir, cap_out, ): path = make_repo(tempdir_factory, 'script_hooks_repo') write_config('.', make_config_from_repo(path)) store.mark_config_used(C.CONFIG_FILE) # to trigger a clone all_hooks(load_config(C.CONFIG_FILE), store) with modify_config() as config: # add a hook which does not exist, make sure we don't crash config['repos'][0]['hooks'].append({'id': 'does-not-exist'}) assert _config_count(store) == 1 assert _repo_count(store) == 1 assert not gc(store) assert _config_count(store) == 1 assert _repo_count(store) == 1 assert cap_out.get().splitlines()[-1] == '0 repo(s) removed.' _remove_config_assert_cleared(store, cap_out)
def test_lots_of_files(mock_out_store_directory, tempdir_factory): # windows xargs seems to have a bug, here's a regression test for # our workaround git_path = make_consuming_repo(tempdir_factory, 'python_hooks_repo') with cwd(git_path): # Override files so we run against them with modify_config() as config: config[0]['hooks'][0]['files'] = '' # Write a crap ton of files for i in range(400): filename = '{}{}'.format('a' * 100, i) open(filename, 'w').close() cmd_output('bash', '-c', 'git add .') install(Runner(git_path, C.CONFIG_FILE)) cmd_output_mocked_pre_commit_home( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, tempdir_factory=tempdir_factory, )
def test_lots_of_files(store, tempdir_factory): # windows xargs seems to have a bug, here's a regression test for # our workaround git_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(git_path): # Override files so we run against them with modify_config() as config: config['repos'][0]['hooks'][0]['files'] = '' # Write a crap ton of files for i in range(400): filename = '{}{}'.format('a' * 100, i) open(filename, 'w').close() cmd_output('git', 'add', '.') install(C.CONFIG_FILE, store) git_commit( fn=cmd_output_mocked_pre_commit_home, # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, tempdir_factory=tempdir_factory, )
def test_stdout_write_bug_py26( repo_with_failing_hook, mock_out_store_directory, tempdir_factory, ): with cwd(repo_with_failing_hook): with modify_config() as config: config[0]['hooks'][0]['args'] = ['☃'] stage_a_file() install(Runner(repo_with_failing_hook)) # Don't want to write to home directory env = dict(os.environ, PRE_COMMIT_HOME=tempdir_factory.get()) # Have to use subprocess because pytest monkeypatches sys.stdout _, stdout, _ = cmd_output( 'git', 'commit', '-m', 'Commit!', # git commit puts pre-commit to stderr stderr=subprocess.STDOUT, env=env, retcode=None, ) assert 'UnicodeEncodeError' not in stdout # Doesn't actually happen, but a reasonable assertion assert 'UnicodeDecodeError' not in stdout
def modified_config_repo(repo_with_passing_hook): with modify_config(repo_with_passing_hook, commit=False) as config: # Some minor modification config[0]['hooks'][0]['files'] = '' yield repo_with_passing_hook
def test_always_run(repo_with_passing_hook, mock_out_store_directory): with modify_config() as config: config[0]['hooks'][0]['always_run'] = True _test_run(repo_with_passing_hook, {}, (b'Bash hook', b'Passed'), 0, False)
def modified_config_repo(repo_with_passing_hook): with modify_config(repo_with_passing_hook, commit=False) as config: # Some minor modification config['repos'][0]['hooks'][0]['files'] = '' yield repo_with_passing_hook