You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by no...@apache.org on 2021/05/09 12:33:18 UTC

[cordova-android] branch master updated: fix(requirements check): use regex to get java version from javac output (#1220)

This is an automated email from the ASF dual-hosted git repository.

normanbreau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cordova-android.git


The following commit(s) were added to refs/heads/master by this push:
     new a458043  fix(requirements check): use regex to get java version from javac output (#1220)
a458043 is described below

commit a45804329ba7bd50ebec8634ad0ffceab8e7aab9
Author: David <Da...@users.noreply.github.com>
AuthorDate: Sun May 9 14:33:08 2021 +0200

    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
---
 bin/templates/cordova/lib/env/java.js | 23 ++++++++++++++++++++++-
 spec/unit/check_reqs.spec.js          | 19 +++++++++++++++++++
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/bin/templates/cordova/lib/env/java.js b/bin/templates/cordova/lib/env/java.js
index 6eed6f6..90d5340 100644
--- a/bin/templates/cordova/lib/env/java.js
+++ b/bin/templates/cordova/lib/env/java.js
@@ -43,7 +43,28 @@ 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.
+            */
+
+            const match = /javac\s+([\d.]+)/i.exec(javacOutput);
+            if (match && match[1]) {
+                version = match[1];
+            }
         } catch (ex) {
             events.emit('verbose', ex.shortMessage);
 
diff --git a/spec/unit/check_reqs.spec.js b/spec/unit/check_reqs.spec.js
index 6547200..68c3d38 100644
--- a/spec/unit/check_reqs.spec.js
+++ b/spec/unit/check_reqs.spec.js
@@ -64,6 +64,25 @@ 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 () => {
+                        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 () {

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org