pest icon indicating copy to clipboard operation
pest copied to clipboard

`./lib/vendor/bin/pest --init` fails if vendor dir is not in project root

Open abernh opened this issue 2 years ago • 2 comments

The bug

If the vendor directory is not placed on the root level, but e.g. in a sub directory like ./lib, then running ./lib/vendor/bin/pest --init is failing with

  Pest\Exceptions\FileOrFolderNotFound 

  The file or folder with the name `composer.json` not found.

Expected

Pest should properly run independent of the directory structure.

Setup

  • PHP: 7.4.9
  • Pest 1.21

Steps

  • add special sub directory in composer.json: "config": {"vendor-dir": "lib/vendor"}
  • require phppest via composer
  • try to init pest: ./lib/vendor/bin/pest --init --> fails

Notes

This seems to happen because bin/pest expects to be located 2 levels above the root directory https://github.com/pestphp/pest/blob/ffb20c9956ed5bc3b6677e202c107897178ab3b1/bin/pest#L29

When I adjust the hardcoded 2 levels to be in my case 3 levels then the file error is gone but a new error is shown:

Unknown option "--init"

because pest-plugin/src/Loader.php makes now the same wrong assumption that the vendor directory is in root

https://github.com/pestphp/pest-plugin/blob/c782378be8c5bc4ba1a44c30876534e74ea28be0/src/Loader.php#L62

which causes it to find no plugins, never run the needed 'pest/src/Plugins/Init' plugin and therefore allowing the --init parameter to be passed on to phpunit.

Possible fix

When looking into PhpUnits CLI code it seems they are "fishing" a little for the correct "depth" where to find the autoload.php and once that is found they move on.

https://github.com/sebastianbergmann/phpunit/blob/3e2809bb4ac5edd16150d41b063e48fd27fecf9a/phpunit#L66

Something similar could be done here as well. Instead of trying only 2 levels deep the levels could be tested until maybe a max of 5 levels deep (most likely it would not exceed 3 levels but who knows).

And pest-plugin will have to adapt the pest CLI approach for finding pest-plugins.json: instead of using cwd() and guessing its way up it can use dirname(__DIR__, 5) and rely on being 5 levels above the vendor dir when running Loader::getPluginInstances() But that issue will have to be filed in the other repo.

abernh avatar Apr 25 '22 22:04 abernh

Another possible message is this:

   Pest\Exceptions\FileOrFolderNotFound 

  The file or folder with the name `phpunit.xml` not found.

rinodrummer avatar May 02 '22 13:05 rinodrummer

Same error of Unknown option "--init" also occurs when pestphp/pest-plugin is not allowed upon installation of pest using composer

When running composer require pestphp/pest --dev --with-all-dependencies it should ask you to allow pestphp/pest-plugin:

> Do you trust "pestphp/pest-plugin" to execute code and wish to enable it now? (writes "allow-plugins" to composer.json) [y,n,d,?]

After running composer require pestphp/pest --dev --with-all-dependencies for the second time, I entered y to allow it. Then running ./vendor/bin/pest --init works 🚀 . Also you should run it in the root directory where your vendor folder and tests folder lives.

lnfel avatar Jul 20 '22 07:07 lnfel

Closing due of lack of activity.

nunomaduro avatar Nov 09 '22 20:11 nunomaduro