Skip to content

Commit 036bcb7

Browse files
committed
Test packaging folders with symlinks to other folders
1 parent e115b28 commit 036bcb7

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

src/main/php/xp/lambda/PackageLambda.class.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,11 @@ private function add(ZipArchiveWriter $zip, Path $path, Path $base, $prefix= '')
4343
// - Files: Add to ZIP
4444
// - Folders: Recursively add all subfolders and files therein
4545
if (self::IS_LINK === ($stat['mode'] & self::IS_LINK)) {
46-
$resolved= new Path(readlink($path));
47-
$base= $resolved->isFile() ? new Path(dirname($resolved)) : $resolved;
48-
yield from $this->add($zip, $resolved, $base, $relative.DIRECTORY_SEPARATOR);
46+
$resolved= Path::real([dirname($path), readlink($path)], $base);
47+
if ($resolved->exists()) {
48+
$base= $resolved->isFile() ? new Path(dirname($resolved)) : $resolved;
49+
yield from $this->add($zip, $resolved, $base, $relative.DIRECTORY_SEPARATOR);
50+
}
4951
} else if (self::IS_FILE === ($stat['mode'] & self::IS_FILE)) {
5052
$file= $zip->add(new ZipFileEntry($relative));
5153

src/test/php/com/amazon/aws/lambda/unittest/PackagingTest.class.php

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ private function tempDir() {
1818
}
1919

2020
#[After]
21-
private function cleanup() {
22-
$this->tempDir->unlink();
21+
private function cleanup(Folder $folder= null) {
22+
$folder ?? $folder= $this->tempDir;
23+
foreach ($folder->entries() as $entry) {
24+
switch ($m= lstat($entry)['mode'] & 0170000) {
25+
case PackageLambda::IS_LINK: unlink($entry); break;
26+
case PackageLambda::IS_FILE: $entry->asFile()->unlink(); break;
27+
case PackageLambda::IS_FOLDER: $this->cleanup($entry->asFolder()); break;
28+
}
29+
}
2330
}
2431

2532
/** Creates files and directory from given definitions */
@@ -28,13 +35,17 @@ private function create(array $definitions): Path {
2835
// Create sources from definitions
2936
foreach ($definitions as $name => $definition) {
3037
switch ($definition[0]) {
31-
case 'file':
38+
case PackageLambda::IS_FILE:
3239
Files::write(new File($this->tempDir, $name), $definition[1]);
3340
break;
3441

35-
case 'dir':
42+
case PackageLambda::IS_FOLDER:
3643
(new Folder($this->tempDir, $name))->create($definition[1]);
3744
break;
45+
46+
case PackageLambda::IS_LINK:
47+
symlink($definition[1], new Path($this->tempDir, $name));
48+
break;
3849
}
3950
}
4051

@@ -60,7 +71,7 @@ private function package(Sources $sources): ZipIterator {
6071

6172
#[Test]
6273
public function single_file() {
63-
$zip= $this->package(new Sources($this->create(['file.txt' => ['file', 'Test']]), ['file.txt']));
74+
$zip= $this->package(new Sources($this->create(['file.txt' => [PackageLambda::IS_FILE, 'Test']]), ['file.txt']));
6475

6576
$file= $zip->next();
6677
Assert::equals('file.txt', $file->getName());
@@ -70,7 +81,7 @@ public function single_file() {
7081

7182
#[Test]
7283
public function single_directory() {
73-
$zip= $this->package(new Sources($this->create(['src' => ['dir', 0755]]), ['src']));
84+
$zip= $this->package(new Sources($this->create(['src' => [PackageLambda::IS_FOLDER, 0755]]), ['src']));
7485

7586
$dir= $zip->next();
7687
Assert::equals('src/', $dir->getName());
@@ -81,8 +92,8 @@ public function single_directory() {
8192
#[Test]
8293
public function file_inside_directory() {
8394
$path= $this->create([
84-
'src' => ['dir', 0755],
85-
'src/file.txt' => ['file', 'Test']
95+
'src' => [PackageLambda::IS_FOLDER, 0755],
96+
'src/file.txt' => [PackageLambda::IS_FILE, 'Test']
8697
]);
8798
$zip= $this->package(new Sources($path, ['src']));
8899

@@ -96,4 +107,40 @@ public function file_inside_directory() {
96107

97108
Assert::false($zip->hasNext());
98109
}
110+
111+
#[Test]
112+
public function link_inside_directory() {
113+
$path= $this->create([
114+
'core/' => [PackageLambda::IS_FOLDER, 0755],
115+
'core/composer.json' => [PackageLambda::IS_FILE, '{"require":{"php":">=7.0"}}'],
116+
'project' => [PackageLambda::IS_FOLDER, 0755],
117+
'project/src' => [PackageLambda::IS_FOLDER, 0755],
118+
'project/src/file.txt' => [PackageLambda::IS_FILE, 'Test'],
119+
'project/lib' => [PackageLambda::IS_FOLDER, 0755],
120+
'project/lib/core' => [PackageLambda::IS_LINK, '../../core'],
121+
]);
122+
$zip= $this->package(new Sources(new Path($path, 'project'), ['src', 'lib']));
123+
124+
$dir= $zip->next();
125+
Assert::equals('src/', $dir->getName());
126+
Assert::true($dir->isDirectory());
127+
128+
$file= $zip->next();
129+
Assert::equals('src/file.txt', $file->getName());
130+
Assert::equals(4, $file->getSize());
131+
132+
$lib= $zip->next();
133+
Assert::equals('lib/', $lib->getName());
134+
Assert::true($lib->isDirectory());
135+
136+
$core= $zip->next();
137+
Assert::equals('lib/core/', $core->getName());
138+
Assert::true($core->isDirectory());
139+
140+
$composer= $zip->next();
141+
Assert::equals('lib/core/composer.json', $composer->getName());
142+
Assert::equals(27, $composer->getSize());
143+
144+
Assert::false($zip->hasNext());
145+
}
99146
}

0 commit comments

Comments
 (0)