code-distortion / adapt

A Laravel package that builds databases for your tests, improving their speed
MIT License
24 stars 3 forks source link

CodeDistortion\Adapt\Exceptions\AdaptConfigException : The .env.testing file could not be loaded #4

Closed carltondickson closed 1 year ago

carltondickson commented 1 year ago

Description:

It's hard to recreate reliably but it happens a lot. It feels like the first time I run a test CLASS when I haven't run one for a few minutes in PHPSTORM it fails with this error, subsequent runs are fine if I run them back to back. Same can happen with test methods, run a test class, switch to another class and run a specific test and it can fail in the same way...running it immediately afterwards will always pass.

CodeDistortion\Adapt\Exceptions\AdaptConfigException : The .env.testing file could not be loaded
 /opt/project/vendor/code-distortion/adapt/src/Exceptions/AdaptConfigException.php:20
 /opt/project/vendor/code-distortion/adapt/src/Boot/BootTestLaravel.php:68
 /opt/project/vendor/code-distortion/adapt/src/Boot/BootTestLaravel.php:52
 /opt/project/vendor/code-distortion/adapt/src/Boot/BootTestAbstract.php:175
 /opt/project/vendor/code-distortion/adapt/src/PreBoot/PreBootTestLaravel.php:114
 /opt/project/vendor/code-distortion/adapt/src/Initialise/InitialiseAdapt.php:194
 /opt/project/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:94

Logs:

The failing case

2022-12-30 17:11:57] testing.DEBUG: ADAPT: Disconnecting established database connection "testing"  
[2022-12-30 17:11:57] testing.ERROR: ADAPT: ┌── An Exception Occurred - AdaptConfigException ──────────────────────────────────────────────┐  
[2022-12-30 17:11:57] testing.ERROR: ADAPT: │ The .env.testing file could not be loaded                                                    │  
[2022-12-30 17:11:57] testing.ERROR: ADAPT: │ /opt/project/vendor/code-distortion/adapt/src/Exceptions/AdaptConfigException.php on line 20 │  
[2022-12-30 17:11:57] testing.ERROR: ADAPT: └──────────────────────────────────────────────────────────────────────────────────────────────┘

Running a second time is fine

[2022-12-30 17:13:00] testing.DEBUG: ADAPT: Disconnecting established database connection "testing"  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: Changed the default connection to: "testing-mysql"  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: Looking for stale things to remove  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: Generated a build-source checksum based on file modified timestamps (6ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: Retrieved database list (connection: "testing-mysql", driver: "mysql") (2ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: - Found database "our-project" (not usable - ignoring) (1ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: - Found database "our-project_integration_2877d4_25f5c604d394" (stale - within grace period) (1ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: - Found database "our-project_integration_2877d4_26890819d052" (stale - within grace period) (1ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: - Found database "our-project_integration_2877d4_88a76f25ae61" (stale - within grace period) (1ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: - Found database "our-project_integration_53cfcf_26890819d052" (usable) (1ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: - Found database "our-project_integration_7a1beb_26890819d052" (stale - within grace period) (1ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: - Found database "our-project_integration_ddfa27_26890819d052" (stale - within grace period) (1ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: Retrieved snapshot list (0ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: - Found snapshot "/opt/project/database/adapt-test-storage/snapshots/snapshot.our-project-integration.2877d4-0c919462cf02.mysql" (stale - within grace period) (0ms)  
[2022-12-30 17:13:00] testing.DEBUG: ADAPT: Nothing found to remove - total time taken (17ms)

Other Relevant Details:

code-distortion commented 1 year ago

Hi @carltondickson. I don't get many issues and didn't notice. I do apologise for not getting back to you sooner!

Thanks for the detailed description of the problem. This is an interesting issue, and I have an idea about what might be causing it.

Adapt performs some checks before building the database. When it throws the "The .env.testing file could not be loaded" exception, it failed when checking to see that .env.testing exists, and is a file.

I'm not sure at the moment about the intermittency you get, but I can trigger the same error when I run phpunit with a command line like yours.

When running phpunit whilst in the project root-directory, my tests run fine. e.g.

cd /var/www/html/ && ./vendor/bin/phpunit

But when running it from outside the project root-dir, I get the exception. e.g.

cd / && php /var/www/html/vendor/bin/phpunit --configuration /var/www/html/phpunit.xml

For you, the equivalent of running it from the project root-dir would be:

cd /opt/project/ && php vendor/phpunit/phpunit/phpunit --configuration phpunit.xml --filter "/(OurProject\\Tests\\Integration\\TestClass::testFunction)( .*)?$/" --test-suffix TestClass.php tests/integration --teamcity

I'll look into this further and get back to you. Ultimately Adapt should use the project root-dir when looking for the .env.testing file.


Here are some other notes I wrote before working out a way to trigger the error:

Yes, Laravel's Dusk testing process does do some file copying, but it doesn't touch .env.testing. So I don't think that's playing a role here.

When running php artisan dusk, Laravel backs up your .env file to .env.backup and copies .env.dusk over .env (it swaps it back afterwards). It does this so that when Laravel answers the browser's request, it runs using your intended dusk settings.

Adapt doesn't do any file copying like this. As an aside, Adapt approaches this problem differently which makes this copying unnecessary. When using Adapt, I'd recommend adding your Dusk tests as a test-suite in phpunit.xml so it runs alongside your other tests.

code-distortion commented 1 year ago

Hi @carltondickson. I've released a new version to address this.

It should now handle the situation when you run the tests from a directory other than the base project directory.

In this situation, you will still need to specify the --configuration path (so Laravel picks up the phpunit.xml settings), but you were doing this anyway.

php /opt/project/vendor/phpunit/phpunit/phpunit --configuration /opt/project/phpunit.xml --filter "/(OurProject\\Tests\\Integration\\TestClass::testFunction)( .*)?$/" --test-suffix TestClass.php /opt/project/tests/integration --teamcity

I was able to reproduce the problem, however I'm a bit unsure about the intermittency you were seeing - where it would sometimes work and sometimes not. That makes me thing something else is going on as well.

If you're able to, would you mind updating to the latest version, and seeing how it goes?

carltondickson commented 1 year ago

Thanks, will give it a try and if I notice it again I'll raise an issue with specific steps