Improved Java version parsing (#138155)

Fixes #135402
Add fallback logic for a different format of java version output and handle no patch versions. 
Add tests for new logic output to prevent regressions.
This commit is contained in:
Reid Baker 2023-11-09 13:38:18 -05:00 committed by GitHub
parent 750e232be1
commit 94079b9c61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -153,6 +153,17 @@ class Java {
final Iterable<RegExpMatch> matches =
jdkVersionRegex.allMatches(rawVersionOutput);
if (matches.isEmpty) {
// Fallback to second string format like "java 21.0.1 2023-09-19 LTS"
final RegExp secondJdkVersionRegex =
RegExp(r'java\s+(?<version>\d+(\.\d+)?(\.\d+)?)\s+\d\d\d\d-\d\d-\d\d');
final RegExpMatch? match = secondJdkVersionRegex.firstMatch(versionLines[0]);
if (match != null) {
final Version? parsedVersion = Version.parse(match.namedGroup('version'));
if (parsedVersion == null) {
return null;
}
return parsedVersion;
}
_logger.printWarning(_formatJavaVersionWarning(rawVersionOutput));
return null;
}

View File

@ -280,6 +280,26 @@ OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
expect(version.toString(), 'openjdk 19.0 2023-01-17');
expect(version, equals(Version(19, 0, null)));
});
testWithoutContext('parses jdk 21 with patch numbers', () {
addJavaVersionCommand('''
java 21.0.1 2023-09-19 LTS
Java(TM) SE Runtime Environment (build 21+35-LTS-2513)
Java HotSpot(TM) 64-Bit Server VM (build 21+35-LTS-2513, mixed mode, sharing)
''');
final Version? version = java.version;
expect(version, equals(Version(21, 0, 1)));
});
testWithoutContext('parses jdk 21 with no patch numbers', () {
addJavaVersionCommand('''
java 21 2023-09-19 LTS
Java(TM) SE Runtime Environment (build 21+35-LTS-2513)
Java HotSpot(TM) 64-Bit Server VM (build 21+35-LTS-2513, mixed mode, sharing)
''');
final Version? version = java.version;
expect(version, equals(Version(21, 0, 0)));
});
});
});
}