From f97cce1c56a561cdfd964b4afb0f2682fb212e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Sat, 10 Dec 2022 22:05:43 +0100 Subject: [PATCH] Fix the Wmic commands (#83) --- e2e/expected-output-windows | 4 ++-- src/Finder/WmicLogicalFinder.php | 15 +++++++++++++++ src/Finder/WmicPhysicalFinder.php | 17 ++++++++++++++++- tests/Finder/WmicLogicalFinderTest.php | 15 +++++++++++++++ tests/Finder/WmicPhysicalFinderTest.php | 15 +++++++++++++++ 5 files changed, 63 insertions(+), 3 deletions(-) diff --git a/e2e/expected-output-windows b/e2e/expected-output-windows index 6118f6a..86ab40d 100644 --- a/e2e/expected-output-windows +++ b/e2e/expected-output-windows @@ -9,5 +9,5 @@ NProcFinder(all=false): F NullCpuCoreFinder: F OnlyOnWindowsFinder(DummyCpuCoreFinder(value=1)): . SkipOnWindowsFinder(DummyCpuCoreFinder(value=1)): F -WmicPhysicalFinder: F -WmicLogicalFinder: F +WmicPhysicalFinder: . +WmicLogicalFinder: . diff --git a/src/Finder/WmicLogicalFinder.php b/src/Finder/WmicLogicalFinder.php index 2451a49..3652538 100644 --- a/src/Finder/WmicLogicalFinder.php +++ b/src/Finder/WmicLogicalFinder.php @@ -13,6 +13,8 @@ namespace Fidry\CpuCoreCounter\Finder; +use function preg_match; + /** * Find the number of logical CPU cores for Windows. * @@ -20,6 +22,8 @@ */ final class WmicLogicalFinder extends ProcOpenBasedFinder { + private const CPU_CORE_COUNT_REGEX = '/NumberOfLogicalProcessors[\s\n]+(?\d+)/'; + protected function getCommand(): string { return 'wmic cpu get NumberOfLogicalProcessors'; @@ -29,4 +33,15 @@ public function toString(): string { return 'WmicLogicalFinder'; } + + public static function countCpuCores(string $process): ?int + { + if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) { + return parent::countCpuCores($process); + } + + $count = $matches['count']; + + return parent::countCpuCores($count); + } } diff --git a/src/Finder/WmicPhysicalFinder.php b/src/Finder/WmicPhysicalFinder.php index b33f3dc..5a60197 100644 --- a/src/Finder/WmicPhysicalFinder.php +++ b/src/Finder/WmicPhysicalFinder.php @@ -13,6 +13,8 @@ namespace Fidry\CpuCoreCounter\Finder; +use function preg_match; + /** * Find the number of physical CPU cores for Windows. * @@ -20,13 +22,26 @@ */ final class WmicPhysicalFinder extends ProcOpenBasedFinder { + private const CPU_CORE_COUNT_REGEX = '/NumberOfCores[\s\n]+(?\d+)/'; + protected function getCommand(): string { - return 'wmic cpu get NumberOfProcessors'; + return 'wmic cpu get NumberOfCores'; } public function toString(): string { return 'WmicPhysicalFinder'; } + + public static function countCpuCores(string $process): ?int + { + if (0 === preg_match(self::CPU_CORE_COUNT_REGEX, $process, $matches)) { + return parent::countCpuCores($process); + } + + $count = $matches['count']; + + return parent::countCpuCores($count); + } } diff --git a/tests/Finder/WmicLogicalFinderTest.php b/tests/Finder/WmicLogicalFinderTest.php index 01a765d..fe01efd 100644 --- a/tests/Finder/WmicLogicalFinderTest.php +++ b/tests/Finder/WmicLogicalFinderTest.php @@ -27,4 +27,19 @@ protected function getFinder(): ProcOpenBasedFinder { return new WmicLogicalFinder(); } + + public static function processResultProvider(): iterable + { + yield from parent::processResultProvider(); + + yield 'example from the GitHub Actions machine' => [ + <<<'EOF' +NumberOfLogicalProcessors + +2 +EOF + , + 2, + ]; + } } diff --git a/tests/Finder/WmicPhysicalFinderTest.php b/tests/Finder/WmicPhysicalFinderTest.php index b3f76e4..75131a6 100644 --- a/tests/Finder/WmicPhysicalFinderTest.php +++ b/tests/Finder/WmicPhysicalFinderTest.php @@ -27,4 +27,19 @@ protected function getFinder(): ProcOpenBasedFinder { return new WmicPhysicalFinder(); } + + public static function processResultProvider(): iterable + { + yield from parent::processResultProvider(); + + yield 'example from the GitHub Actions machine' => [ + <<<'EOF' +NumberOfCores + +2 +EOF + , + 2, + ]; + } }