Skip to content

Commit

Permalink
Merge pull request #441 from nextcloud/enh/raspi-cpu-name
Browse files Browse the repository at this point in the history
refactor(cpu): simplify cpu name reading
  • Loading branch information
kesselb committed Apr 23, 2023
2 parents 6008be7 + c03367a commit 02cb078
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/OperatingSystems/DefaultOs.php
Expand Up @@ -89,16 +89,16 @@ public function getCpuName(): string {
}

$matches = [];
$pattern = '/model name\s:\s(.+)/';

if (str_contains($cpuinfo, 'Raspberry Pi')) {
$pattern = '/Model\s+:\s(.+)/';
} else {
$pattern = '/model name\s:\s(.+)/';
}

$result = preg_match_all($pattern, $cpuinfo, $matches);
if ($result === 0 || $result === false) {
// For Raspberry Pi 4B
$pattern = '/Model\s+:\s(.+)/';
$result = preg_match_all($pattern, $cpuinfo, $matches);
if ($result === 0 || $result === false) {
return $data;
}
return $data;
}

$model = $matches[1][0];
Expand Down
44 changes: 44 additions & 0 deletions tests/data/linux_cpuinfo_pi3b
@@ -0,0 +1,44 @@
processor : 0
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4

processor : 1
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4

processor : 2
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4

processor : 3
model name : ARMv7 Processor rev 4 (v7l)
BogoMIPS : 38.40
Features : half thumb fastmult vfp edsp neon vfpv3 tls vfpv4 idiva idivt vfpd32 lpae evtstrm crc32
CPU implementer : 0x41
CPU architecture: 7
CPU variant : 0x0
CPU part : 0xd03
CPU revision : 4

Hardware : BCM2835
Revision : a02082
Serial : 000000001e625fa1
Model : Raspberry Pi 3 Model B Rev 1.2
12 changes: 10 additions & 2 deletions tests/lib/DefaultOsTest.php
Expand Up @@ -101,6 +101,14 @@ public function testGetCPUNameOneCore(): void {
$this->assertEquals('Intel(R) Core(TM) i5-6500 CPU @ 3.20GHz (1 core)', $this->os->getCpuName());
}

public function testGetCPUNamePi3b(): void {
$this->os->method('readContent')
->with('/proc/cpuinfo')
->willReturn(file_get_contents(__DIR__ . '/../data/linux_cpuinfo_pi3b'));

$this->assertEquals('Raspberry Pi 3 Model B Rev 1.2 (4 cores)', $this->os->getCpuName());
}

public function testGetCPUNamePi4b(): void {
$this->os->method('readContent')
->with('/proc/cpuinfo')
Expand All @@ -109,15 +117,15 @@ public function testGetCPUNamePi4b(): void {
$this->assertEquals('Raspberry Pi 4 Model B Rev 1.2 (4 cores)', $this->os->getCpuName());
}

public function testGetCPUNameNoData(): void {
public function testGetCpuNameNoData(): void {
$this->os->method('readContent')
->with('/proc/cpuinfo')
->willThrowException(new RuntimeException('Unable to read: "/proc/cpuinfo"'));

$this->assertEquals('Unknown Processor', $this->os->getCpuName());
}

public function testGetCPUNameInvalidData(): void {
public function testGetCpuNameInvalidData(): void {
$this->os->method('readContent')
->with('/proc/cpuinfo')
->willReturn('invalid_data');
Expand Down

0 comments on commit 02cb078

Please sign in to comment.