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 2019/11/06 19:20:14 UTC

[cordova-lib] branch master updated: Extend and improve plugin tests in preparation of supporting scoped plugins (#818)

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-lib.git


The following commit(s) were added to refs/heads/master by this push:
     new 8b2444e  Extend and improve plugin tests in preparation of supporting scoped plugins (#818)
8b2444e is described below

commit 8b2444e906931972a0223d139466c964faf8e376
Author: Raphael von der Grün <ra...@gmail.com>
AuthorDate: Wed Nov 6 20:20:05 2019 +0100

    Extend and improve plugin tests in preparation of supporting scoped plugins (#818)
    
    * Extend tests for addHelper.installPluginsForNewPlatform
    
    Co-authored-by: Raphael von der Grün <ra...@gmail.com>
    
    * Test plugin/add.getFetchVersion w/ scoped plugins
    
    Co-authored-by: Raphael von der Grün <ra...@gmail.com>
    
    * Add passing unit tests for plugman/util/metadata
    
    Co-authored-by: Raphael von der Grün <ra...@gmail.com>
    
    * Simplify and extend unit tests for cordova/util.findPlugins
    
    * Less strict path comparison in plugman_fetch.spec
---
 integration-tests/plugman_fetch.spec.js |   7 +-
 spec/cordova/platform/addHelper.spec.js |  62 ++++++++++++---
 spec/cordova/plugin/add.spec.js         |   4 +-
 spec/cordova/util.spec.js               |  62 +++++++--------
 spec/helpers.js                         |   9 +++
 spec/plugman/util/metadata.spec.js      | 132 ++++++++++++++++++++++++++++++++
 6 files changed, 230 insertions(+), 46 deletions(-)

diff --git a/integration-tests/plugman_fetch.spec.js b/integration-tests/plugman_fetch.spec.js
index 10874d2..ac17410 100644
--- a/integration-tests/plugman_fetch.spec.js
+++ b/integration-tests/plugman_fetch.spec.js
@@ -29,6 +29,7 @@ var test_pkgjson_plugin = path.join(plugins_dir, 'pkgjson-test-plugin');
 var test_plugin_searchpath = path.join(test_plugin, '..');
 var test_plugin_id = 'org.test.plugins.childbrowser';
 var test_plugin_version = '0.6.0';
+const { asymmetricMatchers: { pathNormalizingTo } } = require('../spec/helpers');
 
 describe('fetch', function () {
     describe('local plugins', function () {
@@ -65,7 +66,11 @@ describe('fetch', function () {
 
         it('Test 002 : should copy locally-available plugin to plugins directory when adding a plugin with searchpath argument', function () {
             return fetch(test_plugin_id, temp, { searchpath: test_plugin_searchpath }).then(function () {
-                expect(fs.copySync).toHaveBeenCalledWith(test_plugin, path.join(temp, test_plugin_id), jasmine.objectContaining({ dereference: true }));
+                expect(fs.copySync).toHaveBeenCalledWith(
+                    pathNormalizingTo(test_plugin),
+                    path.join(temp, test_plugin_id),
+                    jasmine.objectContaining({ dereference: true })
+                );
             });
         });
         it('Test 003 : should create a symlink if used with `link` param', function () {
diff --git a/spec/cordova/platform/addHelper.spec.js b/spec/cordova/platform/addHelper.spec.js
index 5bf4c10..c395d58 100644
--- a/spec/cordova/platform/addHelper.spec.js
+++ b/spec/cordova/platform/addHelper.spec.js
@@ -297,32 +297,70 @@ describe('cordova/platform/addHelper', function () {
     });
     describe('installPluginsForNewPlatform', function () {
         beforeEach(function () {
-            spyOn(fetch_metadata, 'get_fetch_metadata');
             spyOn(plugman, 'install').and.returnValue(Promise.resolve());
+            spyOn(cordova_util, 'findPlugins').and.returnValue(['cordova-plugin-whitelist']);
+            spyOn(fetch_metadata, 'get_fetch_metadata').and.returnValue({});
             platform_addHelper.installPluginsForNewPlatform.and.callThrough();
         });
 
+        // Call installPluginsForNewPlatform with some preset test arguments
+        function installPluginsForNewPlatformWithTestArgs () {
+            return platform_addHelper.installPluginsForNewPlatform('atari', projectRoot, {});
+        }
+
         it('should immediately return if there are no plugins to install into the platform', function () {
-            return platform_addHelper.installPluginsForNewPlatform('android', projectRoot).then(function () {
+            cordova_util.findPlugins.and.returnValue([]);
+
+            return installPluginsForNewPlatformWithTestArgs().then(() => {
                 expect(plugman.install).not.toHaveBeenCalled();
             });
         });
 
         it('should invoke plugman.install, giving correct platform, plugin and other arguments', function () {
-            spyOn(cordova_util, 'findPlugins').and.returnValue(['cordova-plugin-whitelist']);
-            fetch_metadata.get_fetch_metadata.and.returnValue({ });
-            return platform_addHelper.installPluginsForNewPlatform('browser', projectRoot, { save: true }).then(function () {
-                expect(plugman.install).toHaveBeenCalled();
-                expect(events.emit).toHaveBeenCalledWith('verbose', 'Installing plugin "cordova-plugin-whitelist" following successful platform add of browser');
+            return installPluginsForNewPlatformWithTestArgs().then(() => {
+                expect(events.emit).toHaveBeenCalledWith(
+                    'verbose',
+                    'Installing plugin "cordova-plugin-whitelist" following successful platform add of atari'
+                );
+                expect(plugman.install).toHaveBeenCalledTimes(1);
+                expect(plugman.install).toHaveBeenCalledWith(
+                    'atari',
+                    path.normalize('/some/path/platforms/atari'),
+                    'cordova-plugin-whitelist',
+                    path.normalize('/some/path/plugins'),
+                    {
+                        searchpath: undefined,
+                        usePlatformWww: true,
+                        is_top_level: undefined,
+                        force: undefined,
+                        save: false
+                    }
+                );
+            });
+        });
+
+        it('should properly signal a top level plugin to plugman.install,', () => {
+            fetch_metadata.get_fetch_metadata.and.returnValue({ is_top_level: true });
+
+            return installPluginsForNewPlatformWithTestArgs().then(() => {
+                expect(plugman.install).toHaveBeenCalledTimes(1);
+                const installOptions = plugman.install.calls.argsFor(0)[4];
+                expect(installOptions.is_top_level).toBe(true);
             });
         });
 
         it('should include any plugin variables as options when invoking plugman install', function () {
-            spyOn(cordova_util, 'findPlugins').and.returnValue(['cordova-plugin-camera']);
-            fetch_metadata.get_fetch_metadata.and.returnValue({ source: {}, variables: {} });
-            return platform_addHelper.installPluginsForNewPlatform('browser', projectRoot, { save: true }).then(function () {
-                expect(plugman.install).toHaveBeenCalled();
-                expect(events.emit).toHaveBeenCalledWith('verbose', 'Found variables for "cordova-plugin-camera". Processing as cli_variables.');
+            const variables = {};
+            fetch_metadata.get_fetch_metadata.and.returnValue({ variables });
+
+            return installPluginsForNewPlatformWithTestArgs().then(() => {
+                expect(events.emit).toHaveBeenCalledWith(
+                    'verbose',
+                    'Found variables for "cordova-plugin-whitelist". Processing as cli_variables.'
+                );
+                expect(plugman.install).toHaveBeenCalledTimes(1);
+                const installOptions = plugman.install.calls.argsFor(0)[4];
+                expect(installOptions.cli_variables).toBe(variables);
             });
         });
     });
diff --git a/spec/cordova/plugin/add.spec.js b/spec/cordova/plugin/add.spec.js
index 28171ae..99e9141 100644
--- a/spec/cordova/plugin/add.spec.js
+++ b/spec/cordova/plugin/add.spec.js
@@ -381,7 +381,7 @@ describe('cordova/plugin/add', function () {
                     });
             });
             it('should retrieve installed plugins and installed platforms version and feed that information into determinePluginVersionToFetch', function () {
-                plugin_util.getInstalledPlugins.and.returnValue([{ 'id': 'cordova-plugin-camera', 'version': '2.0.0' }]);
+                plugin_util.getInstalledPlugins.and.returnValue([{ 'id': '@cordova/plugin-thinger', 'version': '2.0.0' }]);
                 cordova_util.getInstalledPlatformsWithVersions.and.returnValue(Promise.resolve({ 'android': '6.0.0' }));
                 pluginInfo.engines = {};
                 pluginInfo.engines.cordovaDependencies = { '^1.0.0': { 'cordova': '>7.0.0' } };
@@ -389,7 +389,7 @@ describe('cordova/plugin/add', function () {
                     .then(function () {
                         expect(plugin_util.getInstalledPlugins).toHaveBeenCalledWith(projectRoot);
                         expect(cordova_util.getInstalledPlatformsWithVersions).toHaveBeenCalledWith(projectRoot);
-                        expect(add.determinePluginVersionToFetch).toHaveBeenCalledWith(pluginInfo, { 'cordova-plugin-camera': '2.0.0' }, { 'android': '6.0.0' }, '7.0.0');
+                        expect(add.determinePluginVersionToFetch).toHaveBeenCalledWith(pluginInfo, { '@cordova/plugin-thinger': '2.0.0' }, { 'android': '6.0.0' }, '7.0.0');
                     });
             });
         });
diff --git a/spec/cordova/util.spec.js b/spec/cordova/util.spec.js
index 653bd77..0dcfdb4 100644
--- a/spec/cordova/util.spec.js
+++ b/spec/cordova/util.spec.js
@@ -108,42 +108,42 @@ describe('util module', function () {
         });
     });
     describe('findPlugins method', function () {
+        let pluginsDir, plugins;
+
+        function expectFindPluginsToReturn (expectedPlugins) {
+            expect(util.findPlugins(pluginsDir))
+                .toEqual(jasmine.arrayWithExactContents(expectedPlugins));
+        }
+
+        beforeEach(function () {
+            pluginsDir = path.join(temp, 'plugins');
+            plugins = ['foo', 'bar', 'baz'];
+
+            plugins.forEach(plugin => {
+                fs.ensureDirSync(path.join(pluginsDir, plugin));
+            });
+        });
+
         it('Test 011 : should only return plugin directories present in a cordova project dir', function () {
-            var plugins = path.join(temp, 'plugins');
-            var android = path.join(plugins, 'android');
-            var ios = path.join(plugins, 'ios');
-            var wp8_dir = path.join(plugins, 'wp8');
-            var atari = path.join(plugins, 'atari');
-            fs.ensureDirSync(android);
-            fs.ensureDirSync(ios);
-            fs.ensureDirSync(wp8_dir);
-            fs.ensureDirSync(atari);
-            var res = util.findPlugins(plugins);
-            expect(res.length).toEqual(4);
+            expectFindPluginsToReturn(plugins);
         });
+
         it('Test 012 : should not return ".svn" directories', function () {
-            var plugins = path.join(temp, 'plugins');
-            var android = path.join(plugins, 'android');
-            var ios = path.join(plugins, 'ios');
-            var svn = path.join(plugins, '.svn');
-            fs.ensureDirSync(android);
-            fs.ensureDirSync(ios);
-            fs.ensureDirSync(svn);
-            var res = util.findPlugins(plugins);
-            expect(res.length).toEqual(2);
-            expect(res.indexOf('.svn')).toEqual(-1);
+            fs.ensureDirSync(path.join(pluginsDir, '.svn'));
+            expectFindPluginsToReturn(plugins);
         });
+
         it('Test 013 : should not return "CVS" directories', function () {
-            var plugins = path.join(temp, 'plugins');
-            var android = path.join(plugins, 'android');
-            var ios = path.join(plugins, 'ios');
-            var cvs = path.join(plugins, 'CVS');
-            fs.ensureDirSync(android);
-            fs.ensureDirSync(ios);
-            fs.ensureDirSync(cvs);
-            var res = util.findPlugins(plugins);
-            expect(res.length).toEqual(2);
-            expect(res.indexOf('CVS')).toEqual(-1);
+            fs.ensureDirSync(path.join(pluginsDir, 'CVS'));
+            expectFindPluginsToReturn(plugins);
+        });
+
+        it('Test 031 : should return plugin symlinks', function () {
+            const linkedPluginPath = path.join(temp, 'linked-plugin');
+            const pluginLinkPath = path.join(pluginsDir, 'plugin-link');
+            fs.ensureDirSync(linkedPluginPath);
+            fs.ensureSymlinkSync(linkedPluginPath, pluginLinkPath);
+            expectFindPluginsToReturn(plugins.concat('plugin-link'));
         });
     });
 
diff --git a/spec/helpers.js b/spec/helpers.js
index 87a70b3..da80dee 100644
--- a/spec/helpers.js
+++ b/spec/helpers.js
@@ -189,6 +189,15 @@ module.exports.writeConfigContent = function (appPath, configContent) {
     fs.writeFileSync(configFile, configContent, 'utf-8');
 };
 
+module.exports.asymmetricMatchers = {
+    pathNormalizingTo (expectedPath) {
+        return {
+            asymmetricMatch: actualPath => path.normalize(actualPath) === expectedPath,
+            jasmineToString: _ => `<pathNormalizingTo(${expectedPath})>`
+        };
+    }
+};
+
 const customMatchers = {
     toExist: () => ({ compare (file) {
         const pass = fs.existsSync(file);
diff --git a/spec/plugman/util/metadata.spec.js b/spec/plugman/util/metadata.spec.js
new file mode 100644
index 0000000..f7dda9a
--- /dev/null
+++ b/spec/plugman/util/metadata.spec.js
@@ -0,0 +1,132 @@
+/*!
+    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 path = require('path');
+const rewire = require('rewire');
+
+const pluginsDir = path.normalize('/plugins_dir/');
+const fetchJsonPath = path.join(pluginsDir, 'fetch.json');
+
+let fetchJson = null;
+const fsMock = {
+    readFileSync: () => fetchJson,
+    existsSync: () => fetchJson !== null,
+    writeFileSync (_, data) { fetchJson = data; },
+    unlinkSync () { fetchJson = null; }
+};
+
+// expect fsMock to only operate on fetchJsonPath
+Object.entries(fsMock).map(([key, fn]) => {
+    fsMock[key] = (...args) => {
+        expect(args[0]).toBe(fetchJsonPath);
+        return fn(...args);
+    };
+});
+
+describe('plugman.metadata', () => {
+    const TEST_PLUGIN = 'cordova-plugin-thinger';
+    let metadata;
+
+    beforeEach(() => {
+        metadata = rewire('../../../src/plugman/util/metadata');
+        metadata.__set__('fs', fsMock);
+        fetchJson = JSON.stringify({
+            [TEST_PLUGIN]: { metadata: 'matches' }
+        });
+    });
+
+    describe('get_fetch_metadata', () => {
+        const get_fetch_metadata = pluginId =>
+            metadata.get_fetch_metadata(path.join(pluginsDir, pluginId));
+
+        it('should return an empty object if there is no record', () => {
+            fetchJson = null;
+            expect(get_fetch_metadata(TEST_PLUGIN)).toEqual({});
+        });
+
+        it('should return the fetch metadata in plugins_dir/fetch.json if it is there', () => {
+            expect(get_fetch_metadata(TEST_PLUGIN)).toEqual({ metadata: 'matches' });
+        });
+
+        describe('cache behaviour', () => {
+            beforeEach(() => {
+                spyOn(fsMock, 'readFileSync').and.callThrough();
+                spyOn(fsMock, 'existsSync').and.callThrough();
+            });
+
+            it('with no cache, it should read from the filesystem', () => {
+                expect(get_fetch_metadata(TEST_PLUGIN)).toEqual({ metadata: 'matches' });
+                expect(fsMock.existsSync).toHaveBeenCalled();
+                expect(fsMock.readFileSync).toHaveBeenCalled();
+            });
+
+            it('with a cache, it should read from the cache', () => {
+                metadata.__set__('cachedJson', {
+                    'cordova-plugin-thinger': { metadata: 'cached' }
+                });
+
+                expect(get_fetch_metadata(TEST_PLUGIN)).toEqual({ metadata: 'cached' });
+                expect(fsMock.existsSync).not.toHaveBeenCalled();
+                expect(fsMock.readFileSync).not.toHaveBeenCalled();
+            });
+
+        });
+    });
+
+    describe('save_fetch_metadata', () => {
+        const save_fetch_metadata = (...args) =>
+            metadata.save_fetch_metadata(pluginsDir, ...args);
+
+        it('should save plugin metadata to a new fetch.json', () => {
+            fetchJson = null;
+            const meta = { metadata: 'saved' };
+
+            save_fetch_metadata('@cordova/plugin-thinger', meta);
+
+            expect(JSON.parse(fetchJson)).toEqual({
+                '@cordova/plugin-thinger': meta
+            });
+        });
+
+        it('should save plugin metadata to an existing fetch.json', () => {
+            const meta = { metadata: 'saved' };
+            const oldFetchJson = {
+                'some-other-plugin': { metadata: 'not-touched' }
+            };
+            fetchJson = JSON.stringify(oldFetchJson);
+
+            save_fetch_metadata('@cordova/plugin-thinger', meta);
+
+            expect(JSON.parse(fetchJson)).toEqual({
+                '@cordova/plugin-thinger': meta,
+                ...oldFetchJson
+            });
+        });
+    });
+
+    describe('remove_fetch_metadata', () => {
+        const remove_fetch_metadata = (...args) =>
+            metadata.remove_fetch_metadata(pluginsDir, ...args);
+
+        it('should remove metadata', () => {
+            remove_fetch_metadata(TEST_PLUGIN);
+            expect(JSON.parse(fetchJson)).toEqual({});
+        });
+    });
+});


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