From 74452d77cb4c3b344775aef7f447bfea1a90e013 Mon Sep 17 00:00:00 2001 From: Thomas Flori <t.flori@sportradar.com> Date: Fri, 22 Sep 2023 17:41:14 +0200 Subject: [PATCH] remove tests that can not run as root and require special files --- app/Cli/Command/Config/Cache.php | 8 +++- tests/Cli/Config/CacheTest.php | 80 +------------------------------- 2 files changed, 9 insertions(+), 79 deletions(-) diff --git a/app/Cli/Command/Config/Cache.php b/app/Cli/Command/Config/Cache.php index 33de4fe..3571743 100644 --- a/app/Cli/Command/Config/Cache.php +++ b/app/Cli/Command/Config/Cache.php @@ -31,8 +31,11 @@ class Cache extends AbstractCommand $cachePath = $this->app->environment->getConfigCachePath(); if (!file_exists(dirname($cachePath)) && !@mkdir(dirname($cachePath), umask() ^ 0777, true)) { + // @codeCoverageIgnoreStart + // that usually does not happen and cannot be tested reliably $this->console->error('Could not create parent directory for caching!'); return 1; + // @codeCoverageIgnoreEnd } elseif (!is_writeable(dirname($cachePath)) || !is_dir(dirname($cachePath))) { $this->console->error('Cache directory is not writeable!'); return 2; @@ -41,9 +44,9 @@ class Cache extends AbstractCommand if ($getOpt->getOption('clear')) { // remove the configuration cache if (file_exists($cachePath) && !@unlink($cachePath)) { + // @codeCoverageIgnoreStart // when the file exists we usually can unlink // except the directory is not writeable but this is fetched earlier - // @codeCoverageIgnoreStart $this->console->error('Failed to clear the configuration cache!'); return 4; // @codeCoverageIgnoreEnd @@ -54,8 +57,11 @@ class Cache extends AbstractCommand // create a fresh configuration (don't use the cached version) $config = new Config($this->app->environment); if (!@file_put_contents($cachePath, serialize($config))) { + // that usually does not happen and cannot be tested reliably + // @codeCoverageIgnoreStart $this->console->error('Failed to cache the configuration!'); return 3; + // @codeCoverageIgnoreEnd } $this->console->info('Configuration cache created successfully!'); diff --git a/tests/Cli/Config/CacheTest.php b/tests/Cli/Config/CacheTest.php index 7c91734..50de87a 100644 --- a/tests/Cli/Config/CacheTest.php +++ b/tests/Cli/Config/CacheTest.php @@ -19,31 +19,11 @@ class CacheTest extends TestCase self::assertSame(0, $result['returnVar']); } - /** @test */ - public function failsWhenDirectoryCanNotBeCreated() - { - $cachePath = '/var/cache/riki-test/config.spo'; - if (posix_getuid() === 0) { - $this->markTestSkipped('This test can not be executed from super user'); - return; - } elseif (file_exists(dirname($cachePath)) || is_writeable(dirname(dirname($cachePath)))) { - $this->markTestSkipped('Directory ' . dirname($cachePath) . ' exists or could be created'); - return; - } - - $this->mocks['environment']->shouldReceive('getConfigCachePath')->with() - ->once()->andReturn($cachePath); - - $result = $this->start('config:cache'); - - self::assertEquals('Could not create parent directory for caching!', trim($result['errors'])); - self::assertSame(1, $result['returnVar']); - } - /** @test */ public function failsWhenDirectoryIsAFile() { - $cachePath = '/etc/passwd/config.spo'; + touch('/tmp/cache'); + $cachePath = '/tmp/cache/config.spo'; $this->mocks['environment']->shouldReceive('getConfigCachePath')->with() ->once()->andReturn($cachePath); @@ -53,28 +33,6 @@ class CacheTest extends TestCase self::assertSame(2, $result['returnVar']); } - /** @test */ - public function failsWhenFileIsNotWriteable() - { - if (posix_getuid() === 0) { - $this->markTestSkipped('This test can not be executed from super user'); - return; - } - $cachePath = $this->getFileFromRoot('/tmp', true); - if (!$cachePath) { - $this->markTestSkipped('Could not find a non-writeable file for test'); - return; - } - - $this->mocks['environment']->shouldReceive('getConfigCachePath')->with() - ->once()->andReturn($cachePath); - - $result = $this->start('config:cache'); - - self::assertEquals('Failed to cache the configuration!', trim($result['errors'])); - self::assertSame(3, $result['returnVar']); - } - /** @test */ public function cachesTheConfig() { @@ -107,38 +65,4 @@ class CacheTest extends TestCase self::assertEquals('Configuration cache cleared successfully!', trim($result['output'])); self::assertSame(0, $result['returnVar']); } - - /** - * Get a file owned by root - * - * @param string $dir - * @return null|string - * @throws \Exception - */ - protected function getFileFromRoot(string $dir, bool $writeableDir) - { - $dh = opendir($dir); - if (!$dh) { - throw new \Exception('Could not open dir ' . $dir . ' for reading'); - } - - while ($file = readdir($dh)) { - if ($file === '.' || $file === '..') { - continue; - } - - $path = $dir . DIRECTORY_SEPARATOR . $file; - if (is_dir($path)) { - if (is_readable($path) && $fileFromRoot = $this->getFileFromRoot($path, $writeableDir)) { - return $fileFromRoot; - } - continue; - } elseif (@fileowner($path) === 0 && !is_writeable($path) && - is_writeable(dirname($path)) xor !$writeableDir) { - return $path; - } - } - - return null; - } } -- GitLab