-
Notifications
You must be signed in to change notification settings - Fork 6
GH-119: Add module-scoped phpunit GrumPHP task #121
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
Open
hkirsman
wants to merge
24
commits into
main
Choose a base branch
from
feature/119-Optimize-GrumPHP-phpunit-to-run-only-affected-modules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
de80ce2
GH-119: Add module-scoped phpunit GrumPHP task
hkirsman 92e3d14
GH-119: Add phpunit_drupal_modules GrumPHP task
hkirsman 9f9638e
119: Optimize Drupal module phpunit task
hkirsman 81f1c24
#119: Add configurable phpunit config file for Drupal modules task
hkirsman 3866dde
#119: Add testsuite option to Drupal modules phpunit task
hkirsman e993a86
#119: Print affected modules before running phpunit
hkirsman 2a5f9eb
#119: Clarify and tighten Drupal modules phpunit task output
hkirsman dfa3a7e
119: Report modules without tests in phpunit task
hkirsman cca6935
#119: Highlight modules without tests in phpunit task
hkirsman 51ed9d2
#119: Make phpunit_drupal_modules module roots configurable
hkirsman f2b754a
#119: Add tests for PhpUnitDrupalModulesTask arguments
hkirsman 3238a92
119: Avoid callback functions in PhpUnitDrupalModulesTask
hkirsman a6a243d
119: Remove trailing blank lines from files
hkirsman 6d5c8df
119: Remove empty line from PhpUnitDrupalModulesExtensionLoader
hkirsman bc89f31
#119: Refactor PhpUnitDrupalModulesTask for improved readability
hkirsman 478d369
#119: Run phpunit per affected Drupal module
hkirsman ba7a752
#119: Log per-module progress in Drupal phpunit task
hkirsman 18cf0f5
#119: Add timeout option for Drupal modules phpunit task
hkirsman e3c5837
#119: Add integration test for Drupal modules phpunit task
hkirsman 42dd850
#119: Improve visibility and configuration of Drupal modules phpunit …
hkirsman a1a54e5
#119 Fix tests and coding standards.
hkirsman 8b175c8
#119 Fix tests.
hkirsman 7a2403f
#119: Assert skipped result code
hkirsman 1688231
#119 We phpunit_drupal_modules task name as it's used on screen.
hkirsman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Task/PhpUnitDrupalModules/PhpUnitDrupalModulesExtensionLoader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Wunderio\GrumPHP\Task\PhpUnitDrupalModules; | ||
|
|
||
| use Wunderio\GrumPHP\Task\AbstractExternalExtensionLoader; | ||
|
|
||
| /** | ||
| * Registers the PhpUnitDrupalModules task in GrumPHP. | ||
| */ | ||
| class PhpUnitDrupalModulesExtensionLoader extends AbstractExternalExtensionLoader {} |
229 changes: 229 additions & 0 deletions
229
src/Task/PhpUnitDrupalModules/PhpUnitDrupalModulesTask.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Wunderio\GrumPHP\Task\PhpUnitDrupalModules; | ||
|
|
||
| use GrumPHP\Collection\ProcessArgumentsCollection; | ||
| use GrumPHP\Runner\TaskResult; | ||
| use GrumPHP\Runner\TaskResultInterface; | ||
| use GrumPHP\Task\Context\ContextInterface; | ||
| use Wunderio\GrumPHP\Task\AbstractMultiPathProcessingTask; | ||
|
|
||
| /** | ||
| * Class PhpUnitDrupalModulesTask. | ||
| * | ||
| * Runs phpunit only for affected Drupal custom modules. | ||
| */ | ||
| class PhpUnitDrupalModulesTask extends AbstractMultiPathProcessingTask { | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function run(ContextInterface $context): TaskResultInterface { | ||
| $config = $this->getConfig()->getOptions(); | ||
| $paths = $this->getPathsOrResult($context, $config, $this); | ||
|
|
||
| if ($paths instanceof TaskResultInterface) { | ||
| return $paths; | ||
| } | ||
|
|
||
| $moduleRoots = $this->getModuleRoots($config); | ||
| $modules = $this->collectModulesFromPaths($paths, $moduleRoots); | ||
|
|
||
| [$modulesWithTests, $modulesWithoutTests] = $this->splitModulesByTests($modules); | ||
|
|
||
| $this->printModulesWithoutTests($modulesWithoutTests); | ||
|
|
||
| if (!$modulesWithTests) { | ||
| return TaskResult::createSkipped($this, $context); | ||
| } | ||
|
|
||
| // Run phpunit once per affected module so that each module's directory | ||
| // is honoured even when a testsuite is used in the configuration file. | ||
| $this->printModulesWithTests($modulesWithTests); | ||
|
|
||
| $moduleCount = count($modulesWithTests); | ||
| $timeout = $config['timeout'] ?? NULL; | ||
|
|
||
| foreach ($modulesWithTests as $index => $modulePath) { | ||
| $process = $this->processBuilder->buildProcess($this->buildArguments([$modulePath])); | ||
|
|
||
| if ($timeout !== NULL) { | ||
| $process->setTimeout($timeout); | ||
| } | ||
|
|
||
| $process->run(); | ||
|
|
||
| $result = $this->getTaskResult($process, $context); | ||
|
|
||
| fwrite( | ||
| STDOUT, | ||
| sprintf( | ||
| "%s: finished module %d/%d: %s [%s]\n\n", | ||
| $this->getName(), | ||
| $index + 1, | ||
| $moduleCount, | ||
| $modulePath, | ||
| $result->isPassed() ? 'OK' : 'FAILED' | ||
| ) | ||
| ); | ||
|
|
||
| if (!$result->isPassed()) { | ||
| // Stop on first failure/error to keep feedback fast and clear. | ||
| return $result; | ||
| } | ||
| } | ||
|
|
||
| return TaskResult::createPassed($this, $context); | ||
| } | ||
|
|
||
| /** | ||
| * Determine which directory roots should be treated as Drupal module roots. | ||
| * | ||
| * Defaults to web/modules/custom for backward compatibility, but can be | ||
| * configured via the run_on option in tasks.yml. | ||
| * | ||
| * @param array $config | ||
| * Task configuration options. | ||
| * | ||
| * @return string[] | ||
| * Normalised module root paths. | ||
| */ | ||
| private function getModuleRoots(array $config): array { | ||
| $moduleRoots = $config['run_on'] ?? ['web/modules/custom']; | ||
| $normalisedRoots = []; | ||
| foreach ($moduleRoots as $root) { | ||
| $normalisedRoots[] = rtrim((string) $root, '/'); | ||
| } | ||
| return array_values($normalisedRoots); | ||
| } | ||
|
|
||
| /** | ||
| * Collect all affected modules from the changed file paths. | ||
| * | ||
| * @param iterable $paths | ||
| * Changed paths. | ||
| * @param string[] $moduleRoots | ||
| * Module root directories. | ||
| * | ||
| * @return string[] | ||
| * Module paths keyed by path for uniqueness. | ||
| */ | ||
| protected function collectModulesFromPaths(iterable $paths, array $moduleRoots): array { | ||
| $modules = []; | ||
| foreach ($paths as $file) { | ||
| $path = (string) $file; | ||
| foreach ($moduleRoots as $root) { | ||
| $rootWithSlash = $root . '/'; | ||
|
|
||
| if (!str_starts_with($path, $rootWithSlash)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (preg_match('#^(' . preg_quote($root, '#') . '/[^/]+)#', $path, $matches)) { | ||
hkirsman marked this conversation as resolved.
Show resolved
Hide resolved
hkirsman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $modules[$matches[1]] = $matches[1]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return $modules; | ||
| } | ||
|
|
||
| /** | ||
| * Split modules into ones with and without tests directories. | ||
| * | ||
| * @param string[] $modules | ||
| * All affected modules. | ||
| * | ||
| * @return array{0: string[], 1: string[]} | ||
| * First array contains modules with tests, second without. | ||
| */ | ||
| protected function splitModulesByTests(array $modules): array { | ||
| $modulesWithTests = []; | ||
| foreach ($modules as $modulePath) { | ||
| if (is_dir($modulePath . '/tests')) { | ||
| $modulesWithTests[] = $modulePath; | ||
| } | ||
| } | ||
|
|
||
| $modulesWithoutTests = array_values(array_diff($modules, $modulesWithTests)); | ||
|
|
||
| return [$modulesWithTests, $modulesWithoutTests]; | ||
| } | ||
|
|
||
| /** | ||
| * Print a note about affected modules that do not have tests. | ||
| * | ||
| * @param string[] $modulesWithoutTests | ||
| * Affected modules without tests. | ||
| */ | ||
| private function printModulesWithoutTests(array $modulesWithoutTests): void { | ||
| if (!$modulesWithoutTests) { | ||
| return; | ||
| } | ||
|
|
||
| $lines = []; | ||
| foreach ($modulesWithoutTests as $modulePath) { | ||
| $lines[] = ' - ' . $modulePath; | ||
| } | ||
|
|
||
| fwrite( | ||
| STDOUT, | ||
| sprintf( | ||
| "\n%s: NOTE: affected modules without tests:\n%s\n\n", | ||
| $this->getName(), | ||
| implode("\n", $lines) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Print a list of modules for which tests will be executed. | ||
| * | ||
| * @param string[] $modulesWithTests | ||
| * Affected modules with tests. | ||
| */ | ||
| private function printModulesWithTests(array $modulesWithTests): void { | ||
| $lines = []; | ||
| foreach ($modulesWithTests as $modulePath) { | ||
| $lines[] = ' - ' . $modulePath; | ||
| } | ||
|
|
||
| fwrite( | ||
| STDOUT, | ||
| sprintf( | ||
| "%s: running tests for modules:\n%s\n\n", | ||
| $this->getName(), | ||
| implode("\n", $lines) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function buildArguments(iterable $modules): ProcessArgumentsCollection { | ||
| $config = $this->getConfig()->getOptions(); | ||
|
|
||
| $arguments = $this->processBuilder->createArgumentsForCommand('phpunit'); | ||
|
|
||
| if (!empty($config['config_file'])) { | ||
| // Mirror GrumPHP's core phpunit task: allow passing a custom config file. | ||
| $arguments->add('-c'); | ||
| $arguments->add($config['config_file']); | ||
| } | ||
|
|
||
| if (!empty($config['testsuite'])) { | ||
| $arguments->add('--testsuite'); | ||
| $arguments->add($config['testsuite']); | ||
| } | ||
|
|
||
| foreach ($modules as $modulePath) { | ||
| $arguments->add($modulePath); | ||
| } | ||
|
|
||
| return $arguments; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| services: | ||
| Wunderio\GrumPHP\Task\PhpUnitDrupalModules\PhpUnitDrupalModulesTask: | ||
| class: Wunderio\GrumPHP\Task\PhpUnitDrupalModules\PhpUnitDrupalModulesTask | ||
| arguments: | ||
| - '@process_builder' | ||
| - '@formatter.raw_process' | ||
| tags: | ||
| - { name: grumphp.task, task: phpunit_drupal_modules } | ||
hkirsman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.