How to remove all tests in composer php?

后端 未结 3 2050
不思量自难忘°
不思量自难忘° 2021-02-19 23:57

I\'m new in composer development. I just start to work with composer in my current project. And I think my question is already asked before or I\'m noob about composer :D

<
相关标签:
3条回答
  • 2021-02-20 00:38

    Not directly, but if the package maintainers followed some best practises it is possible.

    Use the --prefer-dist argument for composer install and composer update, then composer will try to download the packages' distributable instead of its source. For packages on GitHub this means, it downloads a zip file instead of cloning the repository.

    It is possible that this still includes all the tests, but it is recommended to not include tests in the distributable. For packages on GitHub, tests are excluded if there is a .gitattributes file with content like:

    /tests              export-ignore
    /phpunit.xml        export-ignore
    

    Read more: I don't need your tests in my production (Reddit)

    0 讨论(0)
  • 2021-02-20 00:39

    Is there any command to remove those tests OR I need to remove it manually OR what...? :'(

    Thats an interesting question.

    At the moment, you as the package consumer can't ignore tests automatically. There is no Composer command to clean all the folders after the download of vendors. To solve the problem, clean up the vendor dir as part of your application build process. Its a delete run during bootstrap on a manually selected file set, then upload. This is a setup step, comparable to a cache warmup or inital database setup for production. Boring work :(

    The topic of removing the test folder (and other development stuff) from the vendor folder was requested and discussed before, see for example Composer Issues #1750 and #4438.

    A lot of users want this feature, but unfortunately Composer doesn't provide it, yet. I guess, the Composer maintainers would merge an exclude folders (reduce feature), if someone invests the time to solve the problem. Its hard work to establish a standard. Its also possible to create a Composer Plugin to provide this feature.


    How could such a feature look like?

    1. One way of solving this would be to provide a general blacklist-/whitelisting feature for files to keep for production in the composer.json file. Adding only an exclude section solves the problem only partly in my humble opinion, because you can't override decision made in packages.

      • First one would probably build a blacklist by iterating over all composer.json files generating a list of files and folders to delete.
      • Then one could use a whitelist from the main project to kick stuff from the blacklist (= whitelist stuff). This is to override exclude decisions made in fetched packages.
      • Finally, use the blacklist for the delete run in the vendor folder.
      • That means that the project pulling the vendor packages has full control. This approach provides great flexibility: if a package provider, blacklists a test folder, but the package consuming developer want to keep it, he can whitelist the folder of that package. But he can also do nothing, and go with the normal blacklists.

      Maybe one could also respect the export-ignore settings in the .gitattributes file of a package, when fetching the Source and not the Dist.

    2. Another way is to concentrate on the autoloading description.

      Composer provides require-dev and autoload-dev next to require and autoload. That means we have a clear separation between development and production classes. Think about the phpunit dependency and your tests folder, defined in require-dev and test namespace defined in autoload-dev.

      That makes it possible to use the autoloading map and remove all files which are not included in Composers "autoload scope" for production.

      David Grudl (@dg) used this approach in his Composer Cleaner.

      Its experimental. Do a backup.

    Regarding the usage of a .gitattributes file with export-ignore directive

    Yes, this is one way to reduce the size of git archives, but its never been adopted as a standard or best practice by the PHP commmunity.

    The Composer maintainers are promoting its usage (see the comments of alcohol and naderman), while for instance Symfony dropped its usage.

    There is no clear guidance for a best practice on this issue at the moment. So, i'm not sure that this is a best practice and we should really promote or suggest this.

    Its for "Dists", fetched with composer --prefer-dist.

    And even if some developers adopt this practice, a lot of ways to fetch the "Source" with Composer are not taken care of: hg, svn, git source.

    0 讨论(0)
  • 2021-02-20 00:40

    There is a plugin to do exactly that: composer cleanup. The author warns that the package is experimental, so be careful.

    0 讨论(0)
提交回复
热议问题