mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 16:24:40 +04:00
Handle CPU detection errors and running on Travis
This commit is contained in:
parent
ba5e27e4ec
commit
ec0ed8aef5
2 changed files with 34 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import multiprocessing
|
||||
import os
|
||||
import shlex
|
||||
|
||||
from pre_commit.util import cmd_output
|
||||
|
|
@ -52,5 +53,11 @@ def target_concurrency(hook):
|
|||
if hook['require_serial']:
|
||||
return 1
|
||||
else:
|
||||
# TODO: something smart!
|
||||
return multiprocessing.cpu_count()
|
||||
# Travis appears to have a bunch of CPUs, but we can't use them all.
|
||||
if 'TRAVIS' in os.environ:
|
||||
return 2
|
||||
else:
|
||||
try:
|
||||
return multiprocessing.cpu_count()
|
||||
except NotImplementedError:
|
||||
return 1
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
from __future__ import absolute_import
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import multiprocessing
|
||||
import os
|
||||
import sys
|
||||
|
||||
import mock
|
||||
import pytest
|
||||
|
||||
from pre_commit.languages import helpers
|
||||
|
|
@ -28,3 +31,25 @@ def test_failed_setup_command_does_not_unicode_error():
|
|||
# an assertion that this does not raise `UnicodeError`
|
||||
with pytest.raises(CalledProcessError):
|
||||
helpers.run_setup_cmd(Prefix('.'), (sys.executable, '-c', script))
|
||||
|
||||
|
||||
def test_target_concurrency_normal():
|
||||
with mock.patch.object(multiprocessing, 'cpu_count', return_value=123):
|
||||
with mock.patch.dict(os.environ, {}, clear=True):
|
||||
assert helpers.target_concurrency({'require_serial': False}) == 123
|
||||
|
||||
|
||||
def test_target_concurrency_cpu_count_require_serial_true():
|
||||
assert helpers.target_concurrency({'require_serial': True}) == 1
|
||||
|
||||
|
||||
def test_target_concurrency_on_travis():
|
||||
with mock.patch.dict(os.environ, {'TRAVIS': '1'}, clear=True):
|
||||
assert helpers.target_concurrency({'require_serial': False}) == 2
|
||||
|
||||
|
||||
def test_target_concurrency_cpu_count_not_implemented():
|
||||
with mock.patch.object(
|
||||
multiprocessing, 'cpu_count', side_effect=NotImplementedError,
|
||||
):
|
||||
assert helpers.target_concurrency({'require_serial': False}) == 1
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue