From 75e51be2a98d6699149b732e5862e1a9d9c3dbec Mon Sep 17 00:00:00 2001 From: Felix Angelov Date: Wed, 9 Oct 2024 15:10:45 -0500 Subject: [PATCH] refactor(shorebird_cli): simplify organization name in `shorebird init` (#2521) --- .../lib/src/code_push_client_wrapper.dart | 12 ------ .../lib/src/commands/init_command.dart | 4 +- .../lib/src/extensions/organization.dart | 12 ------ .../src/code_push_client_wrapper_test.dart | 35 --------------- .../test/src/commands/init_command_test.dart | 8 +--- .../src/extensions/organization_test.dart | 43 ------------------- 6 files changed, 3 insertions(+), 111 deletions(-) delete mode 100644 packages/shorebird_cli/lib/src/extensions/organization.dart delete mode 100644 packages/shorebird_cli/test/src/extensions/organization_test.dart diff --git a/packages/shorebird_cli/lib/src/code_push_client_wrapper.dart b/packages/shorebird_cli/lib/src/code_push_client_wrapper.dart index d04a3cf37..6340bb7dd 100644 --- a/packages/shorebird_cli/lib/src/code_push_client_wrapper.dart +++ b/packages/shorebird_cli/lib/src/code_push_client_wrapper.dart @@ -105,18 +105,6 @@ class CodePushClientWrapper { ); } - /// Returns the currently logged in user, or null if no user is logged in. - Future getCurrentUser() async { - final progress = logger.progress('Fetching user'); - try { - final user = await codePushClient.getCurrentUser(); - progress.complete(); - return user; - } catch (error) { - _handleErrorAndExit(error, progress: progress); - } - } - Future> getOrganizationMemberships() async { final progress = logger.progress('Fetching organizations'); final List memberships; diff --git a/packages/shorebird_cli/lib/src/commands/init_command.dart b/packages/shorebird_cli/lib/src/commands/init_command.dart index c167564c8..28239b5eb 100644 --- a/packages/shorebird_cli/lib/src/commands/init_command.dart +++ b/packages/shorebird_cli/lib/src/commands/init_command.dart @@ -5,7 +5,6 @@ import 'package:shorebird_cli/src/code_push_client_wrapper.dart'; import 'package:shorebird_cli/src/config/config.dart'; import 'package:shorebird_cli/src/doctor.dart'; import 'package:shorebird_cli/src/executables/executables.dart'; -import 'package:shorebird_cli/src/extensions/organization.dart'; import 'package:shorebird_cli/src/logger.dart'; import 'package:shorebird_cli/src/platform/ios.dart'; import 'package:shorebird_cli/src/pubspec_editor.dart'; @@ -74,13 +73,12 @@ Please make sure you are running "shorebird init" from within your Flutter proje return ExitCode.software.code; } - final user = await codePushClientWrapper.getCurrentUser(); final Organization organization; if (orgs.length > 1) { organization = logger.chooseOne( 'Which organization should this app belong to?', choices: orgs.map((o) => o.organization).toList(), - display: (o) => o.displayName(user: user!), + display: (o) => o.name, ); } else { organization = orgs.first.organization; diff --git a/packages/shorebird_cli/lib/src/extensions/organization.dart b/packages/shorebird_cli/lib/src/extensions/organization.dart deleted file mode 100644 index 73ab0ba56..000000000 --- a/packages/shorebird_cli/lib/src/extensions/organization.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart'; - -/// {@template organization_display} -/// Returns the user-facing display name of an [Organization]. -/// {@endtemplate} -extension OrganizationDisplay on Organization { - /// {@macro organization_display} - String displayName({required PrivateUser user}) => switch (organizationType) { - OrganizationType.team => name, - OrganizationType.personal => user.displayName ?? user.email, - }; -} diff --git a/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart b/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart index 172fed738..f5ecfe302 100644 --- a/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart +++ b/packages/shorebird_cli/test/src/code_push_client_wrapper_test.dart @@ -212,41 +212,6 @@ void main() { ).thenAnswer((_) async => flutterVersion); }); - group('getCurrentUser', () { - group('when getCurrentUser request fails', () { - setUp(() { - when( - () => codePushClient.getCurrentUser(), - ).thenThrow(Exception('something went wrong')); - }); - - test('exits with code 70', () async { - await expectLater( - () async => runWithOverrides(codePushClientWrapper.getCurrentUser), - exitsWithCode(ExitCode.software), - ); - verify(() => progress.fail(any())).called(1); - }); - }); - - group('when getCurrentUser request succeeds', () { - final user = PrivateUser.forTest(); - setUp(() { - when(() => codePushClient.getCurrentUser()).thenAnswer( - (_) async => user, - ); - }); - - test('returns current user', () async { - final result = await runWithOverrides( - codePushClientWrapper.getCurrentUser, - ); - expect(result, user); - verify(() => progress.complete()).called(1); - }); - }); - }); - group('app', () { const organizationId = 123; diff --git a/packages/shorebird_cli/test/src/commands/init_command_test.dart b/packages/shorebird_cli/test/src/commands/init_command_test.dart index 6c1dd3cd0..e08d4db72 100644 --- a/packages/shorebird_cli/test/src/commands/init_command_test.dart +++ b/packages/shorebird_cli/test/src/commands/init_command_test.dart @@ -36,7 +36,6 @@ version: $version environment: sdk: ">=2.19.0 <3.0.0"'''; const organizationId = 123; - final currentUser = PrivateUser.forTest(); late ArgResults argResults; late Doctor doctor; @@ -94,9 +93,6 @@ environment: shorebirdEnv = MockShorebirdEnv(); shorebirdValidator = MockShorebirdValidator(); - when(() => codePushClientWrapper.getCurrentUser()).thenAnswer( - (_) async => currentUser, - ); when(() => codePushClientWrapper.getOrganizationMemberships()).thenAnswer( (_) async => [ OrganizationMembership( @@ -379,8 +375,8 @@ Please make sure you are running "shorebird init" from within your Flutter proje display: captureAny(named: 'display'), ), ).captured.single as String Function(Organization); - expect(capturedDisplay(org1), equals(currentUser.email)); - expect(capturedDisplay(org2), equals('org2')); + expect(capturedDisplay(org1), equals(org1.name)); + expect(capturedDisplay(org2), equals(org2.name)); verify( () => codePushClientWrapper.createApp( appName: appName, diff --git a/packages/shorebird_cli/test/src/extensions/organization_test.dart b/packages/shorebird_cli/test/src/extensions/organization_test.dart deleted file mode 100644 index 1fa42b1ab..000000000 --- a/packages/shorebird_cli/test/src/extensions/organization_test.dart +++ /dev/null @@ -1,43 +0,0 @@ -import 'package:shorebird_cli/src/extensions/organization.dart'; -import 'package:shorebird_code_push_protocol/shorebird_code_push_protocol.dart'; -import 'package:test/test.dart'; - -void main() { - group('OrganizationDisplay', () { - group('displayName', () { - test('should return the name for a team organization', () { - final organization = Organization.forTest( - organizationType: OrganizationType.team, - name: 'My Organization', - ); - expect( - organization.displayName(user: PrivateUser.forTest()), - equals('My Organization'), - ); - }); - - group('for a personal organization', () { - group('when the user has a display name', () { - final user = PrivateUser.forTest(displayName: 'John Doe'); - - test("should return the user's display name", () { - final organization = Organization.forTest(); - expect(organization.displayName(user: user), 'John Doe'); - }); - }); - - group('when the user does not have a display name', () { - final user = PrivateUser.forTest(email: 'test@test.com'); - - test("should return the user's email", () { - final organization = Organization.forTest(); - expect( - organization.displayName(user: user), - equals('test@test.com'), - ); - }); - }); - }); - }); - }); -}