You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ra...@apache.org on 2020/10/19 08:41:00 UTC

[cordova-android] branch master updated: test: fix unit test failures for certain random orders (#1094)

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

raphinesse 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 335b0f2  test: fix unit test failures for certain random orders (#1094)
335b0f2 is described below

commit 335b0f2575bdcb432583132f17b5f141c64e7c0b
Author: Raphael von der GrĂ¼n <ra...@gmail.com>
AuthorDate: Mon Oct 19 10:38:37 2020 +0200

    test: fix unit test failures for certain random orders (#1094)
    
    * test(Api): do not clobber global events.emit w/ spy
    
    * test(Api): remove unnecessary rewiring
    
    * test(check_reqs): add missing spyOn call
    
    * test(check_reqs): fix process.env restoration
    
    * test(check_reqs): restore module under test before each test
---
 spec/unit/Api.spec.js        | 14 ++++----------
 spec/unit/check_reqs.spec.js | 25 +++++++++++++------------
 2 files changed, 17 insertions(+), 22 deletions(-)

diff --git a/spec/unit/Api.spec.js b/spec/unit/Api.spec.js
index 19b417a..a7d1a93 100644
--- a/spec/unit/Api.spec.js
+++ b/spec/unit/Api.spec.js
@@ -20,8 +20,9 @@
 var os = require('os');
 var path = require('path');
 var common = require('cordova-common');
-var rewire = require('rewire');
+const EventEmitter = require('events');
 
+var Api = require('../../bin/templates/cordova/Api');
 var AndroidProject = require('../../bin/templates/cordova/lib/AndroidProject');
 
 var PluginInfo = common.PluginInfo;
@@ -31,11 +32,9 @@ var FAKE_PROJECT_DIR = path.join(os.tmpdir(), 'plugin-test-project');
 
 describe('Api', () => {
     describe('addPlugin method', function () {
-        var api, Api;
+        var api;
 
         beforeEach(function () {
-            Api = rewire('../../bin/templates/cordova/Api');
-
             var pluginManager = jasmine.createSpyObj('pluginManager', ['addPlugin']);
             pluginManager.addPlugin.and.resolveTo();
             spyOn(common.PluginManager, 'get').and.returnValue(pluginManager);
@@ -43,12 +42,7 @@ describe('Api', () => {
             var projectSpy = jasmine.createSpyObj('AndroidProject', ['getPackageName', 'write', 'isClean']);
             spyOn(AndroidProject, 'getProjectFile').and.returnValue(projectSpy);
 
-            Api.__set__('Api.prototype.clean', async () => {});
-
-            // Prevent logging to avoid polluting the test reports
-            Api.__set__('selfEvents.emit', jasmine.createSpy());
-
-            api = new Api('android', FAKE_PROJECT_DIR);
+            api = new Api('android', FAKE_PROJECT_DIR, new EventEmitter());
             spyOn(api._builder, 'prepBuildFiles');
         });
 
diff --git a/spec/unit/check_reqs.spec.js b/spec/unit/check_reqs.spec.js
index ff6a5ea..e5531cc 100644
--- a/spec/unit/check_reqs.spec.js
+++ b/spec/unit/check_reqs.spec.js
@@ -18,7 +18,6 @@
 */
 
 var rewire = require('rewire');
-var check_reqs = rewire('../../bin/templates/cordova/lib/check_reqs');
 var android_sdk = require('../../bin/templates/cordova/lib/android_sdk');
 var fs = require('fs-extra');
 var path = require('path');
@@ -29,14 +28,22 @@ var which = require('which');
 const DEFAULT_TARGET_API = 29;
 
 describe('check_reqs', function () {
+    let check_reqs;
+    beforeEach(() => {
+        check_reqs = rewire('../../bin/templates/cordova/lib/check_reqs');
+    });
+
     var original_env;
     beforeAll(function () {
-        original_env = Object.create(process.env);
+        original_env = Object.assign({}, process.env);
     });
     afterEach(function () {
-        Object.keys(original_env).forEach(function (k) {
-            process.env[k] = original_env[k];
+        // process.env has some special behavior, so we do not
+        // replace it but only restore its original properties
+        Object.keys(process.env).forEach(k => {
+            delete process.env[k];
         });
+        Object.assign(process.env, original_env);
     });
     describe('check_android', function () {
         describe('find and set ANDROID_HOME when ANDROID_HOME and ANDROID_SDK_ROOT is not set', function () {
@@ -55,9 +62,6 @@ describe('check_reqs', function () {
                     process.env.ProgramFiles = 'windows-program-files';
                     return check_reqs.check_android().then(function () {
                         expect(process.env.ANDROID_SDK_ROOT).toContain('windows-local-app-data');
-                    }).finally(function () {
-                        delete process.env.LOCALAPPDATA;
-                        delete process.env.ProgramFiles;
                     });
                 });
                 it('it should set ANDROID_SDK_ROOT on Darwin', () => {
@@ -66,8 +70,6 @@ describe('check_reqs', function () {
                     process.env.HOME = 'home is where the heart is';
                     return check_reqs.check_android().then(function () {
                         expect(process.env.ANDROID_SDK_ROOT).toContain('home is where the heart is');
-                    }).finally(function () {
-                        delete process.env.HOME;
                     });
                 });
             });
@@ -220,9 +222,6 @@ describe('check_reqs', function () {
                 process.env.ANDROID_SDK_ROOT = 'let the children play';
                 spyOn(fs, 'existsSync').and.returnValue(true);
             });
-            afterEach(function () {
-                delete process.env.ANDROID_SDK_ROOT;
-            });
             it('should add tools/bin,tools,platform-tools to PATH if `avdmanager`,`android`,`adb` is not found', () => {
                 return check_reqs.check_android().then(function () {
                     expect(process.env.PATH).toContain('let the children play' + path.sep + 'tools');
@@ -348,6 +347,8 @@ describe('check_reqs', function () {
                 }
             });
 
+            spyOn(events, 'emit');
+
             getPreferenceSpy.and.returnValue(DEFAULT_TARGET_API - 1);
 
             var target = check_reqs.get_target();


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