def in_env(repo_cmd_runner, language_version): envdir = os.path.join( repo_cmd_runner.prefix_dir, helpers.environment_dir(ENVIRONMENT_DIR, language_version), ) with envcontext(get_env_patch(envdir)): yield
def in_env( prefix: Prefix, language_version: str, ) -> Generator[None, None, None]: envdir = prefix.path(helpers.environment_dir(_dir, language_version)) with envcontext(get_env_patch(envdir)): yield
def in_env( prefix: Prefix, language_version: str, ) -> Generator[None, None, None]: envdir = _get_env_dir(prefix, language_version) with envcontext(get_env_patch(envdir)): yield
def _run_legacy( hook_type: str, hook_dir: str, args: Sequence[str], ) -> Tuple[int, bytes]: if os.environ.get('PRE_COMMIT_RUNNING_LEGACY'): raise SystemExit( f"bug: pre-commit's script is installed in migration mode\n" f'run `pre-commit install -f --hook-type {hook_type}` to fix ' f'this\n\n' f'Please report this bug at ' f'https://github.com/pre-commit/pre-commit/issues', ) if hook_type == 'pre-push': stdin = sys.stdin.buffer.read() else: stdin = b'' # not running in legacy mode legacy_hook = os.path.join(hook_dir, f'{hook_type}.legacy') if not os.access(legacy_hook, os.X_OK): return 0, stdin with envcontext((('PRE_COMMIT_RUNNING_LEGACY', '1'), )): cmd = normalize_cmd((legacy_hook, *args)) return subprocess.run(cmd, input=stdin).returncode, stdin
def in_env( prefix: Prefix, ) -> Generator[None, None, None]: # pragma: win32 no cover target_dir = prefix.path( helpers.environment_dir(ENVIRONMENT_DIR, get_default_version()), ) with envcontext(get_env_patch(target_dir)): yield
def test_validate_config_skip_via_env_variable(capsys): with pytest.raises(SystemExit) as excinfo: with envcontext((('PRE_COMMIT_ALLOW_NO_CONFIG', '1'), )): hook_impl._validate_config(0, 'DNE.yaml', False) ret, = excinfo.value.args assert ret == 0 expected = '`DNE.yaml` config file not found. Skipping `pre-commit`.\n' assert capsys.readouterr().out == expected
def in_env( prefix: Prefix, language_version: str, ) -> Generator[None, None, None]: directory = helpers.environment_dir(ENVIRONMENT_DIR, language_version) envdir = prefix.path(directory) with envcontext(get_env_patch(envdir)): yield
def test_healthy_venv_creator(python_dir): # venv creator produces slightly different pyvenv.cfg prefix, tmpdir = python_dir with envcontext((('VIRTUALENV_CREATOR', 'venv'), )): python.install_environment(prefix, C.DEFAULT, ()) assert python.healthy(prefix, C.DEFAULT) is True
def test_exception_safety(): class MyError(RuntimeError): pass env = {'hello': 'world'} with pytest.raises(MyError): with envcontext((('foo', 'bar'), ), _env=env): raise MyError() assert env == {'hello': 'world'}
def test_exception_safety(): class MyError(RuntimeError): pass env = {} with pytest.raises(MyError): with envcontext([('foo', 'bar')], _env=env): raise MyError() assert env == {}
def _test(**kwargs): before = kwargs.pop('before') patch = kwargs.pop('patch') expected = kwargs.pop('expected') assert not kwargs env = before.copy() with envcontext(patch, _env=env): assert env == expected assert env == before
def test_init_templatedir_not_set(tmpdir, store, cap_out): # set HOME to ignore the current `.gitconfig` with envcontext([('HOME', str(tmpdir))]): with tmpdir.join('tmpl').ensure_dir().as_cwd(): # we have not set init.templateDir so this should produce a warning init_templatedir(C.CONFIG_FILE, store, '.', hook_type='pre-commit') lines = cap_out.get().splitlines() assert len(lines) == 3 assert lines[1] == ( '[WARNING] `init.templateDir` not set to the target directory' )
def test_unhealthy_if_system_node_goes_missing(tmpdir): bin_dir = tmpdir.join('bin').ensure_dir() node_bin = bin_dir.join('node') node_bin.mksymlinkto(shutil.which('node')) prefix_dir = tmpdir.join('prefix').ensure_dir() prefix_dir.join('package.json').write('{"name": "t", "version": "1.0.0"}') path = ('PATH', (str(bin_dir), os.pathsep, envcontext.Var('PATH'))) with envcontext.envcontext((path, )): prefix = Prefix(str(prefix_dir)) node.install_environment(prefix, 'system', ()) assert node.healthy(prefix, 'system') node_bin.remove() assert not node.healthy(prefix, 'system')
def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out): target = str(tmpdir.join('tmpl')) init_templatedir(C.CONFIG_FILE, store, target, hook_types=['pre-commit']) lines = cap_out.get().splitlines() assert lines[0].startswith('pre-commit installed at ') assert lines[1] == ( '[WARNING] `init.templateDir` not set to the target directory') assert lines[2].startswith( '[WARNING] maybe `git config --global init.templateDir', ) with envcontext((('GIT_TEMPLATE_DIR', target), )): path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') with cwd(path): retcode, output = git_commit( fn=cmd_output_mocked_pre_commit_home, tempdir_factory=tempdir_factory, ) assert retcode == 0 assert 'Bash hook....' in output
def test_init_templatedir_skip_on_missing_config( tmpdir, tempdir_factory, store, cap_out, skip, commit_retcode, commit_output_snippet, ): target = str(tmpdir.join('tmpl')) init_git_dir = git_dir(tempdir_factory) with cwd(init_git_dir): cmd_output('git', 'config', 'init.templateDir', target) init_templatedir( C.CONFIG_FILE, store, target, hook_types=['pre-commit'], skip_on_missing_config=skip, ) lines = cap_out.get().splitlines() assert len(lines) == 1 assert lines[0].startswith('pre-commit installed at') with envcontext((('GIT_TEMPLATE_DIR', target), )): verify_git_dir = git_dir(tempdir_factory) with cwd(verify_git_dir): retcode, output = git_commit( fn=cmd_output_mocked_pre_commit_home, tempdir_factory=tempdir_factory, retcode=None, ) assert retcode == commit_retcode assert commit_output_snippet in output
def test_integration_os_environ(): with mock.patch.dict(os.environ, {'FOO': 'bar'}, clear=True): assert os.environ == {'FOO': 'bar'} with envcontext((('HERP', 'derp'), )): assert os.environ == {'FOO': 'bar', 'HERP': 'derp'} assert os.environ == {'FOO': 'bar'}
def in_env(prefix: Prefix) -> Generator[None, None, None]: with envcontext(get_env_patch(_envdir(prefix))): yield
def set_git_templatedir(tmpdir_factory): tdir = str(tmpdir_factory.mktemp('git_template_dir')) with envcontext((('GIT_TEMPLATE_DIR', tdir), )): yield
def in_env(prefix, language_version): with envcontext(get_env_patch(_envdir(prefix, language_version))): yield
def in_env(repo_cmd_runner): envdir = repo_cmd_runner.path( helpers.environment_dir(ENVIRONMENT_DIR, 'default'), ) with envcontext(get_env_patch(envdir)): yield
def in_env(prefix): target_dir = prefix.path( helpers.environment_dir(ENVIRONMENT_DIR, 'default'), ) with envcontext(get_env_patch(target_dir)): yield
def bin_on_path(): bindir = os.path.join(os.getcwd(), 'bin') with envcontext((('PATH', (bindir, os.pathsep, Var('PATH'))),)): yield
def test_autoupdate_with_core_useBuiltinFSMonitor(out_of_date, tmpdir, store): # force the setting on "globally" for git home = tmpdir.join('fakehome').ensure_dir() home.join('.gitconfig').write('[core]\nuseBuiltinFSMonitor = true\n') with envcontext.envcontext((('HOME', str(home)), )): test_autoupdate_out_of_date_repo(out_of_date, tmpdir, store)
def in_env(prefix, language_version): # pragma: windows no cover envdir = prefix.path( helpers.environment_dir(ENVIRONMENT_DIR, language_version), ) with envcontext(get_env_patch(envdir, language_version)): yield
def test_golang_hook_still_works_when_gobin_is_set(tempdir_factory, store): gobin_dir = tempdir_factory.get() with envcontext((('GOBIN', gobin_dir),)): test_golang_hook(tempdir_factory, store) assert os.listdir(gobin_dir) == []
def in_env(prefix, language_version): envdir = prefix.path(helpers.environment_dir(_dir, language_version)) with envcontext(get_env_patch(envdir)): yield
def _test(*, before, patch, expected): env = before.copy() with envcontext(patch, _env=env): assert env == expected assert env == before
def in_env(prefix, language_version): # pragma: windows no cover with envcontext(get_env_patch(_envdir(prefix, language_version))): yield
def test_integration_os_environ(): with mock.patch.dict(os.environ, {'FOO': 'bar'}, clear=True): assert os.environ == {'FOO': 'bar'} with envcontext([('HERP', 'derp')]): assert os.environ == {'FOO': 'bar', 'HERP': 'derp'} assert os.environ == {'FOO': 'bar'}
def in_env(repo_cmd_runner): # pragma: windows no cover envdir = repo_cmd_runner.path(helpers.environment_dir(ENVIRONMENT_DIR, "default")) with envcontext(get_env_patch(envdir)): yield
def in_env(prefix): # pragma: windows no cover envdir = prefix.path( helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT), ) with envcontext(get_env_patch(envdir)): yield
def in_env(prefix): target_dir = prefix.path( helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT), ) with envcontext(get_env_patch(target_dir)): yield
def in_env(repo_cmd_runner, language_version): # pragma: windows no cover envdir = repo_cmd_runner.path( helpers.environment_dir(ENVIRONMENT_DIR, language_version), ) with envcontext(get_env_patch(envdir)): yield
def in_env(prefix: Prefix) -> Generator[None, None, None]: envdir = prefix.path(helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT), ) with envcontext(get_env_patch(envdir)): yield