Skip to content

Commit

Permalink
test(generic): add tests for lots of the utils
Browse files Browse the repository at this point in the history
  • Loading branch information
MarshallOfSound authored and malept committed Jan 4, 2017
1 parent 93fb48f commit d0962b9
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 76 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
"lint": "eslint src test gulpfile.babel.js",
"prepublish": "gulp build",
"pretest": "gulp build",
"test": "npm run lint && mocha test --compilers js:babel-register --timeout=300000",
"test": "npm run lint && npm run test-all",
"test-all": "mocha test/**/*_spec*.js --compilers js:babel-register --timeout=300000",
"test-fast": "mocha test/**/*_spec.js --compilers js:babel-register --timeout=10000",
"test-slow": "mocha test/**/*_spec_slow.js --compilers js:babel-register --timeout=300000",
"release:patch": "changelog -p && node ci/fix-changelog.js && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version patch && git push origin && git push origin --tags",
"release:minor": "changelog -m && node ci/fix-changelog.js && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version minor && git push origin && git push origin --tags",
"release:major": "changelog -M && node ci/fix-changelog.js && git add CHANGELOG.md && git commit -m 'updated CHANGELOG.md' && npm version major && git push origin && git push origin --tags",
Expand Down
5 changes: 5 additions & 0 deletions src/util/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class BasicConfigStore {
this._dir = path.resolve(os.tmpdir(), 'electron-forge');
this._path = path.resolve(this._dir, '.runtime.config');
fs.mkdirsSync(this._dir);

process.on('exit', () => {
if (fs.existsSync(this._path)) fs.unlinkSync(this._path);
this._store = {};
});
}

get(key) {
Expand Down
19 changes: 19 additions & 0 deletions test/fast/config_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from 'chai';

import config from '../../src/util/config';

describe('cross-process config', () => {
it('should get all values as undefined initially', () => {
expect(config.get('foobar')).to.equal(undefined);
});

it('should set a value in the current process', () => {
config.set('foobar', 'magical');
expect(config.get('foobar')).to.equal('magical');
});

it('should reset the value on process exit', () => {
process.emit('exit');
expect(config.get('foobar')).to.equal(undefined);
});
});
13 changes: 13 additions & 0 deletions test/fast/electron-host-arch_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect } from 'chai';

import electronHostArch from '../../src/util/electron-host-arch';

describe('electron-host-arch', () => {
if (process.arch !== 'arm') {
describe('on non-arm systems', () => {
it('should return the current arch', () => {
expect(electronHostArch()).to.equal(process.arch);
});
});
}
});
50 changes: 50 additions & 0 deletions test/fast/ensure-output_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { expect } from 'chai';
import fs from 'fs-promise';
import os from 'os';
import path from 'path';

import { ensureDirectory, ensureFile } from '../../src/util/ensure-output';

