Skip to content
Permalink
Browse files

test(core): add tests for updateElectronDependency and getElectronVer…

…sion

Also refactor src/util/electron-version
  • Loading branch information
malept committed Jan 3, 2019
1 parent 648ef33 commit cc93be9e740000b1c7863fbde4e0bb3caa462525
@@ -7,6 +7,7 @@ import path from 'path';
import initGit from './init-scripts/init-git';
import { deps, devDeps, exactDevDeps } from './init-scripts/init-npm';

import { updateElectronDependency } from '../util/electron-version';
import { setInitialForgeConfig } from '../util/forge-config';
import { info, warn } from '../util/messages';
import installDepList, { DepType, DepVersionRestriction } from '../util/install-dependencies';
@@ -15,25 +16,6 @@ import upgradeForgeConfig, { updateUpgradedForgeDevDeps } from'../util/upgrade-f

const d = debug('electron-forge:import');

function findElectronDep(dep: string): boolean {
return ['electron', 'electron-prebuilt', 'electron-prebuilt-compile'].includes(dep);
}

function updateElectronDependency(packageJSON: any, dev: string[], exact: string[]): [string[], string[]] {
if (Object.keys(packageJSON.devDependencies).find(findElectronDep)) {
exact = exact.filter(dep => dep !== 'electron');
} else {
const electronKey = Object.keys(packageJSON.dependencies).find(findElectronDep);
if (electronKey) {
d(`Moving ${electronKey} from dependencies to devDependencies`);
dev.push(`${electronKey}@${packageJSON.dependencies[electronKey]}`);
delete packageJSON.dependencies[electronKey];
}
}

return [dev, exact];
}

export interface ImportOptions {
/**
* The path to the app to be imported
@@ -12,7 +12,7 @@ import parseArchs from '../util/parse-archs';
import { readMutatedPackageJson } from '../util/read-package-json';
import resolveDir from '../util/resolve-dir';
import getCurrentOutDir from '../util/out-dir';
import getElectronVersion from '../util/electron-version';
import { getElectronVersion } from '../util/electron-version';
import requireSearch from '../util/require-search';

import packager from './package';
@@ -16,7 +16,7 @@ import rebuildHook from '../util/rebuild';
import requireSearch from '../util/require-search';
import resolveDir from '../util/resolve-dir';
import getCurrentOutDir from '../util/out-dir';
import getElectronVersion from '../util/electron-version';
import { getElectronVersion } from '../util/electron-version';

const { host: hostArch }: { host: () => ForgeArch | 'all' } = require('electron-download/lib/arch');

@@ -9,7 +9,7 @@ import rebuild from '../util/rebuild';
import resolveDir from '../util/resolve-dir';
import getForgeConfig from '../util/forge-config';
import { runHook } from '../util/hook';
import getElectronVersion from '../util/electron-version';
import { getElectronVersion } from '../util/electron-version';

export { StartOptions };

@@ -1,6 +1,40 @@
export default (packageJSON: any) => {
if (!packageJSON.devDependencies) return null;
return (packageJSON.devDependencies['electron-prebuilt-compile']
|| packageJSON.devDependencies['electron-prebuilt']
|| packageJSON.devDependencies.electron);
import debug from 'debug';

const d = debug('electron-forge:electron-version');

const electronPackageNames = [
'electron-prebuilt-compile',
'electron-prebuilt',
'electron',
];

function findElectronDep(dep: string): boolean {
return electronPackageNames.includes(dep);
}

export function getElectronVersion (packageJSON: any) {
if (!packageJSON.devDependencies) {
throw new Error('package.json for app does not have any devDependencies'.red);
}
const packageName = electronPackageNames.find(pkg => packageJSON.devDependencies[pkg]);
if (packageName === undefined) {
throw new Error('Could not find any Electron packages in devDependencies');
}
return packageJSON.devDependencies[packageName];
};

export function updateElectronDependency(packageJSON: any, dev: string[], exact: string[]): [string[], string[]] {
if (Object.keys(packageJSON.devDependencies).find(findElectronDep)) {
exact = exact.filter(dep => dep !== 'electron');
} else {
const electronKey = Object.keys(packageJSON.dependencies).find(findElectronDep);
if (electronKey) {
exact = exact.filter(dep => dep !== 'electron');
d(`Moving ${electronKey} from dependencies to devDependencies`);
dev.push(`${electronKey}@${packageJSON.dependencies[electronKey]}`);
delete packageJSON.dependencies[electronKey];
}
}

return [dev, exact];
}
@@ -2,7 +2,7 @@ import debug from 'debug';
import fs from 'fs-extra';
import path from 'path';
import { readRawPackageJson } from './read-package-json';
import getElectronVersion from './electron-version';
import { getElectronVersion } from './electron-version';

const d = debug('electron-forge:project-resolver');

@@ -0,0 +1,61 @@
import { expect } from 'chai';
import { getElectronVersion, updateElectronDependency } from '../../src/util/electron-version';
import { deps, devDeps, exactDevDeps } from '../../src/api/init-scripts/init-npm';

describe('updateElectronDependency', () => {
it('adds an Electron dep if one does not already exist', () => {
const packageJSON = { dependencies: {}, devDependencies: {} };
const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps);
expect(dev).to.deep.equal(devDeps);
expect(exact).to.deep.equal(exactDevDeps);
});
it('does not add an Electron dep if one already exists', () => {
const packageJSON = {
dependencies: {},
devDependencies: { electron: '0.37.0' },
};
const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps);
expect(dev).to.deep.equal(devDeps);
expect(exact).to.deep.equal([]);
});
it('moves an Electron dependency from dependencies to devDependencies', () => {
const packageJSON = {
dependencies: { electron: '0.37.0'},
devDependencies: { },
};
const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps);
expect(dev.includes('electron@0.37.0')).to.equal(true);
expect(exact).to.deep.equal([]);
});
});

describe('getElectronVersion', () => {
it('fails without devDependencies', () => {
expect(() => getElectronVersion({})).to.throw('does not have any devDependencies');
});

it('fails without electron devDependencies', () => {
expect(() => getElectronVersion({ devDependencies: {} })).to.throw('Electron packages in devDependencies');
});

it('works with electron-prebuilt-compile', () => {
const packageJSON = {
devDependencies: { 'electron-prebuilt-compile': '1.0.0' },
};
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0');
});

it('works with electron-prebuilt', () => {
const packageJSON = {
devDependencies: { 'electron-prebuilt': '1.0.0' },
};
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0');
});

it('works with electron', () => {
const packageJSON = {
devDependencies: { 'electron': '1.0.0' },
};
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0');
});
});

0 comments on commit cc93be9

Please sign in to comment.
You can’t perform that action at this time.