Skip to content

Commit

Permalink
test(plugin-local-electron): add tests for the local electron plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound committed May 3, 2018
1 parent bd8bb41 commit 58ca309
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/plugin/local-electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 12 additions & 2 deletions packages/plugin/local-electron/src/LocalElectronPlugin.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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;
Expand Down
116 changes: 116 additions & 0 deletions packages/plugin/local-electron/test/LocalElectronPlugin_spec.ts
Original file line number Diff line number Diff line change
@@ -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 "darwin" 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 "darwin"',
);
});

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 "x64"',
);
});

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 58ca309

Please sign in to comment.