From dd17b8e5506dd79c77afce3f0743cf1b7d18db53 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Wed, 18 Sep 2019 20:23:35 +0200 Subject: [PATCH] tests: TEMP_DIR replaced with getTempDir() --- tests/Tracy/Debugger.log().error.phpt | 4 +-- tests/Tracy/Debugger.log().samefile.phpt | 6 ++-- ...gger.logSeverity.E_NOTICE.development.phpt | 6 ++-- .../Tracy/Debugger.logSeverity.E_NOTICE.phpt | 6 ++-- tests/Tracy/Debugger.logging.error.phpt | 2 +- tests/Tracy/Debugger.logging.warnings.phpt | 2 +- tests/Tracy/Logger.extensible.phpt | 4 +-- tests/Tracy/Logger.log().errors.phpt | 8 +++--- tests/Tracy/Logger.log().phpt | 12 ++++---- tests/bootstrap.php | 28 +++++++++++++++---- 10 files changed, 47 insertions(+), 31 deletions(-) diff --git a/tests/Tracy/Debugger.log().error.phpt b/tests/Tracy/Debugger.log().error.phpt index 8951e5670..c9f24601b 100644 --- a/tests/Tracy/Debugger.log().error.phpt +++ b/tests/Tracy/Debugger.log().error.phpt @@ -19,11 +19,11 @@ Assert::exception(function () { // no error -Debugger::$logDirectory = TEMP_DIR; +Debugger::$logDirectory = getTempDir(); Debugger::log('Hello'); -Debugger::$logDirectory = TEMP_DIR . '/unknown'; +Debugger::$logDirectory = getTempDir() . '/unknown'; Assert::exception(function () { Debugger::log('Hello'); }, 'RuntimeException', "Logging directory '%a%' is not found or is not directory."); diff --git a/tests/Tracy/Debugger.log().samefile.phpt b/tests/Tracy/Debugger.log().samefile.phpt index 70afe81a9..212825168 100644 --- a/tests/Tracy/Debugger.log().samefile.phpt +++ b/tests/Tracy/Debugger.log().samefile.phpt @@ -14,7 +14,7 @@ require __DIR__ . '/../bootstrap.php'; // Setup environment -Debugger::$logDirectory = TEMP_DIR; +Debugger::$logDirectory = getTempDir(); function foo($fp) @@ -24,9 +24,9 @@ function foo($fp) for ($i = 0; $i < 3; $i++) { - $path = TEMP_DIR . "/$i"; + $path = getTempDir() . "/$i"; try { - $files[] = $file = fopen(TEMP_DIR . "/$i", 'w'); + $files[] = $file = fopen(getTempDir() . "/$i", 'w'); foo($file); } catch (Exception $e) { $name[] = Debugger::log($e); diff --git a/tests/Tracy/Debugger.logSeverity.E_NOTICE.development.phpt b/tests/Tracy/Debugger.logSeverity.E_NOTICE.development.phpt index 0d641dfa2..0292154a7 100644 --- a/tests/Tracy/Debugger.logSeverity.E_NOTICE.development.phpt +++ b/tests/Tracy/Debugger.logSeverity.E_NOTICE.development.phpt @@ -14,10 +14,10 @@ require __DIR__ . '/../bootstrap.php'; // Setup environment -Debugger::enable(Debugger::DEVELOPMENT, TEMP_DIR); +Debugger::enable(Debugger::DEVELOPMENT, getTempDir()); Debugger::$logSeverity = E_NOTICE; $variable = $missingVariable; -Assert::count(0, glob(TEMP_DIR . '/exception*.html')); -Assert::count(0, glob(TEMP_DIR . '/error.log')); +Assert::count(0, glob(getTempDir() . '/exception*.html')); +Assert::count(0, glob(getTempDir() . '/error.log')); diff --git a/tests/Tracy/Debugger.logSeverity.E_NOTICE.phpt b/tests/Tracy/Debugger.logSeverity.E_NOTICE.phpt index 560c4b968..647857f35 100644 --- a/tests/Tracy/Debugger.logSeverity.E_NOTICE.phpt +++ b/tests/Tracy/Debugger.logSeverity.E_NOTICE.phpt @@ -14,10 +14,10 @@ require __DIR__ . '/../bootstrap.php'; // Setup environment -Debugger::enable(Debugger::PRODUCTION, TEMP_DIR); +Debugger::enable(Debugger::PRODUCTION, getTempDir()); Debugger::$logSeverity = E_NOTICE; $variable = $missingVariable; -Assert::count(1, glob(TEMP_DIR . '/error*.html')); -Assert::count(1, glob(TEMP_DIR . '/error.log')); +Assert::count(1, glob(getTempDir() . '/error*.html')); +Assert::count(1, glob(getTempDir() . '/error.log')); diff --git a/tests/Tracy/Debugger.logging.error.phpt b/tests/Tracy/Debugger.logging.error.phpt index bd6d6c329..d10d9bf2b 100644 --- a/tests/Tracy/Debugger.logging.error.phpt +++ b/tests/Tracy/Debugger.logging.error.phpt @@ -19,7 +19,7 @@ require __DIR__ . '/../bootstrap.php'; // Setup environment $_SERVER['HTTP_HOST'] = 'nette.org'; -Debugger::$logDirectory = TEMP_DIR; +Debugger::$logDirectory = getTempDir(); Debugger::getLogger()->mailer = function () {}; diff --git a/tests/Tracy/Debugger.logging.warnings.phpt b/tests/Tracy/Debugger.logging.warnings.phpt index 1b84f4abe..6e7f42283 100644 --- a/tests/Tracy/Debugger.logging.warnings.phpt +++ b/tests/Tracy/Debugger.logging.warnings.phpt @@ -16,7 +16,7 @@ require __DIR__ . '/../bootstrap.php'; // Setup environment $_SERVER['HTTP_HOST'] = 'nette.org'; -$logDirectory = TEMP_DIR; +$logDirectory = getTempDir(); Debugger::getLogger()->mailer = function () {}; diff --git a/tests/Tracy/Logger.extensible.phpt b/tests/Tracy/Logger.extensible.phpt index 6226fac3b..8143f12d3 100644 --- a/tests/Tracy/Logger.extensible.phpt +++ b/tests/Tracy/Logger.extensible.phpt @@ -37,7 +37,7 @@ class CustomLogger extends Logger test(function () { - $logger = new CustomLogger(TEMP_DIR); + $logger = new CustomLogger(getTempDir()); $logger->log(new Exception('First'), 'a'); Assert::match('a', $logger->collector[0][0]); @@ -47,7 +47,7 @@ test(function () { }); test(function () { - $logger = new CustomLogger(TEMP_DIR); + $logger = new CustomLogger(getTempDir()); $logger->log('message', 'b'); Assert::match('b', $logger->collector[0][0]); diff --git a/tests/Tracy/Logger.log().errors.phpt b/tests/Tracy/Logger.log().errors.phpt index 31bc82729..311391c81 100644 --- a/tests/Tracy/Logger.log().errors.phpt +++ b/tests/Tracy/Logger.log().errors.phpt @@ -13,18 +13,18 @@ use Tracy\Logger; require __DIR__ . '/../bootstrap.php'; -$logger = new Logger(TEMP_DIR); +$logger = new Logger(getTempDir()); $logger->log('Hello'); // no error Assert::exception(function () { - $logger = new Logger(TEMP_DIR . '/unknown'); + $logger = new Logger(getTempDir() . '/unknown'); $logger->log('Hello'); }, 'RuntimeException', "Logging directory '%a%' is not found or is not directory."); Assert::exception(function () { - $logger = new Logger(TEMP_DIR); - mkdir(TEMP_DIR . '/test.log'); + $logger = new Logger(getTempDir()); + mkdir(getTempDir() . '/test.log'); $logger->log('Hello', 'test'); }, 'RuntimeException', "Unable to write to log file '%a%'. Is directory writable?"); diff --git a/tests/Tracy/Logger.log().phpt b/tests/Tracy/Logger.log().phpt index 7dfa867b1..1c47d0ba5 100644 --- a/tests/Tracy/Logger.log().phpt +++ b/tests/Tracy/Logger.log().phpt @@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php'; test(function () { $e = new Exception('First'); - $logger = new Logger(TEMP_DIR); + $logger = new Logger(getTempDir()); $logger->log($e, 'a'); Assert::match('[%a%] Exception: First in %a%:%d% %A%', file_get_contents($logger->directory . '/a.log')); }); @@ -24,31 +24,31 @@ test(function () { $e = new Exception('First'); $e = new InvalidArgumentException('Second', 0, $e); $e = new RuntimeException('Third', 0, $e); - $logger = new Logger(TEMP_DIR); + $logger = new Logger(getTempDir()); $logger->log($e, 'b'); Assert::match('[%a%] RuntimeException: Third in %a%:%d% caused by InvalidArgumentException: Second in %a%:%d% caused by Exception: First in %a%:%d% %A%', file_get_contents($logger->directory . '/b.log')); }); test(function () { - $logger = new Logger(TEMP_DIR); + $logger = new Logger(getTempDir()); $logger->log(new ErrorException('Msg', 0, E_ERROR, __FILE__, __LINE__), 'c'); Assert::match('[%a%] Fatal Error: Msg in %a%Logger.log().phpt:%d% @ CLI (PID: %d%): %a% @@ c-%a%.html', file_get_contents($logger->directory . '/c.log')); }); test(function () { - $logger = new Logger(TEMP_DIR); + $logger = new Logger(getTempDir()); $logger->log(new ErrorException('Msg', 0, E_WARNING, __FILE__, __LINE__), 'd'); Assert::match('[%a%] Warning: Msg in %a%Logger.log().phpt:%d% @ CLI (PID: %d%): %a% @@ d-%a%.html', file_get_contents($logger->directory . '/d.log')); }); test(function () { - $logger = new Logger(TEMP_DIR); + $logger = new Logger(getTempDir()); $logger->log(new ErrorException('Msg', 0, E_COMPILE_ERROR, __FILE__, __LINE__), 'e'); Assert::match('[%a%] Compile Error: Msg in %a%Logger.log().phpt:%d% @ CLI (PID: %d%): %a% @@ e-%a%.html', file_get_contents($logger->directory . '/e.log')); }); test(function () { - $logger = new Logger(TEMP_DIR); + $logger = new Logger(getTempDir()); $logger->log(new ErrorException('Msg', 0, E_NOTICE, __FILE__, __LINE__), 'f'); Assert::match('[%a%] Notice: Msg in %a%Logger.log().phpt:%d% @ CLI (PID: %d%): %a% @@ f-%a%.html', file_get_contents($logger->directory . '/f.log')); }); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d443f15ca..8733d39bb 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -16,12 +16,7 @@ date_default_timezone_set('Europe/Prague'); ini_set('serialize_precision', '14'); $_GET = $_POST = $_COOKIE = []; - - -// create temporary directory -define('TEMP_DIR', __DIR__ . '/tmp/' . getmypid()); -@mkdir(dirname(TEMP_DIR)); // @ - directory may already exist -Tester\Helpers::purge(TEMP_DIR); +define('USER_CONST', 1); if (extension_loaded('xdebug')) { @@ -29,6 +24,27 @@ } +function getTempDir(): string +{ + $dir = __DIR__ . '/tmp/' . getmypid(); + + if (empty($GLOBALS['\\lock'])) { + // garbage collector + $GLOBALS['\\lock'] = $lock = fopen(__DIR__ . '/lock', 'w'); + if (rand(0, 100)) { + flock($lock, LOCK_SH); + @mkdir(dirname($dir)); + } elseif (flock($lock, LOCK_EX)) { + Tester\Helpers::purge(dirname($dir)); + } + + @mkdir($dir); + } + + return $dir; +} + + function test(\Closure $function): void { $function();