[go: nahoru, domu]

Skip to content

Commit

Permalink
[flutter_tools] Fix Future error handling ArgumentError in doctor --a…
Browse files Browse the repository at this point in the history
…ndroid-licenses (#119977)

* wip

* write test

* make error handling printError

* remove diff
  • Loading branch information
christopherfujino committed Feb 5, 2023
1 parent 2e39bad commit b8f5394
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/flutter_tools/lib/src/android/android_workflow.dart
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,14 @@ class AndroidLicenseValidator extends DoctorValidator {
unawaited(process.stdin.addStream(_stdio.stdin)
// If the process exits unexpectedly with an error, that will be
// handled by the caller.
.catchError((dynamic err, StackTrace stack) {
_logger.printTrace('Echoing stdin to the licenses subprocess failed:');
_logger.printTrace('$err\n$stack');
}
));
.then(
(Object? socket) => socket,
onError: (dynamic err, StackTrace stack) {
_logger.printError('Echoing stdin to the licenses subprocess failed:');
_logger.printError('$err\n$stack');
},
),
);

// Wait for stdout and stderr to be fully processed, because process.exitCode
// may complete first.
Expand All @@ -450,8 +453,8 @@ class AndroidLicenseValidator extends DoctorValidator {
_stdio.addStderrStream(process.stderr),
]);
} on Exception catch (err, stack) {
_logger.printTrace('Echoing stdout or stderr from the license subprocess failed:');
_logger.printTrace('$err\n$stack');
_logger.printError('Echoing stdout or stderr from the license subprocess failed:');
_logger.printError('$err\n$stack');
}

final int exitCode = await process.exitCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:flutter_tools/src/android/android_sdk.dart';
import 'package:flutter_tools/src/android/android_studio.dart';
import 'package:flutter_tools/src/android/android_workflow.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/user_messages.dart';
Expand Down Expand Up @@ -311,6 +312,37 @@ Review licenses that have not been accepted (y/N)?
expect(licenseValidator.runLicenseManager(), throwsToolExit());
});

testWithoutContext('runLicenseManager handles broken pipe without ArgumentError', () async {
sdk.sdkManagerPath = '/foo/bar/sdkmanager';
const String exceptionMessage = 'Write failed (OS Error: Broken pipe, errno = 32), port = 0';
const SocketException exception = SocketException(exceptionMessage);
// By using a `Socket` generic parameter, the stdin.addStream will return a `Future<Socket>`
// We are testing that our error handling properly handles futures of this type
final ThrowingStdin<Socket> fakeStdin = ThrowingStdin<Socket>(exception);
final FakeCommand licenseCommand = FakeCommand(
command: <String>[sdk.sdkManagerPath!, '--licenses'],
stdin: fakeStdin,
);
processManager.addCommand(licenseCommand);
final BufferLogger logger = BufferLogger.test();

final AndroidLicenseValidator licenseValidator = AndroidLicenseValidator(
androidSdk: sdk,
fileSystem: fileSystem,
processManager: processManager,
platform: FakePlatform(environment: <String, String>{'HOME': '/home/me'}),
stdio: stdio,
logger: logger,
userMessages: UserMessages(),
androidStudio: FakeAndroidStudio(),
operatingSystemUtils: FakeOperatingSystemUtils(),
);

await licenseValidator.runLicenseManager();
expect(logger.errorText, contains(exceptionMessage));
expect(processManager, hasNoRemainingExpectations);
});

testWithoutContext('runLicenseManager errors when sdkmanager fails to run', () async {
sdk.sdkManagerPath = '/foo/bar/sdkmanager';
processManager.excludedExecutables.add('/foo/bar/sdkmanager');
Expand Down Expand Up @@ -574,3 +606,14 @@ class FakeAndroidStudio extends Fake implements AndroidStudio {
@override
String get javaPath => 'java';
}

class ThrowingStdin<T> extends Fake implements IOSink {
ThrowingStdin(this.exception);

final Exception exception;

@override
Future<dynamic> addStream(Stream<List<int>> stream) {
return Future<T>.error(exception);
}
}

0 comments on commit b8f5394

Please sign in to comment.