diff --git a/infection.json5 b/infection.json5 index 2e2680d..36a0cf5 100644 --- a/infection.json5 +++ b/infection.json5 @@ -29,12 +29,19 @@ "Fidry\\CpuCoreCounter\\Executor\\ProcOpenExecutor::execute" ] }, + "Continue_": false, "FunctionCallRemoval": { "ignore": [ // I can't find a case in practice where this would happen - "Fidry\\CpuCoreCounter\\Executor\\ProcOpenExecutor::execute", + "Fidry\\CpuCoreCounter\\Executor\\ProcOpenExecutor::execute" ] }, - "PublicVisibility": false + "PublicVisibility": false, + "TrueValue": { + "ignore": [ + // This is a case where the value does not matter + "Fidry\\CpuCoreCounter\\Finder\\LscpuPhysicalFinder::countCpuCores" + ] + } } } diff --git a/src/Finder/FinderRegistry.php b/src/Finder/FinderRegistry.php index 7cb2607..4f82458 100644 --- a/src/Finder/FinderRegistry.php +++ b/src/Finder/FinderRegistry.php @@ -54,8 +54,8 @@ public static function getDefaultLogicalFinders(): array new HwLogicalFinder(), new _NProcessorFinder(), new NProcessorFinder(), - new CpuInfoFinder(), new LscpuLogicalFinder(), + new CpuInfoFinder(), ]; } diff --git a/src/Finder/LscpuLogicalFinder.php b/src/Finder/LscpuLogicalFinder.php index da81a15..bce09eb 100644 --- a/src/Finder/LscpuLogicalFinder.php +++ b/src/Finder/LscpuLogicalFinder.php @@ -13,6 +13,12 @@ namespace Fidry\CpuCoreCounter\Finder; +use function count; +use function explode; +use function is_array; +use function preg_grep; +use const PHP_EOL; + /** * The number of logical cores. * @@ -25,13 +31,12 @@ public function getCommand(): string return 'lscpu -p'; } - protected function countCpuCores(string $lscpu): ?int + protected function countCpuCores(string $process): ?int { - $lines = explode(PHP_EOL, $lscpu); - - $actualLines = preg_grep('/^[0-9]+\,/', $lines); + $lines = explode(PHP_EOL, $process); + $actualLines = preg_grep('/^\d+,/', $lines); - if (false === $actualLines) { + if (!is_array($actualLines)) { return null; } diff --git a/src/Finder/LscpuPhysicalFinder.php b/src/Finder/LscpuPhysicalFinder.php index 95abc7a..58523ce 100644 --- a/src/Finder/LscpuPhysicalFinder.php +++ b/src/Finder/LscpuPhysicalFinder.php @@ -13,6 +13,13 @@ namespace Fidry\CpuCoreCounter\Finder; +use function count; +use function explode; +use function is_array; +use function preg_grep; +use function strtok; +use const PHP_EOL; + /** * The number of physical processors. * @@ -30,13 +37,12 @@ public function getCommand(): string return 'lscpu -p'; } - protected function countCpuCores(string $lscpu): ?int + protected function countCpuCores(string $process): ?int { - $lines = explode(PHP_EOL, $lscpu); - - $actualLines = preg_grep('/^[0-9]+\,/', $lines); + $lines = explode(PHP_EOL, $process); + $actualLines = preg_grep('/^\d+/', $lines); - if (false === $actualLines) { + if (!is_array($actualLines)) { return null; } @@ -51,7 +57,6 @@ protected function countCpuCores(string $lscpu): ?int $cores[$core] = true; } - unset($cores['-']); $count = count($cores); diff --git a/tests/Finder/LscpuLogicalFinderTest.php b/tests/Finder/LscpuLogicalFinderTest.php index fe9cbaa..57179c4 100644 --- a/tests/Finder/LscpuLogicalFinderTest.php +++ b/tests/Finder/LscpuLogicalFinderTest.php @@ -13,68 +13,84 @@ namespace Fidry\CpuCoreCounter\Test\Finder; -use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; +use Fidry\CpuCoreCounter\Executor\ProcessExecutor; use Fidry\CpuCoreCounter\Finder\LscpuLogicalFinder; -use Fidry\CpuCoreCounter\Test\Executor\DummyExecutor; -use PHPUnit\Framework\TestCase; +use Fidry\CpuCoreCounter\Finder\ProcOpenBasedFinder; /** * @covers \Fidry\CpuCoreCounter\Finder\LscpuLogicalFinder * * @internal */ -final class LscpuLogicalFinderTest extends TestCase +final class LscpuLogicalFinderTest extends ProcOpenBasedFinderTestCase { - /** - * @var DummyExecutor - */ - protected $executor; - - protected function setUp(): void + public static function processResultProvider(): iterable { - $this->executor = new DummyExecutor(); - } + yield 'command could not be executed' => [ + null, + null, + ]; - protected function tearDown(): void - { - unset($this->executor); - } + yield 'empty stdout & stderr' => [ + ['', ''], + null, + ]; - /** - * @dataProvider finderProvider - */ - public function test_it_can_describe_itself(CpuCoreFinder $finder, string $expected): void - { - $actual = $finder->toString(); + yield 'whitespace stdout' => [ + [' ', ''], + null, + ]; - self::assertSame($expected, $actual); - } + yield 'whitespace stderr' => [ + ['', ' '], + null, + ]; - public static function finderProvider(): iterable - { - yield [ - new LscpuLogicalFinder(), - FinderShortClassName::get(new LscpuLogicalFinder()) + yield 'whitespace stdout & stderr' => [ + [' ', ' '], + null, ]; - } - /** - * @dataProvider lscpuProvider - */ - public function test_it_can_count_the_number_of_cpu_cores( - ?array $processResult, - ?int $expected - ): void { - $finder = new LscpuLogicalFinder($this->executor); - $this->executor->setOutput($processResult); + yield 'linux line return for stdout' => [ + ["\n", ''], + null, + ]; - $actual = $finder->find(); + yield 'linux line return for stderr' => [ + ['', "\n"], + null, + ]; - self::assertSame($expected, $actual); - } + yield 'linux line return for stdout & stderr' => [ + ["\n", "\n"], + null, + ]; + + yield 'windows line return for stdout' => [ + ["\r\n", ''], + null, + ]; + + yield 'windows line return for stderr' => [ + ['', "\r\n"], + null, + ]; + + yield 'windows line return for stdout & stderr' => [ + ["\r\n", "\r\n"], + null, + ]; + + yield 'no processor' => [ + ['0', ''], + null, + ]; + + yield 'valid result with stderr' => [ + ['3', 'something'], + null, + ]; - public static function lscpuProvider(): iterable - { yield 'example with four logical but two physical' => [ [ <<<'EOF' @@ -122,4 +138,9 @@ public static function lscpuProvider(): iterable null ]; } + + protected function createFinder(ProcessExecutor $executor): ProcOpenBasedFinder + { + return new LscpuLogicalFinder($executor); + } } diff --git a/tests/Finder/LscpuPhysicalFinderTest.php b/tests/Finder/LscpuPhysicalFinderTest.php index e0ffa47..2a57b96 100644 --- a/tests/Finder/LscpuPhysicalFinderTest.php +++ b/tests/Finder/LscpuPhysicalFinderTest.php @@ -13,68 +13,84 @@ namespace Fidry\CpuCoreCounter\Test\Finder; -use Fidry\CpuCoreCounter\Finder\CpuCoreFinder; +use Fidry\CpuCoreCounter\Executor\ProcessExecutor; use Fidry\CpuCoreCounter\Finder\LscpuPhysicalFinder; -use Fidry\CpuCoreCounter\Test\Executor\DummyExecutor; -use PHPUnit\Framework\TestCase; +use Fidry\CpuCoreCounter\Finder\ProcOpenBasedFinder; /** * @covers \Fidry\CpuCoreCounter\Finder\LscpuPhysicalFinder * * @internal */ -final class LscpuPhysicalFinderTest extends TestCase +final class LscpuPhysicalFinderTest extends ProcOpenBasedFinderTestCase { - /** - * @var DummyExecutor - */ - protected $executor; - - protected function setUp(): void + public static function processResultProvider(): iterable { - $this->executor = new DummyExecutor(); - } + yield 'command could not be executed' => [ + null, + null, + ]; - protected function tearDown(): void - { - unset($this->executor); - } + yield 'empty stdout & stderr' => [ + ['', ''], + null, + ]; - /** - * @dataProvider finderProvider - */ - public function test_it_can_describe_itself(CpuCoreFinder $finder, string $expected): void - { - $actual = $finder->toString(); + yield 'whitespace stdout' => [ + [' ', ''], + null, + ]; - self::assertSame($expected, $actual); - } + yield 'whitespace stderr' => [ + ['', ' '], + null, + ]; - public static function finderProvider(): iterable - { - yield [ - new LscpuPhysicalFinder(), - FinderShortClassName::get(new LscpuPhysicalFinder()) + yield 'whitespace stdout & stderr' => [ + [' ', ' '], + null, ]; - } - /** - * @dataProvider lscpuProvider - */ - public function test_it_can_count_the_number_of_cpu_cores( - ?array $processResult, - ?int $expected - ): void { - $finder = new LscpuPhysicalFinder($this->executor); - $this->executor->setOutput($processResult); + yield 'linux line return for stdout' => [ + ["\n", ''], + null, + ]; - $actual = $finder->find(); + yield 'linux line return for stderr' => [ + ['', "\n"], + null, + ]; - self::assertSame($expected, $actual); - } + yield 'linux line return for stdout & stderr' => [ + ["\n", "\n"], + null, + ]; + + yield 'windows line return for stdout' => [ + ["\r\n", ''], + null, + ]; + + yield 'windows line return for stderr' => [ + ['', "\r\n"], + null, + ]; + + yield 'windows line return for stdout & stderr' => [ + ["\r\n", "\r\n"], + null, + ]; + + yield 'no processor' => [ + ['0', ''], + null, + ]; + + yield 'valid result with stderr' => [ + ['3', 'something'], + null, + ]; - public static function lscpuProvider(): iterable - { yield 'example with four logical but two physical' => [ [ <<<'EOF' @@ -139,4 +155,9 @@ public static function lscpuProvider(): iterable null ]; } + + protected function createFinder(ProcessExecutor $executor): ProcOpenBasedFinder + { + return new LscpuPhysicalFinder($executor); + } }