From 95babfeb7bedbd611a590187c893e191fd784f7e Mon Sep 17 00:00:00 2001 From: Luca Tumedei Date: Mon, 14 Oct 2024 08:57:28 +0200 Subject: [PATCH] refactor(LoadSandbox) move debug information to exception Since the output of `codecept_debug` will only show if the user is running the Codeception command using the `--debug` output, I've moved the information that will help the user debug the issue to the exception to make sure it will always print. I've changed the wording a bit to be more general and cover other commands (e.g. the `console` one) that will use the LoadSandbox class and might fail like the `run` command does. Fix test. --- src/WordPress/InstallationException.php | 5 ++-- src/WordPress/LoadSandbox.php | 8 ++--- .../WPBrowser/WordPress/LoadSandboxTest.php | 30 +++++++++++-------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/WordPress/InstallationException.php b/src/WordPress/InstallationException.php index 0b7a85a21..4a713b8cc 100644 --- a/src/WordPress/InstallationException.php +++ b/src/WordPress/InstallationException.php @@ -76,8 +76,9 @@ public static function becauseWordPressMultsiteIsNotInstalled(bool $isSubdomainI public static function becauseCodeceptionCommandDidNotFinish(): self { return new self( - "Codeception `run` command did not finish properly; WordPress exited early during sandbox installation. " - ."A plugin, theme, or WP-CLI package may have exited before the wp_loaded action could run.", + "The current Codeception command did not finish properly. WordPress exited early while loading. " + ."A plugin, theme, or WP-CLI package may have exited before the wp_loaded action could be fired. " . + "If there is error output above, it may provide clues.", self::COMMAND_DID_NOT_FINISH_PROPERLY ); } diff --git a/src/WordPress/LoadSandbox.php b/src/WordPress/LoadSandbox.php index c41f161cf..92fcbc5a1 100644 --- a/src/WordPress/LoadSandbox.php +++ b/src/WordPress/LoadSandbox.php @@ -124,12 +124,8 @@ class_exists(InstallationException::class); } if ($bodyContent === 'COMMAND DID NOT FINISH PROPERLY.') { - // we got here from \Codeception\Subscriber\ErrorHandler::shutdownHandler() - codecept_debug('Codeception error: ' .$bodyContent); - codecept_debug( - 'DEBUG: Something caused WordPress to exit early. If there is error output above, it may provide clues. ' - .'(For instance, a WP-CLI package mistakenly attempting to handle the `codecept run` command.)' - ); + // We got here from \Codeception\Subscriber\ErrorHandler::shutdownHandler(). + // We'll try to provide some clues to the user by adding some debug output. throw InstallationException::becauseCodeceptionCommandDidNotFinish(); } diff --git a/tests/unit/lucatume/WPBrowser/WordPress/LoadSandboxTest.php b/tests/unit/lucatume/WPBrowser/WordPress/LoadSandboxTest.php index 596c78392..fb44690b9 100644 --- a/tests/unit/lucatume/WPBrowser/WordPress/LoadSandboxTest.php +++ b/tests/unit/lucatume/WPBrowser/WordPress/LoadSandboxTest.php @@ -3,11 +3,9 @@ namespace Unit\lucatume\WPBrowser\WordPress; -use Codeception\Subscriber\ErrorHandler; use Codeception\Test\Unit; use Exception; -use lucatume\WPBrowser\Process\ProcessException; -use lucatume\WPBrowser\Process\WorkerException; +use lucatume\WPBrowser\Tests\Traits\Fork; use lucatume\WPBrowser\Tests\Traits\LoopIsolation; use lucatume\WPBrowser\Tests\Traits\TmpFilesCleanup; use lucatume\WPBrowser\Traits\UopzFunctions; @@ -363,25 +361,31 @@ public function should_handle_codeception_command_not_finished_error(): void { * @see \Codeception\Subscriber\ErrorHandler::shutdownHandler() */ add_action('after_setup_theme', function () { - // Output and exit from \Codeception\Subscriber\ErrorHandler::shutdownHandler + // Output and exit from \Codeception\Subscriber\ErrorHandler::shutdownHandler. echo "\n\n\nCOMMAND DID NOT FINISH PROPERLY.\n"; exit(125); }); PHP; - if (!file_exists($installation->getMuPluginsDir())) { - mkdir($installation->getMuPluginsDir()); + $muPluginsDir = $installation->getMuPluginsDir(); + if ( + !is_dir($muPluginsDir) + && !( + mkdir($muPluginsDir, 0755, true) + && is_dir($muPluginsDir) + ) + ) { + throw new \RuntimeException('Could not create mu-plugins directory.'); + } + if(!file_put_contents($muPluginsDir . '/exiting-mu-plugin.php', $exitingPluginCode)){ + throw new \RuntimeException('Could not write exiting-mu-plugin.php.'); } - file_put_contents($installation->getMuPluginsDir() . '/exiting-mu-plugin.php', $exitingPluginCode); - - $this->preventExit(); $this->expectException(InstallationException::class); - $this->expectExceptionMessageMatches('WordPress exited early during sandbox installation'); + $this->expectExceptionMessage(InstallationException::becauseCodeceptionCommandDidNotFinish()->getMessage()); - $loadSandbox = new LoadSandbox($wpRootDir, 'wordpress.test'); - - $this->assertInIsolation(static function () use ($loadSandbox) { + $this->assertInIsolation(static function () use ($wpRootDir) { + $loadSandbox = new LoadSandbox($wpRootDir, 'wordpress.test'); $loadSandbox->load(); }); }