refactor(env/java): improve tests and implementation (#1246)

This basically fixes up the changes from #1220.

* test(env/java): replace test that duplicates implementation
* test(env/java): stub _ensure to focus on unit under test
* test(env/java): add test for invalid output
* refactor(env/java): keep try block small
* refactor(env/java): shorten excessive comment
This commit is contained in:
Raphael von der Grün
2021-06-23 18:22:04 +02:00
committed by GitHub
parent 6d803e2f72
commit 0f13f4a5ac
3 changed files with 27 additions and 56 deletions
+7 -24
View File
@@ -41,30 +41,9 @@ const java = {
await java._ensure(process.env);
// Java <= 8 writes version info to stderr, Java >= 9 to stdout
let version = null;
let javacOutput;
try {
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];
}
javacOutput = (await execa('javac', ['-version'], { all: true })).all;
} catch (ex) {
events.emit('verbose', ex.shortMessage);
@@ -79,7 +58,11 @@ const java = {
throw new CordovaError(msg);
}
return semver.coerce(version);
// We have to filter the output, because javac prints _JAVA_OPTIONS
// before printing the version which causes semver.coerce to fail to get
// the correct version if those options contain any numbers.
const match = /javac\s+([\d.]+)/i.exec(javacOutput);
return semver.coerce(match && match[1]);
},
/**