From 225e3e792989fee9b7f42e43b350df0dea231896 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 12 Jan 2023 11:42:15 +0900 Subject: [PATCH] Unit test fixes with permissions - in case the unit test is run as root, skip test for cannot read (0000) - set read (0664) for all must read files - write .env file int all folders for test so that __DIR__ base will always find one --- test/phpUnitTests/DotEnvTest.php | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/test/phpUnitTests/DotEnvTest.php b/test/phpUnitTests/DotEnvTest.php index 00e5910..91d2e71 100644 --- a/test/phpUnitTests/DotEnvTest.php +++ b/test/phpUnitTests/DotEnvTest.php @@ -18,18 +18,22 @@ final class DotEnvTest extends TestCase * * @return void */ - protected function setUp(): void + public static function setUpBeforeClass(): void { // create .env files $file_content = __DIR__ . DIRECTORY_SEPARATOR . 'dotenv' . DIRECTORY_SEPARATOR . 'test.env'; + // copy to all folder levels $env_files = [ __DIR__ . DIRECTORY_SEPARATOR . 'dotenv' . DIRECTORY_SEPARATOR . '.env', __DIR__ . DIRECTORY_SEPARATOR . '.env', + __DIR__ . DIRECTORY_SEPARATOR + . '..' . DIRECTORY_SEPARATOR + . '.env', ]; // if not found, skip -> all will fail if (is_file($file_content)) { @@ -102,21 +106,24 @@ final class DotEnvTest extends TestCase 'file' => 'cannot_read.env', 'status' => 2, 'content' => [], - 'chmod' => '000', + // 0000 + 'chmod' => '100000', ], 'empty file' => [ 'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv', 'file' => 'empty.env', 'status' => 1, 'content' => [], - 'chmod' => null, + // 0664 + 'chmod' => '100664', ], 'override all' => [ 'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv', 'file' => 'test.env', 'status' => 0, 'content' => $dot_env_content, - 'chmod' => null, + // 0664 + 'chmod' => '100664', ], 'override directory' => [ 'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv', @@ -149,8 +156,21 @@ final class DotEnvTest extends TestCase array $expected_env, ?string $chmod ): void { - // if we have file + chmod set + if ( + !empty($chmod) && + $chmod == '100000' && + getmyuid() == 0 + ) { + $this->markTestSkipped( + "Skip cannot read file test because run user is root" + ); + return; + } + // reset $_ENV for clean compare + $_ENV = []; + // previous file perm $old_chmod = null; + // if we have change permission for file if ( is_file($folder . DIRECTORY_SEPARATOR . $file) && !empty($chmod)