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

Skip to content

Commit 64745fb

Browse files
committed
Merge pull request #37 from pre-commit/fix_lots_of_files
Fix lots of files problem
2 parents cd0714d + f66b3f6 commit 64745fb

5 files changed

Lines changed: 76 additions & 28 deletions

File tree

pre_commit/languages/helpers.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11

2+
import subprocess
3+
4+
25
def run_hook(env, hook, file_args):
36
return env.run(
4-
' '.join([hook['entry']] + hook.get('args', []) + list(file_args)),
5-
retcode=None,
6-
)
7+
' '.join(['xargs', hook['entry']] + hook.get('args', [])),
8+
stdin='\n'.join(list(file_args) + ['']),
9+
)
10+
11+
12+
class Environment(object):
13+
@property
14+
def env_prefix(self):
15+
"""env_prefix is a value that is prefixed to the command that is run.
16+
17+
Usually this is to source a virtualenv, etc.
18+
19+
Commands basically end up looking like:
20+
21+
bash -c '{env_prefix} {cmd}'
22+
23+
so you'll often want to end your prefix with &&
24+
"""
25+
raise NotImplementedError
26+
27+
def run(self, cmd, stdin=None, **kwargs):
28+
"""Returns (returncode, stdout, stderr)."""
29+
proc = subprocess.Popen(
30+
['bash', '-c', ' '.join([self.env_prefix, cmd])],
31+
stdin=subprocess.PIPE,
32+
stdout=subprocess.PIPE,
33+
stderr=subprocess.PIPE,
34+
)
35+
stdout, stderr = proc.communicate(stdin)
36+
37+
return proc.returncode, stdout, stderr

pre_commit/languages/node.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,42 @@
88
NODE_ENV = 'node_env'
99

1010

11-
class NodeEnv(object):
12-
def __init__(self, py_env):
13-
self.py_env = py_env
14-
self.env_prefix = '. {0}/bin/activate &&'.format(NODE_ENV)
15-
16-
def run(self, cmd, **kwargs):
17-
return self.py_env.run(' '.join([self.env_prefix, cmd]), **kwargs)
11+
class NodeEnv(python.PythonEnv):
12+
@property
13+
def env_prefix(self):
14+
base = super(NodeEnv, self).env_prefix
15+
return ' '.join([base, '. {0}/bin/activate &&'.format(NODE_ENV)])
1816

1917

2018
@contextlib.contextmanager
21-
def in_env(py_env):
22-
yield NodeEnv(py_env)
19+
def in_env():
20+
yield NodeEnv()
2321

2422

2523
def install_environment():
2624
assert local.path('package.json').exists()
2725

28-
if local.path('node_env').exists():
26+
if local.path(NODE_ENV).exists():
2927
return
3028

3129
local['virtualenv'][python.PY_ENV]()
3230

3331
with python.in_env() as python_env:
3432
python_env.run('pip install nodeenv')
35-
python_env.run('nodeenv --jobs 4 {0}'.format(NODE_ENV))
3633

37-
with in_env(python_env) as node_env:
34+
try:
35+
# Try and use the system level node executable first
36+
python_env.run('nodeenv -n system {0}'.format(NODE_ENV))
37+
except Exception:
38+
# TODO: log exception here
39+
# cleanup
40+
local.path(NODE_ENV).remove()
41+
python_env.run('nodeenv --jobs 4 {0}'.format(NODE_ENV))
42+
43+
with in_env() as node_env:
3844
node_env.run('npm install -g')
3945

4046

4147
def run_hook(hook, file_args):
42-
with python.in_env() as py_env:
43-
with in_env(py_env) as node_env:
44-
return helpers.run_hook(node_env, hook, file_args)
48+
with in_env() as node_env:
49+
return helpers.run_hook(node_env, hook, file_args)

pre_commit/languages/python.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
PY_ENV = 'py_env'
77

88

9-
class PythonEnv(object):
10-
def __init__(self):
11-
self.env_prefix = '. {0}/bin/activate &&'.format(PY_ENV)
12-
13-
def run(self, cmd, **kwargs):
14-
return local['bash']['-c', ' '.join([self.env_prefix, cmd])].run(**kwargs)
9+
class PythonEnv(helpers.Environment):
10+
@property
11+
def env_prefix(self):
12+
return '. {0}/bin/activate &&'.format(PY_ENV)
1513

1614

1715
@contextlib.contextmanager

tests/conftest.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def python_pre_commit_git_repo(dummy_git_repo):
6767
local.path('__init__.py').write('')
6868
local.path('main.py').write("""
6969
def func():
70+
import sys
71+
print repr(sys.argv[1:])
7072
print 'Hello World'
7173
return 0
7274
""")
@@ -142,4 +144,4 @@ def config_for_python_pre_commit_git_repo(python_pre_commit_git_repo):
142144

143145
jsonschema.validate([config], CONFIG_JSON_SCHEMA)
144146

145-
return config
147+
return config

tests/repository_test.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,25 @@ def test_install_python_repo_in_env(python_pre_commit_git_repo, config_for_pytho
4646
def test_run_a_python_hook(config_for_python_pre_commit_git_repo):
4747
repo = Repository(config_for_python_pre_commit_git_repo)
4848
repo.install()
49-
ret = repo.run_hook('foo', [])
49+
ret = repo.run_hook('foo', ['/dev/null'])
50+
51+
assert ret[0] == 0
52+
assert ret[1] == "['/dev/null']\nHello World\n"
53+
54+
55+
@pytest.mark.integration
56+
def test_run_a_hook_lots_of_files(config_for_python_pre_commit_git_repo):
57+
repo = Repository(config_for_python_pre_commit_git_repo)
58+
repo.install()
59+
ret = repo.run_hook('foo', ['/dev/null'] * 15000)
5060

5161
assert ret[0] == 0
52-
assert ret[1] == 'Hello World\n'
5362

5463

55-
@pytest.mark.skipif(True, reason="TODO: make this test not super slow")
64+
@pytest.mark.skipif(
65+
os.environ.get('slowtests', None) == 'false',
66+
reason="TODO: make this test not super slow",
67+
)
5668
def test_run_a_node_hook(config_for_node_pre_commit_git_repo):
5769
repo = Repository(config_for_node_pre_commit_git_repo)
5870
repo.install()

0 commit comments

Comments
 (0)