Problem/Motivation

run-tests.sh contains two regular expressions used to find test classes inside files. These presume that the class keyword is never preceded by a modifier. The comment clearly states this is done to skip abstract classes, but as an unintended side-effect this also skips those classes that are final.

Proposed resolution

Allow final classes in the regular expression.

Remaining tasks

None.

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Xano created an issue. See original summary.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Assigned: Xano » Unassigned
Status: Active » Needs review
Related issues: +#2551981: Add --directory option to run-tests.sh test discovery
FileSize
669 bytes
Wim Leers’s picture

Re-discovered while working on #3081705: Make DrupalCI discover and run JSON:API Hypermedia's tests. Incredible that not a single person touched this issue in almost two years :O

Lendude’s picture

Should we add a final test for this, to test if this works? I don't see a way to verify this automatically but we can at least check manually in Drupal CI results

Wim Leers’s picture

#7++ — yes, but only if this is acceptable at all. I wonder if we explicitly do not want to allow for final test classes, and that's perhaps why this was never fixed?

alexpott’s picture

I think we should allow for final tests because that follows the principle of least surprise. If you as contrib or custom declare a test as final because well why not... and the test system doesn't discover it even though you can run it by pointing at the file directly that will take an age to debug. Adding a test with final is not enough to test this though. We need to test that run-tests.sh can discover it and I'm not sure we have that level of run-tests.sh testing anywhere. @mile23 has been trying to make run-tests.sh testable but it's a slow process because it's currently untested so changes are risky and complex.

I think a way forward is manual testing - or uploading a test-only path with a final test that's not discovered and one with the fix where it is discovered but still commit the same patch as in #5 as the tests require manually looking at what tests have run.

Mile23’s picture

This is the code path that applies to run-tests.sh --directory, which is used by drupalci for contrib. https://git.drupalcode.org/project/drupal/blob/8.8.x/core/scripts/run-te...

The same thing applies to --file. We still allow multiple test classes per file, BTW. That regex is supposed to also exclude abstract classes, but the regex is different. https://git.drupalcode.org/project/drupal/blob/8.8.x/core/scripts/run-te...

One semi-sustainable solution here is to add another function named something like simpletest_script_get_test_list_from_file() so we can at least have the regexes in the same place. That includes the one that discovers the namespace.

The tricky part about testing this core change with drupalci is that drupalci won't use --directory or --file for core, so even if you add a final test to core as a patch, drupalci will (should?) still discover it.

In an ideal world, we'd have a tiny utility class in Drupal\Core\Tests\ that knows how to do this, instead of adding simpletest_script_get_test_list_from_file(). Then we could build vfsStream mocks to our heart's content and check its behavior. That makes for easier patch review and also easier maintenance down the road.

Mile23’s picture

Issue tags: +run-tests.sh
Mile23’s picture

Here's a patch that refactors --file and --directory to use a utility class.

This will fail the final class unit test, because the regex in #5 isn't quite right. This uses the old regex.

Mile23’s picture

And phail on the follow-through. Let's try that again.

Mile23’s picture

Todo: Mark this as @internal. :-)

Status: Needs review » Needs work

The last submitted patch, 13: 2913819_13.patch, failed testing. View results

Mile23’s picture

Status: Needs work » Needs review
FileSize
7.55 KB
3.11 KB

Fixes regex:

@^\s*(?!abstract\s+)(?:final\s+|\s*)class ([^ ]+)@m

Adds another test case.

CS.

Marks this thing as @internal so we can refactor later as needed.

Wim Leers’s picture

Much better than my rough solution! :)

+++ b/core/tests/Drupal/Tests/Core/Test/RunTests/TestFileParserTest.php
@@ -68,10 +81,9 @@ public function testParseContents($expected, $contents) {
+    // This test.

🤔 This is an odd comment.

If it weren't for this, I'd RTBC :)

Wim Leers’s picture

Status: Needs review » Needs work
Mile23’s picture

Status: Needs work » Needs review
FileSize
7.53 KB
653 bytes

Removed offending comment.

Want to see something funny? #2871289-11: Make run-tests.sh --directory testable

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/lib/Drupal/Core/Test/RunTests/TestFileParser.php
@@ -0,0 +1,61 @@
+namespace Drupal\Core\Test\RunTests;

Hm, this is a new namespace. I … am not sure about this. But I don't feel strongly about it either. I'll see what a core committer thinks — in the end it doesn't matter much, especially because the sole class here is @internal anyway.

Mile23’s picture

  • catch committed 73972cc on 8.8.x
    Issue #2913819 by Mile23, Wim Leers: run-tests.sh ignores final classes
    
catch’s picture

Status: Reviewed & tested by the community » Fixed

If we have another class to add to the new namespace elsewhere, I think it's OK to add here. We can always move things around later (famous last words etc. but in theory at least).

Committed 73972cc and pushed to 8.8.x. Thanks!

catch’s picture

Version: 8.8.x-dev » 8.7.x-dev
Status: Fixed » Patch (to be ported)
Issue tags: +Needs re-roll

Wim Leers asked in slack if we could backport this - I think it's OK for backport, but it doesn't cherry-pick, so will need a re-roll against 8.7.x

Mile23’s picture

Status: Patch (to be ported) » Needs review
Issue tags: -Needs re-roll
FileSize
7.54 KB

Rerolled for 8.7.x

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 616d769 and pushed to 8.7.x. Thanks!

  • catch committed 616d769 on 8.7.x
    Issue #2913819 by Mile23, Wim Leers, catch: run-tests.sh ignores final...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.