Skip to content

Commit 339b0d4

Browse files
committed
Ensure we close the opened ZIP files
1 parent 854c9ec commit 339b0d4

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

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

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,60 @@
55
use io\{File, Files, Folder, Path};
66
use lang\Environment;
77
use test\verify\Runtime;
8-
use test\{After, Assert, Before, Test, Values};
8+
use test\{After, Assert, Test, Values};
99
use util\cmd\Console;
1010
use xp\lambda\{PackageLambda, Sources};
1111

1212
class PackagingTest {
13-
private $tempDir;
13+
private $archives= [], $cleanup= [];
1414

15-
#[Before]
16-
private function tempDir() {
17-
$this->tempDir= new Folder(Environment::tempDir(), uniqid());
18-
$this->tempDir->create();
15+
/** Creates a new temporary folder */
16+
private function tempDir(): Folder {
17+
$this->cleanup[]= $f= new Folder(Environment::tempDir(), uniqid());
18+
$f->create();
19+
return $f;
1920
}
2021

21-
#[After]
22-
private function cleanup(Folder $folder= null) {
23-
$folder ?? $folder= $this->tempDir;
22+
/** @return void */
23+
private function removeDir(Folder $folder) {
2424
foreach ($folder->entries() as $entry) {
2525
switch ($m= lstat($entry)['mode'] & 0170000) {
2626
case Sources::IS_LINK: unlink($entry); break;
2727
case Sources::IS_FILE: $entry->asFile()->unlink(); break;
28-
case Sources::IS_FOLDER: $this->cleanup($entry->asFolder()); break;
28+
case Sources::IS_FOLDER: $this->removeDir($entry->asFolder()); break;
2929
}
3030
}
3131
}
3232

3333
/** Creates files and directory from given definitions */
34-
private function create(array $definitions): Path {
35-
$this->cleanup();
34+
private function create(array $definitions, Folder $folder= null): Path {
35+
$folder ?? $folder= $this->tempDir();
3636

3737
// Create sources from definitions
3838
foreach ($definitions as $name => $definition) {
3939
switch ($definition[0]) {
4040
case Sources::IS_FILE:
41-
Files::write(new File($this->tempDir, $name), $definition[1]);
41+
Files::write(new File($folder, $name), $definition[1]);
4242
break;
4343

4444
case Sources::IS_FOLDER:
45-
(new Folder($this->tempDir, $name))->create($definition[1]);
45+
(new Folder($folder, $name))->create($definition[1]);
4646
break;
4747

4848
case Sources::IS_LINK:
49-
symlink($definition[1], new Path($this->tempDir, $name));
49+
symlink($definition[1], new Path($folder, $name));
5050
break;
5151
}
5252
}
5353

54-
return new Path($this->tempDir);
54+
return new Path($folder);
5555
}
5656

5757
/** Creates package from given sources */
5858
private function package(Sources $sources): ZipIterator {
5959

6060
// Run packaging command
61-
$target= new Path($this->tempDir, 'test.zip');
61+
$target= new Path($this->tempDir(), 'test.zip');
6262
$out= Console::$out->stream();
6363
Console::$out->redirect(new MemoryOutputStream());
6464
try {
@@ -68,7 +68,19 @@ private function package(Sources $sources): ZipIterator {
6868
Console::$out->redirect($out);
6969
}
7070

71-
return ZipFile::open($target)->iterator();
71+
// Remember to close the archive
72+
$this->archives[]= $zip= ZipFile::open($target);
73+
return $zip->iterator();
74+
}
75+
76+
#[After]
77+
private function cleanup() {
78+
foreach ($this->files as $file) {
79+
$file->close();
80+
}
81+
foreach ($this->cleanup as $folder) {
82+
$this->removeDir($folder);
83+
}
7284
}
7385

7486
#[Test]
@@ -112,7 +124,9 @@ public function file_inside_directory() {
112124

113125
#[Test, Runtime(os: 'Linux'), Values(['../../core', '%s/core'])]
114126
public function link_inside_directory($target) {
115-
$link= sprintf($target, rtrim($this->tempDir->getURI(), DIRECTORY_SEPARATOR));
127+
$tempDir= $this->tempDir();
128+
129+
$link= sprintf($target, rtrim($tempDir->getURI(), DIRECTORY_SEPARATOR));
116130
$path= $this->create([
117131
'core/' => [Sources::IS_FOLDER, 0755],
118132
'core/composer.json' => [Sources::IS_FILE, '{"require":{"php":">=7.0"}}'],
@@ -121,7 +135,7 @@ public function link_inside_directory($target) {
121135
'project/src/file.txt' => [Sources::IS_FILE, 'Test'],
122136
'project/lib' => [Sources::IS_FOLDER, 0755],
123137
'project/lib/core' => [Sources::IS_LINK, $link],
124-
]);
138+
], $tempDir);
125139
$zip= $this->package(new Sources(new Path($path, 'project'), ['src', 'lib']));
126140

127141
$dir= $zip->next();

0 commit comments

Comments
 (0)