Skip to content

Commit

Permalink
feat(shorebird_cli): shorebird preview should filter releases by pl…
Browse files Browse the repository at this point in the history
…atform
  • Loading branch information
felangel committed Jan 17, 2025
1 parent 09dfd60 commit d5c5fa9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
36 changes: 24 additions & 12 deletions packages/shorebird_cli/lib/src/commands/preview_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,27 @@ This is only applicable when previewing Android releases.''',
//
// With these two lists, we can now determine if a platform is previewable
// or not, by making a difference between the two lists.
final (releasesWithAllPlatforms, releasesWithPreviewablePlatforms) = await (
final (allReleases, sideloadableReleases) = await (
codePushClientWrapper.getReleases(appId: appId),
codePushClientWrapper.getReleases(
appId: appId,
sideloadableOnly: true,
)
).wait;

final maybePlatform = results['platform'] != null
? ReleasePlatform.values.byName(results['platform'] as String)
: null;

final releaseVersion = results['release-version'] as String? ??
// Prompt only for releases that have previewable platforms.
await promptForReleaseVersion(releasesWithPreviewablePlatforms);
// Prompt only for releases that have previewable platforms
// and filter by the specified platform.
await promptForReleaseVersion(
sideloadableReleases,
maybePlatform,
);

final release = releasesWithPreviewablePlatforms.firstWhereOrNull(
final release = sideloadableReleases.firstWhereOrNull(
(r) => r.version == releaseVersion,
);

Expand All @@ -232,7 +240,7 @@ This is only applicable when previewing Android releases.''',
return ExitCode.software.code;
}

final releaseWithAllPlatforms = releasesWithAllPlatforms.firstWhere(
final releaseWithAllPlatforms = allReleases.firstWhere(
(r) => r.id == release.id,
);
_assertPreviewableReleases(
Expand All @@ -241,13 +249,9 @@ This is only applicable when previewing Android releases.''',
);

final ReleasePlatform releasePlatform;
if (results['platform'] != null) {
final platformName = ReleasePlatform.values.byName(
results['platform'] as String,
);

if (maybePlatform != null) {
final matchedPlatform = release.activePlatforms.firstWhere(
(p) => p == platformName,
(p) => p == maybePlatform,
);

releasePlatform = matchedPlatform;
Expand Down Expand Up @@ -300,8 +304,16 @@ This is only applicable when previewing Android releases.''',
}

/// Prompts the user to choose a release version to preview.
Future<String?> promptForReleaseVersion(List<Release> releases) async {
Future<String?> promptForReleaseVersion(
List<Release> releases,
ReleasePlatform? platform,
) async {
if (releases.isEmpty) return null;
if (platform != null) {
releases.removeWhere(
(release) => !release.platformStatuses.keys.contains(platform),
);
}
final release = logger.chooseOne(
'Which release would you like to preview?',
choices: releases,
Expand Down
45 changes: 45 additions & 0 deletions packages/shorebird_cli/test/src/commands/preview_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,51 @@ channel: ${track.channel}
});
});

group('when release-version is not specified', () {
late Release otherRelease;

setUp(() {
otherRelease = MockRelease();
when(() => otherRelease.appId).thenReturn(appId);
when(() => otherRelease.version).thenReturn(releaseVersion);
when(() => otherRelease.displayName).thenReturn('2.0.0+1');
when(() => otherRelease.platformStatuses).thenReturn({
ReleasePlatform.macos: ReleaseStatus.active,
ReleasePlatform.windows: ReleaseStatus.active,
});
when(() => argResults['release-version']).thenReturn(null);
when(() => argResults['platform']).thenReturn(releasePlatform.name);
when(
() => logger.chooseOne<Release>(
any(),
choices: any(named: 'choices'),
display: any(named: 'display'),
),
).thenReturn(release);
when(
() => codePushClientWrapper.getReleases(
appId: any(named: 'appId'),
sideloadableOnly: any(named: 'sideloadableOnly'),
),
).thenAnswer((_) async => [release, otherRelease]);
});

test(
'only prompts for release versions that '
'support the current platform', () async {
await runWithOverrides(command.run);
final releases = verify(
() => logger.chooseOne<Release>(
any(),
choices: captureAny(named: 'choices'),
display: any(named: 'display'),
),
).captured.single as List<Release>;
expect(releases.length, equals(1));
expect(releases.first.version, equals(releaseVersion));
});
});

group('when platform is not specified', () {
setUp(() {
when(
Expand Down

0 comments on commit d5c5fa9

Please sign in to comment.