GeezerVison is showing github.com — use contextlib.chdir · pre-commit/pre-commit@9905e88
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: testing/util.py -11Lines changed: 0 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | from __future__ import annotations | 2 | 2 |
| 3 | | -import contextlib | 4 | 3 | import os.path | 5 | 4 | import subprocess | 6 | 5 | import sys | @@ -86,16 +85,6 @@ def run_opts( | 86 | 85 | ) | 87 | 86 |
| 88 | 87 |
| 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 | | - | 99 | 88 | def git_commit(*args, fn=cmd_output, msg='commit!', all_files=True, **kwargs): | 100 | 89 | kwargs.setdefault('stderr', subprocess.STDOUT) | 101 | 90 |
| |
Collapse file Copy file name to clipboardExpand all lines: tests/commands/hazmat_test.py +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | from __future__ import annotations | 2 | 2 |
| | 3 | +import contextlib | 3 | 4 | import sys | 4 | 5 |
| 5 | 6 | import pytest | 6 | 7 |
| 7 | 8 | from pre_commit.commands.hazmat import _cmd_filenames | 8 | 9 | from pre_commit.commands.hazmat import main | 9 | | -from testing.util import cwd | 10 | 10 |
| 11 | 11 |
| 12 | 12 | def test_cmd_filenames_no_dash_dash(): | @@ -51,7 +51,7 @@ def test_cd(tmp_path, capfd): | 51 | 51 | subdir.joinpath('a').write_text('a') | 52 | 52 | subdir.joinpath('b').write_text('b') | 53 | 53 |
| 54 | | - with cwd(tmp_path): | | 54 | + with contextlib.chdir(tmp_path): | 55 | 55 | ret = main(( | 56 | 56 | 'cd', 'subdir', | 57 | 57 | sys.executable, '-c', | |
Collapse file Copy file name to clipboardExpand all lines: tests/commands/hook_impl_test.py +9-9Lines changed: 9 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | from __future__ import annotations | 2 | 2 |
| | 3 | +import contextlib | 3 | 4 | import subprocess | 4 | 5 | import sys | 5 | 6 | from unittest import mock | | 15 | 16 | from testing.fixtures import git_dir | 16 | 17 | from testing.fixtures import sample_local_config | 17 | 18 | from testing.fixtures import write_config | 18 | | -from testing.util import cwd | 19 | 19 | from testing.util import git_commit | 20 | 20 |
| 21 | 21 |
| @@ -269,7 +269,7 @@ def push_example(tempdir_factory): | 269 | 269 | def test_run_ns_pre_push_updating_branch(push_example): | 270 | 270 | src, src_head, clone, clone_head = push_example | 271 | 271 |
| 272 | | - with cwd(clone): | | 272 | + with contextlib.chdir(clone): | 273 | 273 | args = ('origin', src) | 274 | 274 | stdin = f'HEAD {clone_head} refs/heads/b {src_head}\n'.encode() | 275 | 275 | ns = hook_impl._run_ns('pre-push', False, args, stdin) | @@ -287,7 +287,7 @@ def test_run_ns_pre_push_updating_branch(push_example): | 287 | 287 | def test_run_ns_pre_push_new_branch(push_example): | 288 | 288 | src, src_head, clone, clone_head = push_example | 289 | 289 |
| 290 | | - with cwd(clone): | | 290 | + with contextlib.chdir(clone): | 291 | 291 | args = ('origin', src) | 292 | 292 | stdin = f'HEAD {clone_head} refs/heads/b {hook_impl.Z40}\n'.encode() | 293 | 293 | ns = hook_impl._run_ns('pre-push', False, args, stdin) | @@ -300,7 +300,7 @@ def test_run_ns_pre_push_new_branch(push_example): | 300 | 300 | def test_run_ns_pre_push_new_branch_existing_rev(push_example): | 301 | 301 | src, src_head, clone, _ = push_example | 302 | 302 |
| 303 | | - with cwd(clone): | | 303 | + with contextlib.chdir(clone): | 304 | 304 | args = ('origin', src) | 305 | 305 | stdin = f'HEAD {src_head} refs/heads/b2 {hook_impl.Z40}\n'.encode() | 306 | 306 | 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): | 311 | 311 | def test_run_ns_pre_push_ref_with_whitespace(push_example): | 312 | 312 | src, src_head, clone, _ = push_example | 313 | 313 |
| 314 | | - with cwd(clone): | | 314 | + with contextlib.chdir(clone): | 315 | 315 | args = ('origin', src) | 316 | 316 | line = f'HEAD^{{/ }} {src_head} refs/heads/b2 {hook_impl.Z40}\n' | 317 | 317 | stdin = line.encode() | @@ -327,7 +327,7 @@ def test_pushing_orphan_branch(push_example): | 327 | 327 | git_commit(cwd=clone, msg='something else to get unique hash') | 328 | 328 | clone_rev = git.head_rev(clone) | 329 | 329 |
| 330 | | - with cwd(clone): | | 330 | + with contextlib.chdir(clone): | 331 | 331 | args = ('origin', src) | 332 | 332 | stdin = f'HEAD {clone_rev} refs/heads/b2 {hook_impl.Z40}\n'.encode() | 333 | 333 | ns = hook_impl._run_ns('pre-push', False, args, stdin) | @@ -339,7 +339,7 @@ def test_pushing_orphan_branch(push_example): | 339 | 339 | def test_run_ns_pre_push_deleting_branch(push_example): | 340 | 340 | src, src_head, clone, _ = push_example | 341 | 341 |
| 342 | | - with cwd(clone): | | 342 | + with contextlib.chdir(clone): | 343 | 343 | args = ('origin', src) | 344 | 344 | stdin = f'(delete) {hook_impl.Z40} refs/heads/b {src_head}'.encode() | 345 | 345 | 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): | 352 | 352 |
| 353 | 353 | stdin = f'(delete) {hook_impl.Z40} refs/heads/b {src_head}'.encode() | 354 | 354 | with mock.patch.object(sys.stdin.buffer, 'read', return_value=stdin): | 355 | | - with cwd(clone): | | 355 | + with contextlib.chdir(clone): | 356 | 356 | write_config('.', sample_local_config()) | 357 | 357 | ret = hook_impl.hook_impl( | 358 | 358 | store, | @@ -368,7 +368,7 @@ def test_hook_impl_main_noop_pre_push(cap_out, store, push_example): | 368 | 368 |
| 369 | 369 |
| 370 | 370 | 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)): | 372 | 372 | write_config('.', sample_local_config()) | 373 | 373 | ret = hook_impl.hook_impl( | 374 | 374 | store, | |
Collapse file Copy file name to clipboardExpand all lines: tests/commands/init_templatedir_test.py +7-7Lines changed: 7 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | from __future__ import annotations | 2 | 2 |
| | 3 | +import contextlib | 3 | 4 | import os.path | 4 | 5 | from unittest import mock | 5 | 6 |
| | 12 | 13 | from testing.fixtures import git_dir | 13 | 14 | from testing.fixtures import make_consuming_repo | 14 | 15 | from testing.util import cmd_output_mocked_pre_commit_home | 15 | | -from testing.util import cwd | 16 | 16 | from testing.util import git_commit | 17 | 17 |
| 18 | 18 |
| @@ -31,7 +31,7 @@ def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out): | 31 | 31 | with envcontext((('GIT_TEMPLATE_DIR', target),)): | 32 | 32 | path = make_consuming_repo(tempdir_factory, 'script_hooks_repo') | 33 | 33 |
| 34 | | - with cwd(path): | | 34 | + with contextlib.chdir(path): | 35 | 35 | retcode, output = git_commit( | 36 | 36 | fn=cmd_output_mocked_pre_commit_home, | 37 | 37 | tempdir_factory=tempdir_factory, | @@ -43,7 +43,7 @@ def test_init_templatedir(tmpdir, tempdir_factory, store, cap_out): | 43 | 43 | def test_init_templatedir_already_set(tmpdir, tempdir_factory, store, cap_out): | 44 | 44 | target = str(tmpdir.join('tmpl')) | 45 | 45 | tmp_git_dir = git_dir(tempdir_factory) | 46 | | - with cwd(tmp_git_dir): | | 46 | + with contextlib.chdir(tmp_git_dir): | 47 | 47 | cmd_output('git', 'config', 'init.templateDir', target) | 48 | 48 | init_templatedir( | 49 | 49 | C.CONFIG_FILE, store, target, hook_types=['pre-commit'], | @@ -73,7 +73,7 @@ def test_init_templatedir_not_set(tmpdir, store, cap_out): | 73 | 73 | def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out): | 74 | 74 | target = str(tmpdir.join('tmpl')) | 75 | 75 | tmp_git_dir = git_dir(tempdir_factory) | 76 | | - with cwd(tmp_git_dir): | | 76 | + with contextlib.chdir(tmp_git_dir): | 77 | 77 | cmd_output('git', 'config', 'init.templateDir', '~/templatedir') | 78 | 78 | with mock.patch.object(os.path, 'expanduser', return_value=target): | 79 | 79 | init_templatedir( | @@ -88,7 +88,7 @@ def test_init_templatedir_expanduser(tmpdir, tempdir_factory, store, cap_out): | 88 | 88 | def test_init_templatedir_hookspath_set(tmpdir, tempdir_factory, store): | 89 | 89 | target = tmpdir.join('tmpl') | 90 | 90 | tmp_git_dir = git_dir(tempdir_factory) | 91 | | - with cwd(tmp_git_dir): | | 91 | + with contextlib.chdir(tmp_git_dir): | 92 | 92 | cmd_output('git', 'config', '--local', 'core.hooksPath', 'hooks') | 93 | 93 | init_templatedir( | 94 | 94 | C.CONFIG_FILE, store, target, hook_types=['pre-commit'], | @@ -114,7 +114,7 @@ def test_init_templatedir_skip_on_missing_config( | 114 | 114 | ): | 115 | 115 | target = str(tmpdir.join('tmpl')) | 116 | 116 | init_git_dir = git_dir(tempdir_factory) | 117 | | - with cwd(init_git_dir): | | 117 | + with contextlib.chdir(init_git_dir): | 118 | 118 | cmd_output('git', 'config', 'init.templateDir', target) | 119 | 119 | init_templatedir( | 120 | 120 | C.CONFIG_FILE, | @@ -131,7 +131,7 @@ def test_init_templatedir_skip_on_missing_config( | 131 | 131 | with envcontext((('GIT_TEMPLATE_DIR', target),)): | 132 | 132 | verify_git_dir = git_dir(tempdir_factory) | 133 | 133 |
| 134 | | - with cwd(verify_git_dir): | | 134 | + with contextlib.chdir(verify_git_dir): | 135 | 135 | retcode, output = git_commit( | 136 | 136 | fn=cmd_output_mocked_pre_commit_home, | 137 | 137 | tempdir_factory=tempdir_factory, | |
|