Problem/Motivation
Following up from #2927012: _drupal_log_error() returns a 0 exit code on errors, I've been investigating how to make \Drupal\simpletest\TestDiscovery
not load PHP fixtures and other PHP files which may not be PHP classes.
The suggestion was made to simply exclude any directory named 'fixtures'. On closer reading of the PSR-4 spec, it seems that all it requires is that a given class name maps to a file path - not that a file path in a PSR-4 directory maps back to a valid class. Yet, this is exactly what we do in \Drupal\simpletest\TestDiscovery::scanDirectory
.
I took a quick look at adding test classes to the autoloader in \Drupal\Core\Composer\Composer
, which would let us remove TestDiscovery entirely. Unfortunately, TestDiscovery has a dependency on the module handler, so it can call hook_simpletest_alter()
, and it's impossible to call that from the Composer script. Since that would be a compatibility break, it seems like this would be a 9.x task (which I'll file a followup for).
Proposed resolution
For 8.x, we simply ignore all directories named 'fixtures'. In 9.x, use a composer script to discover test classes.
Remaining tasks
Document this somewhere obvious so test writers understand that a 'fixtures' directory will never load classes, and what the best directories are for fixtures for a variety of use cases.
Comment | File | Size | Author |
---|---|---|---|
#10 | 2942530.10-test-discovery-fixtures.patch | 4.02 KB | deviantintegral |
#2 | 2942530.2-test-discovery-fixtures.patch | 4.02 KB | deviantintegral |
Comments
Comment #2
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedThis patch:
Comment #3
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedComment #5
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedRetesting as there's strange database errors and this and the same test passes fine locally.
Comment #6
Mile23We're trying to remove TestDiscovery's dependencies #2863055: Move TestDiscovery out of simpletest module, minimize dependencies and #2460521: Deprecate hook_simpletest_alter(). One solution to requiring the module handler is that there be two TestDiscovery classes: One that does the discovering, and one that's wraps it to be the service for simpletest module.
Generally +1 on this idea. Requiring *Test.php allows for loading fixture tests that maybe we shouldn't.
Comment #9
Mile23Pretty sure this is solved by #2973127: Exclude *TestBase.php, *Trait.php and *Interface.php files from test discovery reflection
Comment #10
deviantintegral CreditAttribution: deviantintegral at Lullabot commentedThe above issue doesn't cover non-class php files, and the test case in this issue fails on 8.7.x:
Here's a reroll against 8.7.x.
Comment #11
Mile23TestDiscovery
comes from The Time Before PSR-4... It builds a hypothetical class map and then discovers files that would be in it. It's a thing we should change, but can't (apparently).Actually, use phpunit's bootstrap.php, which we already do. :-) bootstrap.php calls
TestDiscovery::scanDirectory()
in order to provide the filtering we're working on here. We don't want autoloading of test files. We want them to be discovered as tests with as little class loading as possible, auto or otherwise.Also, the dependency on the module handler should go away with #2863055: Move TestDiscovery out of simpletest module, minimize dependencies which would necessitate a re-roll here.
This is in
scanDirectory()
, so the file might not be subsequently loaded. Should read 'Fixtures may contain PHP scripts that should not be discovered.'Moving to 8.7.x since this is a testing improvement.
Also citing #2927012-21: _drupal_log_error() returns a 0 exit code on errors because that's where @alexpott says it's a good idea. :-)
Comment #14
quietone CreditAttribution: quietone as a volunteer commentedTriaging issues in simpletest.module as part of the Bug Smash Initiative to determine if they should be in the Simpletest Project or core.
This looks like it a Phpunit issue, changing component.