Skip to content

Commit f1355ef

Browse files
authored
MC-41912: 'magento-patches apply' command doesn't allow to apply some patches multiple times (#25)
1 parent 30594d7 commit f1355ef

File tree

9 files changed

+62
-15
lines changed

9 files changed

+62
-15
lines changed

src/Command/Apply.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ public function execute(InputInterface $input, OutputInterface $output)
8888
try {
8989
$this->applyOptional->run($input, $output);
9090
} catch (RuntimeException $e) {
91+
$output->writeln($this->magentoVersion->get());
9192
$output->writeln('<error>' . $e->getMessage() . '</error>');
93+
9294
$this->logger->info($this->magentoVersion->get());
9395
$this->logger->error($e->getMessage());
9496

src/Command/Ece/Apply.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public function execute(InputInterface $input, OutputInterface $output)
9898
$this->applyOptional->run($input, $output);
9999
$this->applyLocal->run($input, $output);
100100
} catch (RuntimeException $e) {
101+
$output->writeln($this->magentoVersion->get());
101102
$output->writeln('<error>' . $e->getMessage() . '</error>');
102103
$this->logger->info($this->magentoVersion->get());
103104
$this->logger->error($e->getMessage());

src/Command/Ece/Revert.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function execute(InputInterface $input, OutputInterface $output)
7878
try {
7979
$this->revert->run($input, $output);
8080
} catch (RuntimeException $e) {
81+
$output->writeln($this->magentoVersion->get());
8182
$output->writeln('<error>' . $e->getMessage() . '</error>');
8283
$this->logger->info($this->magentoVersion->get());
8384
$this->logger->error($e->getMessage());

src/Command/Process/Action/ProcessDeprecatedAction.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\CloudPatches\App\RuntimeException;
1111
use Magento\CloudPatches\Command\Process\Renderer;
12-
use Magento\CloudPatches\Patch\Data\AggregatedPatch;
1312
use Magento\CloudPatches\Patch\Data\AggregatedPatchInterface;
1413
use Magento\CloudPatches\Patch\Pool\OptionalPool;
1514
use Magento\CloudPatches\Patch\Aggregator;
@@ -93,6 +92,13 @@ public function execute(InputInterface $input, OutputInterface $output, array $p
9392
throw new RuntimeException($e->getMessage(), $e->getCode(), $e);
9493
}
9594

95+
$patches = array_filter(
96+
$patches,
97+
function ($patch) {
98+
return !$this->statusPool->isApplied($patch->getId());
99+
}
100+
);
101+
96102
foreach ($patches as $patch) {
97103
$this->processDeprecation($patch, $output, $input);
98104
$this->processReplacement($patch, $output, $input);
@@ -144,21 +150,23 @@ function ($patchId) {
144150
}
145151
);
146152

147-
if ($requireRevertAndReplace) {
148-
$ids = implode(' ', $requireRevertAndReplace);
149-
$warning = sprintf('%s should be reverted and replaced with %s', $ids, $patch->getId());
150-
$output->writeln('<info>' . $warning . '</info>');
151-
$this->logger->warning($warning);
153+
if (empty($requireRevertAndReplace)) {
154+
return;
155+
}
152156

153-
$question = 'Do you want to proceed with reverting?';
154-
if (!$this->renderer->printQuestion($input, $output, $question)) {
155-
$errorMessage = sprintf('%s can\'t be applied without reverting %s', $patch->getId(), $ids);
157+
$ids = implode(' ', $requireRevertAndReplace);
158+
$warning = sprintf('%s should be reverted and replaced with %s', $ids, $patch->getId());
159+
$output->writeln('<info>' . $warning . '</info>');
160+
$this->logger->warning($warning);
156161

157-
throw new RuntimeException($errorMessage);
158-
}
162+
$question = 'Do you want to proceed with reverting?';
163+
if (!$this->renderer->printQuestion($input, $output, $question)) {
164+
$errorMessage = sprintf('%s can\'t be applied without reverting %s', $patch->getId(), $ids);
159165

160-
$this->revert->execute($input, $output, $requireRevertAndReplace);
166+
throw new RuntimeException($errorMessage);
161167
}
168+
169+
$this->revert->execute($input, $output, $requireRevertAndReplace);
162170
}
163171

164172
/**

src/Command/Process/ApplyLocal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function run(InputInterface $input, OutputInterface $output)
8989
$this->printInfo($output, $message);
9090
array_push($appliedPatches, $patch);
9191
} catch (ApplierException $exception) {
92-
$this->printError($output, 'Error: patch can\'t be applied');
92+
$this->printError($output, 'Error: patch ' . $patch->getPath() . ' can\'t be applied');
9393
$messages = $this->rollbackProcessor->process($appliedPatches);
9494
$output->writeln($messages);
9595
$errorMessage = sprintf(

src/Command/Revert.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public function execute(InputInterface $input, OutputInterface $output)
101101
try {
102102
$this->revert->run($input, $output);
103103
} catch (RuntimeException $e) {
104+
$output->writeln($this->magentoVersion->get());
104105
$output->writeln('<error>' . $e->getMessage() . '</error>');
105106
$this->logger->info($this->magentoVersion->get());
106107
$this->logger->error($e->getMessage());

src/Patch/Conflict/Processor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function process(
7373
array $appliedPatches,
7474
string $exceptionMessage
7575
) {
76-
$errorMessage = 'Error: patch can\'t be applied';
76+
$errorMessage = 'Error: patch ' . $patch->getId() . ' can\'t be applied';
7777
$this->logger->error($errorMessage);
7878
$output->writeln('<error>' . $errorMessage . '</error>');
7979

src/Test/Unit/Command/Process/Action/ProcessDeprecatedActionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,40 @@ public function testProcessReplacementSuccessful()
208208
$this->action->execute($inputMock, $outputMock, $patchFilter);
209209
}
210210

211+
/**
212+
* Tests successful skipping of replacement check when patch is already applied.
213+
*/
214+
public function testSkippingReplacementProcessForAppliedPatch()
215+
{
216+
$patch1 = $this->createPatch('MC-11111', false);
217+
$this->statusPool->method('isApplied')
218+
->willReturnMap([
219+
['MC-11111', true]
220+
]);
221+
$patchFilter = [$patch1->getId()];
222+
223+
/** @var InputInterface|MockObject $inputMock */
224+
$inputMock = $this->getMockForAbstractClass(InputInterface::class);
225+
/** @var OutputInterface|MockObject $outputMock */
226+
$outputMock = $this->getMockForAbstractClass(OutputInterface::class);
227+
228+
$patchMock = $this->getMockForAbstractClass(PatchInterface::class);
229+
230+
$this->optionalPool->expects($this->once())
231+
->method('getList')
232+
->withConsecutive([$patchFilter])
233+
->willReturn([$patchMock]);
234+
235+
$this->aggregator->expects($this->once())
236+
->method('aggregate')
237+
->willReturn([$patch1]);
238+
239+
$this->renderer->expects($this->never())
240+
->method('printQuestion');
241+
242+
$this->action->execute($inputMock, $outputMock, $patchFilter);
243+
}
244+
211245
/**
212246
* Tests a case when user rejected to revert deprecated patches before applying a new one.
213247
*/

src/Test/Unit/Patch/Conflict/ProcessorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public function testProcess()
9292
$outputMock->expects($this->exactly(2))
9393
->method('writeln')
9494
->withConsecutive(
95-
[$this->stringContains('Error: patch can\'t be applied')],
95+
[$this->stringContains('Error: patch ' . $failedPatch->getId() . ' can\'t be applied')],
9696
[$rollbackMessages]
9797
);
9898

0 commit comments

Comments
 (0)