Skip to content
Permalink
Browse files
Factor out common project test helpers
  • Loading branch information
raphinesse committed Apr 13, 2019
1 parent 07a0dd8 commit 38f7c1389a46b8277f5da88abace5fc928043fdf
Showing 3 changed files with 104 additions and 71 deletions.
@@ -20,23 +20,25 @@
const path = require('path');
const fs = require('fs-extra');
const semver = require('semver');
const { ConfigParser } = require('cordova-common');
const { listPlatforms, requireNoCache } = require('../src/cordova/util');
const { tmpDir: getTmpDir, testPlatform, setDefaultTimeout } = require('../spec/helpers');
const projectTestHelpers = require('../spec/project-test-helpers');
const cordova = require('../src/cordova/cordova');

describe('pkgJson', function () {
const TIMEOUT = 150 * 1000;
setDefaultTimeout(TIMEOUT);

const fixturesPath = path.join(__dirname, '../spec/cordova/fixtures');
var tmpDir, project, pkgJsonPath, configXmlPath;
let tmpDir, project, pkgJsonPath;
const {
getPkgJsonPath, setupBaseProject, getCfg, getPkgJson
} = projectTestHelpers(() => project);

beforeEach(() => {
tmpDir = getTmpDir('pkgJson');
project = path.join(tmpDir, 'project');
pkgJsonPath = path.join(project, 'package.json');
configXmlPath = path.join(project, 'config.xml');
pkgJsonPath = getPkgJsonPath();
delete process.env.PWD;
});

@@ -75,20 +77,6 @@ describe('pkgJson', function () {
return fs.readJsonSync(p).version;
}

function getPkgJson (propPath) {
expect(pkgJsonPath).toExist();
const keys = propPath ? propPath.split('.') : [];
return keys.reduce((obj, key) => {
expect(obj).toBeDefined();
return obj[key];
}, fs.readJsonSync(pkgJsonPath));
}

function getCfg () {
expect(configXmlPath).toExist();
return new ConfigParser(configXmlPath);
}

function specSatisfiedBy (version) {
return {
asymmetricMatch: spec => semver.satisfies(version, spec),
@@ -130,7 +118,7 @@ describe('pkgJson', function () {
const pluginId = 'cordova-plugin-device';

beforeEach(function () {
useProject('basePkgJson');
setupBaseProject();
// Copy some platform to avoid working on a project with no platforms.
// FIXME Use a fixture that is properly promisified. This one
// causes spurious test failures when tests reuse the project path.
@@ -296,7 +284,7 @@ describe('pkgJson', function () {

// This group of tests checks if platforms are added and removed as expected from package.json.
describe('platform end-to-end with --save', function () {
beforeEach(() => useProject('basePkgJson'));
beforeEach(() => setupBaseProject());

it('Test#006 : platform is added and removed correctly with --save', function () {
expect(pkgJsonPath).toExist();
@@ -18,23 +18,25 @@
const path = require('path');
const fs = require('fs-extra');
const rewire = require('rewire');
const { ConfigParser } = require('cordova-common');
const { tmpDir: getTmpDir, testPlatform } = require('../helpers');
const projectTestHelpers = require('../project-test-helpers');

/**
* Checks if "cordova/restore-util" is restoring platforms and plugins as
* expected given different configurations of package.json and/or config.xml.
*/
describe('cordova/restore-util', () => {
const fixturesPath = path.join(__dirname, './fixtures');
var tmpDir, project, pkgJsonPath, configXmlPath;
var restore, cordovaPlatform, cordovaPlugin;
const {
getPkgJsonPath, getConfigXmlPath, setupBaseProject, getCfg, getPkgJson, setPkgJson
} = projectTestHelpers(() => project);

beforeEach(() => {
tmpDir = getTmpDir('pkgJson');
project = path.join(tmpDir, 'project');
pkgJsonPath = path.join(project, 'package.json');
configXmlPath = path.join(project, 'config.xml');
pkgJsonPath = getPkgJsonPath();
configXmlPath = getConfigXmlPath();
delete process.env.PWD;

cordovaPlugin = require('../../src/cordova/plugin');
@@ -54,57 +56,10 @@ describe('cordova/restore-util', () => {
fs.removeSync(tmpDir);
});

function setupBaseProject () {
fs.copySync(path.join(fixturesPath, 'basePkgJson'), project);
process.chdir(project);

// It's quite bland, I assure you
expect(getCfg().getPlugins()).toEqual([]);
expect(getCfg().getEngines()).toEqual([]);
expect(getPkgJson('cordova')).toBeUndefined();
expect(getPkgJson('dependencies')).toBeUndefined();
}

// TODO remove this once apache/cordova-common#38
// and apache/cordova-common#39 are resolved
class TestConfigParser extends ConfigParser {
addPlugin (plugin) {
return (super.addPlugin(plugin, plugin.variables), this);
}
addEngine (...args) {
return (super.addEngine(...args), this);
}
}
function getCfg () {
expect(configXmlPath).toExist();
return new TestConfigParser(configXmlPath);
}

function getCfgEngineNames (cfg = getCfg()) {
return cfg.getEngines().map(({ name }) => name);
}

function getPkgJson (propPath) {
expect(pkgJsonPath).toExist();
const keys = propPath ? propPath.split('.') : [];
return keys.reduce((obj, key) => {
expect(obj).toBeDefined();
return obj[key];
}, fs.readJsonSync(pkgJsonPath));
}

function setPkgJson (propPath, value) {
expect(pkgJsonPath).toExist();
const keys = propPath.split('.');
const target = keys.pop();
const pkgJsonObj = fs.readJsonSync(pkgJsonPath);
const parentObj = keys.reduce((obj, key) => {
return obj[key] || (obj[key] = {});
}, pkgJsonObj);
parentObj[target] = value;
fs.writeJsonSync(pkgJsonPath, pkgJsonObj);
}

function platformPkgName (platformName) {
return platformName.replace(/^(?:cordova-)?/, 'cordova-');
}
@@ -0,0 +1,90 @@
/*!
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 fs = require('fs-extra');
const { ConfigParser } = require('cordova-common');

const fixturesPath = path.join(__dirname, 'cordova/fixtures');

// TODO remove this once apache/cordova-common#38
// and apache/cordova-common#39 are resolved
class TestConfigParser extends ConfigParser {
addPlugin (plugin) {
return (super.addPlugin(plugin, plugin.variables), this);
}
addEngine (...args) {
return (super.addEngine(...args), this);
}
}

module.exports = function projectTestHelpers (getProjectPath) {
const getPkgJsonPath = () => path.join(getProjectPath(), 'package.json');
const getConfigXmlPath = () => path.join(getProjectPath(), 'config.xml');

function setupBaseProject () {
const projectPath = getProjectPath();
fs.copySync(path.join(fixturesPath, 'basePkgJson'), projectPath);
process.chdir(projectPath);

// It's quite bland, I assure you
expect(getCfg().getPlugins()).toEqual([]);
expect(getCfg().getEngines()).toEqual([]);
expect(getPkgJson('cordova')).toBeUndefined();
expect(getPkgJson('dependencies')).toBeUndefined();
}

function getCfg () {
const configXmlPath = getConfigXmlPath();
expect(configXmlPath).toExist();
return new TestConfigParser(configXmlPath);
}

function getPkgJson (propPath) {
const pkgJsonPath = getPkgJsonPath();
expect(pkgJsonPath).toExist();
const keys = propPath ? propPath.split('.') : [];
return keys.reduce((obj, key) => {
expect(obj).toBeDefined();
return obj[key];
}, fs.readJsonSync(pkgJsonPath));
}

function setPkgJson (propPath, value) {
const pkgJsonPath = getPkgJsonPath();
expect(pkgJsonPath).toExist();
const keys = propPath.split('.');
const target = keys.pop();
const pkgJsonObj = fs.readJsonSync(pkgJsonPath);
const parentObj = keys.reduce((obj, key) => {
return obj[key] || (obj[key] = {});
}, pkgJsonObj);
parentObj[target] = value;
fs.writeJsonSync(pkgJsonPath, pkgJsonObj);
}

return {
getPkgJsonPath,
getConfigXmlPath,
setupBaseProject,
getCfg,
getPkgJson,
setPkgJson
};
};

0 comments on commit 38f7c13

Please sign in to comment.