Skip to content

Commit b84b310

Browse files
committed
Merge pull request #83 from davidalger/issue-76
Resolves bug reported in Issue #76 does not break any existing unittests, currently failing unittests were already failing before
2 parents 0c93d05 + 5053721 commit b84b310

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

src/MagentoHackathon/Composer/Magento/Deploystrategy/Copy.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class Copy extends DeploystrategyAbstract
2020
*/
2121
public function createDelegate($source, $dest)
2222
{
23+
list($mapSource, $mapDest) = $this->getCurrentMapping();
24+
$mapSource = $this->removeTrailingSlash($mapSource);
25+
$mapDest = $this->removeTrailingSlash($mapDest);
26+
$cleanDest = $this->removeTrailingSlash($dest);
27+
2328
$sourcePath = $this->getSourceDir() . '/' . $this->removeTrailingSlash($source);
2429
$destPath = $this->getDestDir() . '/' . $this->removeTrailingSlash($dest);
2530

@@ -36,20 +41,28 @@ public function createDelegate($source, $dest)
3641
// Namespace/ModuleDir => Namespace/, but Namespace/ModuleDir may exist
3742
// Namespace/ModuleDir => Namespace/ModuleDir, but ModuleDir may exist
3843

44+
// first iteration through, we need to update the mappings to correctly handle mismatch globs
45+
if ($mapSource == $this->removeTrailingSlash($source) && $mapDest == $this->removeTrailingSlash($dest)) {
46+
if (basename($sourcePath) !== basename($destPath)) {
47+
$this->setCurrentMapping(array($mapSource, $mapDest . '/' . basename($source)));
48+
$cleanDest = $cleanDest . '/' . basename($source);
49+
}
50+
}
51+
3952
if (file_exists($destPath) && is_dir($destPath)) {
40-
if (basename($sourcePath) === basename($destPath)) {
53+
if (strcmp(substr($cleanDest, strlen($mapDest)+1), substr($source, strlen($mapSource)+1)) === 0) {
4154
// copy each child of $sourcePath into $destPath
4255
foreach (new \DirectoryIterator($sourcePath) as $item) {
4356
$item = (string) $item;
4457
if (!strcmp($item, '.') || !strcmp($item, '..')) {
4558
continue;
4659
}
47-
$childSource = $source . '/' . $item;
60+
$childSource = $this->removeTrailingSlash($source) . '/' . $item;
4861
$this->create($childSource, substr($destPath, strlen($this->getDestDir())+1));
4962
}
5063
return true;
5164
} else {
52-
$destPath .= '/' . basename($source);
65+
$destPath = $this->removeTrailingSlash($destPath) . '/' . basename($source);
5366
return $this->create($source, substr($destPath, strlen($this->getDestDir())+1));
5467
}
5568
}

src/MagentoHackathon/Composer/Magento/Deploystrategy/DeploystrategyAbstract.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ abstract class DeploystrategyAbstract
1717
*/
1818
protected $mappings = array();
1919

20+
/**
21+
* The current mapping of the deployment iteration
22+
*
23+
* @var array
24+
*/
25+
protected $currentMapping = array();
26+
2027
/**
2128
* The magento installation's base directory
2229
*
@@ -59,6 +66,7 @@ public function deploy()
5966
{
6067
foreach ($this->getMappings() as $data) {
6168
list ($source, $dest) = $data;
69+
$this->setCurrentMapping($data);
6270
$this->create($source, $dest);
6371
}
6472
return $this;
@@ -139,6 +147,26 @@ public function setMappings(array $mappings)
139147
$this->mappings = $mappings;
140148
}
141149

150+
/**
151+
* Gets the current mapping used on the deployment iteration
152+
*
153+
* @return array
154+
*/
155+
public function getCurrentMapping()
156+
{
157+
return $this->currentMapping;
158+
}
159+
160+
/**
161+
* Sets the current mapping used on the deployment iteration
162+
*
163+
* @param array $mapping
164+
*/
165+
public function setCurrentMapping($mapping)
166+
{
167+
$this->currentMapping = $mapping;
168+
}
169+
142170
/**
143171
* Add a key value pair to mapping
144172
*/
@@ -159,6 +187,7 @@ protected function removeTrailingSlash($path)
159187
*
160188
* @param string $source
161189
* @param string $dest
190+
* @throws \ErrorException
162191
* @return bool
163192
*/
164193
public function create($source, $dest)
@@ -184,7 +213,7 @@ public function create($source, $dest)
184213
file app/etc/ --> link app/etc/file to file
185214
file app/etc/a --> link app/etc/a to file
186215
file app/etc/a --> if app/etc/a is a file throw exception unless force is set, in that case rm and see above
187-
file app/etc/a/ --> link app/etc/a/file to file regardless if app/etc/a existst or not
216+
file app/etc/a/ --> link app/etc/a/file to file regardless if app/etc/a exists or not
188217
189218
*/
190219

tests/MagentoHackathon/Composer/Magento/Deploystrategy/AbstractTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public function testCreate()
146146
touch($this->sourceDir . DS . $src);
147147
$this->assertTrue(is_readable($this->sourceDir . DS . $src));
148148
$this->assertFalse(is_readable($this->destDir . DS . $dest));
149+
$this->strategy->setCurrentMapping(array($src, $dest));
149150
$this->strategy->create($src, $dest);
150151
$this->assertTrue(is_readable($this->destDir . DS . $dest));
151152
}
@@ -158,6 +159,7 @@ public function testCopyDirToDir()
158159
touch($this->sourceDir . DS . $src . DS . "local.xml");
159160
$this->assertTrue(is_readable($this->sourceDir . DS . $src . DS . "local.xml"));
160161
$this->assertFalse(is_readable($this->destDir . DS . $dest . DS . "local.xml"));
162+
$this->strategy->setCurrentMapping(array($src, $dest));
161163
$this->strategy->create($src, $dest);
162164
$this->assertTrue(is_readable($this->destDir . DS . $dest . DS . "local.xml"));
163165
}
@@ -173,6 +175,7 @@ public function testGlobTargetDirExists()
173175

