Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 184e22e

Browse files
authored
Merge pull request #714 from pre-commit/remove_cwd_fn
Move cwd() to tests-only
2 parents 86da772 + 29033f1 commit 184e22e

21 files changed

Lines changed: 84 additions & 103 deletions

pre_commit/commands/autoupdate.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from pre_commit.repository import Repository
1919
from pre_commit.util import CalledProcessError
2020
from pre_commit.util import cmd_output
21-
from pre_commit.util import cwd
2221

2322

2423
class RepositoryCannotBeUpdatedError(RuntimeError):
@@ -35,17 +34,17 @@ def _update_repo(repo_config, runner, tags_only):
3534
"""
3635
repo_path = runner.store.clone(repo_config['repo'], repo_config['sha'])
3736

38-
with cwd(repo_path):
39-
cmd_output('git', 'fetch')
40-
tag_cmd = ('git', 'describe', 'origin/master', '--tags')
41-
if tags_only:
42-
tag_cmd += ('--abbrev=0',)
43-
else:
44-
tag_cmd += ('--exact',)
45-
try:
46-
rev = cmd_output(*tag_cmd)[1].strip()
47-
except CalledProcessError:
48-
rev = cmd_output('git', 'rev-parse', 'origin/master')[1].strip()
37+
cmd_output('git', '-C', repo_path, 'fetch')
38+
tag_cmd = ('git', '-C', repo_path, 'describe', 'origin/master', '--tags')
39+
if tags_only:
40+
tag_cmd += ('--abbrev=0',)
41+
else:
42+
tag_cmd += ('--exact',)
43+
try:
44+
rev = cmd_output(*tag_cmd)[1].strip()
45+
except CalledProcessError:
46+
tag_cmd = ('git', '-C', repo_path, 'rev-parse', 'origin/master')
47+
rev = cmd_output(*tag_cmd)[1].strip()
4948

5049
# Don't bother trying to update if our sha is the same
5150
if rev == repo_config['sha']:

pre_commit/make_archives.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from pre_commit import output
1010
from pre_commit.util import cmd_output
11-
from pre_commit.util import cwd
1211
from pre_commit.util import resource_filename
1312
from pre_commit.util import rmtree
1413
from pre_commit.util import tmpdir
@@ -42,8 +41,7 @@ def make_archive(name, repo, ref, destdir):
4241
with tmpdir() as tempdir:
4342
# Clone the repository to the temporary directory
4443
cmd_output('git', 'clone', repo, tempdir)
45-
with cwd(tempdir):
46-
cmd_output('git', 'checkout', ref)
44+
cmd_output('git', '-C', tempdir, 'checkout', ref)
4745

4846
# We don't want the '.git' directory
4947
# It adds a bunch of size to the archive and we don't use it at

pre_commit/store.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from pre_commit.util import clean_path_on_failure
1515
from pre_commit.util import cmd_output
1616
from pre_commit.util import copy_tree_to_path
17-
from pre_commit.util import cwd
1817
from pre_commit.util import no_git_env
1918
from pre_commit.util import resource_filename
2019

@@ -142,16 +141,14 @@ def _get_result():
142141
def clone(self, repo, ref, deps=()):
143142
"""Clone the given url and checkout the specific ref."""
144143
def clone_strategy(directory):
145-
cmd_output(
146-
'git', 'clone', '--no-checkout', repo, directory,
147-
env=no_git_env(),
148-
)
149-
with cwd(directory):
150-
cmd_output('git', 'reset', ref, '--hard', env=no_git_env())
151-
cmd_output(
152-
'git', 'submodule', 'update', '--init', '--recursive',
153-
env=no_git_env(),
154-
)
144+
env = no_git_env()
145+
146+
def _git_cmd(*args):
147+
return cmd_output('git', '-C', directory, *args, env=env)
148+
149+
_git_cmd('clone', '--no-checkout', repo, '.')
150+
_git_cmd('reset', ref, '--hard')
151+
_git_cmd('submodule', 'update', '--init', '--recursive')
155152

156153
return self._new_repo(repo, ref, deps, clone_strategy)
157154

pre_commit/util.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@
1616
from pre_commit import parse_shebang
1717

1818

19-
@contextlib.contextmanager
20-
def cwd(path):
21-
original_cwd = os.getcwd()
22-
os.chdir(path)
23-
try:
24-
yield
25-
finally:
26-
os.chdir(original_cwd)
27-
28-
2919
def mkdirp(path):
3020
try:
3121
os.makedirs(path)

testing/fixtures.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from pre_commit.clientlib import load_manifest
1818
from pre_commit.util import cmd_output
1919
from pre_commit.util import copy_tree_to_path
20-
from pre_commit.util import cwd
2120
from testing.util import get_resource_path
2221

2322

@@ -30,9 +29,8 @@ def git_dir(tempdir_factory):
3029
def make_repo(tempdir_factory, repo_source):
3130
path = git_dir(tempdir_factory)
3231
copy_tree_to_path(get_resource_path(repo_source), path)
33-
with cwd(path):
34-
cmd_output('git', 'add', '.')
35-
cmd_output('git', 'commit', '-m', 'Add hooks')
32+
cmd_output('git', '-C', path, 'add', '.')
33+
cmd_output('git', '-C', path, 'commit', '-m', 'Add hooks')
3634
return path
3735

3836

@@ -116,17 +114,15 @@ def write_config(directory, config, config_file=C.CONFIG_FILE):
116114

117115
def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):
118116
write_config(git_path, config, config_file=config_file)
119-
with cwd(git_path):
120-
cmd_output('git', 'add', config_file)
121-
cmd_output('git', 'commit', '-m', 'Add hooks config')
117+
cmd_output('git', '-C', git_path, 'add', config_file)
118+
cmd_output('git', '-C', git_path, 'commit', '-m', 'Add hooks config')
122119
return git_path
123120

124121

125122
def remove_config_from_repo(git_path, config_file=C.CONFIG_FILE):
126123
os.unlink(os.path.join(git_path, config_file))
127-
with cwd(git_path):
128-
cmd_output('git', 'add', config_file)
129-
cmd_output('git', 'commit', '-m', 'Remove hooks config')
124+
cmd_output('git', '-C', git_path, 'add', config_file)
125+
cmd_output('git', '-C', git_path, 'commit', '-m', 'Remove hooks config')
130126
return git_path
131127

132128

testing/util.py

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

3+
import contextlib
34
import os.path
45
import sys
56

@@ -103,3 +104,13 @@ def run_opts(
103104
show_diff_on_failure=show_diff_on_failure,
104105
commit_msg_filename=commit_msg_filename,
105106
)
107+
108+
109+
@contextlib.contextmanager
110+
def cwd(path):
111+
original_cwd = os.getcwd()
112+
os.chdir(path)
113+
try:
114+
yield
115+
finally:
116+
os.chdir(original_cwd)

tests/commands/autoupdate_test.py

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

3+
import os.path
34
import pipes
45
import shutil
56
from collections import OrderedDict
@@ -14,7 +15,6 @@
1415
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
1516
from pre_commit.runner import Runner
1617
from pre_commit.util import cmd_output
17-
from pre_commit.util import cwd
1818
from testing.auto_namedtuple import auto_namedtuple
1919
from testing.fixtures import add_config_to_repo
2020
from testing.fixtures import config_with_local_hooks
@@ -62,14 +62,13 @@ def test_autoupdate_old_revision_broken(
6262
path = make_repo(tempdir_factory, 'python_hooks_repo')
6363
config = make_config_from_repo(path, check=False)
6464

65-
with cwd(path):
66-
cmd_output('git', 'mv', C.MANIFEST_FILE, 'nope.yaml')
67-
cmd_output('git', 'commit', '-m', 'simulate old repo')
68-
# Assume this is the revision the user's old repository was at
69-
rev = git.head_sha(path)
70-
cmd_output('git', 'mv', 'nope.yaml', C.MANIFEST_FILE)
71-
cmd_output('git', 'commit', '-m', 'move hooks file')
72-
update_rev = git.head_sha(path)
65+
cmd_output('git', '-C', path, 'mv', C.MANIFEST_FILE, 'nope.yaml')
66+
cmd_output('git', '-C', path, 'commit', '-m', 'simulate old repo')
67+
# Assume this is the revision the user's old repository was at
68+
rev = git.head_sha(path)
69+
cmd_output('git', '-C', path, 'mv', 'nope.yaml', C.MANIFEST_FILE)
70+
cmd_output('git', '-C', path, 'commit', '-m', 'move hooks file')
71+
update_rev = git.head_sha(path)
7372

7473
config['sha'] = rev
7574
write_config('.', config)
@@ -87,8 +86,7 @@ def out_of_date_repo(tempdir_factory):
8786
original_sha = git.head_sha(path)
8887

8988
# Make a commit
90-
with cwd(path):
91-
cmd_output('git', 'commit', '--allow-empty', '-m', 'foo')
89+
cmd_output('git', '-C', path, 'commit', '--allow-empty', '-m', 'foo')
9290
head_sha = git.head_sha(path)
9391

9492
yield auto_namedtuple(
@@ -223,8 +221,7 @@ def test_loses_formatting_when_not_detectable(
223221

224222
@pytest.fixture
225223
def tagged_repo(out_of_date_repo):
226-
with cwd(out_of_date_repo.path):
227-
cmd_output('git', 'tag', 'v1.2.3')
224+
cmd_output('git', '-C', out_of_date_repo.path, 'tag', 'v1.2.3')
228225
yield out_of_date_repo
229226

230227

@@ -243,8 +240,8 @@ def test_autoupdate_tagged_repo(
243240

244241
@pytest.fixture
245242
def tagged_repo_with_more_commits(tagged_repo):
246-
with cwd(tagged_repo.path):
247-
cmd_output('git', 'commit', '--allow-empty', '-m', 'commit!')
243+
cmd = ('git', '-C', tagged_repo.path, 'commit', '--allow-empty', '-mfoo')
244+
cmd_output(*cmd)
248245
yield tagged_repo
249246

250247

@@ -267,13 +264,12 @@ def hook_disappearing_repo(tempdir_factory):
267264
path = make_repo(tempdir_factory, 'python_hooks_repo')
268265
original_sha = git.head_sha(path)
269266

270-
with cwd(path):
271-
shutil.copy(
272-
get_resource_path('manifest_without_foo.yaml'),
273-
C.MANIFEST_FILE,
274-
)
275-
cmd_output('git', 'add', '.')
276-
cmd_output('git', 'commit', '-m', 'Remove foo')
267+
shutil.copy(
268+
get_resource_path('manifest_without_foo.yaml'),
269+
os.path.join(path, C.MANIFEST_FILE),
270+
)
271+
cmd_output('git', '-C', path, 'add', '.')
272+
cmd_output('git', '-C', path, 'commit', '-m', 'Remove foo')
277273

278274
yield auto_namedtuple(path=path, original_sha=original_sha)
279275

tests/commands/install_uninstall_test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
from pre_commit.commands.install_uninstall import uninstall
2121
from pre_commit.runner import Runner
2222
from pre_commit.util import cmd_output
23-
from pre_commit.util import cwd
2423
from pre_commit.util import make_executable
2524
from pre_commit.util import mkdirp
2625
from pre_commit.util import resource_filename
2726
from testing.fixtures import git_dir
2827
from testing.fixtures import make_consuming_repo
2928
from testing.fixtures import remove_config_from_repo
3029
from testing.util import cmd_output_mocked_pre_commit_home
30+
from testing.util import cwd
3131
from testing.util import xfailif_no_symlink
3232

3333

@@ -153,9 +153,8 @@ def test_install_pre_commit_and_run_custom_path(tempdir_factory):
153153
def test_install_in_submodule_and_run(tempdir_factory):
154154
src_path = make_consuming_repo(tempdir_factory, 'script_hooks_repo')
155155
parent_path = git_dir(tempdir_factory)
156-
with cwd(parent_path):
157-
cmd_output('git', 'submodule', 'add', src_path, 'sub')
158-
cmd_output('git', 'commit', '-m', 'foo')
156+
cmd_output('git', '-C', parent_path, 'submodule', 'add', src_path, 'sub')
157+
cmd_output('git', '-C', parent_path, 'commit', '-m', 'foo')
159158

160159
sub_pth = os.path.join(parent_path, 'sub')
161160
with cwd(sub_pth):

tests/commands/run_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
from pre_commit.commands.run import run
1919
from pre_commit.runner import Runner
2020
from pre_commit.util import cmd_output
21-
from pre_commit.util import cwd
2221
from pre_commit.util import make_executable
2322
from testing.fixtures import add_config_to_repo
2423
from testing.fixtures import make_consuming_repo
2524
from testing.fixtures import modify_config
2625
from testing.fixtures import read_config
2726
from testing.util import cmd_output_mocked_pre_commit_home
27+
from testing.util import cwd
2828
from testing.util import run_opts
2929
from testing.util import xfailif_no_symlink
3030

tests/commands/try_repo_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
from pre_commit.commands.try_repo import try_repo
77
from pre_commit.util import cmd_output
8-
from pre_commit.util import cwd
98
from testing.auto_namedtuple import auto_namedtuple
109
from testing.fixtures import git_dir
1110
from testing.fixtures import make_repo
11+
from testing.util import cwd
1212
from testing.util import run_opts
1313

1414

0 commit comments

Comments
 (0)