From a39011b8bb341c458ae6f9ed93b8d8067114f314 Mon Sep 17 00:00:00 2001 From: Mark Lee Date: Sun, 17 Sep 2017 15:55:18 -0700 Subject: [PATCH] refactor(maker): DRY up linux config transformations Add fast tests for linux maker config. --- package.json | 1 + src/makers/linux/deb.js | 22 +++++------ src/makers/linux/flatpak.js | 17 +++++---- src/makers/linux/rpm.js | 22 +++++------ src/util/linux-config.js | 19 ++++++++++ test/fast/makers/deb_spec.js | 64 ++++++++++++++++++++++++++++++++ test/fast/makers/dmg_spec.js | 3 +- test/fast/makers/flatpak_spec.js | 64 ++++++++++++++++++++++++++++++++ test/fast/makers/rpm_spec.js | 64 ++++++++++++++++++++++++++++++++ 9 files changed, 245 insertions(+), 31 deletions(-) create mode 100644 src/util/linux-config.js create mode 100644 test/fast/makers/deb_spec.js create mode 100644 test/fast/makers/flatpak_spec.js create mode 100644 test/fast/makers/rpm_spec.js diff --git a/package.json b/package.json index b0002aad9..05e1e0187 100644 --- a/package.json +++ b/package.json @@ -124,6 +124,7 @@ "github": "^12.0.3", "glob": "^7.1.1", "inquirer": "^4.0.0", + "lodash.merge": "^4.6.0", "lodash.template": "^4.4.0", "log-symbols": "^2.0.0", "node-fetch": "^1.6.3", diff --git a/src/makers/linux/deb.js b/src/makers/linux/deb.js index f37b38ef8..dc314f3a3 100644 --- a/src/makers/linux/deb.js +++ b/src/makers/linux/deb.js @@ -2,12 +2,12 @@ import path from 'path'; import pify from 'pify'; import { ensureFile } from '../../util/ensure-output'; -import configFn from '../../util/config-fn'; import isInstalled from '../../util/is-installed'; +import { linuxConfig, populateConfig } from '../../util/linux-config'; export const isSupportedOnCurrentPlatform = async () => isInstalled('electron-installer-debian'); -function debianArch(nodeArch) { +export function debianArch(nodeArch) { switch (nodeArch) { case 'ia32': return 'i386'; case 'x64': return 'amd64'; @@ -21,18 +21,18 @@ export default async ({ dir, targetArch, forgeConfig, packageJSON }) => { const installer = require('electron-installer-debian'); const arch = debianArch(targetArch); - const userConfig = configFn(forgeConfig.electronInstallerDebian, targetArch); - userConfig.options = userConfig.options || {}; - const versionedName = `${userConfig.options.name || packageJSON.name}_${packageJSON.version}_${arch}`; + const config = populateConfig({ forgeConfig, configKey: 'electronInstallerDebian', targetArch }); + const name = config.options.name || packageJSON.name; + const versionedName = `${name}_${packageJSON.version}_${arch}`; const outPath = path.resolve(dir, '../make', `${versionedName}.deb`); await ensureFile(outPath); - const debianDefaults = { - arch, - dest: path.dirname(outPath), - src: dir, - }; - const debianConfig = Object.assign({}, userConfig, debianDefaults); + const debianConfig = linuxConfig({ + config, + pkgArch: arch, + dir, + outPath, + }); await pify(installer)(debianConfig); return [outPath]; diff --git a/src/makers/linux/flatpak.js b/src/makers/linux/flatpak.js index c50e94277..96d96b530 100644 --- a/src/makers/linux/flatpak.js +++ b/src/makers/linux/flatpak.js @@ -2,12 +2,12 @@ import path from 'path'; import pify from 'pify'; import { ensureFile } from '../../util/ensure-output'; -import configFn from '../../util/config-fn'; import isInstalled from '../../util/is-installed'; +import { linuxConfig, populateConfig } from '../../util/linux-config'; export const isSupportedOnCurrentPlatform = async () => isInstalled('electron-installer-flatpak'); -function flatpakArch(nodeArch) { +export function flatpakArch(nodeArch) { switch (nodeArch) { case 'ia32': return 'i386'; case 'x64': return 'x86_64'; @@ -21,15 +21,16 @@ export default async ({ dir, targetArch, forgeConfig, packageJSON }) => { const installer = require('electron-installer-flatpak'); const arch = flatpakArch(targetArch); + const config = populateConfig({ forgeConfig, configKey: 'electronInstallerFlatpak', targetArch }); const outPath = path.resolve(dir, '../make', `${packageJSON.name}_${packageJSON.version}_${arch}.flatpak`); await ensureFile(outPath); - const flatpakDefaults = { - arch, - dest: path.dirname(outPath), - src: dir, - }; - const flatpakConfig = Object.assign({}, configFn(forgeConfig.electronInstallerFlatpak, targetArch), flatpakDefaults); + const flatpakConfig = linuxConfig({ + config, + pkgArch: arch, + dir, + outPath, + }); await pify(installer)(flatpakConfig); return [outPath]; diff --git a/src/makers/linux/rpm.js b/src/makers/linux/rpm.js index 4bf810d76..b0bb729ae 100644 --- a/src/makers/linux/rpm.js +++ b/src/makers/linux/rpm.js @@ -2,12 +2,12 @@ import path from 'path'; import pify from 'pify'; import { ensureFile } from '../../util/ensure-output'; -import configFn from '../../util/config-fn'; import isInstalled from '../../util/is-installed'; +import { linuxConfig, populateConfig } from '../../util/linux-config'; export const isSupportedOnCurrentPlatform = async () => isInstalled('electron-installer-redhat'); -function rpmArch(nodeArch) { +export function rpmArch(nodeArch) { switch (nodeArch) { case 'ia32': return 'i386'; case 'x64': return 'x86_64'; @@ -21,18 +21,18 @@ export default async ({ dir, targetArch, forgeConfig, packageJSON }) => { const installer = require('electron-installer-redhat'); const arch = rpmArch(targetArch); - const userConfig = configFn(forgeConfig.electronInstallerRedhat, targetArch); - userConfig.options = userConfig.options || {}; - const versionedName = `${userConfig.options.name || packageJSON.name}-${packageJSON.version}.${arch}`; + const config = populateConfig({ forgeConfig, configKey: 'electronInstallerRedhat', targetArch }); + const name = config.options.name || packageJSON.name; + const versionedName = `${name}-${packageJSON.version}.${arch}`; const outPath = path.resolve(dir, '../make', `${versionedName}.rpm`); await ensureFile(outPath); - const rpmDefaults = { - arch, - dest: path.dirname(outPath), - src: dir, - }; - const rpmConfig = Object.assign({}, userConfig, rpmDefaults); + const rpmConfig = linuxConfig({ + config, + pkgArch: arch, + dir, + outPath, + }); await pify(installer)(rpmConfig); return [outPath]; diff --git a/src/util/linux-config.js b/src/util/linux-config.js new file mode 100644 index 000000000..eed0c812e --- /dev/null +++ b/src/util/linux-config.js @@ -0,0 +1,19 @@ +import merge from 'lodash.merge'; +import path from 'path'; + +import configFn from './config-fn'; + +export function populateConfig({ forgeConfig, configKey, targetArch }) { + const config = configFn(forgeConfig[configKey] || {}, targetArch); + config.options = config.options || {}; + + return config; +} + +export function linuxConfig({ config, pkgArch, dir, outPath }) { + return merge({}, config, { + arch: pkgArch, + dest: path.dirname(outPath), + src: dir, + }); +} diff --git a/test/fast/makers/deb_spec.js b/test/fast/makers/deb_spec.js new file mode 100644 index 000000000..8ef4c8a07 --- /dev/null +++ b/test/fast/makers/deb_spec.js @@ -0,0 +1,64 @@ +import chai, { expect } from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import path from 'path'; +import proxyquire from 'proxyquire'; +import { stub } from 'sinon'; + +chai.use(chaiAsPromised); + +describe('deb maker', () => { + let debModule; + let debMaker; + let eidStub; + let ensureFileStub; + let forgeConfig; + + const dir = '/my/test/dir/out'; + const appName = 'My Test App'; + const targetArch = process.arch; + const packageJSON = { version: '1.2.3' }; + + beforeEach(() => { + ensureFileStub = stub().returns(Promise.resolve()); + eidStub = stub().callsArg(1); + forgeConfig = { electronInstallerDebian: {} }; + + debModule = proxyquire.noPreserveCache().noCallThru().load('../../../src/makers/linux/deb', { + './config-fn': config => config, + '../../util/ensure-output': { ensureFile: ensureFileStub }, + 'electron-installer-debian': eidStub, + }); + debMaker = debModule.default; + }); + + it('should pass through correct defaults', async () => { + await debMaker({ dir, appName, targetArch, forgeConfig, packageJSON }); + const opts = eidStub.firstCall.args[0]; + expect(opts).to.deep.equal({ + arch: debModule.debianArch(process.arch), + options: {}, + src: dir, + dest: path.resolve(dir, '..', 'make'), + }); + }); + + it('should have config cascade correctly', async () => { + forgeConfig.electronInstallerDebian = { + arch: 'overridden', + options: { + productName: 'Debian', + }, + }; + + await debMaker({ dir, appName, targetArch, forgeConfig, packageJSON }); + const opts = eidStub.firstCall.args[0]; + expect(opts).to.deep.equal({ + arch: debModule.debianArch(process.arch), + options: { + productName: 'Debian', + }, + src: dir, + dest: path.resolve(dir, '..', 'make'), + }); + }); +}); diff --git a/test/fast/makers/dmg_spec.js b/test/fast/makers/dmg_spec.js index 2866bdc9e..7b4ce0283 100644 --- a/test/fast/makers/dmg_spec.js +++ b/test/fast/makers/dmg_spec.js @@ -37,7 +37,8 @@ describe('dmg maker', () => { it('should pass through correct defaults', async () => { await dmgMaker({ dir, appName, targetArch, forgeConfig, packageJSON }); const opts = eidStub.firstCall.args[0]; - expect(opts).to.deep.equal({ overwrite: true, + expect(opts).to.deep.equal({ + overwrite: true, name: appName, appPath: path.resolve(`${dir}/My Test App.app`), out: path.resolve(`${dir.substr(0, dir.length - 4)}/make`), diff --git a/test/fast/makers/flatpak_spec.js b/test/fast/makers/flatpak_spec.js new file mode 100644 index 000000000..e27117b04 --- /dev/null +++ b/test/fast/makers/flatpak_spec.js @@ -0,0 +1,64 @@ +import chai, { expect } from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import path from 'path'; +import proxyquire from 'proxyquire'; +import { stub } from 'sinon'; + +chai.use(chaiAsPromised); + +describe('flatpak maker', () => { + let flatpakModule; + let flatpakMaker; + let eidStub; + let ensureFileStub; + let forgeConfig; + + const dir = '/my/test/dir/out'; + const appName = 'My Test App'; + const targetArch = process.arch; + const packageJSON = { version: '1.2.3' }; + + beforeEach(() => { + ensureFileStub = stub().returns(Promise.resolve()); + eidStub = stub().callsArg(1); + forgeConfig = { electronInstallerFlatpak: {} }; + + flatpakModule = proxyquire.noPreserveCache().noCallThru().load('../../../src/makers/linux/flatpak', { + './config-fn': config => config, + '../../util/ensure-output': { ensureFile: ensureFileStub }, + 'electron-installer-flatpak': eidStub, + }); + flatpakMaker = flatpakModule.default; + }); + + it('should pass through correct defaults', async () => { + await flatpakMaker({ dir, appName, targetArch, forgeConfig, packageJSON }); + const opts = eidStub.firstCall.args[0]; + expect(opts).to.deep.equal({ + arch: flatpakModule.flatpakArch(process.arch), + options: {}, + src: dir, + dest: path.resolve(dir, '..', 'make'), + }); + }); + + it('should have config cascade correctly', async () => { + forgeConfig.electronInstallerFlatpak = { + arch: 'overridden', + options: { + productName: 'Flatpak', + }, + }; + + await flatpakMaker({ dir, appName, targetArch, forgeConfig, packageJSON }); + const opts = eidStub.firstCall.args[0]; + expect(opts).to.deep.equal({ + arch: flatpakModule.flatpakArch(process.arch), + options: { + productName: 'Flatpak', + }, + src: dir, + dest: path.resolve(dir, '..', 'make'), + }); + }); +}); diff --git a/test/fast/makers/rpm_spec.js b/test/fast/makers/rpm_spec.js new file mode 100644 index 000000000..06508bb76 --- /dev/null +++ b/test/fast/makers/rpm_spec.js @@ -0,0 +1,64 @@ +import chai, { expect } from 'chai'; +import chaiAsPromised from 'chai-as-promised'; +import path from 'path'; +import proxyquire from 'proxyquire'; +import { stub } from 'sinon'; + +chai.use(chaiAsPromised); + +describe('rpm maker', () => { + let rpmModule; + let rpmMaker; + let eidStub; + let ensureFileStub; + let forgeConfig; + + const dir = '/my/test/dir/out'; + const appName = 'My Test App'; + const targetArch = process.arch; + const packageJSON = { version: '1.2.3' }; + + beforeEach(() => { + ensureFileStub = stub().returns(Promise.resolve()); + eidStub = stub().callsArg(1); + forgeConfig = { electronInstallerRedhat: {} }; + + rpmModule = proxyquire.noPreserveCache().noCallThru().load('../../../src/makers/linux/rpm', { + './config-fn': config => config, + '../../util/ensure-output': { ensureFile: ensureFileStub }, + 'electron-installer-redhat': eidStub, + }); + rpmMaker = rpmModule.default; + }); + + it('should pass through correct defaults', async () => { + await rpmMaker({ dir, appName, targetArch, forgeConfig, packageJSON }); + const opts = eidStub.firstCall.args[0]; + expect(opts).to.deep.equal({ + arch: rpmModule.rpmArch(process.arch), + options: {}, + src: dir, + dest: path.resolve(dir, '..', 'make'), + }); + }); + + it('should have config cascade correctly', async () => { + forgeConfig.electronInstallerRedhat = { + arch: 'overridden', + options: { + productName: 'Redhat', + }, + }; + + await rpmMaker({ dir, appName, targetArch, forgeConfig, packageJSON }); + const opts = eidStub.firstCall.args[0]; + expect(opts).to.deep.equal({ + arch: rpmModule.rpmArch(process.arch), + options: { + productName: 'Redhat', + }, + src: dir, + dest: path.resolve(dir, '..', 'make'), + }); + }); +});