Commit 136767a3 authored by Wouter Wolters's avatar Wouter Wolters Committed by Christian Kuhn
Browse files

[TASK] Add test method prefix checker to gitlab-ci

Test methods are not allowed to have a prefix "test".
Use the annotation "@test" in phpDoc instead.

Integration into pre-merge and nightly gitlab-ci suites.

Note we now have multiple tests based on nikic/php-parser.
Those should be rewritten to use one parser instance with
multiple visitors to scan php code only once. The
exception code checker should be rewritten to use
the parser, too. This will be done with a dedicated patch.

Resolves: #94859
Releases: master
Change-Id: I43ba692eaf2a342a2e0ed3d8978386ea5719183f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/70524


Tested-by: waldhacker's avatarwaldhacker <hello@waldhacker.dev>
Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: waldhacker's avatarwaldhacker <hello@waldhacker.dev>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
parent e9c09bc8
......@@ -105,6 +105,7 @@ Options:
- checkGruntClean: Verify "grunt build" is clean. Warning: Executes git commands! Usually used in CI only.
- checkPermissions: test some core files for correct executable bits
- checkRst: test .rst files for integrity
- checkTestMethodsPrefix: check tests methods do not start with "test"
- composerInstall: "composer install"
- composerInstallMax: "composer update", with no platform.php config.
- composerInstallMin: "composer update --prefer-lowest", with platform.php set to PHP version x.x.0.
......@@ -515,6 +516,12 @@ case ${TEST_SUITE} in
SUITE_EXIT_CODE=$?
docker-compose down
;;
checkTestMethodsPrefix)
setUpDockerComposeDotEnv
docker-compose run check_test_methods_prefix
SUITE_EXIT_CODE=$?
docker-compose down
;;
checkBom)
setUpDockerComposeDotEnv
docker-compose run check_bom
......
<?php
declare(strict_types=1);
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\ParserFactory;
use Symfony\Component\Console\Output\ConsoleOutput;
require_once __DIR__ . '/../../vendor/autoload.php';
/**
* This script checks tests do not start with "test" like
* "public function testSomething". Instead they should be
* like "public function SomethinIsLike()" and have a @test annotation.
*
* This is done for general consistency and the the "splitter"
* scripts that chunk tests to multiple nodes for functional tests.
*/
class NodeVisitor extends NodeVisitorAbstract
{
public array $matches = [];
public function enterNode(Node $node): void
{
if (($node instanceof Node\Stmt\ClassMethod) && str_starts_with($node->name->name, 'test')) {
$this->matches[$node->getLine()] = $node->name->name;
}
}
}
$parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7);
$finder = new Symfony\Component\Finder\Finder();
$finder->files()
->in([
__DIR__ . '/../../typo3/sysext/*/Tests/Unit/',
__DIR__ . '/../../typo3/sysext/*/Tests/UnitDeprecated/',
__DIR__ . '/../../typo3/sysext/*/Tests/Functional/'
])
->name('/Test\.php$/');
$output = new ConsoleOutput();
$errors = [];
foreach ($finder as $file) {
try {
$ast = $parser->parse($file->getContents());
} catch (Error $error) {
$output->writeln('<error>Parse error: ' . $error->getMessage() . '</error>');
exit(1);
}
$visitor = new NodeVisitor();
$traverser = new NodeTraverser();
$traverser->addVisitor($visitor);
$ast = $traverser->traverse($ast);
if (!empty($visitor->matches)) {
$errors[$file->getRealPath()] = $visitor->matches;
$output->write('<error>F</error>');
} else {
$output->write('<fg=green>.</>');
}
}
$output->writeln('');
if (!empty($errors)) {
$output->writeln('');
foreach ($errors as $file => $matchesPerLine) {
$output->writeln('');
$output->writeln('<error>At least on method starts with "test" in ' . $file . '</error>');
/**
* @var array $matchesPerLine
* @var int $line
* @var array $matches
*/
foreach ($matchesPerLine as $line => $methodName) {
$output->writeln('Method:' . $methodName . ' Line:' . $line);
}
}
exit(1);
}
exit(0);
......@@ -50,6 +50,7 @@ integration various php 7.4:
- Build/Scripts/runTests.sh -s checkCsvFixtures -p 7.4
- Build/Scripts/runTests.sh -s checkBom -p 7.4
- Build/Scripts/runTests.sh -s checkComposer -p 7.4
- Build/Scripts/runTests.sh -s checkTestMethodsPrefix -p 7.4
lint php 7.4:
stage: integrity
......
......@@ -41,6 +41,7 @@ integration various php 7.4 pre-merge:
- Build/Scripts/runTests.sh -s checkCsvFixtures -p 7.4
- Build/Scripts/runTests.sh -s checkBom -p 7.4
- Build/Scripts/runTests.sh -s checkComposer -p 7.4
- Build/Scripts/runTests.sh -s checkTestMethodsPrefix -p 7.4
lint scss ts html pre-merge:
stage: main
......
......@@ -510,6 +510,23 @@ services:
php -dxdebug.mode=off Build/Scripts/annotationChecker.php;
"
check_test_methods_prefix:
image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
user: "${HOST_UID}"
volumes:
- ${CORE_ROOT}:${CORE_ROOT}
- ${HOST_HOME}:${HOST_HOME}
- ${PASSWD_PATH}:/etc/passwd:ro
- /etc/group:/etc/group:ro
working_dir: ${CORE_ROOT}
command: >
/bin/sh -c "
if [ ${SCRIPT_VERBOSE} -eq 1 ]; then
set -x
fi
php -dxdebug.mode=off Build/Scripts/testMethodPrefixChecker.php;
"
check_bom:
image: typo3/core-testing-${DOCKER_PHP_IMAGE}:latest
user: "${HOST_UID}"
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment