Problem/Motivation

run-tests.sh is a mess. It's brittle and untestable.

Proposed resolution

Refactor run-tests.sh to use the Symfony Console component.

This will have to occur over a few different phases:

1) Straightforward conversion of existing functions to Command methods, with calling code moved to execute(). under_score function names are temporarily retained from the original script so I know where I am. (done)

2) Proper injection of various services used by command methods, including DRUPAL_ROOT. They currently use \Drupal. (mostly done)

3) Refactor various methods to reduce individual complexity. (done in some places, such as RunTestsCommand::simpletest_script_get_test_list())

4) Move the classes out of run-tests.sh into a proper PSR-4 namespace to enable testing. (done: Drupal\Core\RunTests)

5) Shim out direct calls to simpletest functions such as \simpletest_clean_results_table() so that we can mock for unit testing.

6) Add unit tests for testable portions.

7) [Your concern here]

8) Figure out how to review. :-)

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Mile23 created an issue. See original summary.

mile23’s picture

Status: Active » Needs review
StatusFileSize
new49.91 KB

This is a first-pass at some of the work in phase 1 mentioned above. It's a WIP. Some things work, some don't.

I've used the file name run-tests.php because it's easier to have run-tests.sh in the next tab to refer to. Obviously we'd change the file name once it's in shape. Or would we? :-)

Because of the way Console works, you have to specify a run-tests command, like this:
php core/scripts/run-tests.php run-tests --list

chi’s picture

Because of the way Console works, you have to specify a run-tests command, like this:
php core/scripts/run-tests.php run-tests --list

We can set run-tests as default command.
$app->setDefaultCommand('run-tests');

mile23’s picture

StatusFileSize
new56.2 KB
new32.08 KB

Yah, thanks. I figured it out. I had to make a single-command application because otherwise there are parsing errors, since run-tests.sh has a lot of options that should be commands.

Still a WIP...

Exception handling is mostly baked. We can't use ConsoleEvents::EXCEPTION handler because that would add complexity in the wrong places. Add that to the @todo list.

Message coloring works with --color which is, sadly, off by default.

How to evaluate:

Apply patch.

Type: php ./core/scripts/run-tests.php --help

Ugly but useable... Still WIP.

Make sure there's an installed Drupal and do something like:

php ./core/scripts/run-tests.php --module node --color

Some tests fail for mysterious reasons, but many do not.

When you're done:

php ./core/scripts/run-tests.php --clean

mile23’s picture

mile23’s picture

Issue tags: +run-tests.sh
StatusFileSize
new101.98 KB
new54.02 KB

Here it is patched as run-tests.sh. I'm also going to let the testbot run it, because there are some tests that fail locally in the UI.

The patch git generated is a confuzzled mess so I'll upload the new script as well.

mile23’s picture

Issue summary: View changes
StatusFileSize
new107.79 KB

Moved most of the code to \Drupal\Core\RunTests which allows us to start writing unit tests without a lot of require_once mumbo jumbo.

Starting to shim out the direct simpletest.module calls so we can mock them for testing.

See the issue update for more details.

Status: Needs review » Needs work

The last submitted patch, 7: 2624926_7.patch, failed testing.

The last submitted patch, 7: 2624926_7.patch, failed testing.

The last submitted patch, 7: 2624926_7.patch, failed testing.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new113.38 KB

Not entirely sure why #7 is failing. It'd be nice if CI began storing artifacts before the test ran, so we could see what's causing the problem. (There's no log of the exception/fatal.)

Anyway, it all works locally, so here is more lightweight refactoring with some unit tests.

Really not ready for prime-time yet.

Status: Needs review » Needs work

The last submitted patch, 11: 2624926_11.patch, failed testing.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new116.48 KB
new9.58 KB

Expanded some unit testing, refactored the Config class to facilitate testing.

CI errors still a mystery.

Status: Needs review » Needs work

The last submitted patch, 13: 2624926_13.patch, failed testing.

mile23’s picture

Local testbot says the error is:

