Skip to content

Commit

Permalink
feat(mason_api): add close (felangel#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
jksevend authored Aug 5, 2022
1 parent 542abc7 commit 6ccb3a4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mason_cli.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ jobs:
dart pub global activate pana
- name: Verify Pub Score
run: ../../tool/verify_pub_score.sh
run: ../../tool/verify_pub_score.sh 90
2 changes: 2 additions & 0 deletions packages/mason_api/example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ Future<void> main() async {

masonApi.logout();
print('Logged out!');

masonApi.close();
}
5 changes: 5 additions & 0 deletions packages/mason_api/lib/src/mason_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,11 @@ class MasonApi {
}
}

/// Closes the client and cleans up any resources associated with it.
/// It's important to close each client when it's done being used;
/// failing to do so can cause the Dart process to hang.
void close() => _httpClient.close();

/// Attempt to refresh the current credentials and return
/// refreshed credentials.
Future<Credentials> _refresh() async {
Expand Down
7 changes: 7 additions & 0 deletions packages/mason_api/test/src/mason_api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ void main() {
});
});

group('close', () {
test('closes the underlying httpClient', () {
MasonApi(httpClient: httpClient).close();
verify(() => httpClient.close()).called(1);
});
});

group('logout', () {
test('clears credentials and user', () {
final credentials = Credentials(
Expand Down
5 changes: 4 additions & 1 deletion packages/mason_cli/lib/src/command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class MasonCommandRunner extends CommandRunner<int> {
MasonApi? masonApi,
}) : _logger = logger ?? Logger(),
_pubUpdater = pubUpdater ?? PubUpdater(),
_masonApi = masonApi ?? MasonApi(hostedUri: BricksJson.hostedUri),
super(executableName, '🧱 mason \u{2022} lay the foundation!') {
final _masonApi = masonApi ?? MasonApi(hostedUri: BricksJson.hostedUri);
argParser.addFlags();
addCommand(AddCommand(logger: _logger));
addCommand(CacheCommand(logger: _logger));
Expand All @@ -47,6 +47,7 @@ class MasonCommandRunner extends CommandRunner<int> {
}

final Logger _logger;
final MasonApi _masonApi;
final PubUpdater _pubUpdater;

@override
Expand Down Expand Up @@ -74,6 +75,8 @@ class MasonCommandRunner extends CommandRunner<int> {
} catch (error) {
_logger.err('$error');
return ExitCode.software.code;
} finally {
_masonApi.close();
}
}

Expand Down
11 changes: 11 additions & 0 deletions packages/mason_cli/test/src/command_runner_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// ignore_for_file: no_adjacent_strings_in_list
import 'package:args/command_runner.dart';
import 'package:mason/mason.dart' hide packageVersion;
import 'package:mason_api/mason_api.dart';
import 'package:mason_cli/src/command_runner.dart';
import 'package:mason_cli/src/version.dart';
import 'package:mocktail/mocktail.dart';
Expand All @@ -9,6 +10,8 @@ import 'package:test/test.dart';

import '../helpers/helpers.dart';

class MockMasonApi extends Mock implements MasonApi {}

class MockLogger extends Mock implements Logger {}

class MockPubUpdater extends Mock implements PubUpdater {}
Expand Down Expand Up @@ -53,12 +56,14 @@ Run ${cyan.wrap('mason update')} to update''';
void main() {
group('MasonCommandRunner', () {
late Logger logger;
late MasonApi masonApi;
late PubUpdater pubUpdater;
late MasonCommandRunner commandRunner;

setUp(() {
printLogs = [];
logger = MockLogger();
masonApi = MockMasonApi();
pubUpdater = MockPubUpdater();

when(
Expand All @@ -67,6 +72,7 @@ void main() {

commandRunner = MasonCommandRunner(
logger: logger,
masonApi: masonApi,
pubUpdater: pubUpdater,
);
});
Expand Down Expand Up @@ -135,6 +141,11 @@ void main() {
}),
);

test('closes MasonApi', () async {
await commandRunner.run(['--version']);
verify(() => masonApi.close()).called(1);
});

group('--help', () {
test(
'outputs usage',
Expand Down

0 comments on commit 6ccb3a4

Please sign in to comment.