Skip to content

Commit

Permalink
TestCase: add willPluginOutputShow() method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrfnl committed Mar 5, 2022
1 parent f8d256f commit d4f64b8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
30 changes: 4 additions & 26 deletions tests/IntegrationTest/BaseLineTest.php
Expand Up @@ -64,29 +64,18 @@ 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;

$this->writeComposerJsonFile($config, static::$tempGlobalPath);
$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.'
);
Expand Down Expand Up @@ -119,29 +108,18 @@ 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;

$this->writeComposerJsonFile($config, static::$tempLocalPath);
$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.'
);
Expand Down
19 changes: 19 additions & 0 deletions tests/TestCase.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit d4f64b8

Please sign in to comment.