Skip to content
Permalink
Browse files
Merge pull request #761 from raphinesse/misc-test-improvements
Various test improvements
  • Loading branch information
raphinesse committed Apr 1, 2019
2 parents 6aed5d0 + 8271fae commit c017729de6050145da02023d9dfe7a3fa3a3d196
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 119 deletions.
@@ -26,7 +26,7 @@ const HooksRunner = require('../src/hooks/HooksRunner');
const cordovaUtil = require('../src/cordova/util');
const cordova = require('../src/cordova/cordova');
const { tmpDir, testPlatform } = require('../spec/helpers');
const { PluginInfo, superspawn } = require('cordova-common');
const { PluginInfo } = require('cordova-common');
const { Q_chainmap } = require('../src/util/promise-util');

const tmp = tmpDir('hooks_test');
@@ -252,19 +252,6 @@ describe('HooksRunner', function () {
});

describe('plugin hooks', function () {
beforeEach(function () {
spyOn(superspawn, 'spawn').and.callFake(function (cmd, args) {
if (cmd.match(/create\b/)) {
// This is a call to the bin/create script, so do the copy ourselves.
fs.copySync(path.join(fixtures, 'platforms/android'), path.join(project, 'platforms/android'));
} else if (cmd.match(/update\b/)) {
fs.writeFileSync(path.join(project, 'platforms', testPlatform, 'updated'), 'I was updated!', 'utf-8');
} else if (cmd.match(/version/)) {
return '3.6.0';
}
return Promise.resolve();
});
});

it('Test 009 : should execute hook scripts serially from plugin.xml', function () {
usePluginConfig('OnlyNonPlatformScripts');
@@ -57,8 +57,6 @@ describe('fetch', function () {

describe('local plugins', function () {
var sym;
var revertLocal;
var revertFetch;
var fetchCalls = 0;

beforeEach(function () {
@@ -69,16 +67,14 @@ describe('fetch', function () {
spyOn(fs, 'copySync').and.callThrough();
spyOn(metadata, 'save_fetch_metadata');

revertLocal = fetch.__set__('localPlugins', null);
revertFetch = fetch.__set__('fetch', function (pluginDir) {
fetch.__set__('localPlugins', null);
fetch.__set__('fetch', function (pluginDir) {
fetchCalls++;
return Promise.resolve(pluginDir);
});
});

afterEach(function () {
revertLocal();
revertFetch();
fetchCalls = 0;
});

Empty file.
Empty file.
@@ -20,19 +20,15 @@ var platform = rewire('../../../src/cordova/platform');
var cordova_util = require('../../../src/cordova/util');

describe('cordova/platform', function () {
var hooksRunnerRevert;
var projectRoot = 'somepath';

beforeEach(function () {
// TODO: if we can change HooksRunner from a prototypal class to a function or object,
// we could eliminate the need for rewire here and use just jasmine spies.
hooksRunnerRevert = platform.__set__('HooksRunner', function () {});
platform.__set__('HooksRunner', function () {});
spyOn(cordova_util, 'cdProjectRoot').and.returnValue(projectRoot);
});

afterEach(function () {
hooksRunnerRevert();
});

describe('main module function', function () {
describe('error/warning conditions', function () {
// TODO: what about other commands? update? save?
@@ -31,17 +31,15 @@ describe('cordova/plugin/add', function () {
var projectRoot = '/some/path';
var hook_mock;
var Cfg_parser_mock = function () {};
var cfg_parser_revert_mock;
var plugin_info_provider_mock = function () {};
var plugin_info_provider_revert_mock;
var plugin_info;
var package_json_mock;

beforeEach(function () {
hook_mock = jasmine.createSpyObj('hooks runner mock', ['fire']);
hook_mock.fire.and.returnValue(Promise.resolve());
Cfg_parser_mock.prototype = jasmine.createSpyObj('config parser prototype mock', ['getPlugin']);
cfg_parser_revert_mock = add.__set__('ConfigParser', Cfg_parser_mock);
add.__set__('ConfigParser', Cfg_parser_mock);
plugin_info = jasmine.createSpyObj('pluginInfo', ['getPreferences']);
plugin_info.getPreferences.and.returnValue({});
plugin_info.dir = 'some\\plugin\\path';
@@ -52,7 +50,7 @@ describe('cordova/plugin/add', function () {
// id version dir getPreferences() engines engines.cordovaDependencies name versions
return plugin_info;
};
plugin_info_provider_revert_mock = add.__set__('PluginInfoProvider', plugin_info_provider_mock);
add.__set__('PluginInfoProvider', plugin_info_provider_mock);
spyOn(fs, 'existsSync').and.returnValue(false);
spyOn(fs, 'writeFileSync');
package_json_mock = jasmine.createSpyObj('package json mock', ['cordova', 'dependencies', 'devDependencies']);
@@ -66,10 +64,7 @@ describe('cordova/plugin/add', function () {
spyOn(add, 'getFetchVersion').and.returnValue(Promise.resolve());
spyOn(plugin_util, 'saveToConfigXmlOn').and.returnValue(true);
});
afterEach(function () {
cfg_parser_revert_mock();
plugin_info_provider_revert_mock();
});

describe('main method', function () {

beforeEach(function () {
@@ -32,10 +32,8 @@ describe('cordova/plugin/remove', function () {
var projectRoot = '/some/path';
var hook_mock;
var cfg_parser_mock = function () {};
var cfg_parser_revert_mock;
var package_json_mock;
var plugin_info_provider_mock = function () {};
var plugin_info_provider_revert_mock;
var plugin_info;
package_json_mock = jasmine.createSpyObj('package json mock', ['cordova', 'dependencies']);
package_json_mock.dependencies = {};
@@ -53,18 +51,13 @@ describe('cordova/plugin/remove', function () {
spyOn(prepare, 'preparePlatforms').and.returnValue(true);
hook_mock.fire.and.returnValue(Promise.resolve());
cfg_parser_mock.prototype = jasmine.createSpyObj('config parser mock', ['write', 'getPlugin', 'removePlugin']);
cfg_parser_revert_mock = remove.__set__('ConfigParser', cfg_parser_mock);
remove.__set__('ConfigParser', cfg_parser_mock);
plugin_info_provider_mock.prototype = jasmine.createSpyObj('plugin info provider mock', ['get', 'getPreferences']);
plugin_info_provider_mock.prototype.get = function (directory) {
// id version dir getPreferences() engines engines.cordovaDependencies name versions
return plugin_info;
};
plugin_info_provider_revert_mock = remove.__set__('PluginInfoProvider', plugin_info_provider_mock);
});

afterEach(function () {
cfg_parser_revert_mock();
plugin_info_provider_revert_mock();
remove.__set__('PluginInfoProvider', plugin_info_provider_mock);
});

describe('error/warning conditions', function () {
@@ -26,26 +26,21 @@ var semver = require('semver');
describe('cordova/plugin/save', function () {
var projectRoot = '/some/path';
var cfg_parser_mock = function () {};
var cfg_parser_revert_mock;
var fake_plugin_list = ['VRPlugin', 'MastodonSocialPlugin'];
var fake_fetch_json = { 'VRPlugin': {}, 'MastodonSocialPlugin': {} };
var plugin_info_provider_mock = function () {};
var plugin_info_provider_revert_mock;

beforeEach(function () {
cfg_parser_mock.prototype = jasmine.createSpyObj('config parser protytpe mock', ['getPluginIdList', 'removePlugin', 'write', 'addPlugin']);
cfg_parser_mock.prototype.getPluginIdList.and.returnValue(fake_plugin_list);
cfg_parser_revert_mock = save.__set__('ConfigParser', cfg_parser_mock);
save.__set__('ConfigParser', cfg_parser_mock);
spyOn(cordova_util, 'projectConfig').and.returnValue(projectRoot + '/config.xml');
spyOn(fs, 'readFileSync').and.returnValue(JSON.stringify(fake_fetch_json));
spyOn(save, 'versionString');
plugin_info_provider_mock.prototype = jasmine.createSpyObj('plugin info provider mock', ['get']);
plugin_info_provider_revert_mock = save.__set__('PluginInfoProvider', plugin_info_provider_mock);
});
afterEach(function () {
cfg_parser_revert_mock();
plugin_info_provider_revert_mock();
save.__set__('PluginInfoProvider', plugin_info_provider_mock);
});

describe('error conditions', function () {
it('should explode if there was an issue parsing or reading from fetch.json file', function () {
fs.readFileSync.and.callFake(function () {
@@ -24,20 +24,14 @@ var events = require('cordova-common').events;

describe('cordova/plugin/util', function () {
var plugin_info_mock = function () {};
var plugin_info_revert_mock;
var cfg_parser_revert_mock;
var cfg_parser_mock = function () {};
beforeEach(function () {
spyOn(fs, 'removeSync');
spyOn(events, 'emit');
cfg_parser_mock.prototype = jasmine.createSpyObj('config parser protytpe mock', ['getPlugin']);
cfg_parser_revert_mock = plugin_util.__set__('ConfigParser', cfg_parser_mock);
plugin_util.__set__('ConfigParser', cfg_parser_mock);
plugin_info_mock.prototype = jasmine.createSpyObj('plugin info provider prototype mock', ['getAllWithinSearchPath', 'getPreferences']);
plugin_info_revert_mock = plugin_util.__set__('PluginInfoProvider', plugin_info_mock);
});
afterEach(function () {
plugin_info_revert_mock();
cfg_parser_revert_mock();
plugin_util.__set__('PluginInfoProvider', plugin_info_mock);
});
describe('getInstalledPlugins helper method', function () {
it('should return result of PluginInfoProvider\'s getAllWithinSearchPath method', function () {
@@ -137,27 +137,21 @@ describe('cordova/prepare', function () {

describe('preparePlatforms helper method', function () {
var cfg_parser_mock = function () {};
var cfg_parser_revert_mock;
var platform_munger_mock = function () {};
var platform_munger_revert_mock;
var platform_munger_save_mock;
beforeEach(function () {
spyOn(prepare, 'restoreMissingPluginsForPlatform').and.returnValue(Promise.resolve());
cfg_parser_revert_mock = prepare.__set__('ConfigParser', cfg_parser_mock);
prepare.__set__('ConfigParser', cfg_parser_mock);
platform_munger_save_mock = jasmine.createSpy('platform munger save mock');
platform_munger_mock.prototype = jasmine.createSpyObj('platform munger prototype mock', ['add_config_changes']);
platform_munger_mock.prototype.add_config_changes.and.returnValue({
save_all: platform_munger_save_mock
});
platform_munger_revert_mock = prepare.__set__('PlatformMunger', platform_munger_mock);
prepare.__set__('PlatformMunger', platform_munger_mock);
spyOn(util, 'projectConfig').and.returnValue(project_dir);
spyOn(util, 'projectWww').and.returnValue(path.join(project_dir, 'www'));

});
afterEach(function () {
cfg_parser_revert_mock();
platform_munger_revert_mock();
});
it('should call restoreMissingPluginsForPlatform', function () {
return prepare.preparePlatforms(['android'], project_dir, {}).then(function () {
expect(prepare.restoreMissingPluginsForPlatform).toHaveBeenCalled();
@@ -22,85 +22,66 @@ var fs = require('fs-extra');
var util = require('../../src/cordova/util');
var events = require('../../cordova-lib').events;
var helpers = require('../helpers');
var temp = path.join(__dirname, '..', 'temp');

var cwd = process.cwd();
var home = process.env[(process.platform === 'win32') ? 'USERPROFILE' : 'HOME'];
var origPWD = process.env['PWD'];

describe('util module', function () {
let temp;
beforeEach(() => {
temp = helpers.tmpDir('cordova.util.spec');
});
afterEach(() => {
process.chdir(__dirname);
fs.removeSync(temp);
});

describe('isCordova method', function () {
let somedir, anotherdir;
beforeEach(() => {
// Base test directory setup
somedir = path.join(temp, 'somedir');
anotherdir = path.join(somedir, 'anotherdir');
fs.ensureDirSync(anotherdir);
});
afterEach(function () {
process.env['PWD'] = origPWD;
process.chdir(cwd);
});
function removeDir (directory) {
fs.removeSync(directory);
}
it('Test 001 : should return false if it hits the home directory', function () {
var somedir = path.join(home, 'somedir');
removeDir(somedir);
fs.ensureDirSync(somedir);
it('Test 002 : should return false if it cannot find a Cordova project directory up the directory tree', function () {
expect(util.isCordova(somedir)).toEqual(false);
});
it('Test 002 : should return false if it cannot find a .cordova directory up the directory tree', function () {
var somedir = path.join(home, '..');
expect(util.isCordova(somedir)).toEqual(false);
});
it('Test 003 : should return the first directory it finds with a .cordova folder in it', function () {
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
fs.ensureDirSync(anotherdir);
it('Test 003 : should recognize a Cordova project directory', function () {
fs.ensureDirSync(path.join(somedir, 'www', 'config.xml'));
expect(util.isCordova(somedir)).toEqual(somedir);
});
it('Test 004 : should ignore PWD when its undefined', function () {
it('Test 004 : should ignore PWD when it is undefined', function () {
delete process.env['PWD'];
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(somedir, 'www'));
fs.ensureDirSync(path.join(somedir, 'config.xml'));
process.chdir(anotherdir);
expect(util.isCordova()).toEqual(somedir);
});
it('Test 005 : should use PWD when available', function () {
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(somedir, 'www', 'config.xml'));
process.env['PWD'] = anotherdir;
process.chdir(path.sep);
expect(util.isCordova()).toEqual(somedir);
});
it('Test 006 : should use cwd as a fallback when PWD is not a cordova dir', function () {
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(somedir, 'www', 'config.xml'));
process.env['PWD'] = path.sep;
process.chdir(anotherdir);
expect(util.isCordova()).toEqual(somedir);
});
it('Test 007 : should ignore platform www/config.xml', function () {
var somedir = path.join(home, 'somedir');
var anotherdir = path.join(somedir, 'anotherdir');
removeDir(somedir);
fs.ensureDirSync(anotherdir);
fs.ensureDirSync(path.join(anotherdir, 'www', 'config.xml'));
fs.ensureDirSync(path.join(somedir, 'www'));
fs.ensureDirSync(path.join(somedir, 'config.xml'));
expect(util.isCordova(anotherdir)).toEqual(somedir);
});
});
describe('deleteSvnFolders method', function () {
afterEach(function () {
fs.removeSync(temp);
});
it('Test 008 : should delete .svn folders in any subdirectory of specified dir', function () {
var one = path.join(temp, 'one');
var two = path.join(temp, 'two');
@@ -114,9 +95,6 @@ describe('util module', function () {
});
});
describe('listPlatforms method', function () {
afterEach(function () {
fs.removeSync(temp);
});
it('Test 009 : should only return supported platform directories present in a cordova project dir', function () {
var platforms = path.join(temp, 'platforms');

@@ -133,9 +111,6 @@ describe('util module', function () {
});
});
describe('getInstalledPlatformsWithVersions method', function () {
afterEach(function () {
fs.removeSync(temp);
});
it('Test 010 : should get the supported platforms in the cordova project dir along with their reported versions', function () {
var platforms = path.join(temp, 'platforms');
var android = path.join(platforms, 'android');
@@ -150,9 +125,6 @@ describe('util module', function () {
});
});
describe('findPlugins method', function () {
afterEach(function () {
fs.removeSync(temp);
});
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');
@@ -22,7 +22,6 @@ var underscore = require('underscore');

describe('mergeVariables', function () {
var plugin_info_provider_mock = function () {};
var plugin_info_provider_revert_mock;
var plugin_info;

beforeEach(function () {
@@ -34,10 +33,7 @@ describe('mergeVariables', function () {
plugin_info_provider_mock.prototype.get = function (directory) {
return plugin_info;
};
plugin_info_provider_revert_mock = variable_merge.__set__('PluginInfoProvider', plugin_info_provider_mock);
});
afterEach(function () {
plugin_info_provider_revert_mock();
variable_merge.__set__('PluginInfoProvider', plugin_info_provider_mock);
});
it('use plugin.xml if no cli/config variables', function () {
plugin_info.getPreferences.and.returnValue({ FCM_VERSION: '11.0.1' });

0 comments on commit c017729

Please sign in to comment.