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

Indicate file on TypeInferenceTestCase validation errors #3166

Merged
merged 16 commits into from
Jul 11, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jun 19, 2024

was just hunting down a bug in one of my PRs, but wasn't able to find the offending file, because the error message did not indicate which file the error related to

before this PR:

There was 1 error:

1) Error
The data provider specified for PHPStan\Analyser\NodeScopeResolverTest::testFileAsserts is invalid.
PHPUnit\Framework\AssertionFailedError: Expected type must be a literal string, string given on line 78.
/Users/staabm/workspace/phpstan-src/src/Testing/TypeInferenceTestCase.php:166
/Users/staabm/workspace/phpstan-src/src/Node/ClassStatementsGatherer.php:128
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:650
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:759
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:3938
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:2124
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:758
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:339
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:649
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:339
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:835
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:339
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:803
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:296
/Users/staabm/workspace/phpstan-src/src/Testing/TypeInferenceTestCase.php:92
/Users/staabm/workspace/phpstan-src/src/Testing/TypeInferenceTestCase.php:145
/Users/staabm/workspace/phpstan-src/src/Testing/TypeInferenceTestCase.php:271
/Users/staabm/workspace/phpstan-src/tests/PHPStan/Analyser/NodeScopeResolverTest.php:17

after this PR:

1) Error
The data provider specified for PHPStan\Analyser\NodeScopeResolverTest::testFileAsserts is invalid.
PHPUnit\Framework\AssertionFailedError: Expected type must be a literal string, string given in tests/PHPStan/Analyser/nsrt/param-closure-this.php on line 78.
/Users/staabm/workspace/phpstan-src/src/Testing/TypeInferenceTestCase.php:166
/Users/staabm/workspace/phpstan-src/src/Node/ClassStatementsGatherer.php:128
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:650
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:759
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:3938
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:2124
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:758
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:339
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:649
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:339
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:835
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:339
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:803
/Users/staabm/workspace/phpstan-src/src/Analyser/NodeScopeResolver.php:296
/Users/staabm/workspace/phpstan-src/src/Testing/TypeInferenceTestCase.php:92
/Users/staabm/workspace/phpstan-src/src/Testing/TypeInferenceTestCase.php:145
/Users/staabm/workspace/phpstan-src/src/Testing/TypeInferenceTestCase.php:273
/Users/staabm/workspace/phpstan-src/tests/PHPStan/Analyser/NodeScopeResolverTest.php:17

note the added filename in the error message

@staabm staabm marked this pull request as ready for review June 19, 2024 08:00
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm
Copy link
Contributor Author

staabm commented Jun 19, 2024

I will finalize this PR when back at my windows laptop next week

@staabm staabm marked this pull request as draft June 19, 2024 09:18
@staabm staabm marked this pull request as ready for review July 9, 2024 14:43
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

$functionName,
$pathHelper->getRelativePath(strtr($file, '\\', '/')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to change the backslashes here I think. And if you wanted to, we have FileHelper::normalizePath() for that. You can run this function on both sides here (implementation & tests).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the currentWorkingDirectory in

if ($this->currentWorkingDirectory !== '' && str_starts_with($filename, $this->currentWorkingDirectory)) {
is normalized to forward slashes.

when not doing strtr($file, '\\', '/') we pass on windows a path with backslashes and this makes the path not getting returned relative.

Copy link
Contributor Author

@staabm staabm Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just tried FileHelper::normalize instead of strtr($file, '\\', '/'): it will not turn the slashes in the correct form to make $pathHelper->getRelativePath return a proper relative path

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why wouldn't it? It works everywhere else in PHPStan's codebase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.

in other places we also do the \\ -> / dance to make it work:

private function relativizePath(string $path): string
{
$path = str_replace('\\', '/', $path);
$helper = new SimpleRelativePathHelper(str_replace('\\', '/', __DIR__ . '/PHP-Parser'));
return $helper->getRelativePath($path);
}

but I found a way to make it work.

@clxmstaab clxmstaab force-pushed the lines branch 2 times, most recently from 45b7661 to afa4cb9 Compare July 10, 2024 12:46
@@ -143,8 +145,10 @@ public function assertFileAsserts(
*/
public static function gatherAssertTypes(string $file): array
{
$pathHelper = new SimpleRelativePathHelper(dirname(__DIR__, 2));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make sense as it will be somewhere deep in the PHAR, for users that use TypeInferenceTestCase outside of phpstan-src.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last try in the latest git push. if this doesn't work for you I will close this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I committed my vision how it should have been done: 0e53118

@ondrejmirtes ondrejmirtes merged commit 18706de into phpstan:1.11.x Jul 11, 2024
449 of 452 checks passed
@ondrejmirtes
Copy link
Member

Thank you.

As a next step we could stop using SimpleRelativePathHelper in more places in tests and start using SystemAgnosticSimpleRelativePathHelper instead. It could find more issues like it did here in PathConstantsTest.

@staabm staabm deleted the lines branch July 11, 2024 12:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants