Skip to content
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

WP_Env: tests-cli phpunit executable file not found in $PATH: unknown #51686

Closed
JUVOJustin opened this issue Jun 20, 2023 · 15 comments · Fixed by #54508
Closed

WP_Env: tests-cli phpunit executable file not found in $PATH: unknown #51686

JUVOJustin opened this issue Jun 20, 2023 · 15 comments · Fixed by #54508
Assignees
Labels
Needs Testing Needs further testing to be confirmed. [Package] Env /packages/env [Type] Bug An existing feature does not function as intended

Comments

@JUVOJustin
Copy link

JUVOJustin commented Jun 20, 2023

Description

I want to run my phpunit tests within wp-env tests-cli container as it is suggested in the documentation. However, when running wp-env run tests-cli wp-env run tests-cli --env-cwd=wp-content/plugins/juvo-session-manager phpunit i get the following error message:

ℹ Starting 'wp-env run tests-cli phpunit' on the tests-cli container. 

OCI runtime exec failed: exec failed: unable to start container process: exec: "wp-env": executable file not found in $PATH: unknown
✖ Command failed with exit code 126
Command failed with exit code 126

Same command with composer does work. Also, when starting bash inside the container, i see that phpunit is generally available.

Step-by-step reproduction instructions

  1. Go into plugin folder
  2. Star with npm run wp-env start
  3. Run wp-env run tests-cli --env-cwd=wp-content/plugins/juvo-session-manager phpunit

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@Thelmachido Thelmachido added [Type] Bug An existing feature does not function as intended Needs Testing Needs further testing to be confirmed. labels Jun 27, 2023
@github-actions
Copy link

Hi,
This issue has gone 30 days without any activity. This means it is time for a check-in to make sure it is still relevant. If you are still experiencing this issue with the latest versions, you can help the project by responding to confirm the problem and by providing any updated reproduction steps.
Thanks for helping out.

@github-actions github-actions bot added the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Jul 28, 2023
@bgoewert
Copy link

Just ran into this same issue. Using the latest package (8.5.0).

@github-actions github-actions bot removed the [Status] Stale Gives the original author opportunity to update before closing. Can be reopened as needed. label Aug 13, 2023
@antonlukin
Copy link

antonlukin commented Sep 4, 2023

Confirmed on version 8.7.0.
On 7.0.0 it works for me.

@dawidurbanski
Copy link

Same here. If I manually exec into the container (any of these: "mysql", "tests-mysql", "wordpress", "tests-wordpress", "cli", "tests-cli"), and I run phpunit --version, I get:

bash: phpunit: command not found

So it looks like the phpunit executable is just not there at all.

@dawidurbanski
Copy link

dawidurbanski commented Sep 15, 2023

As an alternative, if your project uses composer to install packages (mine does, only dev dependencies), you can install phpunit locally and use it this way.

Before:

$ wp-env run tests-cli --env-cwd=wp-content/plugins/myplugin phpunit

ℹ Starting 'phpunit' on the tests-cli container.

OCI runtime exec failed: exec failed: unable to start container process: exec: "phpunit": executable file not found in $PATH: unknown

PHPUnit installed locally (indirectly via PHPUnit Polyfills):

$ wp-env run cli --env-cwd=wp-content/plugins/myplugin composer install require --dev yoast/phpunit-polyfills:"^1.0"

then

$ wp-env run tests-cli --env-cwd=wp-content/plugins/myplugin ./vendor/bin/phpunit

.                                                                   1 / 1 (100%)

Time: 00:00.074, Memory: 36.50 MB

OK (1 test, 1 assertion)

Please note that I used PHPUnit Polyfills version 1. When trying this with version 2, I got plenty of exceptions and it didn't work (but that's another issue).

E                                                                   1 / 1 (100%)

Time: 00:00.122, Memory: 38.50 MB

There was 1 error:

1) SampleTest::test_sample
Error: Call to undefined method PHPUnit\Util\Test::parseTestMethodAnnotations()

/wordpress-phpunit/includes/abstract-testcase.php:547
/wordpress-phpunit/includes/abstract-testcase.php:137
/var/www/html/wp-content/plugins/myplugin/vendor/yoast/phpunit-polyfills/src/TestCases/TestCasePHPUnitGte8.php:60
@jrtashjian
Copy link
Contributor

Here's what I found while investigating.

In the container:

  • phpunit is not located within the /usr/bin or /usr/local/bin directories.
  • You can find phpunit in the home directory of the container at ~/.composer/vendor/bin/phpunit.

Additionally:

  • The yoast/phpunit-polyfills composer package is globally installed during the construction of the container image, as indicated here which pulls phpunit into the global composer bin directory.

The issue:

  • It appears that the composer directory containing phpunit is not added to the $PATH.

@dawidurbanski's solution works and is what I am doing in my own projects as well.

You can also change the path in their solution like so to use the globally installed version in the meantime:

wp-env run tests-cli --env-cwd=wp-content/plugins/myplugin ~/.composer/vendor/bin/phpunit

Ultimately the global composer bin directory should be added to the $PATH in the container image.

@dawidurbanski
Copy link

The yoast/phpunit-polyfills composer package is globally installed during the construction of the container image, as indicated here which pulls phpunit into the global composer bin directory.

Ah! This could explain why installing yoast/phpunit-polyfills:"^2.0" causes an issue. It's just a conflict between these two probably.

@jrtashjian
Copy link
Contributor

jrtashjian commented Sep 15, 2023

What's interesting is that if I echo $PATH inside the container, the global composer bin directory is returned in the $PATH.

$ npx wp-env run tests-cli bash
ℹ Starting 'bash' on the tests-cli container. Exit bash with ctrl-d.

insidecontainer$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:~/.composer/vendor/bin

I actually missed this line where it is actually added to $PATH before the phpunit-polyfills package is installed.

ENV PATH="\${PATH}:~/.composer/vendor/bin"

@dawidurbanski
Copy link

Interesting. Now I checked, if I do (I have wp-env installed in the project):

$ pnpm wp-env run tests-cli bash

> wp-env "run" "tests-cli" "bash"

ℹ Starting 'bash' on the tests-cli container. Exit bash with ctrl-d.

focus@a1151878116a:/var/www/html$ phpunit --version
PHPUnit 9.6.12 by Sebastian Bergmann and contributors.

xxxxx@a1151878116a:/var/www/html$

Hmmmm....

@dawidurbanski
Copy link

npx wp-env run tests-cli bash
ℹ Starting 'bash' on the tests-cli container. Exit bash with ctrl-d.

xxxxx@a1151878116a:/var/www/html$ phpunit --version
PHPUnit 9.6.12 by Sebastian Bergmann and contributors.
$ npx wp-env run tests-cli phpunit --version
8.7.0

There's different version returned if I use it when running from the inside of the container, and different when using directly as a command in the wp-env.

Also:

$ npx wp-env run tests-cli bash
ℹ Starting 'bash' on the tests-cli container. Exit bash with ctrl-d.

xxxxx@a1151878116a:/var/www/html$ phpunit
PHPUnit 9.6.12 by Sebastian Bergmann and contributors.

Usage:
  phpunit [options] UnitTest.php
  phpunit [options] <directory>

Code Coverage Options:
  --coverage-clover <file>    Generate code coverage report in Clover XML format
  --coverage-cobertura <file> Generate code coverage report in Cobertura XML format
  ....
  ....

But:

$ npx wp-env run tests-cli phpunit
ℹ Starting 'phpunit' on the tests-cli container.

OCI runtime exec failed: exec failed: unable to start container process: exec: "phpunit": executable file not found in $PATH: unknown
✖ Command failed with exit code 126
Command failed with exit code 126
@jrtashjian
Copy link
Contributor

$ npx wp-env run tests-cli phpunit --version
8.7.0

There's different version returned if I use it when running from the inside of the container, and different when using directly as a command in the wp-env.

8.7.0 is actually the version of wp-env and in the latest version if you want to pass flags to the command inside the container you need to put a double-dash before the flags like this:

$ npx wp-env run tests-cli phpunit -- --version
@dawidurbanski
Copy link

This might be issue with the shell version. This works when using bash but fails with sh.

I assume running wp-env command tries to run this with default shell. See this:

$ npx wp-env run tests-cli sh
ℹ Starting 'sh' on the tests-cli container.

/var/www/html $ phpunit
sh: phpunit: not found
@jrtashjian
Copy link
Contributor

Possible culprit:

# Have the container sleep infinitely to keep it alive for us to run commands on it.
CMD [ "/bin/sh", "-c", "while true; do sleep 2073600; done" ]

@jrtashjian
Copy link
Contributor

PR up. It ended up being a difference in how docker exec compared to docker run. The exec command will run commands within an existing container but with /bin/sh as the environment unless it's changed at the time of running docker exec.

Putting /bin/bash -c before the command being executed in the wp-env script was the solution.

@jrtashjian
Copy link
Contributor

@JUVOJustin, while we're awaiting approval/merge for the solution addressing the unavailability of global PHPUnit in #54508, you can implement the interim solution outlined here: #42286 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Testing Needs further testing to be confirmed. [Package] Env /packages/env [Type] Bug An existing feature does not function as intended
7 participants