Skip to content

Commit 48e5039

Browse files
author
Mikko Peltola
committed
Refactored getBlobPath() for clarity, added testApplyPatchOnSubfolder() using same dataprovider as is used for testApplyPatch()
1 parent ae675dc commit 48e5039

File tree

2 files changed

+56
-10
lines changed

2 files changed

+56
-10
lines changed

src/Update/RecipePatcher.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,8 @@ private function execute(string $command, string $cwd): string
168168
private function addMissingBlobs(array $blobs): array
169169
{
170170
$addedBlobs = [];
171-
$gitDir = trim($this->execute('git rev-parse --absolute-git-dir', $this->rootDir));
172171
foreach ($blobs as $hash => $contents) {
173-
$blobPath = $gitDir.'/'.$this->getBlobPath($hash);
172+
$blobPath = $this->getBlobPath($this->rootDir, $hash);
174173
if (file_exists($blobPath)) {
175174
continue;
176175
}
@@ -188,26 +187,27 @@ private function addMissingBlobs(array $blobs): array
188187
private function generateBlobs(array $originalFiles, string $originalFilesRoot): array
189188
{
190189
$addedBlobs = [];
191-
$originalFilesGitDir = trim($this->execute('git rev-parse --absolute-git-dir', $originalFilesRoot));
192190
foreach ($originalFiles as $filename => $contents) {
193191
// if the file didn't originally exist, no blob needed
194192
if (!file_exists($originalFilesRoot.'/'.$filename)) {
195193
continue;
196194
}
197195

198196
$hash = trim($this->execute('git hash-object '.ProcessExecutor::escape($filename), $originalFilesRoot));
199-
$addedBlobs[$hash] = file_get_contents($originalFilesGitDir.'/'.$this->getBlobPath($hash));
197+
$addedBlobs[$hash] = file_get_contents($this->getBlobPath($originalFilesRoot, $hash));
200198
}
201199

202200
return $addedBlobs;
203201
}
204202

205-
private function getBlobPath(string $hash): string
203+
private function getBlobPath(string $gitRoot, string $hash): string
206204
{
205+
$gitDir = trim($this->execute('git rev-parse --absolute-git-dir', $gitRoot));
206+
207207
$hashStart = substr($hash, 0, 2);
208208
$hashEnd = substr($hash, 2);
209209

210-
return 'objects/'.$hashStart.'/'.$hashEnd;
210+
return $gitDir.'/objects/'.$hashStart.'/'.$hashEnd;
211211
}
212212

213213
private function _applyPatchFile(RecipePatch $patch)

tests/Update/RecipePatcherTest.php

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,55 @@ public function testApplyPatch(array $filesCurrentlyInApp, RecipePatch $recipePa
233233
$this->assertSame($expectedConflicts, $hadConflicts);
234234
}
235235

236-
public function getApplyPatchTests(): iterable
236+
/**
237+
* @dataProvider getApplyPatchTests
238+
*/
239+
public function testApplyPatchOnSubfolder(array $filesCurrentlyInApp, RecipePatch $recipePatch, array $expectedFiles, bool $expectedConflicts)
237240
{
238-
$files = $this->getFilesForPatching();
241+
$mainProjectPath = FLEX_TEST_DIR;
242+
$subProjectPath = FLEX_TEST_DIR.'/ProjectA';
243+
244+
## init main project git repo
245+
(new Process(['git', 'init'], $mainProjectPath))->mustRun();
246+
(new Process(['git', 'config', 'user.name', 'Unit test'], $mainProjectPath))->mustRun();
247+
(new Process(['git', 'config', 'user.email', ''], $mainProjectPath))->mustRun();
248+
249+
if (!file_exists($subProjectPath)) {
250+
mkdir($subProjectPath, 0777, true);
251+
}
252+
253+
foreach ($filesCurrentlyInApp as $file => $contents) {
254+
$path = $subProjectPath.'/'.$file;
255+
if (!file_exists(\dirname($path))) {
256+
@mkdir(\dirname($path), 0777, true);
257+
}
258+
file_put_contents($path, $contents);
259+
}
260+
if (\count($filesCurrentlyInApp) > 0) {
261+
(new Process(['git', 'add', '-A'], $subProjectPath))->mustRun();
262+
(new Process(['git', 'commit', '-m', 'Committing original files'], $subProjectPath))->mustRun();
263+
}
264+
265+
$patcher = new RecipePatcher($subProjectPath, $this->createMock(IOInterface::class));
266+
$hadConflicts = !$patcher->applyPatch($recipePatch);
267+
268+
foreach ($expectedFiles as $file => $expectedContents) {
269+
if (null === $expectedContents) {
270+
$this->assertFileDoesNotExist($subProjectPath.'/'.$file);
271+
272+
continue;
273+
}
274+
$this->assertFileExists($subProjectPath.'/'.$file);
275+
$this->assertSame($expectedContents, file_get_contents($subProjectPath.'/'.$file));
276+
}
277+
278+
$this->assertSame($expectedConflicts, $hadConflicts);
279+
}
280+
281+
public function getApplyPatchTests(string $testMethodName): iterable
282+
{
283+
$projectRootPath = ($testMethodName === "testApplyPatchOnSubfolder") ? "ProjectA/" : "";
284+
$files = $this->getFilesForPatching($projectRootPath);
239285
$dotEnvClean = $files['dot_env_clean'];
240286
$packageJsonConflict = $files['package_json_conflict'];
241287
$webpackEncoreAdded = $files['webpack_encore_added'];
@@ -393,7 +439,7 @@ public function getIntegrationTests(): iterable
393439
* * original_recipe
394440
* * updated_recipe.
395441
*/
396-
private function getFilesForPatching(): array
442+
private function getFilesForPatching(string $projectPath=""): array
397443
{
398444
$files = [
399445
// .env
@@ -538,7 +584,7 @@ private function getFilesForPatching(): array
538584
foreach ($files as $key => $data) {
539585
$files[$key] = array_merge(
540586
$data,
541-
$this->generatePatchData($data['filename'], $data['original_recipe'], $data['updated_recipe'])
587+
$this->generatePatchData($projectPath.$data['filename'], $data['original_recipe'], $data['updated_recipe'])
542588
);
543589
}
544590

0 commit comments

Comments
 (0)