Skip to content

Commit 795f0a9

Browse files
milodg
authored andcommitted
Temporary directory preparation moved to Helpers
1 parent 1cb9533 commit 795f0a9

File tree

3 files changed

+26
-17
lines changed

3 files changed

+26
-17
lines changed

src/Framework/Helpers.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,25 @@ public static function escapeArg(string $s): string
130130
? '"' . str_replace('"', '""', $s) . '"'
131131
: escapeshellarg($s);
132132
}
133+
134+
135+
/**
136+
* @internal
137+
*/
138+
public static function prepareTempDir(string $path): string
139+
{
140+
$real = realpath($path);
141+
if ($real === false) {
142+
throw new \RuntimeException("Path '$path' does not exist.");
143+
} elseif (!is_dir($real) || !is_writable($real)) {
144+
throw new \RuntimeException("Path '$real' is not a writable directory.");
145+
}
146+
147+
$path = $real . DIRECTORY_SEPARATOR . 'Tester';
148+
if (!is_dir($path) && @mkdir($path) === false && !is_dir($path)) { // @ - directory may exist
149+
throw new \RuntimeException("Cannot create '$path' directory.");
150+
}
151+
152+
return $path;
153+
}
133154
}

src/Runner/CliTester.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private function loadOptions(): CommandLine
127127
'-c' => [CommandLine::Realpath => true],
128128
'--watch' => [CommandLine::Repeatable => true, CommandLine::Realpath => true],
129129
'--setup' => [CommandLine::Realpath => true],
130-
'--temp' => [CommandLine::Realpath => true],
130+
'--temp' => [],
131131
'paths' => [CommandLine::Repeatable => true, CommandLine::Value => getcwd()],
132132
'--debug' => [],
133133
'--cider' => [],
@@ -174,8 +174,10 @@ private function loadOptions(): CommandLine
174174
} elseif (($real = realpath($temp)) === false) {
175175
echo "Note: System temporary directory '$temp' does not exist.\n";
176176
} else {
177-
$this->options['--temp'] = rtrim($real, DIRECTORY_SEPARATOR);
177+
$this->options['--temp'] = Helpers::prepareTempDir($real);
178178
}
179+
} else {
180+
$this->options['--temp'] = Helpers::prepareTempDir($this->options['--temp']);
179181
}
180182

181183
return $cmd;
@@ -213,10 +215,7 @@ private function createRunner(): Runner
213215
$runner->paths = $this->options['paths'];
214216
$runner->threadCount = max(1, (int) $this->options['-j']);
215217
$runner->stopOnFail = (bool) $this->options['--stop-on-fail'];
216-
217-
if ($this->options['--temp'] !== null) {
218-
$runner->setTempDirectory($this->options['--temp']);
219-
}
218+
$runner->setTempDirectory($this->options['--temp']);
220219

221220
if ($this->stdoutFormat === null) {
222221
$runner->outputHandlers[] = new Output\ConsolePrinter(

src/Runner/Runner.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,6 @@ public function addPhpIniOption(string $name, ?string $value = null): void
6666

6767
public function setTempDirectory(?string $path): void
6868
{
69-
if ($path !== null) {
70-
if (!is_dir($path) || !is_writable($path)) {
71-
throw new \RuntimeException("Path '$path' is not a writable directory.");
72-
}
73-
74-
$path = realpath($path) . DIRECTORY_SEPARATOR . 'Tester';
75-
if (!is_dir($path) && @mkdir($path) === false && !is_dir($path)) { // @ - directory may exist
76-
throw new \RuntimeException("Cannot create '$path' directory.");
77-
}
78-
}
79-
8069
$this->tempDir = $path;
8170
$this->testHandler->setTempDirectory($path);
8271
}

0 commit comments

Comments
 (0)