Skip to content

Commit

Permalink
Introduce config deprecations (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Jul 14, 2022
1 parent bc9cbe6 commit e43ab30
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 8 deletions.
6 changes: 6 additions & 0 deletions e2e/scenario11/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/actual.txt
/.composer/
/composer.lock
/vendor/
/vendor-bin/*/composer.lock
/vendor-bin/*/vendor/
1 change: 1 addition & 0 deletions e2e/scenario11/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Check that the deprecation messages are well rendered.
21 changes: 21 additions & 0 deletions e2e/scenario11/composer.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
19 changes: 19 additions & 0 deletions e2e/scenario11/expected.txt
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions e2e/scenario11/script.sh
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions e2e/scenario11/vendor-bin/ns1/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions e2e/scenario3/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"extra": {
"bamarni-bin": {
"bin-links": false,
"forward-command": true
}
}
Expand Down
2 changes: 0 additions & 2 deletions e2e/scenario3/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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='.'`.
Expand All @@ -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='.'`.
Expand Down
1 change: 1 addition & 0 deletions e2e/scenario8/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"extra": {
"bamarni-bin": {
"bin-links": false,
"forward-command": true
}
}
Expand Down
1 change: 0 additions & 1 deletion e2e/scenario8/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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='.'`.
Expand Down
41 changes: 37 additions & 4 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Composer\Composer;
use UnexpectedValueException;
use function array_key_exists;
use function array_merge;

final class Config
Expand Down Expand Up @@ -37,6 +38,11 @@ final class Config
*/
private $forwardCommand;

/**
* @var list<string>
*/
private $deprecations = [];

public static function fromComposer(Composer $composer): self
{
return new self($composer->getPackage()->getExtra());
Expand All @@ -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';

Expand All @@ -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)) {
Expand All @@ -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;
Expand All @@ -112,4 +137,12 @@ public function isCommandForwarded(): bool
{
return $this->forwardCommand;
}

/**
* @return list<string>
*/
public function getDeprecations(): array
{
return $this->deprecations;
}
}
9 changes: 9 additions & 0 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
) {
Expand Down
31 changes: 30 additions & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,62 @@ final class ConfigTest extends TestCase
{
/**
* @dataProvider provideExtraConfig
*
* @param list<string> $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' => [
['unknown' => 'foo'],
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' => [
Expand All @@ -53,6 +81,7 @@ public static function provideExtraConfig(): iterable
false,
'tools',
true,
[],
];
}

Expand Down

0 comments on commit e43ab30

Please sign in to comment.