1
0
Fork 0

Add "--dry-run" to bump command (#11047)

pull/11071/head
Kuba Werłos 2022-09-19 08:45:49 +02:00 committed by GitHub
parent 22bedfd8d8
commit e870206c4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 8 deletions

View File

@ -355,6 +355,7 @@ are local to the library and do not affect consumers of the package.
* **--dev-only:** Only bump requirements in "require-dev". * **--dev-only:** Only bump requirements in "require-dev".
* **--no-dev-only:** Only bump requirements in "require". * **--no-dev-only:** Only bump requirements in "require".
* **--dry-run:** Outputs the packages to bump, but will not execute anything.
## reinstall ## reinstall

View File

@ -47,6 +47,7 @@ final class BumpCommand extends BaseCommand
new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Optional package name(s) to restrict which packages are bumped.', null, $this->suggestRootRequirement()), new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Optional package name(s) to restrict which packages are bumped.', null, $this->suggestRootRequirement()),
new InputOption('dev-only', 'D', InputOption::VALUE_NONE, 'Only bump requirements in "require-dev".'), new InputOption('dev-only', 'D', InputOption::VALUE_NONE, 'Only bump requirements in "require-dev".'),
new InputOption('no-dev-only', 'R', InputOption::VALUE_NONE, 'Only bump requirements in "require".'), new InputOption('no-dev-only', 'R', InputOption::VALUE_NONE, 'Only bump requirements in "require".'),
new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the packages to bump, but will not execute anything.'),
]) ])
->setHelp( ->setHelp(
<<<EOT <<<EOT
@ -124,12 +125,12 @@ EOT
$bumper = new VersionBumper(); $bumper = new VersionBumper();
$tasks = []; $tasks = [];
if (!$input->getOption('no-dev-only')) {
$tasks['require-dev'] = $composer->getPackage()->getDevRequires();
}
if (!$input->getOption('dev-only')) { if (!$input->getOption('dev-only')) {
$tasks['require'] = $composer->getPackage()->getRequires(); $tasks['require'] = $composer->getPackage()->getRequires();
} }
if (!$input->getOption('no-dev-only')) {
$tasks['require-dev'] = $composer->getPackage()->getDevRequires();
}
$packagesFilter = $input->getArgument('packages'); $packagesFilter = $input->getArgument('packages');
if (count($packagesFilter) > 0) { if (count($packagesFilter) > 0) {
@ -170,7 +171,9 @@ EOT
} }
} }
if (!$this->updateFileCleanly($composerJson, $updates)) { $dryRun = $input->getOption('dry-run');
if (!$dryRun && !$this->updateFileCleanly($composerJson, $updates)) {
$composerDefinition = $composerJson->read(); $composerDefinition = $composerJson->read();
foreach ($updates as $key => $packages) { foreach ($updates as $key => $packages) {
foreach ($packages as $package => $version) { foreach ($packages as $package => $version) {
@ -182,12 +185,21 @@ EOT
$changeCount = array_sum(array_map('count', $updates)); $changeCount = array_sum(array_map('count', $updates));
if ($changeCount > 0) { if ($changeCount > 0) {
$io->write('<info>'.$composerJsonPath.' has been updated ('.$changeCount.' changes).</info>'); if ($dryRun) {
$io->write('<info>' . $composerJsonPath . ' would be updated with:</info>');
foreach ($updates as $requireType => $packages) {
foreach ($packages as $package => $version) {
$io->write(sprintf('<info> - %s.%s: %s</info>', $requireType, $package, $version));
}
}
} else {
$io->write('<info>' . $composerJsonPath . ' has been updated (' . $changeCount . ' changes).</info>');
}
} else { } else {
$io->write('<info>No requirements to update in '.$composerJsonPath.'.</info>'); $io->write('<info>No requirements to update in '.$composerJsonPath.'.</info>');
} }
if ($composer->getLocker()->isLocked() && $changeCount > 0) { if (!$dryRun && $composer->getLocker()->isLocked() && $changeCount > 0) {
$contents = file_get_contents($composerJson->getPath()); $contents = file_get_contents($composerJson->getPath());
if (false === $contents) { if (false === $contents) {
throw new \RuntimeException('Unable to read '.$composerJson->getPath().' contents to update the lock file hash.'); throw new \RuntimeException('Unable to read '.$composerJson->getPath().' contents to update the lock file hash.');
@ -198,6 +210,10 @@ EOT
$lock->write($lockData); $lock->write($lockData);
} }
if ($dryRun && $changeCount > 0) {
return self::ERROR_GENERIC;
}
return 0; return 0;
} }

View File

@ -23,7 +23,7 @@ class BumpCommandTest extends TestCase
* @param array<mixed> $command * @param array<mixed> $command
* @param array<mixed> $expected * @param array<mixed> $expected
*/ */
public function testBump(array $composerJson, array $command, array $expected, bool $lock = true): void public function testBump(array $composerJson, array $command, array $expected, bool $lock = true, int $exitCode = 0): void
{ {
$this->initTempComposer($composerJson); $this->initTempComposer($composerJson);
@ -41,7 +41,7 @@ class BumpCommandTest extends TestCase
} }
$appTester = $this->getApplicationTester(); $appTester = $this->getApplicationTester();
$appTester->run(array_merge(['command' => 'bump'], $command)); $this->assertSame($exitCode, $appTester->run(array_merge(['command' => 'bump'], $command)));
$json = new JsonFile('./composer.json'); $json = new JsonFile('./composer.json');
$this->assertSame($expected, $json->read()); $this->assertSame($expected, $json->read());
@ -153,5 +153,53 @@ class BumpCommandTest extends TestCase
], ],
false, false,
]; ];
yield 'bump with --dry-run with packages to bump' => [
[
'require' => [
'first/pkg' => '^2.0',
'second/pkg' => '3.*',
],
'require-dev' => [
'dev/pkg' => '~2.0',
],
],
['--dry-run' => true],
[
'require' => [
'first/pkg' => '^2.0',
'second/pkg' => '3.*',
],
'require-dev' => [
'dev/pkg' => '~2.0',
],
],
true,
1,
];
yield 'bump with --dry-run without packages to bump' => [
[
'require' => [
'first/pkg' => '^2.3.4',
'second/pkg' => '^3.4',
],
'require-dev' => [
'dev/pkg' => '^2.3.4.5',
],
],
['--dry-run' => true],
[
'require' => [
'first/pkg' => '^2.3.4',
'second/pkg' => '^3.4',
],
'require-dev' => [
'dev/pkg' => '^2.3.4.5',
],
],
true,
0,
];
} }
} }