Skip to content

Commit

Permalink
Bugfix/java checks (apache#1228)
Browse files Browse the repository at this point in the history
* fix: Java version parsing if java executable prints out additional information with --version

* fix: Ensure JAVA_HOME path comes first in the PATH environment

* refactor: Removed redundent code in favour of keeping a change introduced from another PR
  • Loading branch information
breautek authored and wedgberto committed May 17, 2022
1 parent 0ebf18e commit 1829bdd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
9 changes: 4 additions & 5 deletions bin/templates/cordova/lib/env/java.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,15 @@ const java = {
return;
}

const javacPath = utils.forgivingWhichSync('javac');
const javaHome = environment.CORDOVA_JAVA_HOME || environment.JAVA_HOME;
if (javaHome) {
// Ensure that CORDOVA_JAVA_HOME overrides
environment.JAVA_HOME = javaHome;
// Windows java installer doesn't add javac to PATH, nor set JAVA_HOME (ugh).
if (!javacPath) {
environment.PATH += path.delimiter + path.join(environment.JAVA_HOME, 'bin');
}
// Ensure that the JAVA_HOME bin path is before anything else
// to cover cases where different Java versions is in the PATH
environment.PATH = path.join(environment.JAVA_HOME, 'bin') + path.delimiter + environment.PATH;
} else {
const javacPath = utils.forgivingWhichSync('javac');
if (javacPath) {
// OS X has a command for finding JAVA_HOME.
const find_java = '/usr/libexec/java_home';
Expand Down
14 changes: 12 additions & 2 deletions spec/unit/java.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,19 @@ describe('Java', () => {
all: 'javac 1.8.0_275'
}));

console.log('BEFORE', process.env.JAVA_HOME);
const result = await Java.getVersion();
console.log('AFTER', process.env.JAVA_HOME);
expect(result.major).toBe(1);
expect(result.minor).toBe(8);
expect(result.patch).toBe(0);
expect(result.version).toBe('1.8.0');
});

it('detects JDK when additional details are printed', async () => {
Java.__set__('execa', () => Promise.resolve({
all: 'Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8\njavac 1.8.0_275'
}));

const result = await Java.getVersion();
expect(result.major).toBe(1);
expect(result.minor).toBe(8);
expect(result.patch).toBe(0);
Expand Down

0 comments on commit 1829bdd

Please sign in to comment.