diff --git a/src/Framework/Helpers.php b/src/Framework/Helpers.php index a18d90c2..7cfb3e11 100644 --- a/src/Framework/Helpers.php +++ b/src/Framework/Helpers.php @@ -130,4 +130,25 @@ public static function escapeArg(string $s): string ? '"' . str_replace('"', '""', $s) . '"' : escapeshellarg($s); } + + + /** + * @internal + */ + public static function prepareTempDir(string $path): string + { + $real = realpath($path); + if ($real === false) { + throw new \RuntimeException("Path '$path' does not exist."); + } elseif (!is_dir($real) || !is_writable($real)) { + throw new \RuntimeException("Path '$real' is not a writable directory."); + } + + $path = $real . DIRECTORY_SEPARATOR . 'Tester'; + if (!is_dir($path) && @mkdir($path) === false && !is_dir($path)) { // @ - directory may exist + throw new \RuntimeException("Cannot create '$path' directory."); + } + + return $path; + } } diff --git a/src/Runner/CliTester.php b/src/Runner/CliTester.php index aedf1875..76f6063c 100644 --- a/src/Runner/CliTester.php +++ b/src/Runner/CliTester.php @@ -133,7 +133,7 @@ private function loadOptions(): CommandLine '-c' => [CommandLine::Realpath => true], '--watch' => [CommandLine::Repeatable => true, CommandLine::Realpath => true], '--setup' => [CommandLine::Realpath => true], - '--temp' => [CommandLine::Realpath => true], + '--temp' => [], 'paths' => [CommandLine::Repeatable => true, CommandLine::Value => getcwd()], '--debug' => [], '--cider' => [], @@ -179,8 +179,10 @@ private function loadOptions(): CommandLine } elseif (($real = realpath($temp)) === false) { echo "Note: System temporary directory '$temp' does not exist.\n"; } else { - $this->options['--temp'] = rtrim($real, DIRECTORY_SEPARATOR); + $this->options['--temp'] = Helpers::prepareTempDir($real); } + } else { + $this->options['--temp'] = Helpers::prepareTempDir($this->options['--temp']); } return $cmd; @@ -218,10 +220,7 @@ private function createRunner(): Runner $runner->paths = $this->options['paths']; $runner->threadCount = max(1, (int) $this->options['-j']); $runner->stopOnFail = $this->options['--stop-on-fail']; - - if ($this->options['--temp'] !== null) { - $runner->setTempDirectory($this->options['--temp']); - } + $runner->setTempDirectory($this->options['--temp']); if ($this->stdoutFormat === null) { $runner->outputHandlers[] = new Output\ConsolePrinter( diff --git a/src/Runner/Runner.php b/src/Runner/Runner.php index 96aac9f3..6b0d4111 100644 --- a/src/Runner/Runner.php +++ b/src/Runner/Runner.php @@ -84,17 +84,6 @@ public function addPhpIniOption(string $name, ?string $value = null): void public function setTempDirectory(?string $path): void { - if ($path !== null) { - if (!is_dir($path) || !is_writable($path)) { - throw new \RuntimeException("Path '$path' is not a writable directory."); - } - - $path = realpath($path) . DIRECTORY_SEPARATOR . 'Tester'; - if (!is_dir($path) && @mkdir($path) === false && !is_dir($path)) { // @ - directory may exist - throw new \RuntimeException("Cannot create '$path' directory."); - } - } - $this->tempDir = $path; $this->testHandler->setTempDirectory($path); }