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 Aug 29, 2022
1 parent f5381bf commit 0480187
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
21 changes: 21 additions & 0 deletions src/Framework/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
11 changes: 5 additions & 6 deletions src/Runner/CliTester.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
11 changes: 0 additions & 11 deletions src/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 0480187

Please sign in to comment.