Skip to content

Commit

Permalink
Closes #5692 (which was a regression introduced with the fix for #5633)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Feb 4, 2024
1 parent 02c1d45 commit 93b9806
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ChangeLog-10.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

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

## [10.5.10] - 2024-MM-DD

### Fixed

* [#5692](https://github.com/sebastianbergmann/phpunit/issues/5692): `--log-events-text` and `--log-events-verbose-text` require the destination file to exit

## [10.5.9] - 2024-01-22

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

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

[10.5.10]: https://github.com/sebastianbergmann/phpunit/compare/10.5.9...10.5
[10.5.9]: https://github.com/sebastianbergmann/phpunit/compare/10.5.8...10.5.9
[10.5.8]: https://github.com/sebastianbergmann/phpunit/compare/10.5.7...10.5.8
[10.5.7]: https://github.com/sebastianbergmann/phpunit/compare/10.5.6...10.5.7
Expand Down
23 changes: 21 additions & 2 deletions src/TextUI/Configuration/Cli/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use function is_numeric;
use function sprintf;
use PHPUnit\Runner\TestSuiteSorter;
use PHPUnit\Util\Filesystem;
use SebastianBergmann\CliParser\Exception as CliParserException;
use SebastianBergmann\CliParser\Parser as CliParser;

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

case '--log-events-text':
$logEventsText = $option[1];
$logEventsText = Filesystem::resolveStreamOrFile($option[1]);

if ($logEventsText === false) {
throw new Exception(
sprintf(
'The path "%s" specified for the --log-events-text option could not be resolved',
$option[1],
),
);
}

break;

case '--log-events-verbose-text':
$logEventsVerboseText = $option[1];
$logEventsVerboseText = Filesystem::resolveStreamOrFile($option[1]);

if ($logEventsVerboseText === false) {
throw new Exception(
sprintf(
'The path "%s" specified for the --log-events-verbose-text option could not be resolved',
$option[1],
),
);
}

break;

Expand Down
24 changes: 24 additions & 0 deletions src/Util/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
*/
namespace PHPUnit\Util;

use function basename;
use function dirname;
use function is_dir;
use function mkdir;
use function realpath;
use function str_starts_with;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
Expand All @@ -21,4 +25,24 @@ public static function createDirectory(string $directory): bool
{
return !(!is_dir($directory) && !@mkdir($directory, 0o777, true) && !is_dir($directory));
}

/**
* @psalm-param non-empty-string $path
*
* @return false|non-empty-string
*/
public static function resolveStreamOrFile(string $path): false|string
{
if (str_starts_with($path, 'php://') || str_starts_with($path, 'socket://')) {
return $path;
}

$directory = dirname($path);

if (is_dir($directory)) {
return realpath($directory) . DIRECTORY_SEPARATOR . basename($path);
}

return false;
}
}
19 changes: 19 additions & 0 deletions tests/end-to-end/cli/log-events-text-invalid-argument.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Test fails with invalid path
--FILE--
<?php declare(strict_types=1);
$traceFile = sys_get_temp_dir() . '/invalid-directory/invalid.file';

$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--no-output';
$_SERVER['argv'][] = '--log-events-text';
$_SERVER['argv'][] = $traceFile;

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

The path "%s" specified for the --log-events-text option could not be resolved
19 changes: 19 additions & 0 deletions tests/end-to-end/cli/log-events-verbose-text-invalid-argument.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Test fails with invalid path
--FILE--
<?php declare(strict_types=1);
$traceFile = sys_get_temp_dir() . '/invalid-directory/invalid.file';

$_SERVER['argv'][] = '--do-not-cache-result';
$_SERVER['argv'][] = '--no-configuration';
$_SERVER['argv'][] = '--no-output';
$_SERVER['argv'][] = '--log-events-verbose-text';
$_SERVER['argv'][] = $traceFile;

require __DIR__ . '/../../bootstrap.php';

(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

The path "%s" specified for the --log-events-verbose-text option could not be resolved
28 changes: 28 additions & 0 deletions tests/unit/Util/FilesystemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Util;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Small;
use PHPUnit\Framework\TestCase;

#[CoversClass(Filesystem::class)]
#[Small]
final class FilesystemTest extends TestCase
{
public function testCanResolveStreamOrFile(): void
{
$this->assertSame('php://stdout', Filesystem::resolveStreamOrFile('php://stdout'));
$this->assertSame('socket://hostname:port', Filesystem::resolveStreamOrFile('socket://hostname:port'));
$this->assertSame(__FILE__, Filesystem::resolveStreamOrFile(__FILE__));
$this->assertSame(__DIR__ . '/does-not-exist', Filesystem::resolveStreamOrFile(__DIR__ . '/does-not-exist'));
$this->assertFalse(Filesystem::resolveStreamOrFile(__DIR__ . '/does-not-exist/does-not-exist'));
}
}

0 comments on commit 93b9806

Please sign in to comment.