Skip to content

Commit 9905e88

Browse files
committed
use contextlib.chdir
1 parent 59cb792 commit 9905e88

16 files changed

Lines changed: 109 additions & 122 deletions

‎testing/util.py‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
import contextlib
43
import os.path
54
import subprocess
65
import sys
@@ -86,16 +85,6 @@ def run_opts(
8685
)
8786

8887

89-
@contextlib.contextmanager
90-
def cwd(path):
91-
original_cwd = os.getcwd()
92-
os.chdir(path)
93-
try:
94-
yield
95-
finally:
96-
os.chdir(original_cwd)
97-
98-
9988
def git_commit(*args, fn=cmd_output, msg='commit!', all_files=True, **kwargs):
10089
kwargs.setdefault('stderr', subprocess.STDOUT)
10190

‎tests/commands/hazmat_test.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import sys
45

56
import pytest
67

78
from pre_commit.commands.hazmat import _cmd_filenames
89
from pre_commit.commands.hazmat import main
9-
from testing.util import cwd
1010

1111

1212
def test_cmd_filenames_no_dash_dash():
@@ -51,7 +51,7 @@ def test_cd(tmp_path, capfd):
5151
subdir.joinpath('a').write_text('a')
5252
subdir.joinpath('b').write_text('b')
5353

54-
with cwd(tmp_path):
54+
with contextlib.chdir(tmp_path):
5555
ret = main((
5656
'cd', 'subdir',
5757
sys.executable, '-c',

‎tests/commands/hook_impl_test.py‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import subprocess
45
import sys
56
from unittest import mock
@@ -15,7 +16,6 @@
1516
from testing.fixtures import git_dir
1617
from testing.fixtures import sample_local_config
1718
from testing.fixtures import write_config
18-
from testing.util import cwd
1919
from testing.util import git_commit
2020

2121

@@ -269,7 +269,7 @@ def push_example(tempdir_factory):
269269
def test_run_ns_pre_push_updating_branch(push_example):
270270
src, src_head, clone, clone_head = push_example
271271

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

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

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

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

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

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

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

369369

370370
def test_hook_impl_main_runs_hooks(cap_out, tempdir_factory, store):
371-
with cwd(git_dir(tempdir_factory)):
371+
with contextlib.chdir(git_dir(tempdir_factory)):
372372
write_config('.', sample_local_config())
373373
ret = hook_impl.hook_impl(
374374
store,

‎tests/commands/init_templatedir_test.py‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import contextlib
34
import os.path
45
from unittest import mock
56

@@ -12,7 +13,6 @@
1213
from testing.fixtures import git_dir
1314
from testing.fixtures import make_consuming_repo
1415
from testing.util import cmd_output_mocked_pre_commit_home
15-
from testing.util import cwd
1616
from testing.util import git_commit
1717

1818

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

34-
with cwd(path):
34+
with contextlib.chdir(path):
3535
retcode, output = git_commit(
3636
fn=cmd_output_mocked_pre_commit_home,
3737
tempdir_factory=tempdir_factory,
@@ -43,7 +43,7 @@ def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out):
4343
def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out):
4444
target = str(tmpdir.join('tmpl'))
4545
tmp_git_dir = git_dir(tempdir_factory)
46-
with cwd(tmp_git_dir):
46+
with contextlib.chdir(tmp_git_dir):
4747
cmd_output('git', 'config', 'init.templateDir', target)
4848
init_templatedir(
4949
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
@@ -73,7 +73,7 @@ def test_init_templatedir_not_set(tmpdir, store, cap_out):
7373
def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out):
7474
target = str(tmpdir.join('tmpl'))
7575
tmp_git_dir = git_dir(tempdir_factory)
76-
with cwd(tmp_git_dir):
76+
with contextlib.chdir(tmp_git_dir):
7777
cmd_output('git', 'config', 'init.templateDir', '~/templatedir')
7878
with mock.patch.object(os.path, 'expanduser', return_value=target):
7979
init_templatedir(
@@ -88,7 +88,7 @@ def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out):
8888
def test_init_templatedir_hookspath_set(tmpdir, tempdir_factory, store):
8989
target = tmpdir.join('tmpl')
9090
tmp_git_dir = git_dir(tempdir_factory)
91-
with cwd(tmp_git_dir):
91+
with contextlib.chdir(tmp_git_dir):
9292
cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks')
9393
init_templatedir(
9494
C.CONFIG_FILE, store, target, hook_types=['pre-commit'],
@@ -114,7 +114,7 @@ def test_init_templatedir_skip_on_missing_config(
114114
):
115115
target = str(tmpdir.join('tmpl'))
116116
init_git_dir = git_dir(tempdir_factory)
117-
with cwd(init_git_dir):
117+
with contextlib.chdir(init_git_dir):
118118
cmd_output('git', 'config', 'init.templateDir', target)
119119
init_templatedir(
120120
C.CONFIG_FILE,
@@ -131,7 +131,7 @@ def test_init_templatedir_skip_on_missing_config(
131131
with envcontext((('GIT_TEMPLATE_DIR', target),)):
132132
verify_git_dir = git_dir(tempdir_factory)
133133

134-
with cwd(verify_git_dir):
134+
with contextlib.chdir(verify_git_dir):
135135
retcode, output = git_commit(
136136
fn=cmd_output_mocked_pre_commit_home,
137137
tempdir_factory=tempdir_factory,

0 commit comments

Comments
 (0)