Skip to content

Commit 93b9806

Browse files
Closes #5692 (which was a regression introduced with the fix for #5633)
1 parent 02c1d45 commit 93b9806

File tree

6 files changed

+118
-2
lines changed

6 files changed

+118
-2
lines changed

ChangeLog-10.5.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes of the PHPUnit 10.5 release series are documented in this file using the [Keep a CHANGELOG](https://keepachangelog.com/) principles.
44

5+
## [10.5.10] - 2024-MM-DD
6+
7+
### Fixed
8+
9+
* [#5692](https://github.com/sebastianbergmann/phpunit/issues/5692): `--log-events-text` and `--log-events-verbose-text` require the destination file to exit
10+
511
## [10.5.9] - 2024-01-22
612

713
### Fixed
@@ -97,6 +103,7 @@ All notable changes of the PHPUnit 10.5 release series are documented in this fi
97103

98104
* [#5563](https://github.com/sebastianbergmann/phpunit/issues/5563): `createMockForIntersectionOfInterfaces()` does not automatically register mock object for expectation verification
99105

106+
[10.5.10]: https://github.com/sebastianbergmann/phpunit/compare/10.5.9...10.5
100107
[10.5.9]: https://github.com/sebastianbergmann/phpunit/compare/10.5.8...10.5.9
101108
[10.5.8]: https://github.com/sebastianbergmann/phpunit/compare/10.5.7...10.5.8
102109
[10.5.7]: https://github.com/sebastianbergmann/phpunit/compare/10.5.6...10.5.7

src/TextUI/Configuration/Cli/Builder.php

+21-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use function is_numeric;
1818
use function sprintf;
1919
use PHPUnit\Runner\TestSuiteSorter;
20+
use PHPUnit\Util\Filesystem;
2021
use SebastianBergmann\CliParser\Exception as CliParserException;
2122
use SebastianBergmann\CliParser\Parser as CliParser;
2223

@@ -805,12 +806,30 @@ public function fromParameters(array $parameters): Configuration
805806
break;
806807

807808
case '--log-events-text':
808-
$logEventsText = $option[1];
809+
$logEventsText = Filesystem::resolveStreamOrFile($option[1]);
810+
811+
if ($logEventsText === false) {
812+
throw new Exception(
813+
sprintf(
814+
'The path "%s" specified for the --log-events-text option could not be resolved',
815+
$option[1],
816+
),
817+
);
818+
}
809819

810820
break;
811821

812822
case '--log-events-verbose-text':
813-
$logEventsVerboseText = $option[1];
823+
$logEventsVerboseText = Filesystem::resolveStreamOrFile($option[1]);
824+
825+
if ($logEventsVerboseText === false) {
826+
throw new Exception(
827+
sprintf(
828+
'The path "%s" specified for the --log-events-verbose-text option could not be resolved',
829+
$option[1],
830+
),
831+
);
832+
}
814833

815834
break;
816835

src/Util/Filesystem.php

+24
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
*/
1010
namespace PHPUnit\Util;
1111

12+
use function basename;
13+
use function dirname;
1214
use function is_dir;
1315
use function mkdir;
16+
use function realpath;
17+
use function str_starts_with;
1418

1519
/**
1620
* @internal This class is not covered by the backward compatibility promise for PHPUnit
@@ -21,4 +25,24 @@ public static function createDirectory(string $directory): bool
2125
{
2226
return !(!is_dir($directory) && !@mkdir($directory, 0o777, true) && !is_dir($directory));
2327
}
28+
29+
/**
30+
* @psalm-param non-empty-string $path
31+
*
32+
* @return false|non-empty-string
33+
*/
34+
public static function resolveStreamOrFile(string $path): false|string
35+
{
36+
if (str_starts_with($path, 'php://') || str_starts_with($path, 'socket://')) {
37+
return $path;
38+
}
39+
40+
$directory = dirname($path);
41+
42+
if (is_dir($directory)) {
43+
return realpath($directory) . DIRECTORY_SEPARATOR . basename($path);
44+
}
45+
46+
return false;
47+
}
2448
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test fails with invalid path
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$traceFile = sys_get_temp_dir() . '/invalid-directory/invalid.file';
6+
7+
$_SERVER['argv'][] = '--do-not-cache-result';
8+
$_SERVER['argv'][] = '--no-configuration';
9+
$_SERVER['argv'][] = '--no-output';
10+
$_SERVER['argv'][] = '--log-events-text';
11+
$_SERVER['argv'][] = $traceFile;
12+
13+
require __DIR__ . '/../../bootstrap.php';
14+
15+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
16+
--EXPECTF--
17+
PHPUnit %s by Sebastian Bergmann and contributors.
18+
19+
The path "%s" specified for the --log-events-text option could not be resolved
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Test fails with invalid path
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$traceFile = sys_get_temp_dir() . '/invalid-directory/invalid.file';
6+
7+
$_SERVER['argv'][] = '--do-not-cache-result';
8+
$_SERVER['argv'][] = '--no-configuration';
9+
$_SERVER['argv'][] = '--no-output';
10+
$_SERVER['argv'][] = '--log-events-verbose-text';
11+
$_SERVER['argv'][] = $traceFile;
12+
13+
require __DIR__ . '/../../bootstrap.php';
14+
15+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
16+
--EXPECTF--
17+
PHPUnit %s by Sebastian Bergmann and contributors.
18+
19+
The path "%s" specified for the --log-events-verbose-text option could not be resolved

tests/unit/Util/FilesystemTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\Util;
11+
12+
use PHPUnit\Framework\Attributes\CoversClass;
13+
use PHPUnit\Framework\Attributes\Small;
14+
use PHPUnit\Framework\TestCase;
15+
16+
#[CoversClass(Filesystem::class)]
17+
#[Small]
18+
final class FilesystemTest extends TestCase
19+
{
20+
public function testCanResolveStreamOrFile(): void
21+
{
22+
$this->assertSame('php://stdout', Filesystem::resolveStreamOrFile('php://stdout'));
23+
$this->assertSame('socket://hostname:port', Filesystem::resolveStreamOrFile('socket://hostname:port'));
24+
$this->assertSame(__FILE__, Filesystem::resolveStreamOrFile(__FILE__));
25+
$this->assertSame(__DIR__ . '/does-not-exist', Filesystem::resolveStreamOrFile(__DIR__ . '/does-not-exist'));
26+
$this->assertFalse(Filesystem::resolveStreamOrFile(__DIR__ . '/does-not-exist/does-not-exist'));
27+
}
28+
}

0 commit comments

Comments
 (0)