Skip to content

Commit

Permalink
test(core): add tests for updateElectronDependency and getElectronVer…
Browse files Browse the repository at this point in the history
…sion

Also refactor src/util/electron-version
  • Loading branch information
malept committed Jan 3, 2019
1 parent 028d1d3 commit d73eb25
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 28 deletions.
20 changes: 1 addition & 19 deletions packages/api/core/src/api/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion packages/api/core/src/api/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down
44 changes: 39 additions & 5 deletions packages/api/core/src/util/electron-version.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 1 addition & 1 deletion packages/api/core/src/util/resolve-dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
61 changes: 61 additions & 0 deletions packages/api/core/test/fast/electron-version_spec.ts
Original file line number Diff line number Diff line change
@@ -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.only('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('[email protected]')).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 d73eb25

Please sign in to comment.