Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve directories for copy from recipe in update case #950

Open
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Configurator/CopyFromRecipeConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,35 @@ public function unconfigure(Recipe $recipe, $config, Lock $lock)
public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void
{
foreach ($recipeUpdate->getOriginalRecipe()->getFiles() as $filename => $data) {
$filename = $this->resolveTargetFolder($filename, $originalConfig);
$recipeUpdate->setOriginalFile($filename, $data['contents']);
}

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

$files[] = $this->getLocalFilePath($recipeUpdate->getRootDir(), $filename);
}

$recipeUpdate->getLock()->add($recipeUpdate->getPackageName(), ['files' => $files]);
}

/**
* @param array<string, string> $config
*/
private function resolveTargetFolder(string $path, array $config): string
{
foreach ($config as $key => $target) {
if (0 === strpos($path, $key)) {
return $this->options->expandTargetDir($target).substr($path, \strlen($key));
}
}

return $path;
}

private function getRemovableFilesFromRecipeAndLock(Recipe $recipe, Lock $lock): array
{
$lockedFiles = array_unique(
Expand Down
2 changes: 1 addition & 1 deletion tests/Command/UpdateRecipesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private function createCommandUpdateRecipes(): CommandTester
$rfs = Factory::createRemoteFilesystem($this->io, $composer->getConfig());
$rfs = new ParallelDownloader($this->io, $composer->getConfig(), $rfs->getOptions(), $rfs->isTlsDisabled());
}
$options = new Options(['root-dir' => FLEX_TEST_DIR]);
$options = new Options(['root-dir' => FLEX_TEST_DIR, 'bin-dir' => 'bin/']);
$command = new UpdateRecipesCommand(
$flex,
new Downloader($composer, $this->io, $rfs),
Expand Down
80 changes: 79 additions & 1 deletion tests/Configurator/CopyFromRecipeConfiguratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,84 @@ public function testUpdate()
$this->assertSame($newRecipeFiles, $recipeUpdate->getNewFiles());
}

public function testUpdateResolveDirectories()
{
$configurator = $this->createConfigurator();

$lock = $this->createMock(Lock::class);
$lock->expects($this->once())
->method('add')
->with(
'test-package',
[
'files' => [
'config/packages/framework.yaml',
'test.yaml',
],
]
);

$originalRecipeFiles = [
'symfony8config/packages/framework.yaml' => 'before',
'root/test.yaml' => 'before',
];
$newRecipeFiles = [
'symfony8config/packages/framework.yaml' => 'after',
'root/test.yaml' => 'after',
];

$originalRecipeFileData = [];
foreach ($originalRecipeFiles as $file => $contents) {
$originalRecipeFileData[$file] = ['contents' => $contents, 'executable' => false];
}

$newRecipeFileData = [];
foreach ($newRecipeFiles as $file => $contents) {
$newRecipeFileData[$file] = ['contents' => $contents, 'executable' => false];
}

$originalRecipe = $this->createMock(Recipe::class);
$originalRecipe->method('getName')
->willReturn('test-package');
$originalRecipe->method('getFiles')
->willReturn($originalRecipeFileData);

$newRecipe = $this->createMock(Recipe::class);
$newRecipe->method('getFiles')
->willReturn($newRecipeFileData);

$recipeUpdate = new RecipeUpdate(
$originalRecipe,
$newRecipe,
$lock,
FLEX_TEST_DIR
);

$configurator->update(
$recipeUpdate,
[
'root/' => '',
'symfony8config/' => '%CONFIG_DIR%/',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the recipe using symfony8config and mapping it to the usual config dir named config is not at all the use case that you want to solve (it is exactly the opposite use case). I suggest making the test cover the intended use case instead, as that's the use case for which we want to prevent regressions.

Copy link
Contributor Author

@shyim shyim Apr 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to test, when the path is empty or you mapped to it %SOME-VAR%

I can remove it if you want. mapping config to %CONFIG_DIR% makes no difference

],
[
'root/' => '',
'symfony8config/' => '%CONFIG_DIR%/',
]
);

// Due to root/ => '', we expect that root/ has been stripped
$this->assertArrayHasKey('test.yaml', $recipeUpdate->getOriginalFiles());
$this->assertArrayHasKey('test.yaml', $recipeUpdate->getNewFiles());

$this->assertSame('after', $recipeUpdate->getNewFiles()['test.yaml']);

// %CONFIG-DIR%, got resolved to config/packages back
$this->assertArrayHasKey('config/packages/framework.yaml', $recipeUpdate->getOriginalFiles());
$this->assertArrayHasKey('config/packages/framework.yaml', $recipeUpdate->getNewFiles());

$this->assertSame('after', $recipeUpdate->getNewFiles()['config/packages/framework.yaml']);
}

protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -223,7 +301,7 @@ protected function tearDown(): void

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

private function cleanUpTargetFiles()
Expand Down
4 changes: 2 additions & 2 deletions tests/FlexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function testInstallWithPackageJsonToSynchronizeSkipped()

$this->assertStringContainsString(
'Skip synchronizing package.json with PHP packages',
$io->getOutput(),
$io->getOutput()
);
}

Expand All @@ -339,7 +339,7 @@ public function testInstallWithoutPackageJsonToSynchronizeSkipped(array $extra)

$this->assertStringNotContainsString(
'Skip synchronizing package.json with PHP packages',
$io->getOutput(),
$io->getOutput()
);
}

Expand Down
Loading