Skip to content
Permalink
Browse files

test(plugin-local-electron): add tests for the local electron plugin

  • Loading branch information
MarshallOfSound committed May 3, 2018
1 parent 46aaf7a commit 61b36329347c03955f8577754cfe9b66ce59f82d
@@ -8,10 +8,11 @@
"main": "dist/LocalElectronPlugin.js",
"typings": "dist/LocalElectronPlugin.d.ts",
"scripts": {
"test": "exit 0"
"test": "mocha --require ts-node/register test/**/*_spec.ts test/**/**/*_spec.ts --opts ../../../mocha.opts"
},
"devDependencies": {
"chai": "^4.0.0",
"fs-extra": "^5.0.0",
"mocha": "^5.0.0"
},
"engines": {
@@ -1,4 +1,4 @@
import PluginBase, { StartOptions } from '@electron-forge/plugin-base';
import PluginBase from '@electron-forge/plugin-base';
import { spawn } from 'child_process';
import fs from 'fs-extra';

@@ -7,7 +7,17 @@ import { LocalElectronPluginConfig } from './Config';
export default class LocalElectronPlugin extends PluginBase<LocalElectronPluginConfig> {
name = 'local-electron';

async startLogic(startOpts: StartOptions) {
constructor(config: LocalElectronPluginConfig) {
super(config);
if (typeof this.config.enabled === 'undefined') {
this.config = {
...this.config,
enabled: true,
};
}
}

async startLogic() {
if (this.config.enabled) {
this.checkPlatform(process.platform);
process.env.ELECTRON_OVERRIDE_DIST_PATH = this.config.electronPath;
@@ -0,0 +1,116 @@
import { expect } from 'chai';
import fs from 'fs-extra';
import os from 'os';
import path from 'path';

import LocalElectronPlugin from '../src/LocalElectronPlugin';

describe('LocalElectronPlugin', () => {
describe('start logic', () => {
before(() => {
delete process.env.ELECTRON_OVERRIDE_DIST_PATH;
});

afterEach(() => {
delete process.env.ELECTRON_OVERRIDE_DIST_PATH;
});

it('should set ELECTRON_OVERRIDE_DIST_PATH when enabled', async () => {
expect(process.env.ELECTRON_OVERRIDE_DIST_PATH).to.equal(undefined);
const p = new LocalElectronPlugin({ electronPath: 'test/foo' });
await p.startLogic();
expect(process.env.ELECTRON_OVERRIDE_DIST_PATH).to.equal('test/foo');
});

it('should not set ELECTRON_OVERRIDE_DIST_PATH when disabled', async () => {
expect(process.env.ELECTRON_OVERRIDE_DIST_PATH).to.equal(undefined);
const p = new LocalElectronPlugin({ enabled: false, electronPath: 'test/foo' });
await p.startLogic();
expect(process.env.ELECTRON_OVERRIDE_DIST_PATH).to.equal(undefined);
});

it('should throw an error if platforms don\'t match', async () => {
const p = new LocalElectronPlugin({ electronPath: 'test/bar', electronPlatform: 'wut' });
await expect(p.startLogic()).to.eventually.be.rejectedWith(
`Can not use local Electron version, required platform "${process.platform}" but local platform is "wut"`,
);
});

it('should always return false', async () => {
let p = new LocalElectronPlugin({ electronPath: 'test/bar' })
expect(await p.startLogic()).to.equal(false);

p = new LocalElectronPlugin({ enabled: false, electronPath: 'test/bar' })
expect(await p.startLogic()).to.equal(false);
});
});

describe('hooks', () => {
let p: LocalElectronPlugin;

beforeEach(() => {
p = new LocalElectronPlugin({ electronPath: 'test/foo' });
});

it('should return for all other hooks', () => {
expect(p.getHook('prePackage')).to.equal(null);
expect(p.getHook('postMake')).to.equal(null);
});

describe('with afterExtract hook', () => {
let tmpDir: string;

beforeEach(async () => {
tmpDir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'forge-test-'));
await fs.writeFile(path.resolve(tmpDir, 'touch'), 'hey');
});

afterEach(() => {
return fs.remove(tmpDir);
})

it('should return a function for packageAfterExtract', () => {
expect(p.getHook('packageAfterExtract')).to.be.a('function');
});

it('should do nothing when disabled', async () => {
p.config.enabled = false;
const fn = p.getHook('packageAfterExtract')!;

await fn(null, tmpDir, null, process.platform, process.arch);

expect(await fs.pathExists(tmpDir)).to.equal(true);
expect(await fs.pathExists(path.resolve(tmpDir, 'touch'))).to.equal(true);
});

it('should throw an error if the platform doesn\'t match', async () => {
const fn = p.getHook('packageAfterExtract')!;

await expect(fn(null, tmpDir, null, 'bad', process.arch)).to.eventually.be.rejectedWith(
`Can not use local Electron version, required platform "bad" but local platform is "${process.platform}"`,
);
});

it('should throw an error if the arch doesn\'t match', async () => {
const fn = p.getHook('packageAfterExtract')!;

await expect(fn(null, tmpDir, null, process.platform, 'bad')).to.eventually.be.rejectedWith(
`Can not use local Electron version, required arch "bad" but local arch is "${process.arch}"`,
);
});

it('should copy the electron dir to the build dir if everything is ok and enabled', async () => {
const electronDir = await fs.mkdtemp(path.resolve(os.tmpdir(), 'electron-tmp-'));
await fs.writeFile(path.resolve(electronDir, 'electron'), 'hi i am electron I swear');
p.config.electronPath = electronDir;
const fn = p.getHook('packageAfterExtract')!;

await fn(null, tmpDir, null, process.platform, process.arch);

expect(await fs.pathExists(path.resolve(tmpDir, 'touch'))).to.equal(false);
expect(await fs.pathExists(path.resolve(tmpDir, 'electron'))).to.equal(true);
expect(await fs.readFile(path.resolve(tmpDir, 'electron'), 'utf8')).to.equal('hi i am electron I swear');
});
});
});
});

0 comments on commit 61b3632

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