Skip to content

Resolves bug reported in Issue #76 #83

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

Merged
merged 14 commits into from
Apr 12, 2014
Merged
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
19 changes: 16 additions & 3 deletions src/MagentoHackathon/Composer/Magento/Deploystrategy/Copy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Copy extends DeploystrategyAbstract
*/
public function createDelegate($source, $dest)
{
list($mapSource, $mapDest) = $this->getCurrentMapping();
$mapSource = $this->removeTrailingSlash($mapSource);
$mapDest = $this->removeTrailingSlash($mapDest);
$cleanDest = $this->removeTrailingSlash($dest);

$sourcePath = $this->getSourceDir() . '/' . $this->removeTrailingSlash($source);
$destPath = $this->getDestDir() . '/' . $this->removeTrailingSlash($dest);

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

// first iteration through, we need to update the mappings to correctly handle mismatch globs
if ($mapSource == $this->removeTrailingSlash($source) && $mapDest == $this->removeTrailingSlash($dest)) {
if (basename($sourcePath) !== basename($destPath)) {
$this->setCurrentMapping(array($mapSource, $mapDest . '/' . basename($source)));
$cleanDest = $cleanDest . '/' . basename($source);
}
}

if (file_exists($destPath) && is_dir($destPath)) {
if (basename($sourcePath) === basename($destPath)) {
if (strcmp(substr($cleanDest, strlen($mapDest)+1), substr($source, strlen($mapSource)+1)) === 0) {
// copy each child of $sourcePath into $destPath
foreach (new \DirectoryIterator($sourcePath) as $item) {
$item = (string) $item;
if (!strcmp($item, '.') || !strcmp($item, '..')) {
continue;
}
$childSource = $source . '/' . $item;
$childSource = $this->removeTrailingSlash($source) . '/' . $item;
$this->create($childSource, substr($destPath, strlen($this->getDestDir())+1));
}
return true;
} else {
$destPath .= '/' . basename($source);
$destPath = $this->removeTrailingSlash($destPath) . '/' . basename($source);
return $this->create($source, substr($destPath, strlen($this->getDestDir())+1));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ abstract class DeploystrategyAbstract
*/
protected $mappings = array();

/**
* The current mapping of the deployment iteration
*
* @var array
*/
protected $currentMapping = array();

/**
* The magento installation's base directory
*
Expand Down Expand Up @@ -59,6 +66,7 @@ public function deploy()
{
foreach ($this->getMappings() as $data) {
list ($source, $dest) = $data;
$this->setCurrentMapping($data);
$this->create($source, $dest);
}
return $this;
Expand Down Expand Up @@ -139,6 +147,26 @@ public function setMappings(array $mappings)
$this->mappings = $mappings;
}

/**
* Gets the current mapping used on the deployment iteration
*
* @return array
*/
public function getCurrentMapping()
{
return $this->currentMapping;
}

/**
* Sets the current mapping used on the deployment iteration
*
* @param array $mapping
*/
public function setCurrentMapping($mapping)
{
$this->currentMapping = $mapping;
}

/**
* Add a key value pair to mapping
*/
Expand All @@ -159,6 +187,7 @@ protected function removeTrailingSlash($path)
*
* @param string $source
* @param string $dest
* @throws \ErrorException
* @return bool
*/
public function create($source, $dest)
Expand All @@ -184,7 +213,7 @@ public function create($source, $dest)
file app/etc/ --> link app/etc/file to file
file app/etc/a --> link app/etc/a to file
file app/etc/a --> if app/etc/a is a file throw exception unless force is set, in that case rm and see above
file app/etc/a/ --> link app/etc/a/file to file regardless if app/etc/a existst or not
file app/etc/a/ --> link app/etc/a/file to file regardless if app/etc/a exists or not

*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ public function testCreate()
touch($this->sourceDir . DS . $src);
$this->assertTrue(is_readable($this->sourceDir . DS . $src));
$this->assertFalse(is_readable($this->destDir . DS . $dest));
$this->strategy->setCurrentMapping(array($src, $dest));
$this->strategy->create($src, $dest);
$this->assertTrue(is_readable($this->destDir . DS . $dest));
}
Expand All @@ -158,6 +159,7 @@ public function testCopyDirToDir()
touch($this->sourceDir . DS . $src . DS . "local.xml");
$this->assertTrue(is_readable($this->sourceDir . DS . $src . DS . "local.xml"));
$this->assertFalse(is_readable($this->destDir . DS . $dest . DS . "local.xml"));
$this->strategy->setCurrentMapping(array($src, $dest));
$this->strategy->create($src, $dest);
$this->assertTrue(is_readable($this->destDir . DS . $dest . DS . "local.xml"));
}
Expand All @@ -173,6 +175,7 @@ public function testGlobTargetDirExists()

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

$this->strategy->setCurrentMapping(array($globSource, $dest));
$this->strategy->create($globSource, $dest);

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

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

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

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

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

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

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

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

$this->strategy->setCurrentMapping(array($globSource, $dest));
$this->strategy->create($globSource, $dest);

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

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

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

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

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

$dest = "targetdir";

$this->strategy->setCurrentMapping(array($globSource, $dest));
$this->strategy->create($globSource, $dest);

$targetDir = $this->destDir . DS . $dest;
Expand Down Expand Up @@ -312,6 +321,7 @@ public function testGlobWildcardTargetDirDoesExist()
$dest = "targetdir";
$this->mkdir($this->destDir . DS . $dest);

$this->strategy->setCurrentMapping(array($globSource, $dest));
$this->strategy->create($globSource, $dest);

$targetDir = $this->destDir . DS . $dest;
Expand Down Expand Up @@ -348,6 +358,7 @@ public function testSourceAndTargetAreDirsDoNotExist()
$testTarget = $this->destDir . DS . $dest;
$testTargetContent = $testTarget . DS . $sourceDirContent;

$this->strategy->setCurrentMapping(array($globSource, $dest));
$this->strategy->create($globSource, $dest);

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

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

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

$this->strategy->setCurrentMapping(array($globSource, $dest));
$this->strategy->create($globSource, $dest);

$this->assertFileExists($testTarget);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,28 @@ public function getTestDeployStrategyFiletype($isDir = false)

return self::TEST_FILETYPE_FILE;
}
}

public function testCopyDirToDirOfSameName()
{
$sourceRoot = 'root';
$sourceContents = "subdir/subdir/test.xml";

$this->mkdir($this->sourceDir . DS . $sourceRoot . DS . dirname($sourceContents));
touch($this->sourceDir . DS . $sourceRoot . DS . $sourceContents);

// intentionally using a differnt name to verify solution doesn't rely on identical src/dest paths
$dest = "dest/root";
$this->mkdir($this->destDir . DS . $dest);

$testTarget = $this->destDir . DS . $dest . DS . $sourceContents;
$this->strategy->setCurrentMapping(array($sourceRoot, $dest));

$this->strategy->create($sourceRoot, $dest);
$this->assertFileExists($testTarget);

$this->strategy->setIsForced(true);
$this->strategy->create($sourceRoot, $dest);

$this->assertFileNotExists(dirname(dirname($testTarget)) . DS . basename($testTarget));
}
}