Problem/Motivation

Currently you sometimes have to use --php=path-to-php in order to let run-tests.sh know where PHP is because the custom crafted code used to automatically detect the php runtime fails on some circumstances.

Plus, the current implementation fails when the PHP runtime definition includes arguments in itself.

Proposed resolution

Use Symfony's PhpExecutableFinder() to find out the current PHP runtime and do not escape the runtime itself when preparing the command.

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner created an issue. See original summary.

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.

generalredneck’s picture

This has been implemented in #2624926: Refactor run-tests.sh for Console component. for Drupal 8. Maybe the effort should be consolidated to there? Unless we feel like there should be a incremental fix.

dawehner’s picture

If #2624926: Refactor run-tests.sh for Console component. is also fixing this, it seems to do too much, doesn't it?

cilefen’s picture

Issue tags: +Novice

Yes, it seems maybe out of scope. Just a note for anyone fixing this: Unlike in the D7 issue where we must add a conditional for the constant in order to support PHP < 5.4, for D8 we can simply replace the conditional looking for the "_" environment variable with one looking for the constant. Are we interested in (EDITED) deprecating --php as useless?

generalredneck’s picture

Technically, having --php around allows you to use a different version of PHP to test against. So for example... if we have php 5.4 as our Global php, we could use php 5.6 to test against by defining --php

With that said, I can see the other side of it... if you want to use php 5.6... run the script in php 5.6... It really depends on how the CI is set up and if it actually uses this script and/or the --php argument when testing patches and commits.

dawehner’s picture

Technically, having --php around allows you to use a different version of PHP to test against. So for example... if we have php 5.4 as our Global php, we could use php 5.6 to test against by defining --php

you can do the same easily by using the right php binary right from the start. Note: This is a bit of a bad one anyway, as the webserver might still run in a different environment.

cilefen’s picture

True. And we can't just remove --php anyway in case anyone depends on it and it would break existing workflows. But we could toy with deprecating it. But why stress over it, because #2735005: Convert all Simpletest web tests to BrowserTestBase (or UnitTestBase/KernelTestBase).

generalredneck’s picture

@dawehner,
your comment is redundant to what I said in the second part of my comment.

dawehner’s picture

OH yeah you are right.

generalredneck’s picture

Status: Active » Needs review
FileSize
649 bytes

Regardless, lets start with having --php in the patch... that will be a starting point and have a patch for those who need this.

Mile23’s picture

Title: Use the PHP_BINARY constant instead of --php in run-tests.sh » Use the PHP_BINARY constant in addition to --php in run-tests.sh
Status: Needs review » Needs work

The testbot uses --php to force the php version.

If it's not present, you'd want the same binary as the one you're running under, which isn't always what PHP_BINARY tells you. PHP doesn't set it from the currently-running binary, it sets it through some shell that doesn't see your .profile, so it could pick up the wrong one, depending.

That's why getenv('_') is present, too. We want that. :-)

This is also a problem for the simpletest UI runner: #2476139-17: Fix UI test fail in SimpleTestBrowserTest

