Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shorebird_cli): shorebird preview to not use outdated cached preview artifacts #2254

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 55 additions & 27 deletions packages/shorebird_cli/lib/src/commands/preview_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -213,41 +213,54 @@ class PreviewCommand extends ShorebirdCommand {
String? deviceId,
}) async {
const platform = ReleasePlatform.android;
final aabFile = File(
getArtifactPath(

final downloadArtifactProgress = logger.progress('Downloading release');
late File aabFile;
late ReleaseArtifact releaseAabArtifact;

try {
releaseAabArtifact = await codePushClientWrapper.getReleaseArtifact(
appId: appId,
release: release,
releaseId: release.id,
arch: 'aab',
platform: platform,
extension: 'aab',
),
);
);
} catch (e, s) {
logger
..err('Error getting release artifact: $e')
..detail('Stack trace: $s');
return ExitCode.software.code;
}

if (!aabFile.existsSync()) {
aabFile.createSync(recursive: true);
final downloadArtifactProgress = logger.progress('Downloading release');
try {
final releaseAabArtifact =
await codePushClientWrapper.getReleaseArtifact(
try {
aabFile = File(
getArtifactPath(
appId: appId,
releaseId: release.id,
arch: 'aab',
release: release,
artifact: releaseAabArtifact,
platform: platform,
);
extension: 'aab',
),
);

if (!aabFile.existsSync()) {
aabFile.createSync(recursive: true);

await artifactManager.downloadFile(
Uri.parse(releaseAabArtifact.url),
outputPath: aabFile.path,
);
downloadArtifactProgress.complete();
} catch (error) {
downloadArtifactProgress.fail('$error');
return ExitCode.software.code;
}
} catch (error) {
downloadArtifactProgress.fail('$error');
return ExitCode.software.code;
}

final apksPath = getArtifactPath(
appId: appId,
release: release,
artifact: releaseAabArtifact,
platform: platform,
extension: 'apks',
);
Expand Down Expand Up @@ -320,10 +333,28 @@ class PreviewCommand extends ShorebirdCommand {
await iosDeploy.installIfNeeded();

const platform = ReleasePlatform.ios;
final runnerDirectory = Directory(
late Directory runnerDirectory;
late ReleaseArtifact releaseRunnerArtifact;

try {
releaseRunnerArtifact = await codePushClientWrapper.getReleaseArtifact(
appId: appId,
releaseId: release.id,
arch: 'runner',
platform: platform,
);
} catch (e, s) {
logger
..err('Error getting release artifact: $e')
..detail('Stack trace: $s');
return ExitCode.software.code;
}

runnerDirectory = Directory(
getArtifactPath(
appId: appId,
release: release,
artifact: releaseRunnerArtifact,
platform: platform,
extension: 'app',
),
Expand All @@ -332,13 +363,9 @@ class PreviewCommand extends ShorebirdCommand {
if (!runnerDirectory.existsSync()) {
final downloadArtifactProgress = logger.progress('Downloading release');
try {
final releaseRunnerArtifact =
await codePushClientWrapper.getReleaseArtifact(
appId: appId,
releaseId: release.id,
arch: 'runner',
platform: platform,
);
if (!runnerDirectory.existsSync()) {
runnerDirectory.createSync(recursive: true);
}

final archiveFile = await artifactManager.downloadFile(
Uri.parse(releaseRunnerArtifact.url),
Expand Down Expand Up @@ -413,13 +440,14 @@ class PreviewCommand extends ShorebirdCommand {
String getArtifactPath({
required String appId,
required Release release,
required ReleaseArtifact artifact,
required ReleasePlatform platform,
required String extension,
}) {
final previewDirectory = cache.getPreviewDirectory(appId);
return p.join(
previewDirectory.path,
'${platform.name}_${release.version}.$extension',
'${platform.name}_${release.version}_${artifact.id}.$extension',
);
}

Expand Down
60 changes: 57 additions & 3 deletions packages/shorebird_cli/test/src/commands/preview_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void main() {
const releaseVersion = '1.2.3';
const track = DeploymentTrack.production;
const releaseId = 42;
const artifactId = 21;

late AppMetadata app;
late AppleDevice appleDevice;
Expand Down Expand Up @@ -135,6 +136,7 @@ void main() {
platform: any(named: 'platform'),
),
).thenAnswer((_) async => releaseArtifact);
when(() => releaseArtifact.id).thenReturn(artifactId);
when(() => app.appId).thenReturn(appId);
when(() => app.displayName).thenReturn(appDisplayName);
when(() => release.id).thenReturn(releaseId);
Expand Down Expand Up @@ -228,12 +230,12 @@ void main() {

String aabPath() => p.join(
previewDirectory.path,
'${releasePlatform.name}_$releaseVersion.aab',
'${releasePlatform.name}_${releaseVersion}_$artifactId.aab',
);

String apksPath() => p.join(
previewDirectory.path,
'${releasePlatform.name}_$releaseVersion.apks',
'${releasePlatform.name}_${releaseVersion}_$artifactId.apks',
);

R runWithOverrides<R>(R Function() body) {
Expand Down Expand Up @@ -915,6 +917,29 @@ channel: ${track.channel}
() => adb.logcat(filter: any(named: 'filter'), deviceId: deviceId),
).called(1);
});

group('when fetching the artifact fails', () {
setUp(() {
when(
() => codePushClientWrapper.getReleaseArtifact(
appId: any(named: 'appId'),
releaseId: any(named: 'releaseId'),
arch: any(named: 'arch'),
platform: any(named: 'platform'),
),
).thenThrow(Exception('oops'));
});

test('returns error and logs', () async {
final result = await runWithOverrides(command.run);
expect(result, equals(ExitCode.software.code));
verify(
() => logger.err(
'Error getting release artifact: Exception: oops',
),
).called(1);
});
});
});

group('ios', () {
Expand All @@ -925,7 +950,7 @@ channel: ${track.channel}

String runnerPath() => p.join(
previewDirectory.path,
'${releasePlatform.name}_$releaseVersion.app',
'${releasePlatform.name}_${releaseVersion}_$artifactId.app',
);

R runWithOverrides<R>(R Function() body) {
Expand Down Expand Up @@ -1202,6 +1227,35 @@ channel: ${DeploymentTrack.staging.channel}
'''),
);
});

group('when fetching the artifact fails', () {
setUp(() {
when(
() => codePushClientWrapper.getReleaseArtifact(
appId: any(named: 'appId'),
releaseId: any(named: 'releaseId'),
arch: any(named: 'arch'),
platform: any(named: 'platform'),
),
).thenThrow(Exception('oops'));
});

test('returns error and logs', () async {
when(
() => iosDeploy.installAndLaunchApp(
bundlePath: any(named: 'bundlePath'),
deviceId: any(named: 'deviceId'),
),
).thenAnswer((_) async => ExitCode.success.code);
final result = await runWithOverrides(command.run);
expect(result, equals(ExitCode.software.code));
verify(
() => logger.err(
'Error getting release artifact: Exception: oops',
),
).called(1);
});
});
});
});
}
Loading