Skip to content

Commit 6adab40

Browse files
mnoconnicolas-grekas
authored andcommitted
Fix copy from package with directories
1 parent e472606 commit 6adab40

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/Configurator/CopyFromPackageConfigurator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ private function removeFiles(array $manifest, string $from, string $to)
7373

7474
private function copyDir(string $source, string $target, array $options)
7575
{
76+
$overwrite = $options['force'] ?? false;
77+
7678
if (!is_dir($target)) {
7779
mkdir($target, 0777, true);
7880
}
@@ -85,7 +87,7 @@ private function copyDir(string $source, string $target, array $options)
8587
mkdir($targetPath);
8688
$this->write(sprintf(' Created <fg=green>"%s"</>', $this->path->relativize($targetPath)));
8789
}
88-
} elseif (!file_exists($targetPath)) {
90+
} elseif ($overwrite || !file_exists($targetPath)) {
8991
$this->copyFile($item, $targetPath, $options);
9092
}
9193
}

tests/Configurator/CopyDirectoryFromPackageConfiguratorTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,50 @@ public function testConfigureDirectory()
5555
}
5656
}
5757

58+
/**
59+
* @dataProvider providerTestConfigureDirectoryWithExistingFiles
60+
*/
61+
public function testConfigureDirectoryWithExistingFiles(bool $force, string $sourceFileContent, string $existingTargetFileContent, string $expectedFinalTargetFileContent)
62+
{
63+
if (!is_dir($this->sourceDirectory)) {
64+
mkdir($this->sourceDirectory, 0777, true);
65+
}
66+
foreach ($this->sourceFiles as $sourceFile) {
67+
if (!file_exists($sourceFile)) {
68+
file_put_contents($sourceFile, $sourceFileContent);
69+
}
70+
}
71+
72+
if (!is_dir($this->targetDirectory)) {
73+
mkdir($this->targetDirectory, 0777, true);
74+
}
75+
76+
foreach ($this->targetFiles as $targetFile) {
77+
file_put_contents($targetFile, $existingTargetFileContent);
78+
}
79+
80+
$this->createConfigurator()->configure(
81+
$this->recipe,
82+
[$this->sourceFileRelativePath => $this->targetFileRelativePath],
83+
$this->getMockBuilder(Lock::class)->disableOriginalConstructor()->getMock(),
84+
['force' => $force]
85+
);
86+
87+
foreach ($this->targetFiles as $targetFile) {
88+
$this->assertFileExists($targetFile);
89+
$content = file_get_contents($targetFile);
90+
$this->assertEquals($expectedFinalTargetFileContent, $content);
91+
}
92+
}
93+
94+
public function providerTestConfigureDirectoryWithExistingFiles(): array
95+
{
96+
return [
97+
[true, 'NEW_CONTENT', 'OLD_CONTENT', 'NEW_CONTENT'],
98+
[false, 'NEW_CONTENT', 'OLD_CONTENT', 'OLD_CONTENT'],
99+
];
100+
}
101+
58102
protected function setUp(): void
59103
{
60104
parent::setUp();
@@ -74,6 +118,7 @@ protected function setUp(): void
74118
];
75119

76120
$this->io = $this->getMockBuilder(IOInterface::class)->getMock();
121+
$this->io->method('askConfirmation')->willReturn(true);
77122

78123
$package = $this->getMockBuilder(PackageInterface::class)->getMock();
79124
$this->recipe = $this->getMockBuilder(Recipe::class)->disableOriginalConstructor()->getMock();
@@ -106,7 +151,7 @@ protected function tearDown(): void
106151

107152
private function createConfigurator(): CopyFromPackageConfigurator
108153
{
109-
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR]));
154+
return new CopyFromPackageConfigurator($this->composer, $this->io, new Options(['root-dir' => FLEX_TEST_DIR], $this->io));
110155
}
111156

112157
private function cleanUpTargetFiles()

0 commit comments

Comments
 (0)