$ more testgroups.txt 
exception 'Symfony\Component\DependencyInjection\Exception\InvalidArgumentExcept
ion' with message 'The service file "core/core.services.yml" is not valid.' in /
var/www/html/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php:300
Stack trace:
#0 /var/www/html/core/lib/Drupal/Core/DependencyInjection/YamlFileLoader.php(68)
: Drupal\Core\DependencyInjection\YamlFileLoader->loadFile('core/core.servi...')
#1 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(1169): Drupal\Core\Depend
encyInjection\YamlFileLoader->load('core/core.servi...')
#2 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(824): Drupal\Core\DrupalK
ernel->compileContainer()
#3 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(441): Drupal\Core\DrupalK
ernel->initializeContainer()
#4 /var/www/html/core/lib/Drupal/Core/Test/TestRunnerKernel.php(71): Drupal\Core
\DrupalKernel->boot()
#5 /var/www/html/core/lib/Drupal/Core/DrupalKernel.php(686): Drupal\Core\Test\Te
stRunnerKernel->boot()
#6 /var/www/html/core/lib/Drupal/Core/RunTests/RunTestsCommand.php(209): Drupal\
Core\DrupalKernel->prepareLegacyRequest(Object(Symfony\Component\HttpFoundation\
Request))
#7 /var/www/html/vendor/symfony/console/Command/Command.php(256): Drupal\Core\Ru
nTests\RunTestsCommand->execute(Object(Symfony\Component\Console\Input\ArgvInput
), Object(Symfony\Component\Console\Output\ConsoleOutput))
#8 /var/www/html/vendor/symfony/console/Application.php(838): Symfony\Component\
Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), 
Object(Symfony\Component\Console\Output\ConsoleOutput))
#9 /var/www/html/vendor/symfony/console/Application.php(189): Symfony\Component\
Console\Application->doRunCommand(Object(Drupal\Core\RunTests\RunTestsCommand), 
Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Cons
ole\Output\ConsoleOutput))
#10 /var/www/html/vendor/symfony/console/Application.php(120): Symfony\Component
\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), O
bject(Symfony\Component\Console\Output\ConsoleOutput))
#11 /var/www/html/core/scripts/run-tests.sh(16): Symfony\Component\Console\Appli
cation->run()
#12 {main}

So the testrunner kernel's container is failing to initialize because there's something strange about how it tries to load core.services.yml.

If anyone has any ideas... :-)

dawehner’s picture

IMHO run-tests.sh should work outside of Drupal, given that this is what you want to test in the first place. Do you really require those files?

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new119.15 KB
new12.85 KB

IMHO run-tests.sh should work outside of Drupal, given that this is what you want to test in the first place.

Ideally, yes, but I'm only trying to refactor it so it's testable and maintainable, not change any fundamental behavior. So far the patch has a number of unit tests of run-test.sh's behavior, which is the point of the exercise.

Decoupling can come later, along with whatever other changes we desire.

Speaking of which.... :-)

Here's a patch which adds some coding standards stuff, and moves PHP executable discovery away from getenv('_') towards PHP_BINARY. With tests. (The tests were reporting phpunit as the executable when using getenv('_').)

Once the sudo change happens at the testbot level, this will pass and will only need a change away from simpltest_script_underscore method names to camelCase to be acceptable, if not thoroughly tested.

But it will provide a framework by which new behaviors can be tested, and bugs tested against.

It might be that this gets shoved to 8.1.x and that's fine, too.

Status: Needs review » Needs work

The last submitted patch, 18: 2624926_18.patch, failed testing.

mile23’s picture

Version: 8.0.x-dev » 8.1.x-dev
StatusFileSize
new123.52 KB

Bumped to 8.1.x.

Renamed all the under_score names to camelCase. Yanked Console's support for --help, so we only ever see the old help screen we're familiar with.

This is a nice stopping point. Let's call it a DRY normalization, such that the refactored repeated parts are now unit-testable. This is mostly related to test discovery, which is the part I was interested in.

Anyone is welcome to join in, of course. :-)

Next steps:

  • Figure out why the old script can access core/core.services.yml just fine under the testbot, whereas this one can't unless you run it as sudo.
  • Rename it to run-tests.php. Please.
  • Promote e.g. --list and --clean to commands.
mile23’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 20: 2624926_21.patch, failed testing.

dawehner’s picture

@mile23
Please keep in mind that even refactoring is nice, you want to try to keep the issue in scope so its actually reviewable. Otherwise you won't make any progress at all.

mile23’s picture

That's why I called it a stopping point.

The only real issue at this point is why core.services.yml isn't readable by the testbot. My local testbot builds keep flaking out after one run (don't dare vagrant halt or it's all over) so going is slow.

The wins here are:

  • We can let the framework deal with parsing input, so that's code that doesn't need to be maintained.
  • Any part of the test runner code can now be refactored more easily, and made more testable.
  • More of the behavior of the test runner can be re-used by other components like simpletest module.
jhedstrom’s picture

+++ b/core/lib/Drupal/Core/RunTests/RunTestsApplication.php
@@ -0,0 +1,93 @@
+  public function getDefinition() {
+    $input_definition = parent::getDefinition();
+    // Clear out the normal first argument, which is the command name.
+    $input_definition->setArguments();
+    return $input_definition;
+  }

This is very generic to single-command applications. I think we should provide a base application class for single-command apps (the db dump script could be the proof of concept).

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

finne’s picture

+1 on the rename: other .sh scripts in the /core/scripts dir are all valid shell scripts (that mostly boot php on the first line). This file should have a php extention, or the first line should add #!/bin/php

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

mile23’s picture

#2629700: Force run-tests.sh to run as www-data to generate the simpletest test list. is close to happening, so this refactor will be testable.

A lot of the code of run-tests.sh will have changed, so this won't just be a re-roll if/when work resumes.

Version: 8.3.x-dev » 8.4.x-dev

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

mile23’s picture

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.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.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.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

voleger’s picture

Issue tags: +Needs reroll
ravi.shankar’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new127.21 KB

Added reroll of patch #20 on Drupal-9.2.x

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mondrake’s picture

Component: simpletest.module » phpunit
Status: Needs review » Needs work

Change issue component.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.