Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions testing/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import contextlib
import os.path
import subprocess
import sys
Expand Down Expand Up @@ -86,16 +85,6 @@ def run_opts(
)


@contextlib.contextmanager
def cwd(path):
original_cwd = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(original_cwd)


def git_commit(*args, fn=cmd_output, msg='commit!', all_files=True, **kwargs):
kwargs.setdefault('stderr', subprocess.STDOUT)

Expand Down
4 changes: 2 additions & 2 deletions tests/commands/hazmat_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from __future__ import annotations

import contextlib
import sys

import pytest

from pre_commit.commands.hazmat import _cmd_filenames
from pre_commit.commands.hazmat import main
from testing.util import cwd


def test_cmd_filenames_no_dash_dash():
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_cd(tmp_path, capfd):
subdir.joinpath('a').write_text('a')
subdir.joinpath('b').write_text('b')

with cwd(tmp_path):
with contextlib.chdir(tmp_path):
ret = main((
'cd', 'subdir',
sys.executable, '-c',
Expand Down
18 changes: 9 additions & 9 deletions tests/commands/hook_impl_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import subprocess
import sys
from unittest import mock
Expand All @@ -15,7 +16,6 @@
from testing.fixtures import git_dir
from testing.fixtures import sample_local_config
from testing.fixtures import write_config
from testing.util import cwd
from testing.util import git_commit


Expand Down Expand Up @@ -269,7 +269,7 @@ def push_example(tempdir_factory):
def test_run_ns_pre_push_updating_branch(push_example):
src, src_head, clone, clone_head = push_example

with cwd(clone):
with contextlib.chdir(clone):
args = ('origin', src)
stdin = f'HEAD {clone_head} refs/heads/b {src_head}\n'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin)
Expand All @@ -287,7 +287,7 @@ def test_run_ns_pre_push_updating_branch(push_example):
def test_run_ns_pre_push_new_branch(push_example):
src, src_head, clone, clone_head = push_example

with cwd(clone):
with contextlib.chdir(clone):
args = ('origin', src)
stdin = f'HEAD {clone_head} refs/heads/b {hook_impl.Z40}\n'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin)
Expand All @@ -300,7 +300,7 @@ def test_run_ns_pre_push_new_branch(push_example):
def test_run_ns_pre_push_new_branch_existing_rev(push_example):
src, src_head, clone, _ = push_example

with cwd(clone):
with contextlib.chdir(clone):
args = ('origin', src)
stdin = f'HEAD {src_head} refs/heads/b2 {hook_impl.Z40}\n'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin)
Expand All @@ -311,7 +311,7 @@ def test_run_ns_pre_push_new_branch_existing_rev(push_example):
def test_run_ns_pre_push_ref_with_whitespace(push_example):
src, src_head, clone, _ = push_example

with cwd(clone):
with contextlib.chdir(clone):
args = ('origin', src)
line = f'HEAD^{{/ }} {src_head} refs/heads/b2 {hook_impl.Z40}\n'
stdin = line.encode()
Expand All @@ -327,7 +327,7 @@ def test_pushing_orphan_branch(push_example):
git_commit(cwd=clone, msg='something else to get unique hash')
clone_rev = git.head_rev(clone)

with cwd(clone):
with contextlib.chdir(clone):
args = ('origin', src)
stdin = f'HEAD {clone_rev} refs/heads/b2 {hook_impl.Z40}\n'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin)
Expand All @@ -339,7 +339,7 @@ def test_pushing_orphan_branch(push_example):
def test_run_ns_pre_push_deleting_branch(push_example):
src, src_head, clone, _ = push_example

with cwd(clone):
with contextlib.chdir(clone):
args = ('origin', src)
stdin = f'(delete) {hook_impl.Z40} refs/heads/b {src_head}'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin)
Expand All @@ -352,7 +352,7 @@ def test_hook_impl_main_noop_pre_push(cap_out, store, push_example):

stdin = f'(delete) {hook_impl.Z40} refs/heads/b {src_head}'.encode()
with mock.patch.object(sys.stdin.buffer, 'read', return_value=stdin):
with cwd(clone):
with contextlib.chdir(clone):
write_config('.', sample_local_config())
ret = hook_impl.hook_impl(
store,
Expand All @@ -368,7 +368,7 @@ def test_hook_impl_main_noop_pre_push(cap_out, store, push_example):


def test_hook_impl_main_runs_hooks(cap_out, tempdir_factory, store):
with cwd(git_dir(tempdir_factory)):
with contextlib.chdir(git_dir(tempdir_factory)):
write_config('.', sample_local_config())
ret = hook_impl.hook_impl(
store,
Expand Down
14 changes: 7 additions & 7 deletions tests/commands/init_templatedir_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import contextlib
import os.path
from unittest import mock

Expand All @@ -12,7 +13,6 @@
from testing.fixtures import git_dir
from testing.fixtures import make_consuming_repo
from testing.util import cmd_output_mocked_pre_commit_home
from testing.util import cwd
from testing.util import git_commit


Expand All @@ -31,7 +31,7 @@ def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out):
with envcontext((('GIT_TEMPLATE_DIR', target),)):
path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')

with cwd(path):
with contextlib.chdir(path):
retcode, output = git_commit(
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
Expand All @@ -43,7 +43,7 @@ def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out):
def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out):
target = str(tmpdir.join('tmpl'))
tmp_git_dir = git_dir(tempdir_factory)
with cwd(tmp_git_dir):
with contextlib.chdir(tmp_git_dir):
cmd_output('git', 'config', 'init.templateDir', target)
init_templatedir(
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_init_templatedir_not_set(tmpdir, store, cap_out):
def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out):
target = str(tmpdir.join('tmpl'))
tmp_git_dir = git_dir(tempdir_factory)
with cwd(tmp_git_dir):
with contextlib.chdir(tmp_git_dir):
cmd_output('git', 'config', 'init.templateDir', '~/templatedir')
with mock.patch.object(os.path, 'expanduser', return_value=target):
init_templatedir(
Expand All @@ -88,7 +88,7 @@ def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out):
def test_init_templatedir_hookspath_set(tmpdir, tempdir_factory, store):
target = tmpdir.join('tmpl')
tmp_git_dir = git_dir(tempdir_factory)
with cwd(tmp_git_dir):
with contextlib.chdir(tmp_git_dir):
cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks')
init_templatedir(
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
Expand All @@ -114,7 +114,7 @@ def test_init_templatedir_skip_on_missing_config(
):
target = str(tmpdir.join('tmpl'))
init_git_dir = git_dir(tempdir_factory)
with cwd(init_git_dir):
with contextlib.chdir(init_git_dir):
cmd_output('git', 'config', 'init.templateDir', target)
init_templatedir(
C.CONFIG_FILE,
Expand All @@ -131,7 +131,7 @@ def test_init_templatedir_skip_on_missing_config(
with envcontext((('GIT_TEMPLATE_DIR', target),)):
verify_git_dir = git_dir(tempdir_factory)

with cwd(verify_git_dir):
with contextlib.chdir(verify_git_dir):
retcode, output = git_commit(
fn=cmd_output_mocked_pre_commit_home,
tempdir_factory=tempdir_factory,
Expand Down
Loading
Loading