silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
719 stars 820 forks source link

Add support for paratest #11148

Open beerbohmdo opened 4 months ago

beerbohmdo commented 4 months ago

Description

I want to use paratest, but I had sometimes weird issues because of the shared cache folder.

Paratest provides a TEST_TOKEN environment variable.

I just tried to append this to the TEMP_FOLDER and SS_DATABASE_PREFIX and all my issues went away. I currently hardcoded it to TempFolder and TempDatabase class, because I am not really sure how I seed them dynamically.

As the TEMP_FOLDER is defined really early in the code and loaded by composer autoloader its hard to put custom code before that ...

Any suggestion for a better way to handle this are welcome.

Additional context or points of discussion

My hacky and dirty way:

--- /tmp/silverstripe-framework/src/ORM/Connect/TempDatabase.php    2024-02-16 12:27:07.158611326 +0100
+++ src/ORM/Connect/TempDatabase.php    2024-02-16 11:29:26.590707137 +0100
@@ -57,6 +57,11 @@
     protected function isDBTemp($name)
     {
         $prefix = Environment::getEnv('SS_DATABASE_PREFIX') ?: 'ss_';
+
+        if (($token = getenv('TEST_TOKEN')) !== false) {
+            $prefix .= $token . '_';
+        }
+
         $result = preg_match(
             sprintf('/^%stmpdb_[0-9]+_[0-9]+$/i', preg_quote($prefix ?? '', '/')),
             $name ?? ''
@@ -198,6 +203,11 @@
         // Create a temporary database, and force the connection to use UTC for time
         $dbConn = $this->getConn();
         $prefix = Environment::getEnv('SS_DATABASE_PREFIX') ?: 'ss_';
+
+        if (($token = getenv('TEST_TOKEN')) !== false) {
+            $prefix .= $token . '_';
+        }
+
         do {
             $dbname = strtolower(sprintf('%stmpdb_%s_%s', $prefix, time(), rand(1000000, 9999999)));
         } while ($dbConn->databaseExists($dbname));
--- /tmp/silverstripe-framework/src/Core/TempFolder.php 2024-02-16 12:27:07.142611530 +0100
+++ src/Core/TempFolder.php 2024-02-16 11:22:52.855885901 +0100
@@ -54,6 +54,9 @@
             $user = 'unknown';
         }
         $user = preg_replace('/[^A-Za-z0-9_\-]/', '', $user ?? '');
+        if (($token = getenv('TEST_TOKEN')) !== false) {
+            $user .= '-' . $token;
+        }
         return $user;
     }

Validations

emteknetnz commented 4 months ago

Related https://github.com/silverstripeltd/product-issues/issues/689 (private repo)

beerbohmdo commented 4 months ago

More directories which must be seeded :see_no_evil:

if ($token = getenv('TEST_TOKEN')) {
    Requirements::backend()->setCombinedFilesFolder('_combinedfiles' . $token);
    Config::modify()->set(TinyMCECombinedGenerator::class, 'filename_base', '_tinymce' . $token . '/tinymce-{name}-{hash}.js');
}

But this can be set inside a _config.php