Skip to content

Commit 0480187

Browse files
milodg
authored andcommitted
Temporary directory preparation moved to Helpers
1 parent f5381bf commit 0480187

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
@@ -133,7 +133,7 @@ private function loadOptions(): CommandLine
133133
'-c' => [CommandLine::Realpath => true],
134134
'--watch' => [CommandLine::Repeatable => true, CommandLine::Realpath => true],
135135
'--setup' => [CommandLine::Realpath => true],
136-
'--temp' => [CommandLine::Realpath => true],
136+
'--temp' => [],
137137
'paths' => [CommandLine::Repeatable => true, CommandLine::Value => getcwd()],
138138
'--debug' => [],
139139
'--cider' => [],
@@ -179,8 +179,10 @@ private function loadOptions(): CommandLine
179179
} elseif (($real = realpath($temp)) === false) {
180180
echo "Note: System temporary directory '$temp' does not exist.\n";
181181
} else {
182-
$this->options['--temp'] = rtrim($real, DIRECTORY_SEPARATOR);
182+
$this->options['--temp'] = Helpers::prepareTempDir($real);
183183
}
184+
} else {
185+
$this->options['--temp'] = Helpers::prepareTempDir($this->options['--temp']);
184186
}
185187

186188
return $cmd;
@@ -218,10 +220,7 @@ private function createRunner(): Runner
218220
$runner->paths = $this->options['paths'];
219221
$runner->threadCount = max(1, (int) $this->options['-j']);
220222
$runner->stopOnFail = $this->options['--stop-on-fail'];
221-
222-
if ($this->options['--temp'] !== null) {
223-
$runner->setTempDirectory($this->options['--temp']);
224-
}
223+
$runner->setTempDirectory($this->options['--temp']);
225224

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

src/Runner/Runner.php

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

8585
public function setTempDirectory(?string $path): void
8686
{
87-
if ($path !== null) {
88-
if (!is_dir($path) || !is_writable($path)) {
89-
throw new \RuntimeException("Path '$path' is not a writable directory.");
90-
}
91-
92-
$path = realpath($path) . DIRECTORY_SEPARATOR . 'Tester';
93-
if (!is_dir($path) && @mkdir($path) === false && !is_dir($path)) { // @ - directory may exist
94-
throw new \RuntimeException("Cannot create '$path' directory.");
95-
}
96-
}
97-
9887
$this->tempDir = $path;
9988
$this->testHandler->setTempDirectory($path);
10089
}

0 commit comments

Comments
 (0)