+++ b/core/scripts/run-tests.sh
@@ -391,9 +391,9 @@ function simpletest_script_init() {
-  elseif ($php_env = getenv('_')) {
-    // '_' is an environment variable set by the shell. It contains the command
-    // that was executed.

Don't remove this.

Mile23’s picture

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

I would like to see a proof of a PHP constant set in a .profile. If you have some documentation of how PHP_BINARY is set, please share.

In fact the _ variable doesn't exist on Windows and a few other Operating Systems without installing shell.

Mile23’s picture

I would like to see a proof of a PHP constant set in a .profile.

I mean that if you have two PHP binaries, as you do with a MAMP setup, then you're going to set a path to the MAMP binary in your PATH env. This leads to a problem when PHP is executed by the server, because exec() doesn't look at *your* shell dot files, it looks at some shell somewhere's dot files. At least as near as I can tell. Under CLI PHP_BINARY seems to do the right thing since it's executing as the same user.

So I was mistaken about the CLI, having experimented with it a bit.

david_garcia’s picture

Status: Needs work » Needs review
FileSize
1.37 KB

Elaborated on the original patch to make sure the same ini file that was used when calling the tests.

This also unveiled the fact that $php does not need to be escaped when building the shell command, as it is not being used like an argument there.

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.

droplet’s picture

Should we use \Symfony\Component\Process\PhpExecutableFinder instead?

david_garcia’s picture

@droplet That is so very true. Let's get rid of this old custom crafted logic.

It also ensure that the php runtime is generated including parameters (such as the php ini file, etc.).

Did not interdiff because the patch is mostly all new.

david_garcia’s picture

And this is how you make sure this is working as expected:

https://ci.appveyor.com/project/david-garcia-garcia/drupalcore-windows-t...

droplet’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @david_garcia! The patch looks good to me.

david_garcia’s picture

For the same reasons that in #2748883: Use the PHP_BINARY constant in addition to --php in run-tests.sh, tests not working by default in a platform that has > 80% of desktop market share make this major.

Without this fix, the testing script is unable to find the PHP executable on Windows.

david_garcia’s picture

Issue summary: View changes
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/scripts/run-tests.sh
@@ -440,20 +441,14 @@ function simpletest_script_init() {
-  elseif ($php_env = getenv('_')) {
-    // '_' is an environment variable set by the shell. It contains the command
-    // that was executed.
-    $php = $php_env;
-  }

@Mile23 has said that we want this. Since @Mile23 helps maintain DrupalCI we probably should have a good reason to remove this. As far as I can see, this change is not actually required to fix the problem. Why not leave that in?

david_garcia’s picture

Status: Needs work » Needs review

@alexpott, @Mile23 PhpExecutableFinder() has a more elaborate logic to detect the php runtime, and it makes sense to use an upstream tool to do this.

It's true that PhpExecutableFinder() does not consider getenv('_'). Looking at the implementation, PHP_BINARY is only used when running in cli, cli-server and phpdbg, plus it has additional checks. So it looks they added some logic to ensure that PHP_BINARY is only used when it is trully reliable.

If something is broken or no totally correct in PhpExecutableFinder() it would make more sense to fix that upstream, as PhpExecutableFinder() is used in other places of Drupal core.

alexpott’s picture

@david_garcia but the use of _ is specific to run-tests so not appropriate for an upstream fix - because PhpExecutableFinder is designed to work regardless of SAPI where run-tests.sh is designed to work from the CLI where the _ environment variable will be set on linux.

david_garcia’s picture

Brought back evnget('_'). Don't really like it, but will do no harm.

Munavijayalakshmi’s picture

Rerolled the patch.

droplet’s picture

Status: Needs review » Reviewed & tested by the community

RTBC again. This is useful for Windows.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/scripts/run-tests.sh
    @@ -446,15 +447,14 @@ function simpletest_script_init() {
    -  elseif ($sudo = getenv('SUDO_COMMAND')) {
    -    // 'SUDO_COMMAND' is an environment variable set by the sudo program.
    -    // Extract only the PHP interpreter, not the rest of the command.
    -    list($php) = explode(' ', $sudo, 2);
    -  }
    

    I really don't know why we're removing this. It was added in #311160: Enhance run_tests.sh by automatically using right php interpreter and it is not part of doing better detection. I'm all for better detection on windows but there's no need to make unnecessary changes to a script that is used in people's build processes. There's no comment as to why this is breaking anything. As far as I can see, it is not. What is fixing things is the use of Symfony's php executable finder.

  2. +++ b/core/scripts/run-tests.sh
    @@ -446,15 +447,14 @@ function simpletest_script_init() {
    +      $php = $php_executable_finder->find();
    +      if ($php == FALSE) {
    +        simpletest_script_print_error('Unable to automatically determine the path to the PHP interpreter. Supply the --php command line argument.');
    +        simpletest_script_help();
    +        exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
    +      }
    

    The indentation is incorrect here.

droplet’s picture

Hmm. I think we should put SUDO_COMMAND after PhpExecutableFinder as the last attempt if we still want to keep it.

For example, if we must run with sudo on the previous command but PHP does not.

sudo echo 'START Testing' && php ./testing.sh
sudo -i env && php ./testing.sh

(so this is a bug acutally)

And I guess it explained why only run-tests.sh on scripts folder missing #!/usr/bin/env php. So people don't run:

sudo bash ./run-tests.sh

pk188’s picture

Status: Needs work » Needs review
FileSize
2.21 KB
1.31 KB

Fixed #30

alexpott’s picture

+++ b/core/scripts/run-tests.sh
@@ -441,20 +442,24 @@ function simpletest_script_init() {
-  elseif ($php_env = getenv('_')) {
-    // '_' is an environment variable set by the shell. It contains the command
-    // that was executed.
-    $php = $php_env;
-  }
   elseif ($sudo = getenv('SUDO_COMMAND')) {
     // 'SUDO_COMMAND' is an environment variable set by the sudo program.
     // Extract only the PHP interpreter, not the rest of the command.
     list($php) = explode(' ', $sudo, 2);
   }
+  elseif ($php_env = getenv('_')) {
+    // '_' is an environment variable set by the shell. It contains the command
+    // that was executed.
+    $php = $php_env;
+  }

Let's not change this order.

Also @pk188 that's not quite what @droplet was suggesting. However I think doing the smallest possible change here is advisable and not worry too much about SUDO_COMMAND and whether it works.

Let's just use Symfony's thing in the else case and be done.

droplet’s picture

SKIPPED THE PATCH, SEE NEXT COMMENT

OK. Not well coded but it works.

I suggested dropping a @deprecated and @todo over there to remove it at some point. ( I didn't add to current patch )

_ and SUDO_COMMAND was a hack to cheating while we do not PHP_BINARY in the old day (Just introduced from PHP5.2 PHP5.4)

droplet’s picture

FileSize
1.64 KB

The last submitted patch, 34: run-tests.patch, failed testing.

The last submitted patch, 34: run-tests.patch, failed testing.

Mile23’s picture

You know how phpunit does it?

SebastianBergmann\Environment\Runtime->getBinary() which uses both $_SERVER['_'] and PHP_BINARY.

Experimentation tells me that this runs into the problem of double escapeshellarg() in combination with run-tests.sh.

Therefore if we want to benefit from not having to maintain this hunk of code in the future, we should rename global $php to global $escaped_php, adjust simpletest_script_command() to acknowledge that it's already escaped, and rawk on.

Now, will it blend? Let's find out. I had a problem with lots of failing tests locally with unmodified run-tests.sh, so I'm not sure what will happen here.

If it explodes too much, please ignore. :-)

run-tests.sh already warns the user if phpunit is not available, but sebastianbergmann/environment isn't, strictly speaking, the same as phpunit. We might need to explicitly declare it as a dev dependency. Or not.

Status: Needs review » Needs work

The last submitted patch, 38: 2748883_38.patch, failed testing.

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.

neclimdul’s picture

Status: Needs work » Needs review
FileSize
1.07 KB

yowza, means if you use have multiple version of php installed you call different versions and run the quitesuite against different verions :( :(. The indeed is major.

Things at least in the simpletest.module code have changed quite a bit but for the better, specifically fixing the Windows bug in #22, just linux didn't get the fix. We can just use the same code that makes windows work to resolve the php executable for all platforms and that should take care of it.

I've no idea what's going on in run-tests though so... maybe this is all that's needed now since everything is getting switched to phpunit tests?

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.

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.

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.

smustgrave’s picture

Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Needs Review Queue Initiative

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

With simpletest being removed in D10 is this still relevant?
If so please update IS with the rescope.

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.