174176
$testTarget = $this->destDir . DS . $dest . DS . basename($globSource);
175177

178+
$this->strategy->setCurrentMapping(array($globSource, $dest));
176179
$this->strategy->create($globSource, $dest);
177180

178181
$this->assertFileType(dirname($testTarget), self::TEST_FILETYPE_DIR);
@@ -192,6 +195,7 @@ public function testTargetDirWithChildDirExists()
192195

193196
$testTarget = $this->destDir . DS . $dest . DS . basename($globSource) . DS . basename($sourceContents);
194197

198+
$this->strategy->setCurrentMapping(array($globSource, $dest));
195199
$this->strategy->create($globSource, $dest);
196200
//passthru("tree {$this->destDir}/$dest");
197201

@@ -211,6 +215,7 @@ public function testTargetDirWithChildDirNotExists()
211215

212216
$testTarget = $this->destDir . DS . $dest . DS . basename($globSource) . DS . basename($sourceContents);
213217

218+
$this->strategy->setCurrentMapping(array($globSource, $dest));
214219
$this->strategy->create($globSource, $dest);
215220
//passthru("tree {$this->destDir}/$dest");
216221

@@ -228,6 +233,7 @@ public function testGlobTargetDirDoesNotExists()
228233

229234
$testTarget = $this->destDir . DS . $dest;
230235

236+
$this->strategy->setCurrentMapping(array($globSource, $dest));
231237
$this->strategy->create($globSource, $dest);
232238

233239
$this->assertFileType(dirname($testTarget), self::TEST_FILETYPE_DIR);
@@ -247,6 +253,7 @@ public function testGlobSlashDirectoryExists()
247253
$testTarget = $this->destDir . DS . $dest . basename($globSource);
248254

