From ac47058b2c84c0aed957ff859bd593d672f8e3f2 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 6 Feb 2022 07:46:07 +0100 Subject: [PATCH] Tests: add new `RemovePluginTest` Test that the plugin does not throw errors when the plugin is still in memory, but PHPCS and the plugin are uninstalled (Composer 1.x). These tests verify: - That the plugin exits with error code 0 when PHPCS and the plugin are uninstalled due to a no-dev install. - That the plugin exits with error code 0 when the standard which required the plugin is uninstalled. This test is about Composer and the plugin, so does not need to be tested against multiple PHPCS versions. The behaviour also shouldn't differ between a global vs local Composer install, so only testing one type. Note: the behaviour of Composer changed for this in Composer 2.0.0, but still not a bad thing to safeguard this for Composer 2.x as well, in case of regressions in Composer. This test class covers the following bug previously reported: * Dealerdirect/phpcodesniffer-composer-installer issue 103 * Dealerdirect/phpcodesniffer-composer-installer PR 104 --- tests/IntegrationTest/RemovePluginTest.php | 281 +++++++++++++++++++++ 1 file changed, 281 insertions(+) create mode 100644 tests/IntegrationTest/RemovePluginTest.php diff --git a/tests/IntegrationTest/RemovePluginTest.php b/tests/IntegrationTest/RemovePluginTest.php new file mode 100644 index 00000000..cff31670 --- /dev/null +++ b/tests/IntegrationTest/RemovePluginTest.php @@ -0,0 +1,281 @@ + 'phpcs-composer-installer/remove-phpcs-test', + 'require-dev' => array( + 'phpcs-composer-installer/multistandard' => '*', + ), + ); + + private $composerConfigRequire = array( + 'name' => 'phpcs-composer-installer/remove-phpcs-test', + 'require' => array( + 'phpcs-composer-installer/multistandard' => '*', + ), + ); + + /** + * Set up test environment before each test. + */ + protected function set_up() + { + $this->createTestEnvironment(); + } + + /** + * Clean up after each test. + */ + protected function tear_down() + { + $this->removeTestEnvironment(); + } + + /** + * Helper method to get the stdout expectation for when the plugin is uninstalled. + * + * As of Composer 2.0.0, plugins which have been uninstalled will not run anymore + * after the uninstall (which is good). + * It is unclear which particular change this can be attributed to. + * + * @return string + */ + private function getUninstallStdOutExpectation() + { + if (strpos(\COMPOSER_VERSION, '1') === 0) { + return Plugin::MESSAGE_PLUGIN_UNINSTALLED; + } + + return ''; + } + + /** + * Test the plugin doesn't exit with a non-0 exit code and doesn't throw errors when PHPCS and the plugin + * were installed via --dev and a no-dev install is run, which removes the plugin and PHPCS. + * + * @return void + */ + public function testRemovePHPCSViaNoDevGlobal() + { + $this->writeComposerJsonFile($this->composerConfigRequireDev, static::$tempGlobalPath); + + // Install dependencies and make sure the plugin runs. + $this->assertExecute( + 'composer global install -v --no-ansi', + 0, // Expected exit code. + Plugin::MESSAGE_RUNNING_INSTALLER, // Expected stdout. + null, // No stderr expectation. + 'Failed to install dependencies.' + ); + + /* + * Switch to a no-dev install, which should remove the plugin, but the + * plugin will still be loaded in memory, so we need to make sure it doesn't + * throw errors. + */ + $this->assertExecute( + 'composer global install --no-dev -v --no-ansi', + 0, // Expected exit code. + $this->getUninstallStdOutExpectation(), // Expected stdout. + null, // No stderr expectation. + 'Uninstall by switching to no-dev did not meet expectations.' + ); + } + + /** + * Test the plugin doesn't exit with a non-0 exit code and doesn't throw errors when PHPCS and the plugin + * were installed via --dev and a no-dev install is run, which removes the plugin and PHPCS. + * + * @return void + */ + public function testRemovePHPCSViaNoDevLocal() + { + $this->writeComposerJsonFile($this->composerConfigRequireDev, static::$tempLocalPath); + + // Install dependencies and make sure the plugin runs. + $this->assertExecute( + sprintf('composer install -v --no-ansi --working-dir=%s', escapeshellarg(static::$tempLocalPath)), + 0, // Expected exit code. + Plugin::MESSAGE_RUNNING_INSTALLER, // Expected stdout. + null, // No stderr expectation. + 'Failed to install dependencies.' + ); + + /* + * Switch to a no-dev install, which should remove the plugin, but the + * plugin will still be loaded in memory, so we need to make sure it exits with 0. + */ + $this->assertExecute( + sprintf('composer install --no-dev -v --no-ansi --working-dir=%s', escapeshellarg(static::$tempLocalPath)), + 0, // Expected exit code. + $this->getUninstallStdOutExpectation(), // Expected stdout. + null, // No stderr expectation. + 'Uninstall by switching to no-dev did not meet expectations.' + ); + } + + /** + * Test the plugin doesn't exit with a non-0 exit code and doesn't throw errors when removing PHPCS + * and the plugin when installed via require-dev by removing the dependency which installed it. + * + * @return void + */ + public function testRemovePHPCSViaDevUninstallGlobal() + { + $this->writeComposerJsonFile($this->composerConfigRequireDev, static::$tempGlobalPath); + + // Install dependencies and make sure the plugin runs. + $this->assertExecute( + 'composer global install -v --no-ansi', + 0, // Expected exit code. + Plugin::MESSAGE_RUNNING_INSTALLER, // Expected stdout. + null, // No stderr expectation. + 'Failed to install dependencies.' + ); + + /* + * Remove the dev dependency which caused the install of the plugin. + * This should also remove the plugin, but the plugin will still be loaded in memory, + * so we need to make sure it exits with 0. + */ + $this->assertExecute( + 'composer global remove --dev phpcs-composer-installer/multistandard -v --no-ansi', + 0, // Expected exit code. + $this->getUninstallStdOutExpectation(), // Expected stdout. + null, // No stderr expectation. + 'Uninstall of dev dependency did not meet expectations.' + ); + } + + /** + * Test the plugin doesn't exit with a non-0 exit code and doesn't throw errors when removing PHPCS + * and the plugin when installed via require-dev by removing the dependency which installed it. + * + * @return void + */ + public function testRemovePHPCSViaDevUninstallLocal() + { + $this->writeComposerJsonFile($this->composerConfigRequireDev, static::$tempLocalPath); + + // Install dependencies and make sure the plugin runs. + $this->assertExecute( + sprintf('composer install -v --no-ansi --working-dir=%s', escapeshellarg(static::$tempLocalPath)), + 0, // Expected exit code. + Plugin::MESSAGE_RUNNING_INSTALLER, // Expected stdout. + null, // No stderr expectation. + 'Failed to install dependencies.' + ); + + /* + * Remove the dev dependency which caused the install of the plugin. + * This should also remove the plugin, but the plugin will still be loaded in memory, + * so we need to make sure it exits with 0. + */ + $command = sprintf( + 'composer remove --dev phpcs-composer-installer/multistandard -v --no-ansi --working-dir=%s', + escapeshellarg(static::$tempLocalPath) + ); + $this->assertExecute( + $command, + 0, // Expected exit code. + $this->getUninstallStdOutExpectation(), // Expected stdout. + null, // No stderr expectation. + 'Uninstall of dev dependency did not meet expectations.' + ); + } + + /** + * Test the plugin doesn't exit with a non-0 exit code and doesn't throw errors when removing PHPCS + * and the plugin when installed via require by removing the dependency which installed it. + * + * @return void + */ + public function testRemovePHPCSViaNoDevUninstallGlobal() + { + $this->writeComposerJsonFile($this->composerConfigRequire, static::$tempGlobalPath); + + // Install dependencies and make sure the plugin runs. + $this->assertExecute( + 'composer global install -v --no-ansi', + 0, // Expected exit code. + Plugin::MESSAGE_RUNNING_INSTALLER, // Expected stdout. + null, // No stderr expectation. + 'Failed to install dependencies.' + ); + + /* + * Remove the dev dependency which caused the install of the plugin. + * This should also remove the plugin, but the plugin will still be loaded in memory, + * so we need to make sure it exits with 0. + */ + $this->assertExecute( + 'composer global remove phpcs-composer-installer/multistandard -v --no-ansi', + 0, // Expected exit code. + $this->getUninstallStdOutExpectation(), // Expected stdout. + null, // No stderr expectation. + 'Uninstall of no-dev dependency did not meet expectations.' + ); + } + + /** + * Test the plugin doesn't exit with a non-0 exit code and doesn't throw errors when removing PHPCS + * and the plugin when installed via require by removing the dependency which installed it. + * + * @return void + */ + public function testRemovePHPCSViaNoDevUninstallLocal() + { + $this->writeComposerJsonFile($this->composerConfigRequire, static::$tempLocalPath); + + // Install dependencies and make sure the plugin runs. + $this->assertExecute( + sprintf('composer install -v --no-ansi --working-dir=%s', escapeshellarg(static::$tempLocalPath)), + 0, // Expected exit code. + Plugin::MESSAGE_RUNNING_INSTALLER, // Expected stdout. + null, // No stderr expectation. + 'Failed to install dependencies.' + ); + + /* + * Remove the dev dependency which caused the install of the plugin. + * This should also remove the plugin, but the plugin will still be loaded in memory, + * so we need to make sure it exits with 0. + */ + $command = sprintf( + 'composer remove phpcs-composer-installer/multistandard -v --no-ansi --working-dir=%s', + escapeshellarg(static::$tempLocalPath) + ); + $this->assertExecute( + $command, + 0, // Expected exit code. + $this->getUninstallStdOutExpectation(), // Expected stdout. + null, // No stderr expectation. + 'Uninstall of no-dev dependency did not meet expectations.' + ); + } +}