From d4f64b822a76e1aac325f18ac160b9daac01ff49 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sat, 5 Feb 2022 16:32:19 +0100 Subject: [PATCH] TestCase: add `willPluginOutputShow()` method In very select circumstances (PHP 5.5 with Composer 1.x on Windows) and sometimes even only with select PHPCS versions, the plugin output will not be shown in the transcript from Composer, even though the plugin _does_ actually run. Instead of skipping those tests completely, I'm suggestion to selectively skip the output expectation for the `composer install/update` run instead. That way we can still verify that the plugin _has_ actually done its job by checking that paths have been registered with PHPCS. I would recommend to only implement the use of this method for those select tests where we have actually seen failures due to this Composer bug and to just leave the other tests alone. --- tests/IntegrationTest/BaseLineTest.php | 30 ++++---------------------- tests/TestCase.php | 19 ++++++++++++++++ 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/tests/IntegrationTest/BaseLineTest.php b/tests/IntegrationTest/BaseLineTest.php index a87ee410..83356543 100644 --- a/tests/IntegrationTest/BaseLineTest.php +++ b/tests/IntegrationTest/BaseLineTest.php @@ -64,18 +64,6 @@ protected function tear_down() */ public function testBaseLineGlobal($phpcsVersion, $expectedStnds) { - if ( - $phpcsVersion === PHPCSVersions::MASTER - && \CLI_PHP_MINOR === '5.5' - && $this->onWindows() === true - && substr(\COMPOSER_VERSION, 0, 1) === '1' - ) { - $this->markTestSkipped( - 'Composer 1.x on Windows with PHP 5.5 does run the plugin when there are no external standards,' - . ' but doesn\'t consistently show this in the logs' - ); - } - $config = $this->composerConfig; $config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion; @@ -83,10 +71,11 @@ public function testBaseLineGlobal($phpcsVersion, $expectedStnds) $this->assertComposerValidates(static::$tempGlobalPath); // Make sure the plugin runs. + $expectedStdOut = $this->willPluginOutputShow() ? 'Running PHPCodeSniffer Composer Installer' : null; $this->assertExecute( 'composer global install -v --no-ansi', 0, // Expected exit code. - 'Running PHPCodeSniffer Composer Installer', // Expected stdout. + $expectedStdOut, // Expected stdout. null, // No stderr expectation. 'Failed to install dependencies.' ); @@ -119,18 +108,6 @@ public function testBaseLineGlobal($phpcsVersion, $expectedStnds) */ public function testBaseLineLocal($phpcsVersion, $expectedStnds) { - if ( - $phpcsVersion === PHPCSVersions::MASTER - && \CLI_PHP_MINOR === '5.5' - && $this->onWindows() === true - && substr(\COMPOSER_VERSION, 0, 1) === '1' - ) { - $this->markTestSkipped( - 'Composer 1.x on Windows with PHP 5.5 does run the plugin when there are no external standards,' - . ' but doesn\'t consistently show this in the logs' - ); - } - $config = $this->composerConfig; $config['require-dev']['squizlabs/php_codesniffer'] = $phpcsVersion; @@ -138,10 +115,11 @@ public function testBaseLineLocal($phpcsVersion, $expectedStnds) $this->assertComposerValidates(static::$tempLocalPath); // Make sure the plugin runs. + $expectedStdOut = $this->willPluginOutputShow() ? 'Running PHPCodeSniffer Composer Installer' : null; $this->assertExecute( sprintf('composer install -v --no-ansi --working-dir=%s', escapeshellarg(static::$tempLocalPath)), 0, // Expected exit code. - 'Running PHPCodeSniffer Composer Installer', // Expected stdout. + $expectedStdOut, // Expected stdout. null, // No stderr expectation. 'Failed to install dependencies.' ); diff --git a/tests/TestCase.php b/tests/TestCase.php index c0f31d57..2375b1c0 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -208,6 +208,25 @@ protected static function onWindows() return strpos(strtoupper(\PHP_OS), 'WIN') === 0; } + /** + * Determine whether output expectations can be set for a typical Composer `install`/`update` run. + * + * Composer 1.x on Windows with PHP 5.5 DOES run the plugin, but doesn't consistently show this in the logs. + * This method can be used to still test output expectations in _most_ cases, without failing the tests + * in the rare case they won't show. + * + * It is recommended to only add a call to this method to a test when it has been proven + * to fail without it. + * + * @return bool + */ + protected function willPluginOutputShow() + { + return ((\CLI_PHP_MINOR === '5.5' + && $this->onWindows() === true + && substr(\COMPOSER_VERSION, 0, 1) === '1') === false); + } + /** * Create a composer.json file based on a given configuration. *