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
This commit is contained in:
@@ -18,18 +18,22 @@ final class DotEnvTest extends TestCase
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function setUp(): void
|
public static function setUpBeforeClass(): void
|
||||||
{
|
{
|
||||||
// create .env files
|
// create .env files
|
||||||
$file_content = __DIR__ . DIRECTORY_SEPARATOR
|
$file_content = __DIR__ . DIRECTORY_SEPARATOR
|
||||||
. 'dotenv' . DIRECTORY_SEPARATOR
|
. 'dotenv' . DIRECTORY_SEPARATOR
|
||||||
. 'test.env';
|
. 'test.env';
|
||||||
|
// copy to all folder levels
|
||||||
$env_files = [
|
$env_files = [
|
||||||
__DIR__ . DIRECTORY_SEPARATOR
|
__DIR__ . DIRECTORY_SEPARATOR
|
||||||
. 'dotenv' . DIRECTORY_SEPARATOR
|
. 'dotenv' . DIRECTORY_SEPARATOR
|
||||||
. '.env',
|
. '.env',
|
||||||
__DIR__ . DIRECTORY_SEPARATOR
|
__DIR__ . DIRECTORY_SEPARATOR
|
||||||
. '.env',
|
. '.env',
|
||||||
|
__DIR__ . DIRECTORY_SEPARATOR
|
||||||
|
. '..' . DIRECTORY_SEPARATOR
|
||||||
|
. '.env',
|
||||||
];
|
];
|
||||||
// if not found, skip -> all will fail
|
// if not found, skip -> all will fail
|
||||||
if (is_file($file_content)) {
|
if (is_file($file_content)) {
|
||||||
@@ -102,21 +106,24 @@ final class DotEnvTest extends TestCase
|
|||||||
'file' => 'cannot_read.env',
|
'file' => 'cannot_read.env',
|
||||||
'status' => 2,
|
'status' => 2,
|
||||||
'content' => [],
|
'content' => [],
|
||||||
'chmod' => '000',
|
// 0000
|
||||||
|
'chmod' => '100000',
|
||||||
],
|
],
|
||||||
'empty file' => [
|
'empty file' => [
|
||||||
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
||||||
'file' => 'empty.env',
|
'file' => 'empty.env',
|
||||||
'status' => 1,
|
'status' => 1,
|
||||||
'content' => [],
|
'content' => [],
|
||||||
'chmod' => null,
|
// 0664
|
||||||
|
'chmod' => '100664',
|
||||||
],
|
],
|
||||||
'override all' => [
|
'override all' => [
|
||||||
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
||||||
'file' => 'test.env',
|
'file' => 'test.env',
|
||||||
'status' => 0,
|
'status' => 0,
|
||||||
'content' => $dot_env_content,
|
'content' => $dot_env_content,
|
||||||
'chmod' => null,
|
// 0664
|
||||||
|
'chmod' => '100664',
|
||||||
],
|
],
|
||||||
'override directory' => [
|
'override directory' => [
|
||||||
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
'folder' => __DIR__ . DIRECTORY_SEPARATOR . 'dotenv',
|
||||||
@@ -149,8 +156,21 @@ final class DotEnvTest extends TestCase
|
|||||||
array $expected_env,
|
array $expected_env,
|
||||||
?string $chmod
|
?string $chmod
|
||||||
): void {
|
): 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;
|
$old_chmod = null;
|
||||||
|
// if we have change permission for file
|
||||||
if (
|
if (
|
||||||
is_file($folder . DIRECTORY_SEPARATOR . $file) &&
|
is_file($folder . DIRECTORY_SEPARATOR . $file) &&
|
||||||
!empty($chmod)
|
!empty($chmod)
|
||||||
|
|||||||
Reference in New Issue
Block a user