fix(requirements check): use regex to get java version from javac output (#1220)

* fix(requirements check): use regex to get java version from javac output

* fix(lint): format code

* fix(node 10): remove optional chaining from version check
This commit is contained in:
David 2021-05-09 14:33:08 +02:00 committed by GitHub
parent 1b7874607e
commit a45804329b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 1 deletions

View File

@ -43,7 +43,28 @@ const java = {
// Java <= 8 writes version info to stderr, Java >= 9 to stdout // Java <= 8 writes version info to stderr, Java >= 9 to stdout
let version = null; let version = null;
try { try {
version = (await execa('javac', ['-version'], { all: true })).all; const javacOutput = (await execa('javac', ['-version'], { all: true })).all;
/*
A regex match for the java version looks like the following:
version: [
'javac 1.8.0',
'1.8.0',
index: 45,
input: 'Picked up _JAVA_OPTIONS: -Xms1024M -Xmx2048M\njavac 1.8.0_271',
groups: undefined
]
We have to use a regular expression to get the java version, because on some environments
(e.g. macOS Big Sur) javac prints _JAVA_OPTIONS before printing the version and semver.coerce()
will fail to get the correct version from the output.
*/
const match = /javac\s+([\d.]+)/i.exec(javacOutput);
if (match && match[1]) {
version = match[1];
}
} catch (ex) { } catch (ex) {
events.emit('verbose', ex.shortMessage); events.emit('verbose', ex.shortMessage);

View File

@ -64,6 +64,25 @@ describe('check_reqs', function () {
await expectAsync(check_reqs.check_java()).toBeResolvedTo({ version: '1.8.0' }); await expectAsync(check_reqs.check_java()).toBeResolvedTo({ version: '1.8.0' });
}); });
it('should return the correct version if javac prints _JAVA_OPTIONS', async () => {
check_reqs.__set__({
java: {
getVersion: async () => {
let version = null;
const javacOutput = 'Picked up _JAVA_OPTIONS: -Xms1024M -Xmx2048M\njavac 1.8.0_271';
const match = /javac\s+([\d.]+)/i.exec(javacOutput);
if (match && match[1]) {
version = match[1];
}
return { version };
}
}
});
await expectAsync(check_reqs.check_java()).toBeResolvedTo({ version: '1.8.0' });
});
}); });
describe('check_android', function () { describe('check_android', function () {