Skip to content

Commit da41c15

Browse files
bug #950 Resolve directories for copy from recipe in update case (shyim)
This PR was merged into the 2.x branch. Discussion ---------- Resolve directories for copy from recipe in update case The update case of `CopyFromRecipeConfigurator` does not respect the folders in the config. https://symfony.com/doc/current/setup/flex.html#customizing-flex-paths Commits ------- ee3a0d5 Resolve directories for copy from recipe in update case
2 parents 8dfcd09 + ee3a0d5 commit da41c15

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

src/Configurator/CopyFromRecipeConfigurator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,35 @@ public function unconfigure(Recipe $recipe, $config, Lock $lock)
3737
public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
3838
{
3939
foreach ($recipeUpdate->getOriginalRecipe()->getFiles() as $filename => $data) {
40+
$filename = $this->resolveTargetFolder($filename, $originalConfig);
4041
$recipeUpdate->setOriginalFile($filename, $data['contents']);
4142
}
4243

4344
$files = [];
4445
foreach ($recipeUpdate->getNewRecipe()->getFiles() as $filename => $data) {
46+
$filename = $this->resolveTargetFolder($filename, $newConfig);
4547
$recipeUpdate->setNewFile($filename, $data['contents']);
4648

4749
$files[] = $this->getLocalFilePath($recipeUpdate->getRootDir(), $filename);
4850
}
51+
4952
$recipeUpdate->getLock()->add($recipeUpdate->getPackageName(), ['files' => $files]);
5053
}
5154

55+
/**
56+
* @param array<string, string> $config
57+
*/
58+
private function resolveTargetFolder(string $path, array $config): string
59+
{
60+
foreach ($config as $key => $target) {
61+
if (0 === strpos($path, $key)) {
62+
return $this->options->expandTargetDir($target).substr($path, \strlen($key));
63+
}
64+
}
65+
66+
return $path;
67+
}
68+
5269
private function getRemovableFilesFromRecipeAndLock(Recipe $recipe, Lock $lock): array
5370
{
5471
$lockedFiles = array_unique(

tests/Command/UpdateRecipesCommandTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ private function createCommandUpdateRecipes(): CommandTester
100100
$rfs = Factory::createRemoteFilesystem($this->io, $composer->getConfig());
101101
$rfs = new ParallelDownloader($this->io, $composer->getConfig(), $rfs->getOptions(), $rfs->isTlsDisabled());
102102
}
103-
$options = new Options(['root-dir' => FLEX_TEST_DIR]);
103+
$options = new Options(['root-dir' => FLEX_TEST_DIR, 'bin-dir' => 'bin/']);
104104
$command = new UpdateRecipesCommand(
105105
$flex,
106106
new Downloader($composer, $this->io, $rfs),

tests/Configurator/CopyFromRecipeConfiguratorTest.php

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,84 @@ public function testUpdate()
190190
$this->assertSame($newRecipeFiles, $recipeUpdate->getNewFiles());
191191
}
192192

193+
public function testUpdateResolveDirectories()
194+
{
195+
$configurator = $this->createConfigurator();
196+
197+
$lock = $this->createMock(Lock::class);
198+
$lock->expects($this->once())
199+
->method('add')
200+
->with(
201+
'test-package',
202+
[
203+
'files' => [
204+
'config/packages/framework.yaml',
205+
'test.yaml',
206+
],
207+
]
208+
);
209+
210+
$originalRecipeFiles = [
211+
'symfony8config/packages/framework.yaml' => 'before',
212+
'root/test.yaml' => 'before',
213+
];
214+
$newRecipeFiles = [
215+
'symfony8config/packages/framework.yaml' => 'after',
216+
'root/test.yaml' => 'after',
217+
];
218+
219+
$originalRecipeFileData = [];
220+
foreach ($originalRecipeFiles as $file => $contents) {
221+
$originalRecipeFileData[$file] = ['contents' => $contents, 'executable' => false];
222+
}
223+
224+
$newRecipeFileData = [];
225+
foreach ($newRecipeFiles as $file => $contents) {
226+
$newRecipeFileData[$file] = ['contents' => $contents, 'executable' => false];
227+
}
228+
229+
$originalRecipe = $this->createMock(Recipe::class);
230+
$originalRecipe->method('getName')
231+
->willReturn('test-package');
232+
$originalRecipe->method('getFiles')
233+
->willReturn($originalRecipeFileData);
234+
235+
$newRecipe = $this->createMock(Recipe::class);
236+
$newRecipe->method('getFiles')
237+
->willReturn($newRecipeFileData);
238+
239+
$recipeUpdate = new RecipeUpdate(
240+
$originalRecipe,
241+
$newRecipe,
242+
$lock,
243+
FLEX_TEST_DIR
244+
);
245+
246+
$configurator->update(
247+
$recipeUpdate,
248+
[
249+
'root/' => '',
250+
'symfony8config/' => '%CONFIG_DIR%/',
251+
],
252+
[
253+
'root/' => '',
254+
'symfony8config/' => '%CONFIG_DIR%/',
255+
]
256+
);
257+
258+
// Due to root/ => '', we expect that root/ has been stripped
259+
$this->assertArrayHasKey('test.yaml', $recipeUpdate->getOriginalFiles());
260+
$this->assertArrayHasKey('test.yaml', $recipeUpdate->getNewFiles());
261+
262+
$this->assertSame('after', $recipeUpdate->getNewFiles()['test.yaml']);
263+
264+
// %CONFIG-DIR%, got resolved to config/packages back
265+
$this->assertArrayHasKey('config/packages/framework.yaml', $recipeUpdate->getOriginalFiles());
266+
$this->assertArrayHasKey('config/packages/framework.yaml', $recipeUpdate->getNewFiles());
267+
268+
$this->assertSame('after', $recipeUpdate->getNewFiles()['config/packages/framework.yaml']);
269+
}
270+
193271
protected function setUp(): void
194272
{
195273
parent::setUp();
@@ -223,7 +301,7 @@ protected function tearDown(): void
223301

224302
private function createConfigurator(): CopyFromRecipeConfigurator
225303
{
226-
return new CopyFromRecipeConfigurator($this->getMockBuilder(Composer::class)->getMock(), $this->io, new Options(['root-dir' => FLEX_TEST_DIR], $this->io));
304+
return new CopyFromRecipeConfigurator($this->getMockBuilder(Composer::class)->getMock(), $this->io, new Options(['root-dir' => FLEX_TEST_DIR, 'config-dir' => 'config'], $this->io));
227305
}
228306

229307
private function cleanUpTargetFiles()

0 commit comments

Comments
 (0)