describe('ensure-output', () => {
const tmpPath = path.resolve(os.tmpdir(), 'forge-ensure');

before(async () => {
await fs.mkdirs(tmpPath);
});

describe('ensureDirectory', () => {
it('should delete the directory contents if it exists', async () => {
await fs.mkdirs(path.resolve(tmpPath, 'foo'));
fs.writeFileSync(path.resolve(tmpPath, 'foo', 'touchedFile'));
expect(await fs.exists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(true);
await ensureDirectory(path.resolve(tmpPath, 'foo'));
expect(await fs.exists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(false);
});

it('should create the directory if it does not exist', async () => {
expect(await fs.exists(path.resolve(tmpPath, 'bar'))).to.equal(false);
await ensureDirectory(path.resolve(tmpPath, 'bar'));
expect(await fs.exists(path.resolve(tmpPath, 'bar'))).to.equal(true);
});
});

describe('ensureFile', () => {
it('should delete the file if it exists', async () => {
await fs.mkdirs(path.resolve(tmpPath, 'foo'));
fs.writeFileSync(path.resolve(tmpPath, 'foo', 'touchedFile'));
expect(await fs.exists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(true);
await ensureFile(path.resolve(tmpPath, 'foo'));
expect(await fs.exists(path.resolve(tmpPath, 'foo', 'touchedFile'))).to.equal(false);
});

it('should create the containing directory if it does not exist', async () => {
expect(await fs.exists(path.resolve(tmpPath, 'bar'))).to.equal(false);
await ensureFile(path.resolve(tmpPath, 'bar', 'file'));
expect(await fs.exists(path.resolve(tmpPath, 'bar'))).to.equal(true);
});
});

afterEach(async () => {
await fs.remove(tmpPath);
});
});
38 changes: 38 additions & 0 deletions test/fast/forge-config_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect } from 'chai';
import path from 'path';

import findConfig from '../../src/util/forge-config';

const defaults = {
make_targets: {
win32: ['squirrel'],
darwin: ['zip'],
linux: ['deb', 'rpm'],
mas: ['zip'],
},
electronInstallerDMG: {},
electronPackagerConfig: {},
electronWinstallerConfig: {},
electronInstallerDebian: {},
electronInstallerRedhat: {},
};

describe('forge-config', () => {
it('should resolve the object in package.json with defaults if one exists', async () => {
expect(await findConfig(path.resolve(__dirname, '../fixture/dummy_app'))).to.be.deep.equal(Object.assign({}, defaults, {
electronWinstallerConfig: { windows: 'magic' },
}));
});

it('should resolve the JS file exports in config.forge points to a JS file', async () => {
expect(JSON.parse(JSON.stringify(await findConfig(path.resolve(__dirname, '../fixture/dummy_js_conf'))))).to.be.deep.equal(Object.assign({}, defaults, {
electronPackagerConfig: { foo: 'bar' },
}));
});

it('should resolve the JS file exports in config.forge points to a JS file and maintain functions', async () => {
const conf = await findConfig(path.resolve(__dirname, '../fixture/dummy_js_conf'));
expect(conf.magicFn).to.be.a('function');
expect(conf.magicFn()).to.be.equal('magic result');
});
});
15 changes: 15 additions & 0 deletions test/fast/github_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from 'chai';

import GitHub from '../../src/util/github';

describe('GitHub', () => {
it('should read token from constructor', () => {
expect(new GitHub('token1').token).to.equal('token1');
});

it('should fall back to token from environment', () => {
process.env.GITHUB_TOKEN = 'abc123';
expect(new GitHub().token).to.equal('abc123');
delete process.env.GITHUB_TOKEN;
});
});
10 changes: 10 additions & 0 deletions test/fast/read-package-json_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import path from 'path';
import { expect } from 'chai';

import readPackageJSON from '../../src/util/read-package-json';

describe('read-package-json', () => {
it('should find a package.json file from the given directory', async () => {
expect(await readPackageJSON(path.resolve(__dirname, '../..'))).to.deep.equal(require('../..//package.json'));
});
});
16 changes: 16 additions & 0 deletions test/fast/require-search_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai';

import requireSearch from '../../src/util/require-search';
import findConfig from '../../src/util/forge-config';

describe('require-search', () => {
it('should resolve undefined if no file exists', () => {
const resolved = requireSearch(__dirname, ['../../src/util/wizard-secrets']);
expect(resolved).to.equal(undefined);
});

it('should resolve a file if it exists', () => {
const resolved = requireSearch(__dirname, ['../../src/util/forge-config']);
expect(resolved).to.equal(findConfig);
});
});
15 changes: 15 additions & 0 deletions test/fast/resolve-dir_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect } from 'chai';
import path from 'path';

import resolveDir from '../../src/util/resolve-dir';

describe('resolve-dir', () => {
it('should return null if a valid dir can not be found', async () => {
expect(await resolveDir('/foo/var/fake')).to.be.equal(null);
});

it('should return a directory if it finds a node module', async () => {
expect(await resolveDir(path.resolve(__dirname, '../fixture/dummy_app/foo'))).to.not.be.equal(null);
expect(await resolveDir(path.resolve(__dirname, '../fixture/dummy_app/foo'))).to.be.equal(path.resolve(__dirname, '../fixture/dummy_app'));
});
});
2 changes: 1 addition & 1 deletion test/system_spec.js → test/fast/system_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai';

import checkSystem from '../src/util/check-system';
import checkSystem from '../../src/util/check-system';

describe('check-system', () => {
it('should succeed on valid agents', async () => {
Expand Down
16 changes: 8 additions & 8 deletions test/cli_spec.js → test/slow/cli_spec_slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import path from 'path';

import { expect } from 'chai';

import installDeps from '../src/util/install-dependencies';
import readPackageJSON from '../src/util/read-package-json';
import installDeps from '../../src/util/install-dependencies';
import readPackageJSON from '../../src/util/read-package-json';

const pSpawn = async (args = [], opts = {
stdio: process.platform === 'win32' ? 'inherit' : 'pipe',
}) => {
const child = spawn(process.execPath, [path.resolve(__dirname, '../dist/electron-forge.js')].concat(args), opts);
const child = spawn(process.execPath, [path.resolve(__dirname, '../../dist/electron-forge.js')].concat(args), opts);
let stdout = '';
let stderr = '';
if (process.platform !== 'win32') {
Expand Down Expand Up @@ -84,7 +84,7 @@ describe(`electron-forge CLI (with installer=${installer.substr(12)})`, () => {
dirID += 1;
await fs.remove(dir);
execSync('npm link', {
cwd: path.resolve(__dirname, 'fixture/custom_init'),
cwd: path.resolve(__dirname, '../fixture/custom_init'),
});
await pSpawn(['init', dir, '--template=dummy']);
});
Expand All @@ -105,7 +105,7 @@ describe(`electron-forge CLI (with installer=${installer.substr(12)})`, () => {
after(async () => {
await fs.remove(dir);
execSync('npm unlink', {
cwd: path.resolve(__dirname, 'fixture/custom_init'),
cwd: path.resolve(__dirname, '../fixture/custom_init'),
});
});
});
Expand All @@ -130,10 +130,10 @@ describe(`electron-forge CLI (with installer=${installer.substr(12)})`, () => {

describe('after package', () => {
let targets = [];
if (fs.existsSync(path.resolve(__dirname, `../src/makers/${process.platform}`))) {
targets = fs.readdirSync(path.resolve(__dirname, `../src/makers/${process.platform}`)).map(file => path.parse(file).name);
if (fs.existsSync(path.resolve(__dirname, `../../src/makers/${process.platform}`))) {
targets = fs.readdirSync(path.resolve(__dirname, `../../src/makers/${process.platform}`)).map(file => path.parse(file).name);
}
const genericTargets = fs.readdirSync(path.resolve(__dirname, '../src/makers/generic')).map(file => path.parse(file).name);
const genericTargets = fs.readdirSync(path.resolve(__dirname, '../../src/makers/generic')).map(file => path.parse(file).name);

[].concat(targets).concat(genericTargets).forEach((target) => {
describe(`make (with target=${target})`, () => {
Expand Down
4 changes: 2 additions & 2 deletions test/rebuild_spec.js → test/slow/rebuild_spec_slow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { spawn as yarnOrNPMSpawn, hasYarn } from 'yarn-or-npm';

import { expect } from 'chai';

import rebuild from '../src/util/rebuild';
import rebuild from '../../src/util/rebuild';

ora.ora = ora;

Expand All @@ -16,7 +16,7 @@ describe('rebuilder', () => {
before(async () => {
await fs.remove(testModulePath);
await fs.mkdirs(testModulePath);
await fs.writeFile(path.resolve(testModulePath, 'package.json'), await fs.readFile(path.resolve(__dirname, 'fixture/native_app/package.json'), 'utf8'));
await fs.writeFile(path.resolve(testModulePath, 'package.json'), await fs.readFile(path.resolve(__dirname, '../fixture/native_app/package.json'), 'utf8'));
await new Promise((resolve, reject) => {
const child = yarnOrNPMSpawn(hasYarn() ? [] : ['install'], {
cwd: testModulePath,
Expand Down
64 changes: 0 additions & 64 deletions test/util_spec.js

This file was deleted.

0 comments on commit d0962b9

Please sign in to comment.