Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code location hints for PHPT and @requires #3467

Merged
merged 18 commits into from Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions phpunit.xml
Expand Up @@ -12,6 +12,7 @@

<testsuite name="end-to-end">
<directory suffix=".phpt">tests/end-to-end</directory>
<exclude>tests/end-to-end/_files</exclude>
</testsuite>
</testsuites>

Expand Down
30 changes: 30 additions & 0 deletions src/Framework/Assert.php
Expand Up @@ -3000,6 +3000,12 @@ public static function markTestIncomplete(string $message = ''): void
*/
public static function markTestSkipped(string $message = ''): void
{
if ($hint = self::detectLocationHint($message)) {
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);

throw new SyntheticSkippedError($hint['message'], 0, $hint['file'], $hint['line'], $trace);
}

throw new SkippedTestError($message);
}

Expand All @@ -3019,6 +3025,30 @@ public static function resetCount(): void
self::$count = 0;
}

private static function detectLocationHint(string $message): ?array
{
$hint = null;
$lines = \preg_split('/\r\n|\r|\n/', $message);

while (\strpos($lines[0], '__OFFSET') !== false) {
$offset = \explode('=', \array_shift($lines));

if ($offset[0] === '__OFFSET_FILE') {
$hint['file'] = $offset[1];
}

if ($offset[0] === '__OFFSET_LINE') {
$hint['line'] = $offset[1];
}
}

if ($hint) {
$hint['message'] = \implode(\PHP_EOL, $lines);
}

return $hint;
}

private static function isValidAttributeName(string $attributeName): bool
{
return \preg_match('/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/', $attributeName);
Expand Down
14 changes: 14 additions & 0 deletions src/Framework/PHPTAssertionFailedError.php
@@ -0,0 +1,14 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework;

class PHPTAssertionFailedError extends SyntheticError
{
}
14 changes: 14 additions & 0 deletions src/Framework/SyntheticSkippedError.php
@@ -0,0 +1,14 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Framework;

class SyntheticSkippedError extends SyntheticError implements SkippedTest
{
}
107 changes: 101 additions & 6 deletions src/Runner/PhptTestCase.php
Expand Up @@ -11,9 +11,11 @@

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\IncompleteTestError;
use PHPUnit\Framework\PHPTAssertionFailedError;
use PHPUnit\Framework\SelfDescribing;
use PHPUnit\Framework\SkippedTestError;
use PHPUnit\Framework\SyntheticSkippedError;
use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestResult;
use PHPUnit\Util\PHP\AbstractPhpProcess;
Expand Down Expand Up @@ -175,7 +177,16 @@ public function run(TestResult $result = null): TestResult

if ($xfail !== false) {
$failure = new IncompleteTestError($xfail, 0, $e);
} else {
if ($e instanceof ExpectationFailedException && $e->getComparisonFailure()) {
/** @var ExpectationFailedException $e */
$hint = $this->getLocationHintFromDiff($e->getComparisonFailure()->getDiff(), $sections);
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
\array_unshift($trace, $hint);
$failure = new PHPTAssertionFailedError($e->getMessage(), 0, $trace[0]['file'], $trace[0]['line'], $trace);
}
}

$result->addFailure($this, $failure, $time);
} catch (Throwable $t) {
$result->addError($this, $t, $time);
Expand Down Expand Up @@ -328,7 +339,10 @@ private function runSkip(array &$sections, TestResult $result, array $settings):
$message = \substr($skipMatch[1], 2);
}

$result->addFailure($this, new SkippedTestError($message), 0);
$hint = $this->getLocationHint($message, $sections, 'SKIPIF');
$trace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS);
\array_unshift($trace, $hint);
$result->addFailure($this, new SyntheticSkippedError($message, 0, $trace[0]['file'], $trace[0]['line'], $trace), 0);
$result->endTest($this, 0);

return true;
Expand Down Expand Up @@ -374,10 +388,15 @@ private function parse(): array
'PHPDBG',
];

$lineNr = 0;

foreach (\file($this->filename) as $line) {
$lineNr++;

if (\preg_match('/^--([_A-Z]+)--/', $line, $result)) {
$section = $result[1];
$sections[$section] = '';
$section = $result[1];
$sections[$section] = '';
$sections[$section . '_offset'] = $lineNr;

continue;
}
Expand Down Expand Up @@ -440,8 +459,6 @@ private function parseExternal(array &$sections): void
}

$sections[$section] = \file_get_contents($testDirectory . $externalFilename);

unset($sections[$section . '_EXTERNAL']);
}
}
}
Expand Down Expand Up @@ -587,4 +604,82 @@ private function stringifyIni(array $ini): array

return $settings;
}

private function getLocationHintFromDiff(string $message, array $sections): array
{
$needle = '';

if (\preg_match("/--- Expected[^']+^-'([^']*)'/m", $message, $matches)) {
$needle = $matches[1];
}

return $this->getLocationHint($needle, $sections);
}

private function getLocationHint(string $needle, array $sections, ?string $sectionName = null): array
{
$needle = \trim($needle);

if (empty($needle)) {
return [
'file' => \realpath($this->filename),
'line' => 0,
];
}

if ($sectionName) {
$search = [$sectionName];
} else {
$search = [
// 'FILE',
'EXPECT',
'EXPECTF',
'EXPECTREGEX',
];
}

foreach ($search as $section) {
if (!isset($sections[$section])) {
continue;
}

if (isset($sections[$section . '_EXTERNAL'])) {
$file = \trim($sections[$section . '_EXTERNAL']);

return [
'file' => \realpath(\dirname($this->filename) . \DIRECTORY_SEPARATOR . $file),
'line' => 1,
];
}

$sectionOffset = $sections[$section . '_offset'] ?? 0;
$offset = $sectionOffset + 1;

$lines = \preg_split('/\r\n|\r|\n/', $sections[$section]);

foreach ($lines as $line) {
if (\strpos($line, $needle) !== false) {
return [
'file' => \realpath($this->filename),
'line' => $offset,
];
}
$offset++;
}
}

if ($sectionName) {
// String not found in specified section, show user the start of the named section
return [
'file' => \realpath($this->filename),
'line' => $sectionOffset,
];
}

// No section specified, show user start of code
return [
'file' => \realpath($this->filename),
'line' => 0,
];
}
}
3 changes: 1 addition & 2 deletions src/Util/Color.php
Expand Up @@ -93,8 +93,7 @@ public static function colorizePath(string $path, ?string $prevPath = null, bool
$last = \count($path) - 1;
$path[$last] = \preg_replace_callback(
'/([\-_\.]+|phpt$)/',
function ($matches)
{
function ($matches) {
return self::dim($matches[0]);
},
$path[$last]
Expand Down