pestphp / pest

Pest is an elegant PHP testing Framework with a focus on simplicity, meticulously designed to bring back the joy of testing in PHP.
https://pestphp.com
MIT License
9.07k stars 315 forks source link

[Bug]: `--filter` option throws exception #1078

Closed piotrjoniec closed 5 months ago

piotrjoniec commented 5 months ago

What Happened

I get this exception thrown when using --filter, even in a fresh Laravel installation. I'm using Ubuntu on WSL2.

It doesn't happen if I pass a full regex such as /asdf/.

$ ./vendor/bin/pest --filter "asdf"

   INFO  No tests found.

   Pest\Exceptions\FatalException 

  preg_match(): Delimiter must not be alphanumeric, backslash, or NUL

  at vendor/pestphp/pest/overrides/Runner/Filter/NameFilterIterator.php:104
    100▕      * @throws Exception
    101▕      */
    102▕     private function setFilter(string $filter): void
    103▕     {
  ➜ 104▕         if (@preg_match($filter, '') === false) {
    105▕             // Handles:
    106▕             //  * testAssertEqualsSucceeds#4
    107▕             //  * testAssertEqualsSucceeds#4-8
    108▕             if (preg_match('/^(.*?)#(\d+)(?:-(\d+))?$/', $filter, $matches)) {

$ ./vendor/bin/pest --filter "/asdf/"

   INFO  No tests found.

How to Reproduce

Create a new Laravel project with Pest, then run:

./vendor/bin/pest --filter "asdf"

Sample Repository

No response

Pest Version

2.33.0

PHP Version

8.3.1

Operation System

Linux

Notes

No response

Rosendito commented 5 months ago

The same happens to me

PHP Version 8.2.14

Operation System Linux (windows using WSL2)

Using regex like /ExampleTest/ the error does not happen to me, as the author comments.

simon-tma commented 5 months ago

Using a regex delimiter other than / does throw an error in --watch mode:

at 09:10:19 ❯ ./vendor/bin/pest --filter '#asdf#' --watch

   INFO  No tests found.

   Pest\Exceptions\FatalException

  preg_match(): Unknown modifier 'c'

  at vendor/pestphp/pest/overrides/Runner/Filter/NameFilterIterator.php:104
    100▕      * @throws Exception
    101▕      */
    102▕     private function setFilter(string $filter): void
    103▕     {
  ➜ 104▕         if (@preg_match($filter, '') === false) {
    105▕             // Handles:
    106▕             //  * testAssertEqualsSucceeds#4
    107▕             //  * testAssertEqualsSucceeds#4-8
    108▕             if (preg_match('/^(.*?)#(\d+)(?:-(\d+))?$/', $filter, $matches)) {
nunomaduro commented 5 months ago

Fixed on Pest v2.33.2.