From 80d3aec001d1d9f3f7d75ccb6ad595685901ac5a Mon Sep 17 00:00:00 2001 From: Sean Fisher Date: Tue, 14 Nov 2023 11:15:48 -0500 Subject: [PATCH] Add a shutdown handler to the installation script to prevent silent fatals (#464) * Adding a shutdown function handler to catch errors and display them * Only load the shutdown handler during installation * Docs --- src/mantle/testing/class-utils.php | 24 +++++++++++++++++++ .../concerns/trait-rsync-installation.php | 4 ++++ src/mantle/testing/wordpress-bootstrap.php | 5 ++++ 3 files changed, 33 insertions(+) diff --git a/src/mantle/testing/class-utils.php b/src/mantle/testing/class-utils.php index 8e82d5e6a..9d1a99775 100644 --- a/src/mantle/testing/class-utils.php +++ b/src/mantle/testing/class-utils.php @@ -472,8 +472,32 @@ public static function ensure_composer_loaded() { foreach ( $paths as $path ) { if ( ! is_dir( $path ) && file_exists( $path ) ) { require_once $path; + return; } } } + + /** + * Register a shutdown function to handle errors. + * + * Used during the WordPress installation process to catch silent errors. + */ + public static function register_shutdown_function(): void { + register_shutdown_function( [ static::class, 'handle_shutdown' ] ); + } + + /** + * Handle a shutdown error and display it. + */ + public static function handle_shutdown(): void { + $error = error_get_last(); + + if ( ! $error ) { + return; + } + + static::error( '🚨 Error during test run:', 'Shutdown' ); + static::code( $error ); + } } diff --git a/src/mantle/testing/concerns/trait-rsync-installation.php b/src/mantle/testing/concerns/trait-rsync-installation.php index 7f3c2bea3..dc1b2a9ec 100644 --- a/src/mantle/testing/concerns/trait-rsync-installation.php +++ b/src/mantle/testing/concerns/trait-rsync-installation.php @@ -141,6 +141,10 @@ public function maybe_rsync( string $to = null, string $from = null ): static { * rsync the codebase from the wp-content level to the root of the WordPress * installation. Also will attempt to locate the wp-content directory relative * to the current directory. + * + * This isn't a perfect function and can sometimes fail to locate the proper + * `wp-content` directory. If it does fail to work, manually call + * `maybe_rsync()` yourself with the proper paths. */ public function maybe_rsync_wp_content(): static { // Attempt to locate wp-content relative to the current directory. diff --git a/src/mantle/testing/wordpress-bootstrap.php b/src/mantle/testing/wordpress-bootstrap.php index 34932796f..2d3b751d6 100644 --- a/src/mantle/testing/wordpress-bootstrap.php +++ b/src/mantle/testing/wordpress-bootstrap.php @@ -158,6 +158,11 @@ } } +// Ensure that the shutdown function is registered when installing WordPress. +if ( $installing_wp ) { + Utils::register_shutdown_function(); +} + if ( $multisite && ! $installing_wp ) { Utils::info( 'Running as multisite...' ); defined( 'MULTISITE' ) or define( 'MULTISITE', true );