Skip to content

Commit

Permalink
Temporary directory preparation moved to Helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
milo authored and dg committed Feb 5, 2023
1 parent 9ad6a6d commit c00e128
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
19 changes: 19 additions & 0 deletions src/Framework/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,23 @@ 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 || !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;
}
}
11 changes: 5 additions & 6 deletions src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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' => [],
Expand Down Expand Up @@ -174,8 +174,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;
Expand Down Expand Up @@ -213,10 +215,7 @@ private function createRunner(): Runner
$runner->paths = $this->options['paths'];
$runner->threadCount = max(1, (int) $this->options['-j']);
$runner->stopOnFail = (bool) $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(
Expand Down
11 changes: 0 additions & 11 deletions src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,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);
}
Expand Down

0 comments on commit c00e128

Please sign in to comment.