-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathSharedDirsPopulateTest.php
More file actions
112 lines (90 loc) · 3.82 KB
/
Copy pathSharedDirsPopulateTest.php
File metadata and controls
112 lines (90 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer;
use spec\SpecTest;
use const __TEMP_DIR__;
class SharedDirsPopulateTest extends SpecTest
{
public function testPopulateCopiesNewReleaseFilesWithoutOverwriting()
{
$repo = $this->createRepo('populate_repo', ['data/a.txt' => 'first']);
$recipe = $this->writeRecipe('prod1', $repo, true);
$deployPath = __TEMP_DIR__ . '/prod1';
$this->depFile($recipe, 'deploy');
self::assertEquals(0, $this->tester->getStatusCode(), $this->tester->getDisplay());
self::assertFileExists($deployPath . '/shared/data/a.txt');
self::assertSame('first', file_get_contents($deployPath . '/shared/data/a.txt'));
// Simulate a new file being added to the repository over time.
$this->addFileAndCommit($repo, 'data/b.txt', 'second');
$this->depFile($recipe, 'deploy');
self::assertEquals(0, $this->tester->getStatusCode(), $this->tester->getDisplay());
self::assertFileExists($deployPath . '/shared/data/b.txt');
self::assertSame('second', file_get_contents($deployPath . '/shared/data/b.txt'));
self::assertFileExists($deployPath . '/current/data/b.txt');
// Existing shared file must not be overwritten by the release content.
self::assertSame('first', file_get_contents($deployPath . '/shared/data/a.txt'));
}
public function testPopulateDisabledByDefaultDoesNotSyncNewFiles()
{
$repo = $this->createRepo('no_populate_repo', ['data/a.txt' => 'first']);
$recipe = $this->writeRecipe('prod2', $repo, false);
$deployPath = __TEMP_DIR__ . '/prod2';
$this->depFile($recipe, 'deploy');
self::assertEquals(0, $this->tester->getStatusCode(), $this->tester->getDisplay());
self::assertFileExists($deployPath . '/shared/data/a.txt');
$this->addFileAndCommit($repo, 'data/b.txt', 'second');
$this->depFile($recipe, 'deploy');
self::assertEquals(0, $this->tester->getStatusCode(), $this->tester->getDisplay());
self::assertFileDoesNotExist($deployPath . '/shared/data/b.txt');
}
private function createRepo(string $name, array $files): string
{
$dir = __TEMP_DIR__ . '/' . $name;
mkdir($dir, 0777, true);
foreach ($files as $path => $content) {
$full = $dir . '/' . $path;
if (!is_dir(dirname($full))) {
mkdir(dirname($full), 0777, true);
}
file_put_contents($full, $content);
}
exec("cd $dir && git init 2>&1");
exec("cd $dir && git config user.name 'Deployer Test' && git config user.email 'test@example.com'");
exec("cd $dir && git add . && git commit -m 'init' 2>&1");
return $dir;
}
private function addFileAndCommit(string $repo, string $path, string $content): void
{
$full = $repo . '/' . $path;
if (!is_dir(dirname($full))) {
mkdir(dirname($full), 0777, true);
}
file_put_contents($full, $content);
exec("cd $repo && git add . && git commit -m 'add file' 2>&1");
}
private function writeRecipe(string $hostname, string $repository, bool $populate): string
{
$populateValue = $populate ? 'true' : 'false';
$recipe = <<<PHP
<?php
namespace Deployer;
require 'recipe/common.php';
set('application', 'deployer');
set('repository', '$repository');
set('shared_dirs', ['data']);
set('shared_dirs_populate', $populateValue);
set('keep_releases', 5);
set('http_user', false);
localhost('$hostname');
task('deploy:vendors', function () {
});
PHP;
$file = __TEMP_DIR__ . "/$hostname.php";
file_put_contents($file, $recipe);
return $file;
}
}