erisu commented on a change in pull request #1130: URL: https://github.com/apache/cordova-android/pull/1130#discussion_r528295551
########## File path: spec/unit/Java.spec.js ########## @@ -0,0 +1,156 @@ +/** + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you under the Apache License, Version 2.0 (the + "License"); you may not use this file except in compliance + with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, + software distributed under the License is distributed on an + "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + KIND, either express or implied. See the License for the + specific language governing permissions and limitations + under the License. +*/ + +const rewire = require('rewire'); +const { CordovaError } = require('cordova-common'); +const utils = require('../../bin/templates/cordova/lib/utils'); + +describe('Java', () => { + const Java = rewire('../../bin/templates/cordova/lib/Java'); + let originalEnv = null; + + beforeAll(() => { + originalEnv = Object.assign({}, process.env); + }); + + afterAll(() => { + process.env = Object.assign({}, originalEnv); + }); + + describe('getVersion', () => { + beforeEach(() => { + process.env = Object.assign({}, originalEnv); + }); + + it('runs', async () => { + Java.__set__('execa', () => Promise.resolve({ + all: 'javac 1.8.0_275' + })); + + const versionObj = { + major: 1, + minor: 8, + patch: 0, + build: 275, + string: '1.8.0_275' + }; + + await expectAsync(Java.getVersion()).toBeResolvedTo(versionObj); + }); + + it('produces a CordovaError on error', async () => { + Java.__set__('execa', () => Promise.reject({ + shortMessage: 'test error' + })); + const emitSpy = jasmine.createSpy('events.emit'); + Java.__set__('events', { + emit: emitSpy + }); + + await expectAsync(Java.getVersion()).toBeRejectedWithError(CordovaError, /Failed to run "javac -version"/); + expect(emitSpy).toHaveBeenCalledWith('verbose', 'test error'); + }); + }); + + describe('_ensure', () => { + beforeEach(() => { + process.env = Object.assign({}, originalEnv); + Java.__set__('javaIsEnsured', false); + }); + + it('with JAVA_HOME / without javac', async () => { + spyOn(utils, 'forgivingWhichSync').and.returnValue(''); + process.env.JAVA_HOME = '/tmp/jdk'; + + await Java._ensure(); + + expect(process.env.PATH).toMatch(/\/tmp\/jdk+(\/|\\)bin/); + }); + + it('without JAVA_HOME / with javac - Mac OS X - success', async () => { + spyOn(utils, 'forgivingWhichSync').and.returnValue('/tmp/jdk/bin'); + process.env.JAVA_HOME = ''; + const fsSpy = jasmine.createSpy('fs').and.returnValue(true); + Java.__set__('fs', { + existsSync: fsSpy + }); + Java.__set__('execa', jasmine.createSpy('execa').and.returnValue(Promise.resolve({ + stdout: '/tmp/jdk' + }))); + + await Java._ensure(); + + expect(fsSpy).toHaveBeenCalledWith('/usr/libexec/java_home'); + expect(process.env.JAVA_HOME).toBe('/tmp/jdk'); + }); + + it('without JAVA_HOME / with javac - Mac OS X - error', async () => { + spyOn(utils, 'forgivingWhichSync').and.returnValue('/tmp/jdk/bin'); + process.env.JAVA_HOME = ''; + Java.__set__('fs', { + existsSync: jasmine.createSpy('fs').and.returnValue(true) + }); + Java.__set__('execa', jasmine.createSpy('execa').and.returnValue(Promise.reject({ + shortMessage: 'test error' + }))); + const emitSpy = jasmine.createSpy('events.emit'); + Java.__set__('events', { + emit: emitSpy + }); + + await expectAsync(Java._ensure()).toBeRejectedWithError(CordovaError, /Failed to find 'JAVA_HOME' environment variable/); + + expect(emitSpy).toHaveBeenCalledWith('verbose', 'test error'); + }); + + it('derive from javac location - success', async () => { + spyOn(utils, 'forgivingWhichSync').and.returnValue('/tmp/jdk/bin'); + process.env.JAVA_HOME = ''; + Java.__set__('fs', { + existsSync: jasmine.createSpy('fs').and.callFake((path) => { + if (/java_home$/.test(path)) { + return false; + } else { + return true; + } + }) + }); + + await Java._ensure(); + + expect(process.env.JAVA_HOME).toBe('/tmp'); + }); + + it('derive from javac location - error', async () => { + spyOn(utils, 'forgivingWhichSync').and.returnValue('/tmp/jdk/bin'); + process.env.JAVA_HOME = ''; + Java.__set__('fs', { + existsSync: jasmine.createSpy('fs').and.callFake((path) => { + if (/java_home$/.test(path)) { + return false; + } else { + return false; + } Review comment: Always returns `false`? Seems like the `if` condition is not needed. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
