Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
May 9, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion bin/templates/cordova/lib/env/java.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,25 @@ const java = {
// Java <= 8 writes version info to stderr, Java >= 9 to stdout
let version = null;
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.
*/

version = /javac\s+([\d.]+)/i.exec(javacOutput)?.[1];
} catch (ex) {
events.emit('verbose', ex.shortMessage);

Expand Down
15 changes: 15 additions & 0 deletions spec/unit/check_reqs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ describe('check_reqs', function () {

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 () => {
const javacOutput = 'Picked up _JAVA_OPTIONS: -Xms1024M -Xmx2048M\njavac 1.8.0_271';
const version = /javac\s+([\d.]+)/i.exec(javacOutput)?.[1];

return { version };
}
}
});

await expectAsync(check_reqs.check_java()).toBeResolvedTo({ version: '1.8.0' });
});
});

describe('check_android', function () {
Expand Down