Exemplo n.º 1
0
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
Exemplo n.º 2
0
 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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
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
Exemplo n.º 5
0
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
Exemplo n.º 6
0
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
Exemplo n.º 7
0
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
Exemplo n.º 8
0
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
Exemplo n.º 9
0
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
Exemplo n.º 10
0
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'}
Exemplo n.º 11
0
def test_exception_safety():
    class MyError(RuntimeError):
        pass

    env = {}
    with pytest.raises(MyError):
        with envcontext([('foo', 'bar')], _env=env):
            raise MyError()
    assert env == {}
Exemplo n.º 12
0
def test_exception_safety():
    class MyError(RuntimeError):
        pass

    env = {}
    with pytest.raises(MyError):
        with envcontext([('foo', 'bar')], _env=env):
            raise MyError()
    assert env == {}
Exemplo n.º 13
0
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
Exemplo n.º 14
0
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'
    )
Exemplo n.º 16
0
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')
Exemplo n.º 17
0
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
Exemplo n.º 18
0
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
Exemplo n.º 19
0
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'}
Exemplo n.º 20
0
def in_env(prefix: Prefix) -> Generator[None, None, None]:
    with envcontext(get_env_patch(_envdir(prefix))):
        yield
Exemplo n.º 21
0
def set_git_templatedir(tmpdir_factory):
    tdir = str(tmpdir_factory.mktemp('git_template_dir'))
    with envcontext((('GIT_TEMPLATE_DIR', tdir), )):
        yield
Exemplo n.º 22
0
def in_env(prefix, language_version):
    with envcontext(get_env_patch(_envdir(prefix, language_version))):
        yield
Exemplo n.º 23
0
def in_env(repo_cmd_runner):
    envdir = repo_cmd_runner.path(
        helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
    )
    with envcontext(get_env_patch(envdir)):
        yield
Exemplo n.º 24
0
def in_env(prefix):
    target_dir = prefix.path(
        helpers.environment_dir(ENVIRONMENT_DIR, 'default'), )
    with envcontext(get_env_patch(target_dir)):
        yield
Exemplo n.º 25
0
def bin_on_path():
    bindir = os.path.join(os.getcwd(), 'bin')
    with envcontext((('PATH', (bindir, os.pathsep, Var('PATH'))),)):
        yield
Exemplo n.º 26
0
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)
Exemplo n.º 27
0
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
Exemplo n.º 28
0
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) == []
Exemplo n.º 29
0
 def in_env(prefix, language_version):
     envdir = prefix.path(helpers.environment_dir(_dir, language_version))
     with envcontext(get_env_patch(envdir)):
         yield
Exemplo n.º 30
0
def in_env(repo_cmd_runner):
    envdir = repo_cmd_runner.path(
        helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
    )
    with envcontext(get_env_patch(envdir)):
        yield
Exemplo n.º 31
0
def _test(*, before, patch, expected):
    env = before.copy()
    with envcontext(patch, _env=env):
        assert env == expected
    assert env == before
Exemplo n.º 32
0
def in_env(prefix, language_version):  # pragma: windows no cover
    with envcontext(get_env_patch(_envdir(prefix, language_version))):
        yield
Exemplo n.º 33
0
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
Exemplo n.º 34
0
def bin_on_path():
    bindir = os.path.join(os.getcwd(), 'bin')
    with envcontext((('PATH', (bindir, os.pathsep, Var('PATH'))),)):
        yield
Exemplo n.º 35
0
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'}
Exemplo n.º 36
0
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
Exemplo n.º 37
0
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
Exemplo n.º 38
0
def in_env(prefix):
    target_dir = prefix.path(
        helpers.environment_dir(ENVIRONMENT_DIR, C.DEFAULT),
    )
    with envcontext(get_env_patch(target_dir)):
        yield
Exemplo n.º 39
0
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
Exemplo n.º 40
0
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
Exemplo n.º 41
0
def in_env(prefix, language_version):  # pragma: windows no cover
    with envcontext(get_env_patch(_envdir(prefix, language_version))):
        yield
Exemplo n.º 42
0
 def in_env(prefix, language_version):
     envdir = prefix.path(helpers.environment_dir(_dir, language_version))
     with envcontext(get_env_patch(envdir)):
         yield