From cb0eced130ee0f35c0cd2c9b40b67bed586ddfd3 Mon Sep 17 00:00:00 2001 From: Peter Downey Date: Wed, 25 Nov 2026 19:30:41 -0500 Subject: [PATCH] Measure coverage of the main plugin file's top-level code The test bootstrap loads the main plugin file before coverage starts, so its constants, class loading, and hook registrations counted as missed. New tests load the file in a separate process, where the bootstrap skips it (OAM_TEST_DEFER_PLUGIN_LOAD, set by the test's class setup): one checks the constants and hooks, one that a load during uninstall does not register the uninstall hook again. Line coverage rises from about 90% to 97%, so the CI floor rises from 85% to 94%. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/wp-compatibility-test.yml | 2 +- tests/bootstrap.php | 6 +- tests/unit/PluginFileLoadTest.php | 75 +++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 tests/unit/PluginFileLoadTest.php diff --git a/.github/workflows/wp-compatibility-test.yml b/.github/workflows/wp-compatibility-test.yml index 5d56454..9da5e37 100644 --- a/.github/workflows/wp-compatibility-test.yml +++ b/.github/workflows/wp-compatibility-test.yml @@ -1463,7 +1463,7 @@ jobs: env: # The unit suite must cover at least this percentage of the production lines. It is set a few # points below the measured value, so it stops a real drop without failing on small changes. - MINIMUM_LINE_COVERAGE: '85' + MINIMUM_LINE_COVERAGE: '94' steps: - name: Checkout code diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 672fcf8..7609297 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -612,7 +612,11 @@ function plugins_url( $path = '', $plugin = '' ): string { oam_test_reset_state(); -require_once dirname( __DIR__ ) . '/optimizations-ace-mc.php'; +// PluginFileLoadTest loads the plugin file itself, in a separate process, so that coverage records +// the file's top-level code. Its class setup sets this variable, which that process inherits. +if ( false === getenv( 'OAM_TEST_DEFER_PLUGIN_LOAD' ) ) { + require_once dirname( __DIR__ ) . '/optimizations-ace-mc.php'; +} // What loading the plugin file registered, kept apart from the per-test state. $GLOBALS['oam_test_load'] = array( diff --git a/tests/unit/PluginFileLoadTest.php b/tests/unit/PluginFileLoadTest.php new file mode 100644 index 0000000..62338ab --- /dev/null +++ b/tests/unit/PluginFileLoadTest.php @@ -0,0 +1,75 @@ +assertFalse( defined( 'OPTIMIZATIONS_ACE_MC_PLUGIN_FILE' ), 'The bootstrap loaded the plugin file in this process.' ); + + $file = dirname( __DIR__, 2 ) . '/optimizations-ace-mc.php'; + require $file; + + return $file; + } + + #[RunInSeparateProcess] + #[PreserveGlobalState( false )] + public function test_loading_the_plugin_file_defines_its_constants_and_registers_its_hooks(): void { + $file = $this->load_plugin_file(); + + $this->assertSame( realpath( $file ), realpath( OPTIMIZATIONS_ACE_MC_PLUGIN_FILE ) ); + $this->assertMatchesRegularExpression( '/^\d+\.\d+\.\d+$/', OPTIMIZATIONS_ACE_MC_VERSION ); + $this->assertSame( 10, has_action( 'plugins_loaded', 'optimizations_ace_mc_init' ) ); + $this->assertCount( 1, $GLOBALS['oam_test']['uninstall_hooks'] ); + $this->assertSame( array( 'Optimizations_Ace_Mc_Settings', 'uninstall' ), $GLOBALS['oam_test']['uninstall_hooks'][0]['callback'] ); + $this->assertInstanceOf( Optimizations_Ace_Mc::class, optimizations_ace_mc() ); + } + + #[RunInSeparateProcess] + #[PreserveGlobalState( false )] + public function test_loading_the_plugin_file_during_uninstall_does_not_register_the_uninstall_hook_again(): void { + $GLOBALS['oam_test']['did_actions']['pre_uninstall_plugin'] = 1; + + $this->load_plugin_file(); + + $this->assertSame( array(), $GLOBALS['oam_test']['uninstall_hooks'] ); + $this->assertSame( 10, has_action( 'plugins_loaded', 'optimizations_ace_mc_init' ) ); + } +}