From e43ab3074702714ad10c349a018a3c9e4a003204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= <5175937+theofidry@users.noreply.github.com> Date: Thu, 14 Jul 2022 11:45:33 +0200 Subject: [PATCH] Introduce config deprecations (#123) --- e2e/scenario11/.gitignore | 6 +++ e2e/scenario11/README.md | 1 + e2e/scenario11/composer.json | 21 +++++++++++ e2e/scenario11/expected.txt | 19 ++++++++++ e2e/scenario11/script.sh | 28 ++++++++++++++ e2e/scenario11/vendor-bin/ns1/composer.json | 1 + e2e/scenario3/composer.json | 1 + e2e/scenario3/expected.txt | 2 - e2e/scenario8/composer.json | 1 + e2e/scenario8/expected.txt | 1 - src/Config.php | 41 +++++++++++++++++++-- src/Plugin.php | 9 +++++ tests/ConfigTest.php | 31 +++++++++++++++- 13 files changed, 154 insertions(+), 8 deletions(-) create mode 100644 e2e/scenario11/.gitignore create mode 100644 e2e/scenario11/README.md create mode 100644 e2e/scenario11/composer.json create mode 100644 e2e/scenario11/expected.txt create mode 100755 e2e/scenario11/script.sh create mode 100644 e2e/scenario11/vendor-bin/ns1/composer.json diff --git a/e2e/scenario11/.gitignore b/e2e/scenario11/.gitignore new file mode 100644 index 0000000..1fced8e --- /dev/null +++ b/e2e/scenario11/.gitignore @@ -0,0 +1,6 @@ +/actual.txt +/.composer/ +/composer.lock +/vendor/ +/vendor-bin/*/composer.lock +/vendor-bin/*/vendor/ diff --git a/e2e/scenario11/README.md b/e2e/scenario11/README.md new file mode 100644 index 0000000..03ab64e --- /dev/null +++ b/e2e/scenario11/README.md @@ -0,0 +1 @@ +Check that the deprecation messages are well rendered. diff --git a/e2e/scenario11/composer.json b/e2e/scenario11/composer.json new file mode 100644 index 0000000..3061d0c --- /dev/null +++ b/e2e/scenario11/composer.json @@ -0,0 +1,21 @@ +{ + "repositories": [ + { + "type": "path", + "url": "../../" + } + ], + "require": { + "bamarni/composer-bin-plugin": "dev-master" + }, + "config": { + "allow-plugins": { + "bamarni/composer-bin-plugin": true + } + }, + "extra": { + "bamarni-bin": { + "forward-command": true + } + } +} diff --git a/e2e/scenario11/expected.txt b/e2e/scenario11/expected.txt new file mode 100644 index 0000000..0368aeb --- /dev/null +++ b/e2e/scenario11/expected.txt @@ -0,0 +1,19 @@ +Loading composer repositories with package information +Updating dependencies +Lock file operations: 1 install, 0 updates, 0 removals + - Locking bamarni/composer-bin-plugin (dev-hash) +Writing lock file +Installing dependencies from lock file (including require-dev) +Package operations: 1 install, 0 updates, 0 removals + - Installing bamarni/composer-bin-plugin (dev-hash): Symlinking from ../.. +Generating autoload files +[bamarni-bin] The setting "bamarni-bin.bin-links" will be set to "false" from 2.x onwards. If you wish to keep it to "true", you need to set it explicitly. +[bamarni-bin] The command is being forwarded. +[bamarni-bin] Checking namespace vendor-bin/ns1 +Loading composer repositories with package information +Updating dependencies +Nothing to modify in lock file +Writing lock file +Installing dependencies from lock file (including require-dev) +Nothing to install, update or remove +Generating autoload files diff --git a/e2e/scenario11/script.sh b/e2e/scenario11/script.sh new file mode 100755 index 0000000..e73e86f --- /dev/null +++ b/e2e/scenario11/script.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash + +set -Eeuo pipefail + +# Set env envariables in order to experience a behaviour closer to what happens +# in the CI locally. It should not hurt to set those in the CI as the CI should +# contain those values. +export CI=1 +export COMPOSER_NO_INTERACTION=1 + +readonly ORIGINAL_WORKING_DIR=$(pwd) + +trap "cd ${ORIGINAL_WORKING_DIR}" err exit + +# Change to script directory +cd "$(dirname "$0")" + +# Ensure we have a clean state +rm -rf actual.txt || true +rm -rf .composer || true +rm -rf composer.lock || true +rm -rf vendor || true +rm -rf vendor-bin/*/composer.lock || true +rm -rf vendor-bin/*/vendor || true +rm -rf vendor-bin/*/.composer || true + +# Actual command to execute the test itself +composer update 2>&1 | tee > actual.txt diff --git a/e2e/scenario11/vendor-bin/ns1/composer.json b/e2e/scenario11/vendor-bin/ns1/composer.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/e2e/scenario11/vendor-bin/ns1/composer.json @@ -0,0 +1 @@ +{} diff --git a/e2e/scenario3/composer.json b/e2e/scenario3/composer.json index d449a2e..a1cc737 100644 --- a/e2e/scenario3/composer.json +++ b/e2e/scenario3/composer.json @@ -15,6 +15,7 @@ }, "extra": { "bamarni-bin": { + "bin-links": false, "forward-command": true } } diff --git a/e2e/scenario3/expected.txt b/e2e/scenario3/expected.txt index cadbdcb..af2a499 100644 --- a/e2e/scenario3/expected.txt +++ b/e2e/scenario3/expected.txt @@ -16,7 +16,6 @@ Generating autoload files [bamarni-bin] The command is being forwarded. [bamarni-bin] Original input: update --verbose. [bamarni-bin] Current working directory: /path/to/project/e2e/scenario3 -[bamarni-bin] Configuring bin directory to /path/to/project/e2e/scenario3/vendor/bin. [bamarni-bin] Checking namespace vendor-bin/ns1 [bamarni-bin] Changed current directory to vendor-bin/ns1. [bamarni-bin] Running `@composer update --verbose --working-dir='.'`. @@ -35,7 +34,6 @@ Generating autoload files [bamarni-bin] The command is being forwarded. [bamarni-bin] Original input: update --verbose. [bamarni-bin] Current working directory: /path/to/project/e2e/scenario3 -[bamarni-bin] Configuring bin directory to /path/to/project/e2e/scenario3/vendor/bin. [bamarni-bin] Checking namespace vendor-bin/ns1 [bamarni-bin] Changed current directory to vendor-bin/ns1. [bamarni-bin] Running `@composer update --verbose --working-dir='.'`. diff --git a/e2e/scenario8/composer.json b/e2e/scenario8/composer.json index 3061d0c..7681c07 100644 --- a/e2e/scenario8/composer.json +++ b/e2e/scenario8/composer.json @@ -15,6 +15,7 @@ }, "extra": { "bamarni-bin": { + "bin-links": false, "forward-command": true } } diff --git a/e2e/scenario8/expected.txt b/e2e/scenario8/expected.txt index cdef9f5..82a28d3 100644 --- a/e2e/scenario8/expected.txt +++ b/e2e/scenario8/expected.txt @@ -16,7 +16,6 @@ Generating autoload files [bamarni-bin] The command is being forwarded. [bamarni-bin] Original input: update --prefer-lowest --verbose. [bamarni-bin] Current working directory: /path/to/project/e2e/scenario8 -[bamarni-bin] Configuring bin directory to /path/to/project/e2e/scenario8/vendor/bin. [bamarni-bin] Checking namespace vendor-bin/ns1 [bamarni-bin] Changed current directory to vendor-bin/ns1. [bamarni-bin] Running `@composer update --prefer-lowest --verbose --working-dir='.'`. diff --git a/src/Config.php b/src/Config.php index e4c77b9..05ea7ac 100644 --- a/src/Config.php +++ b/src/Config.php @@ -6,6 +6,7 @@ use Composer\Composer; use UnexpectedValueException; +use function array_key_exists; use function array_merge; final class Config @@ -37,6 +38,11 @@ final class Config */ private $forwardCommand; + /** + * @var list + */ + private $deprecations = []; + public static function fromComposer(Composer $composer): self { return new self($composer->getPackage()->getExtra()); @@ -47,10 +53,9 @@ public static function fromComposer(Composer $composer): self */ public function __construct(array $extra) { - $config = array_merge( - self::DEFAULT_CONFIG, - $extra[self::EXTRA_CONFIG_KEY] ?? [] - ); + $userExtra = $extra[self::EXTRA_CONFIG_KEY] ?? []; + + $config = array_merge(self::DEFAULT_CONFIG, $userExtra); $getType = function_exists('get_debug_type') ? 'get_debug_type' : 'gettype'; @@ -67,6 +72,16 @@ public function __construct(array $extra) ); } + $binLinksSetExplicitly = array_key_exists(self::BIN_LINKS_ENABLED, $userExtra); + + if ($binLinks && !$binLinksSetExplicitly) { + $this->deprecations[] = sprintf( + 'The setting "%s.%s" will be set to "false" from 2.x onwards. If you wish to keep it to "true", you need to set it explicitly.', + self::EXTRA_CONFIG_KEY, + self::BIN_LINKS_ENABLED + ); + } + $targetDirectory = $config[self::TARGET_DIRECTORY]; if (!is_string($targetDirectory)) { @@ -93,6 +108,16 @@ public function __construct(array $extra) ); } + $forwardCommandSetExplicitly = array_key_exists(self::FORWARD_COMMAND, $userExtra); + + if (!$forwardCommand && !$forwardCommandSetExplicitly) { + $this->deprecations[] = sprintf( + 'The setting "%s.%s" will be set to "true" from 2.x onwards. If you wish to keep it to "false", you need to set it explicitly.', + self::EXTRA_CONFIG_KEY, + self::FORWARD_COMMAND + ); + } + $this->binLinks = $binLinks; $this->targetDirectory = $targetDirectory; $this->forwardCommand = $forwardCommand; @@ -112,4 +137,12 @@ public function isCommandForwarded(): bool { return $this->forwardCommand; } + + /** + * @return list + */ + public function getDeprecations(): array + { + return $this->deprecations; + } } diff --git a/src/Plugin.php b/src/Plugin.php index 3e24295..da1ca13 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -21,6 +21,7 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Throwable; +use function count; use function in_array; use function sprintf; @@ -118,6 +119,14 @@ private function onEvent( ): bool { $config = Config::fromComposer($this->composer); + $deprecations = $config->getDeprecations(); + + if (count($deprecations) > 0) { + foreach ($deprecations as $deprecation) { + $this->logger->logStandard($deprecation); + } + } + if ($config->isCommandForwarded() && in_array($commandName, self::FORWARDED_COMMANDS, true) ) { diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 948d5f2..14e4256 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -12,27 +12,38 @@ final class ConfigTest extends TestCase { /** * @dataProvider provideExtraConfig + * + * @param list $expectedDeprecations */ public function test_it_can_be_instantiated( array $extra, bool $expectedBinLinksEnabled, string $expectedTargetDirectory, - bool $expectedForwardCommand + bool $expectedForwardCommand, + array $expectedDeprecations ): void { $config = new Config($extra); self::assertSame($expectedBinLinksEnabled, $config->binLinksAreEnabled()); self::assertSame($expectedTargetDirectory, $config->getTargetDirectory()); self::assertSame($expectedForwardCommand, $config->isCommandForwarded()); + self::assertSame($expectedDeprecations, $config->getDeprecations()); } public static function provideExtraConfig(): iterable { + $binLinksEnabledDeprecationMessage = 'The setting "bamarni-bin.bin-links" will be set to "false" from 2.x onwards. If you wish to keep it to "true", you need to set it explicitly.'; + $forwardCommandDeprecationMessage = 'The setting "bamarni-bin.forward-command" will be set to "true" from 2.x onwards. If you wish to keep it to "false", you need to set it explicitly.'; + yield 'default values' => [ [], true, 'vendor-bin', false, + [ + $binLinksEnabledDeprecationMessage, + $forwardCommandDeprecationMessage, + ], ]; yield 'unknown extra entry' => [ @@ -40,6 +51,23 @@ public static function provideExtraConfig(): iterable true, 'vendor-bin', false, + [ + $binLinksEnabledDeprecationMessage, + $forwardCommandDeprecationMessage, + ], + ]; + + yield 'same as default but explicit' => [ + [ + Config::EXTRA_CONFIG_KEY => [ + Config::BIN_LINKS_ENABLED => true, + Config::FORWARD_COMMAND => false, + ], + ], + true, + 'vendor-bin', + false, + [], ]; yield 'nominal' => [ @@ -53,6 +81,7 @@ public static function provideExtraConfig(): iterable false, 'tools', true, + [], ]; }