laravel / dusk

Laravel Dusk provides simple end-to-end testing and browser automation.
https://laravel.com/docs/dusk
MIT License
1.88k stars 322 forks source link

Limiting length of getCallerName return value to avoid issue when storing logs and screenshots #1070

Closed lk77 closed 11 months ago

lk77 commented 11 months ago

Hello,

I have some dusk tests that fails when there is console logs, because the filename is too long

ErrorException : file_put_contents(/var/www/html/tests/Browser/console/Tests_Browser_Pages_SomeFolder_MoreFolders_MyFolder_MyTestClass_testFunction-0.log): Failed to open stream: File name too long

it happens when the test class is in a deep subfolder, in my case i have 5 folders, and also the name of the test itself is pretty long,

i propose to limit the number of characters of the return value of the ProvidesBrowser::getCallerName function

/**
 * Get the browser caller name.
 *
 * @return string
 */
protected function getCallerName()
{
    $name = version_compare(Version::id(), '10', '>=')
        ? $this->name()
        : $this->getName(false); // @phpstan-ignore-line

    return str_replace('\\', '_', substr(get_class($this), 0, 70)).'_'.substr($name, 0, 70);
}

both the class name and the test name will be limited to 70 characters

i added a test, to verify that the test name is correctly truncated,

to test that the class name is correctly truncated would require the creation of another test class with a very long name,

i don't know if it's something you want to do.

ziadoz commented 9 months ago

@lk77 I'm working on a PR that adds data sets to filenames for tests using data providers. Do you have any idea what the maximum file length is? And what operating system this issue occurs on? I just want to make sure I'm not re-introducing this bug.

lk77 commented 9 months ago

@ziadoz it's 255, but for the whole path, that's why i limited the filename to 140 to be sure there is enough for the directory