Unverified Commit e2e313ec authored by Jenn Magder's avatar Jenn Magder Committed by GitHub

Take screenshot on devicelab failure (#122249)

Take screenshot on devicelab failure
parent a599c08c
...@@ -252,10 +252,14 @@ Future<void> main() async { ...@@ -252,10 +252,14 @@ Future<void> main() async {
section('gradlew on build script with error'); section('gradlew on build script with error');
await project.introduceError(); await project.introduceError();
ProcessResult result = await inDirectory(project.rootPath, () { ProcessResult result = await inDirectory(project.rootPath, () {
return executeFlutter('build', options: <String>[ return executeFlutter(
'build',
options: <String>[
'apk', 'apk',
'--release', '--release',
]); ],
canFail: true,
);
}); });
if (result.exitCode == 0) { if (result.exitCode == 0) {
...@@ -278,10 +282,14 @@ Future<void> main() async { ...@@ -278,10 +282,14 @@ Future<void> main() async {
section('flutter build apk on build script with error'); section('flutter build apk on build script with error');
await project.introduceError(); await project.introduceError();
result = await inDirectory(project.rootPath, () { result = await inDirectory(project.rootPath, () {
return executeFlutter('build', options: <String>[ return executeFlutter(
'build',
options: <String>[
'apk', 'apk',
'--release', '--release',
]); ],
canFail: true,
);
}); });
if (result.exitCode == 0) { if (result.exitCode == 0) {
throw failure( throw failure(
...@@ -304,10 +312,14 @@ Future<void> main() async { ...@@ -304,10 +312,14 @@ Future<void> main() async {
section('gradlew assembleDebug forwards stderr'); section('gradlew assembleDebug forwards stderr');
await project.introducePubspecError(); await project.introducePubspecError();
final ProcessResult result = await inDirectory(project.rootPath, () { final ProcessResult result = await inDirectory(project.rootPath, () {
return executeFlutter('build', options: <String>[ return executeFlutter(
'build',
options: <String>[
'apk', 'apk',
'--release', '--release',
]); ],
canFail: true,
);
}); });
if (result.exitCode == 0) { if (result.exitCode == 0) {
throw failure( throw failure(
......
...@@ -471,10 +471,15 @@ Future<int> flutter(String command, { ...@@ -471,10 +471,15 @@ Future<int> flutter(String command, {
bool canFail = false, // as in, whether failures are ok. False means that they are fatal. bool canFail = false, // as in, whether failures are ok. False means that they are fatal.
Map<String, String>? environment, Map<String, String>? environment,
String? workingDirectory, String? workingDirectory,
}) { }) async {
final List<String> args = _flutterCommandArgs(command, options); final List<String> args = _flutterCommandArgs(command, options);
return exec(path.join(flutterDirectory.path, 'bin', 'flutter'), args, final int exitCode = await exec(path.join(flutterDirectory.path, 'bin', 'flutter'), args,
canFail: canFail, environment: environment, workingDirectory: workingDirectory); canFail: canFail, environment: environment, workingDirectory: workingDirectory);
if (exitCode != 0 && !canFail) {
await _flutterScreenshot(workingDirectory: workingDirectory);
}
return exitCode;
} }
/// Starts a Flutter subprocess. /// Starts a Flutter subprocess.
...@@ -506,13 +511,20 @@ Future<Process> startFlutter(String command, { ...@@ -506,13 +511,20 @@ Future<Process> startFlutter(String command, {
String? workingDirectory, String? workingDirectory,
}) async { }) async {
final List<String> args = _flutterCommandArgs(command, options); final List<String> args = _flutterCommandArgs(command, options);
return startProcess( final Process process = await startProcess(
path.join(flutterDirectory.path, 'bin', 'flutter'), path.join(flutterDirectory.path, 'bin', 'flutter'),
args, args,
environment: environment, environment: environment,
isBot: isBot, isBot: isBot,
workingDirectory: workingDirectory, workingDirectory: workingDirectory,
); );
unawaited(process.exitCode.then<void>((int exitCode) async {
if (exitCode != 0) {
await _flutterScreenshot(workingDirectory: workingDirectory);
}
}));
return process;
} }
/// Runs a `flutter` command and returns the standard output as a string. /// Runs a `flutter` command and returns the standard output as a string.
...@@ -530,12 +542,53 @@ Future<String> evalFlutter(String command, { ...@@ -530,12 +542,53 @@ Future<String> evalFlutter(String command, {
Future<ProcessResult> executeFlutter(String command, { Future<ProcessResult> executeFlutter(String command, {
List<String> options = const <String>[], List<String> options = const <String>[],
bool canFail = false, // as in, whether failures are ok. False means that they are fatal.
}) async { }) async {
final List<String> args = _flutterCommandArgs(command, options); final List<String> args = _flutterCommandArgs(command, options);
return _processManager.run( final ProcessResult processResult = await _processManager.run(
<String>[path.join(flutterDirectory.path, 'bin', 'flutter'), ...args], <String>[path.join(flutterDirectory.path, 'bin', 'flutter'), ...args],
workingDirectory: cwd, workingDirectory: cwd,
); );
if (processResult.exitCode != 0 && !canFail) {
await _flutterScreenshot();
}
return processResult;
}
Future<void> _flutterScreenshot({ String? workingDirectory }) async {
try {
final Directory? dumpDirectory = hostAgent.dumpDirectory;
if (dumpDirectory == null) {
return;
}
// On command failure try uploading screenshot of failing command.
final String screenshotPath = path.join(
dumpDirectory.path,
'device-screenshot-${DateTime.now().toLocal().toIso8601String()}.png',
);
final String deviceId = (await devices.workingDevice).deviceId;
print('Taking screenshot of working device $deviceId at $screenshotPath');
final List<String> args = _flutterCommandArgs(
'screenshot',
<String>[
'--out',
screenshotPath,
'-d', deviceId,
],
);
final ProcessResult screenshot = await _processManager.run(
<String>[path.join(flutterDirectory.path, 'bin', 'flutter'), ...args],
workingDirectory: workingDirectory ?? cwd,
);
if (screenshot.exitCode != 0) {
print('Failed to take screenshot. Continuing.');
}
} catch (exception) {
print('Failed to take screenshot. Continuing.\n$exception');
}
} }
String get dartBin => String get dartBin =>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment