[CP (beta)] have Java.version return null if java --version fails or cannot be run (#139838)

cherry-picks changes from https://github.com/flutter/flutter/pull/139614 onto the beta channel
This commit is contained in:
Andrew Kolos 2023-12-12 07:03:08 -08:00 committed by GitHub
parent 593a031efb
commit f5a2ca0b9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -135,6 +135,10 @@ class Java {
/// Returns the version of java in the format \d(.\d)+(.\d)+
/// Returns null if version could not be determined.
late final Version? version = (() {
if (!canRun()) {
return null;
}
final RunResult result = _processUtils.runSync(
<String>[binaryPath, '--version'],
environment: environment,
@ -142,6 +146,7 @@ class Java {
if (result.exitCode != 0) {
_logger.printTrace('java --version failed: exitCode: ${result.exitCode}'
' stdout: ${result.stdout} stderr: ${result.stderr}');
return null;
}
final String rawVersionOutput = result.stdout;
final List<String> versionLines = rawVersionOutput.split('\n');

View File

@ -183,7 +183,7 @@ OpenJDK 64-Bit Server VM Zulu19.32+15-CA (build 19.0.2+7, mixed mode, sharing)
});
});
group('getVersionString', () {
group('version', () {
late Java java;
setUp(() {
@ -208,6 +208,23 @@ OpenJDK 64-Bit Server VM Zulu19.32+15-CA (build 19.0.2+7, mixed mode, sharing)
);
}
testWithoutContext('is null when java binary cannot be run', () async {
addJavaVersionCommand('');
processManager.excludedExecutables.add('java');
expect(java.version, null);
});
testWithoutContext('is null when java --version returns a non-zero exit code', () async {
processManager.addCommand(
FakeCommand(
command: <String>[java.binaryPath, '--version'],
exitCode: 1,
),
);
expect(java.version, null);
});
testWithoutContext('parses jdk 8', () {
addJavaVersionCommand('''
java version "1.8.0_202"