Skip to content

Commit

Permalink
fix(shorebird_cli): too many open files error (#2698)
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel authored Dec 17, 2024
1 parent ca1359d commit 09f7355
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 46 deletions.
6 changes: 3 additions & 3 deletions packages/shorebird_cli/lib/src/archive/directory_archive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ extension DirectoryArchive on Directory {
final tempDir = await Directory.systemTemp.createTemp();
final fileName = name ?? p.basename(path);
final outFile = File(p.join(tempDir.path, '$fileName.zip'));
await Isolate.run(
() => ZipFileEncoder().zipDirectory(this, filename: outFile.path),
);
await Isolate.run(() {
ZipFileEncoder().zipDirectory(this, filename: outFile.path);
});
return outFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class AppleArchiveDiffer extends ArchiveDiffer {

List<ArchiveFile> _filesToUnsign(String archivePath) {
return ZipDecoder()
.decodeStream(InputFileStream(archivePath))
.decodeBuffer(InputFileStream(archivePath))
.files
.where((file) => file.isFile)
.where(
Expand All @@ -110,7 +110,7 @@ class AppleArchiveDiffer extends ArchiveDiffer {

List<ArchiveFile> _carFiles(String archivePath) {
return ZipDecoder()
.decodeStream(InputFileStream(archivePath))
.decodeBuffer(InputFileStream(archivePath))
.files
.where((file) => file.isFile && p.basename(file.name) == 'Assets.car')
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ abstract class ArchiveDiffer {
/// Returns a map of file paths to their respective checksums.
Future<PathHashes> fileHashes(File archive) async {
return Isolate.run(() {
final zipDirectory = ZipDirectory()..read(InputFileStream(archive.path));
final zipDirectory = ZipDirectory.read(InputFileStream(archive.path));

return {
for (final file in zipDirectory.fileHeaders)
// Zip files contain an (optional) crc32 checksum for a file. IPAs and
// AARs seem to always include this for files, so a quick way for us
// to tell if file contents differ is if their checksums differ.
file.filename: file.crc32.toString(),
file.filename: file.crc32!.toString(),
};
});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shorebird_cli/lib/src/artifact_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class ArtifactManager {
}) async {
await Isolate.run(() async {
final inputStream = InputFileStream(zipFile.path);
final archive = ZipDecoder().decodeStream(inputStream);
final archive = ZipDecoder().decodeBuffer(inputStream);
await extractArchiveToDisk(archive, outputDirectory.path);
inputStream.closeSync();
});
Expand Down
12 changes: 2 additions & 10 deletions packages/shorebird_cli/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ packages:
dependency: "direct main"
description:
name: archive
sha256: "08064924cbf0ab88280a0c3f60db9dd24fec693927e725ecb176f16c629d1cb8"
sha256: cb6a278ef2dbb298455e1a713bda08524a175630ec643a242c399c932a0a1f7d
url: "https://pub.dev"
source: hosted
version: "4.0.1"
version: "3.6.1"
args:
dependency: "direct main"
description:
Expand Down Expand Up @@ -533,14 +533,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.5.1"
posix:
dependency: transitive
description:
name: posix
sha256: a0117dc2167805aa9125b82eee515cc891819bac2f538c83646d355b16f58b9a
url: "https://pub.dev"
source: hosted
version: "6.0.1"
propertylistserialization:
dependency: "direct main"
description:
Expand Down
2 changes: 1 addition & 1 deletion packages/shorebird_cli/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
sdk: ">=3.3.0 <4.0.0"

dependencies:
archive: ^4.0.1
archive: ^3.6.1
args: ^2.6.0
barbecue: ^0.5.0
checked_yaml: ^2.0.3
Expand Down
32 changes: 14 additions & 18 deletions packages/shorebird_cli/test/src/archive/directory_archive_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ void main() {
group('DirectoryArchive', () {
group('zipToTempFile', () {
test('zips directory to location in system temp', () async {
const numFiles = 500;
final directoryToZip = Directory.systemTemp.createTempSync();
File('${directoryToZip.path}/a.txt')
..createSync()
..writeAsStringSync('a');
File('${directoryToZip.path}/b.txt')
..createSync()
..writeAsStringSync('b');
for (var i = 0; i < numFiles; i++) {
File('${directoryToZip.path}/$i.txt')
..createSync()
..writeAsStringSync('$i');
}

final zipFile = await directoryToZip.zipToTempFile();
expect(zipFile.existsSync(), isTrue);
Expand All @@ -24,18 +24,14 @@ void main() {
final tempDir = await Directory.systemTemp.createTemp();
await extractFileToDisk(zipFile.path, tempDir.path);
final extractedContents = tempDir.listSync(recursive: true);
expect(extractedContents, hasLength(2));

final extractedFileA = extractedContents.whereType<File>().firstWhere(
(entity) => p.basename(entity.path) == 'a.txt',
);
final extractedFileB = extractedContents.whereType<File>().firstWhere(
(entity) => p.basename(entity.path) == 'b.txt',
);
expect(extractedFileA.existsSync(), isTrue);
expect(extractedFileB.existsSync(), isTrue);
expect(extractedFileA.readAsStringSync(), equals('a'));
expect(extractedFileB.readAsStringSync(), equals('b'));
expect(extractedContents, hasLength(numFiles));
for (var i = 0; i < numFiles; i++) {
final extractedFile = extractedContents.whereType<File>().firstWhere(
(entity) => p.basename(entity.path) == '$i.txt',
);
expect(extractedFile.existsSync(), isTrue);
expect(extractedFile.readAsStringSync(), equals('$i'));
}
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/shorebird_cli/test/src/cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void main() {
);
when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
Stream.value(ZipEncoder().encode(Archive())),
Stream.value(ZipEncoder().encode(Archive())!),
HttpStatus.ok,
),
);
Expand Down Expand Up @@ -282,7 +282,7 @@ void main() {
);
}
return http.StreamedResponse(
Stream.value(ZipEncoder().encode(Archive())),
Stream.value(ZipEncoder().encode(Archive())!),
HttpStatus.ok,
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void main() {
..writeAsStringSync('app_id: $appId', flush: true);
}

Future<File> createAabFile({required String? channel}) async {
File createAabFile({required String? channel}) {
final tempDir = Directory.systemTemp.createTempSync();
final aabDirectory = Directory(p.join(tempDir.path, 'app-release'))
..createSync(recursive: true);
Expand All @@ -127,7 +127,7 @@ void main() {
..createSync(recursive: true)
..writeAsStringSync(yamlContents);

await ZipFileEncoder().zipDirectory(aabDirectory, filename: aabPath());
ZipFileEncoder().zipDirectory(aabDirectory, filename: aabPath());

return File(aabPath());
}
Expand Down Expand Up @@ -495,7 +495,7 @@ void main() {
group('when channel is not set', () {
group('when target channel is production', () {
test('does not change shorebird.yaml', () async {
aabFile = await createAabFile(channel: null);
aabFile = createAabFile(channel: null);
await runWithOverrides(
() => command.setChannelOnAab(
aabFile: aabFile,
Expand All @@ -514,7 +514,7 @@ void main() {

group('when target channel is not production', () {
test('sets shorebird.yaml channel to target channel', () async {
aabFile = await createAabFile(channel: null);
aabFile = createAabFile(channel: null);
await runWithOverrides(
() => command.setChannelOnAab(
aabFile: aabFile,
Expand All @@ -534,7 +534,7 @@ channel: live

group('when channel is set to target channel', () {
test('does not attempt to set channel', () async {
aabFile = await createAabFile(channel: track.channel);
aabFile = createAabFile(channel: track.channel);
final originalModificationTime = aabFile.statSync().modified;
await runWithOverrides(
() => command.setChannelOnAab(
Expand All @@ -556,7 +556,7 @@ channel: ${track.channel}

group('when channel is set to a different channel', () {
test('sets shorebird.yaml channel to target channel', () async {
aabFile = await createAabFile(channel: 'dev');
aabFile = createAabFile(channel: 'dev');
await runWithOverrides(
() => command.setChannelOnAab(
aabFile: aabFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ void main() {
File(p.join(apksDir.path, apkFileName))
..createSync(recursive: true)
..writeAsStringSync('hello');
await ZipFileEncoder().zipDirectory(
ZipFileEncoder().zipDirectory(
apksDir,
filename: apksFile.path,
);
Expand Down

0 comments on commit 09f7355

Please sign in to comment.