Handle CPU detection errors and running on Travis

This commit is contained in:
Chris Kuehl
2018-10-20 17:13:57 -07:00
committed by Chris Kuehl
parent ba5e27e4ec
commit ec0ed8aef5
2 changed files with 34 additions and 2 deletions

View File

@@ -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