From dca85cc94034997f83b3b2fe637fd3207cbf23fd Mon Sep 17 00:00:00 2001 From: Mohamed Hubail Date: Thu, 22 Aug 2024 11:27:58 +0300 Subject: [PATCH] Add "require command with conflicting keys" test (#12072) * Add test case for conflicting both with `--dev` and without * Implement interactive case * Restrcture so that interactive case is actually interactive I don't know why specifying `'--no-interaction' => !$isInteractive,` didn't give the desired behavior of the prompt being interactive. You can verify that by printing the `$appTester->getDisplay()` which doesn't contain a prompt. In fact, it doesn't make any difference whether I set it to true or false. The only difference is if I set it or don't set it. * Fix dreaded trainling comma --- .../Test/Command/RequireCommandTest.php | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/tests/Composer/Test/Command/RequireCommandTest.php b/tests/Composer/Test/Command/RequireCommandTest.php index 0ab7508d0..d7cc95775 100644 --- a/tests/Composer/Test/Command/RequireCommandTest.php +++ b/tests/Composer/Test/Command/RequireCommandTest.php @@ -12,6 +12,7 @@ namespace Composer\Test\Command; +use Composer\Json\JsonFile; use Composer\Test\TestCase; use InvalidArgumentException; @@ -263,4 +264,91 @@ Using version 1.1.0 for required/pkg OUTPUT ]; } + + /** + * @dataProvider provideInconsistentRequireKeys + * @param bool $isDev + * @param bool $isInteractive + * @param string $expectedWarning + */ + public function testInconsistentRequireKeys(bool $isDev, bool $isInteractive, string $expectedWarning): void + { + $currentKey = $isDev ? "require" : "require-dev"; + $otherKey = $isDev ? "require-dev" : "require"; + + $dir = $this->initTempComposer([ + 'repositories' => [ + 'packages' => [ + 'type' => 'package', + 'package' => [ + ['name' => 'required/pkg', 'version' => '1.0.0'], + ], + ], + ], + $currentKey => [ + "required/pkg" => "^1.0", + ], + ]); + + $package = self::getPackage('required/pkg'); + if ($isDev) { + $this->createComposerLock([], [$package]); + $this->createInstalledJson([], [$package]); + } else { + $this->createComposerLock([$package], []); + $this->createInstalledJson([$package], []); + } + + $appTester = $this->getApplicationTester(); + $command = [ + 'command' => 'require', + '--no-audit' => true, + '--dev' => $isDev, + '--no-install' => true, + 'packages' => ['required/pkg'] + ]; + + if ($isInteractive) + $appTester->setInputs(['yes']); + else + $command['--no-interaction'] = true; + + $appTester->run($command); + + self::assertStringContainsString( + $expectedWarning, + $appTester->getDisplay(true) + ); + + $composer_content = (new JsonFile($dir . '/composer.json'))->read(); + self::assertArrayHasKey($otherKey, $composer_content); + self::assertArrayNotHasKey($currentKey, $composer_content); + } + + public function provideInconsistentRequireKeys(): \Generator + { + yield [ + true, + false, + 'required/pkg is currently present in the require key and you ran the command with the --dev flag, which will move it to the require-dev key.' + ]; + + yield [ + false, + false, + 'required/pkg is currently present in the require-dev key and you ran the command without the --dev flag, which will move it to the require key.' + ]; + + yield [ + true, + true, + 'required/pkg is currently present in the require key and you ran the command with the --dev flag, which will move it to the require-dev key.' + ]; + + yield [ + false, + true, + 'required/pkg is currently present in the require-dev key and you ran the command without the --dev flag, which will move it to the require key.' + ]; + } }