Skip to content

Commit

Permalink
feat: show progress when updating cache artifacts (#2448)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanoltman authored Aug 28, 2024
1 parent efae3ab commit 5269a28
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
15 changes: 14 additions & 1 deletion packages/shorebird_cli/lib/src/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ abstract class CachedArtifact {
// Clear any existing artifact files.
await _delete();

final updateProgress = logger.progress('Downloading $fileName...');

final request = http.Request('GET', Uri.parse(storageUrl));
final http.StreamedResponse response;
try {
Expand All @@ -201,17 +203,27 @@ allowed to access $storageUrl.''',
return;
}

updateProgress.fail();
throw CacheUpdateFailure(
'''Failed to download $fileName: ${response.statusCode} ${response.reasonPhrase}''',
);
}

updateProgress.complete();

final extractProgress = logger.progress('Extracting $fileName...');
final artifactDirectory = Directory(p.dirname(file.path));
await extractArtifact(response.stream, artifactDirectory.path);
try {
await extractArtifact(response.stream, artifactDirectory.path);
} catch (_) {
extractProgress.fail();
rethrow;
}

final expectedChecksum = checksum;
if (expectedChecksum != null) {
if (!checksumChecker.checkFile(file, expectedChecksum)) {
extractProgress.fail();
// Delete the artifact directory, so if the download is retried, it will
// be re-downloaded.
artifactDirectory.deleteSync(recursive: true);
Expand All @@ -230,6 +242,7 @@ allowed to access $storageUrl.''',
await result.exitCode;
}

extractProgress.complete();
_writeStampFile();
}

Expand Down
30 changes: 30 additions & 0 deletions packages/shorebird_cli/test/src/cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ void main() {
late ShorebirdLogger logger;
late Platform platform;
late Process chmodProcess;
late Progress progress;
late ShorebirdEnv shorebirdEnv;
late ShorebirdProcess shorebirdProcess;

Expand Down Expand Up @@ -77,6 +78,7 @@ void main() {
httpClient = MockHttpClient();
logger = MockShorebirdLogger();
platform = MockPlatform();
progress = MockProgress();
shorebirdEnv = MockShorebirdEnv();
shorebirdProcess = MockShorebirdProcess();

Expand All @@ -90,6 +92,7 @@ void main() {
(invocation.namedArguments[#outputDirectory] as Directory)
.createSync(recursive: true);
});
when(() => logger.progress(any())).thenReturn(progress);
when(
() => shorebirdEnv.shorebirdEngineRevision,
).thenReturn(shorebirdEngineRevision);
Expand Down Expand Up @@ -276,11 +279,31 @@ void main() {
expect(patchArtifactDirectory.existsSync(), isTrue);
});

group('when extraction fails', () {
setUp(() {
when(
() => artifactManager.extractZip(
zipFile: any(named: 'zipFile'),
outputDirectory: any(named: 'outputDirectory'),
),
).thenThrow(Exception('test'));
});

test('throws exception, logs failure', () async {
await expectLater(
() => runWithOverrides(cache.updateAll),
throwsException,
);
verify(() => progress.fail()).called(3);
});
});

group('when checksum validation fails', () {
setUp(() {
when(() => checksumChecker.checkFile(any(), any()))
.thenReturn(false);
});

test('fails with the correct message', () async {
await expectLater(
() => runWithOverrides(cache.updateAll),
Expand All @@ -294,6 +317,8 @@ void main() {
),
),
);

verify(() => progress.fail()).called(3);
});
});

Expand Down Expand Up @@ -375,6 +400,7 @@ void main() {
late http.Client httpClient;
late ShorebirdLogger logger;
late Platform platform;
late Progress progress;
late _TestCachedArtifact cachedArtifact;

R runWithOverrides<R>(R Function() body) {
Expand All @@ -399,13 +425,17 @@ void main() {
httpClient = MockHttpClient();
logger = MockShorebirdLogger();
platform = MockPlatform();
progress = MockProgress();

when(() => httpClient.send(any())).thenAnswer(
(_) async => http.StreamedResponse(
const Stream.empty(),
HttpStatus.notFound,
),
);

when(() => logger.progress(any())).thenReturn(progress);

cachedArtifact = _TestCachedArtifact(cache: cache, platform: platform);
});

Expand Down

0 comments on commit 5269a28

Please sign in to comment.