Page MenuHomeDevCentral

D2278.id5741.diff
No OneTemporary

D2278.id5741.diff

diff --git a/src/OS/CurrentOS.php b/src/OS/CurrentOS.php
new file mode 100644
--- /dev/null
+++ b/src/OS/CurrentOS.php
@@ -0,0 +1,16 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\OS;
+
+class CurrentOS {
+
+ public static function isWindows () : bool {
+ return PHP_OS === 'CYGWIN' || self::isPureWindows();
+ }
+
+ public static function isPureWindows () : bool {
+ return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
+ }
+
+}
diff --git a/src/OS/CurrentProcess.php b/src/OS/CurrentProcess.php
new file mode 100644
--- /dev/null
+++ b/src/OS/CurrentProcess.php
@@ -0,0 +1,34 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\OS;
+
+class CurrentProcess {
+
+ /**
+ * Determines if the current process, ie the PHP interpreter,
+ * runs as root on UNIX systems or in elevated mode on Windows.
+ *
+ * Cygwin processes are considered as Windows processes.
+ */
+ public static function isPrivileged () : bool {
+ if (CurrentOS::isWindows()) {
+ // `net session` is known to only work as privileged process.
+ // To wrap in cmd allows to avoid /dev/null for Cygwin,
+ // or $null when invoked from PowerShell. NUL: will always be used.
+ exec('cmd /C "net session >NUL 2>&1"', $_, $exitCode);
+
+ return $exitCode === 0;
+ }
+
+ if (!function_exists('posix_geteuid')) {
+ // POSIX PHP functions aren't always available, e.g. on FreeBSD
+ // In such cases, `id` will probably be available.
+ return trim(shell_exec('id -u')) === '0';
+ }
+
+ /** @noinspection PhpComposerExtensionStubsInspection */
+ return posix_geteuid() === 0;
+ }
+
+}
diff --git a/tests/Reflection/CodeFileTest.php b/tests/Reflection/CodeFileTest.php
--- a/tests/Reflection/CodeFileTest.php
+++ b/tests/Reflection/CodeFileTest.php
@@ -3,6 +3,7 @@
namespace Keruald\OmniTools\Tests\Reflection;
+use Keruald\OmniTools\OS\CurrentProcess;
use Keruald\OmniTools\Reflection\CodeFile;
use Keruald\OmniTools\Tests\WithData;
use PHPUnit\Framework\TestCase;
@@ -36,6 +37,12 @@
}
public function testCanBeIncludedWhenFileModeForbidsReading () : void {
+ if (CurrentProcess::isPrivileged()) {
+ $this->markTestSkipped(
+ "This test requires non-root access to run properly."
+ );
+ }
+
$file = $this->getNonReadableFile();
$this->assertFalse(CodeFile::From($file)->canBeIncluded());
@@ -55,6 +62,12 @@
}
public function testIsReadableWhenFileModeForbidsReading () : void {
+ if (CurrentProcess::isPrivileged()) {
+ $this->markTestSkipped(
+ "This test requires non-root access to run properly."
+ );
+ }
+
$file = $this->getNonReadableFile();
$this->assertFalse(CodeFile::From($file)->isReadable());

File Metadata

Mime Type
text/plain
Expires
Mon, Jan 20, 03:33 (21 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2361440
Default Alt Text
D2278.id5741.diff (2 KB)

Event Timeline