mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-19 00:54:42 +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
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
|
import os
|
||||||
import shlex
|
import shlex
|
||||||
|
|
||||||
from pre_commit.util import cmd_output
|
from pre_commit.util import cmd_output
|
||||||
|
|
@ -52,5 +53,11 @@ def target_concurrency(hook):
|
||||||
if hook['require_serial']:
|
if hook['require_serial']:
|
||||||
return 1
|
return 1
|
||||||
else:
|
else:
|
||||||
# TODO: something smart!
|
# Travis appears to have a bunch of CPUs, but we can't use them all.
|
||||||
return multiprocessing.cpu_count()
|
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 absolute_import
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import multiprocessing
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import mock
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from pre_commit.languages import helpers
|
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`
|
# an assertion that this does not raise `UnicodeError`
|
||||||
with pytest.raises(CalledProcessError):
|
with pytest.raises(CalledProcessError):
|
||||||
helpers.run_setup_cmd(Prefix('.'), (sys.executable, '-c', script))
|
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