249255
// second create has to identify symlink
256+
$this->strategy->setCurrentMapping(array($globSource, $dest));
250257
$this->strategy->create($globSource, $dest);
251258

252259
$this->assertFileType(dirname($testTarget), self::TEST_FILETYPE_DIR);
@@ -265,6 +272,7 @@ public function testGlobSlashDirectoryDoesNotExists()
265272
$testTarget = $this->destDir . DS . $dest . basename($globSource);
266273

267274
// second create has to identify symlink
275+
$this->strategy->setCurrentMapping(array($globSource, $dest));
268276
$this->strategy->create($globSource, $dest);
269277

270278
$this->assertFileType(dirname($testTarget), self::TEST_FILETYPE_DIR);
@@ -284,6 +292,7 @@ public function testGlobWildcardTargetDirDoesNotExist()
284292

285293
$dest = "targetdir";
286294

295+
$this->strategy->setCurrentMapping(array($globSource, $dest));
287296
$this->strategy->create($globSource, $dest);
288297

289298
$targetDir = $this->destDir . DS . $dest;
@@ -312,6 +321,7 @@ public function testGlobWildcardTargetDirDoesExist()
312321
$dest = "targetdir";
313322
$this->mkdir($this->destDir . DS . $dest);
314323

324+
$this->strategy->setCurrentMapping(array($globSource, $dest));
315325
$this->strategy->create($globSource, $dest);
316326

317327
$targetDir = $this->destDir . DS . $dest;
@@ -348,6 +358,7 @@ public function testSourceAndTargetAreDirsDoNotExist()
348358
$testTarget = $this->destDir . DS . $dest;
349359
$testTargetContent = $testTarget . DS . $sourceDirContent;
350360

361+
$this->strategy->setCurrentMapping(array($globSource, $dest));
351362
$this->strategy->create($globSource, $dest);
352363

353364
$this->assertFileExists($testTarget);
@@ -378,11 +389,12 @@ public function testSourceAndTargetAreDirsDoExist()
378389
$this->mkdir($this->destDir . DS . $dest);
379390

380391
// The target should be created INSIDE the target directory because the target dir exists exist
381-
// This is how bash commands (and therefore modman) process source and targer
392+
// This is how bash commands (and therefore modman) process source and target
382393

383394
$testTarget = $this->destDir . DS . $dest . DS . basename($globSource);
384395
$testTargetContent = $testTarget . DS . $sourceDirContent;
385396

397+
$this->strategy->setCurrentMapping(array($globSource, $dest));
386398
$this->strategy->create($globSource, $dest);
387399

388400
$this->assertFileExists($testTarget);

tests/MagentoHackathon/Composer/Magento/Deploystrategy/CopyTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,28 @@ public function getTestDeployStrategyFiletype($isDir = false)
2323

2424
return self::TEST_FILETYPE_FILE;
2525
}
26-
}
26+
27+
public function testCopyDirToDirOfSameName()
28+
{
29+
$sourceRoot = 'root';
30+
$sourceContents = "subdir/subdir/test.xml";
31+
32+
$this->mkdir($this->sourceDir . DS . $sourceRoot . DS . dirname($sourceContents));
33+
touch($this->sourceDir . DS . $sourceRoot . DS . $sourceContents);
34+
35+
// intentionally using a differnt name to verify solution doesn't rely on identical src/dest paths
36+
$dest = "dest/root";
37+
$this->mkdir($this->destDir . DS . $dest);
38+
39+
$testTarget = $this->destDir . DS . $dest . DS . $sourceContents;
40+
$this->strategy->setCurrentMapping(array($sourceRoot, $dest));
41+
42+
$this->strategy->create($sourceRoot, $dest);
43+
$this->assertFileExists($testTarget);
44+
45+
$this->strategy->setIsForced(true);
46+
$this->strategy->create($sourceRoot, $dest);
47+
48+
$this->assertFileNotExists(dirname(dirname($testTarget)) . DS . basename($testTarget));
49+
}
50+
}

0 commit comments

Comments
 (0)