Skip to content

Commit ec0ed8a

Browse files
committed
Handle CPU detection errors and running on Travis
1 parent ba5e27e commit ec0ed8a

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

pre_commit/languages/helpers.py

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

33
import multiprocessing
4+
import os
45
import shlex
56

67
from pre_commit.util import cmd_output
@@ -52,5 +53,11 @@ def target_concurrency(hook):
5253
if hook['require_serial']:
5354
return 1
5455
else:
55-
# TODO: something smart!
56-
return multiprocessing.cpu_count()
56+
# Travis appears to have a bunch of CPUs, but we can't use them all.
57+
if 'TRAVIS' in os.environ:
58+
return 2
59+
else:
60+
try:
61+
return multiprocessing.cpu_count()
62+
except NotImplementedError:
63+
return 1

tests/languages/helpers_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from __future__ import absolute_import
22
from __future__ import unicode_literals
33

4+
import multiprocessing
5+
import os
46
import sys
57

8+
import mock
69
import pytest
710

811
from pre_commit.languages import helpers
@@ -28,3 +31,25 @@ def test_failed_setup_command_does_not_unicode_error():
2831
# an assertion that this does not raise `UnicodeError`
2932
with pytest.raises(CalledProcessError):
3033
helpers.run_setup_cmd(Prefix('.'), (sys.executable, '-c', script))
34+
35+
36+
def test_target_concurrency_normal():
37+
with mock.patch.object(multiprocessing, 'cpu_count', return_value=123):
38+
with mock.patch.dict(os.environ, {}, clear=True):
39+
assert helpers.target_concurrency({'require_serial': False}) == 123
40+
41+
42+
def test_target_concurrency_cpu_count_require_serial_true():
43+
assert helpers.target_concurrency({'require_serial': True}) == 1
44+
45+
46+
def test_target_concurrency_on_travis():
47+
with mock.patch.dict(os.environ, {'TRAVIS': '1'}, clear=True):
48+
assert helpers.target_concurrency({'require_serial': False}) == 2
49+
50+
51+
def test_target_concurrency_cpu_count_not_implemented():
52+
with mock.patch.object(
53+
multiprocessing, 'cpu_count', side_effect=NotImplementedError,
54+
):
55+
assert helpers.target_concurrency({'require_serial': False}) == 1

0 commit comments

Comments
 (0)