Skip to content

Commit be8d36b

Browse files
Handle invalid logfile paths
1 parent 7f77f0e commit be8d36b

File tree

6 files changed

+188
-16
lines changed

6 files changed

+188
-16
lines changed

src/TextUI/Application.php

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -211,16 +211,36 @@ public function run(array $argv): int
211211

212212
if ($testDoxResult !== null &&
213213
$configuration->hasLogfileTestdoxHtml()) {
214-
OutputFacade::printerFor($configuration->logfileTestdoxHtml())->print(
215-
(new TestDoxHtmlRenderer)->render($testDoxResult),
216-
);
214+
try {
215+
OutputFacade::printerFor($configuration->logfileTestdoxHtml())->print(
216+
(new TestDoxHtmlRenderer)->render($testDoxResult),
217+
);
218+
} catch (Exception $e) {
219+
EventFacade::emitter()->testRunnerTriggeredWarning(
220+
sprintf(
221+
'Cannot log test results in TestDox HTML format to "%s": %s',
222+
$configuration->logfileTestdoxHtml(),
223+
$e->getMessage(),
224+
),
225+
);
226+
}
217227
}
218228

219229
if ($testDoxResult !== null &&
220230
$configuration->hasLogfileTestdoxText()) {
221-
OutputFacade::printerFor($configuration->logfileTestdoxText())->print(
222-
(new TestDoxTextRenderer)->render($testDoxResult),
223-
);
231+
try {
232+
OutputFacade::printerFor($configuration->logfileTestdoxText())->print(
233+
(new TestDoxTextRenderer)->render($testDoxResult),
234+
);
235+
} catch (Exception $e) {
236+
EventFacade::emitter()->testRunnerTriggeredWarning(
237+
sprintf(
238+
'Cannot log test results in TestDox plain text format to "%s": %s',
239+
$configuration->logfileTestdoxText(),
240+
$e->getMessage(),
241+
),
242+
);
243+
}
224244
}
225245

226246
$result = TestResultFacade::result();
@@ -545,19 +565,39 @@ private function registerLogfileWriters(Configuration $configuration): void
545565
}
546566

547567
if ($configuration->hasLogfileJunit()) {
548-
new JunitXmlLogger(
549-
OutputFacade::printerFor($configuration->logfileJunit()),
550-
EventFacade::instance(),
551-
);
568+
try {
569+
new JunitXmlLogger(
570+
OutputFacade::printerFor($configuration->logfileJunit()),
571+
EventFacade::instance(),
572+
);
573+
} catch (Exception $e) {
574+
EventFacade::emitter()->testRunnerTriggeredWarning(
575+
sprintf(
576+
'Cannot log test results in JUnit XML format to "%s": %s',
577+
$configuration->logfileJunit(),
578+
$e->getMessage(),
579+
),
580+
);
581+
}
552582
}
553583

554584
if ($configuration->hasLogfileTeamcity()) {
555-
new TeamCityLogger(
556-
DefaultPrinter::from(
557-
$configuration->logfileTeamcity(),
558-
),
559-
EventFacade::instance(),
560-
);
585+
try {
586+
new TeamCityLogger(
587+
DefaultPrinter::from(
588+
$configuration->logfileTeamcity(),
589+
),
590+
EventFacade::instance(),
591+
);
592+
} catch (Exception $e) {
593+
EventFacade::emitter()->testRunnerTriggeredWarning(
594+
sprintf(
595+
'Cannot log test results in TeamCity format to "%s": %s',
596+
$configuration->logfileTeamcity(),
597+
$e->getMessage(),
598+
),
599+
);
600+
}
561601
}
562602
}
563603

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\TestFixture\Basic;
11+
12+
use PHPUnit\Framework\TestCase;
13+
14+
final class SuccessTest extends TestCase
15+
{
16+
public function testOne(): void
17+
{
18+
$this->assertTrue(true);
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test runner emits warning when --log-junit is used with an invalid target path
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--log-junit';
8+
$_SERVER['argv'][] = '';
9+
$_SERVER['argv'][] = __DIR__ . '/../_files/basic/SuccessTest.php';
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
14+
--EXPECTF--
15+
PHPUnit %s by Sebastian Bergmann and contributors.
16+
17+
Runtime: %s
18+
19+
. 1 / 1 (100%)
20+
21+
Time: %s, Memory: %s
22+
23+
There was 1 PHPUnit test runner warning:
24+
25+
1) Cannot log test results in JUnit XML format to "": Directory "" does not exist and could not be created
26+
27+
WARNINGS!
28+
Tests: 1, Assertions: 1, Warnings: 1.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test runner emits warning when --log-teamcity is used with an invalid target path
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--log-teamcity';
8+
$_SERVER['argv'][] = '';
9+
$_SERVER['argv'][] = __DIR__ . '/../_files/basic/SuccessTest.php';
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
14+
--EXPECTF--
15+
PHPUnit %s by Sebastian Bergmann and contributors.
16+
17+
Runtime: %s
18+
19+
. 1 / 1 (100%)
20+
21+
Time: %s, Memory: %s
22+
23+
There was 1 PHPUnit test runner warning:
24+
25+
1) Cannot log test results in TeamCity format to "": Directory "" does not exist and could not be created
26+
27+
WARNINGS!
28+
Tests: 1, Assertions: 1, Warnings: 1.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test runner emits warning when --testdox-html is used with an invalid target path
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--testdox-html';
8+
$_SERVER['argv'][] = '';
9+
$_SERVER['argv'][] = __DIR__ . '/../_files/basic/SuccessTest.php';
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
14+
--EXPECTF--
15+
PHPUnit %s by Sebastian Bergmann and contributors.
16+
17+
Runtime: %s
18+
19+
. 1 / 1 (100%)
20+
21+
Time: %s, Memory: %s
22+
23+
There was 1 PHPUnit test runner warning:
24+
25+
1) Cannot log test results in TestDox HTML format to "": Directory "" does not exist and could not be created
26+
27+
WARNINGS!
28+
Tests: 1, Assertions: 1, Warnings: 1.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Test runner emits warning when --testdox-text is used with an invalid target path
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-cache-result';
6+
$_SERVER['argv'][] = '--no-configuration';
7+
$_SERVER['argv'][] = '--testdox-text';
8+
$_SERVER['argv'][] = '';
9+
$_SERVER['argv'][] = __DIR__ . '/../_files/basic/SuccessTest.php';
10+
11+
require_once __DIR__ . '/../../bootstrap.php';
12+
13+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
14+
--EXPECTF--
15+
PHPUnit %s by Sebastian Bergmann and contributors.
16+
17+
Runtime: %s
18+
19+
. 1 / 1 (100%)
20+
21+
Time: %s, Memory: %s
22+
23+
There was 1 PHPUnit test runner warning:
24+
25+
1) Cannot log test results in TestDox plain text format to "": Directory "" does not exist and could not be created
26+
27+
WARNINGS!
28+
Tests: 1, Assertions: 1, Warnings: 1.

0 commit comments

Comments
 (0)