6
6
from __future__ import annotations
7
7
8
8
import os
9
+ import pathlib
9
10
import sys
10
11
from collections .abc import Callable
11
- from unittest .mock import patch
12
+ from unittest .mock import MagicMock , mock_open , patch
12
13
13
14
import pytest
14
15
from py ._path .local import LocalPath # type: ignore[import]
15
16
16
17
from pylint import run_epylint , run_pylint , run_pyreverse , run_symilar
18
+ from pylint .lint import Run
19
+ from pylint .testutils import GenericTestReporter as Reporter
17
20
18
21
19
22
@pytest .mark .parametrize (
@@ -40,3 +43,35 @@ def test_runner_with_arguments(runner: Callable, tmpdir: LocalPath) -> None:
40
43
with pytest .raises (SystemExit ) as err :
41
44
runner (testargs )
42
45
assert err .value .code == 0
46
+
47
+
48
+ def test_pylint_run_jobs_equal_zero_dont_crash_with_cpu_fraction (
49
+ tmpdir : LocalPath ,
50
+ ) -> None :
51
+ """Check that the pylint runner does not crash if `pylint.lint.run._query_cpu`
52
+ determines only a fraction of a CPU core to be available.
53
+ """
54
+ builtin_open = open
55
+
56
+ def _mock_open (* args , ** kwargs ):
57
+ if args [0 ] == "/sys/fs/cgroup/cpu/cpu.cfs_quota_us" :
58
+ return mock_open (read_data = b"-1" )(* args , ** kwargs )
59
+ if args [0 ] == "/sys/fs/cgroup/cpu/cpu.shares" :
60
+ return mock_open (read_data = b"2" )(* args , ** kwargs )
61
+ return builtin_open (* args , ** kwargs )
62
+
63
+ pathlib_path = pathlib .Path
64
+
65
+ def _mock_path (* args , ** kwargs ):
66
+ if args [0 ] == "/sys/fs/cgroup/cpu/cpu.shares" :
67
+ return MagicMock (is_file = lambda : True )
68
+ return pathlib_path (* args , ** kwargs )
69
+
70
+ filepath = os .path .abspath (__file__ )
71
+ testargs = [filepath , "--jobs=0" ]
72
+ with tmpdir .as_cwd ():
73
+ with pytest .raises (SystemExit ) as err :
74
+ with patch ("builtins.open" , _mock_open ):
75
+ with patch ("pylint.lint.run.Path" , _mock_path ):
76
+ Run (testargs , reporter = Reporter ())
77
+ assert err .value .code == 0
0 commit comments