Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isElectronMain env test #13

Merged
merged 11 commits into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
],
"repository": "github:ipfs/js-ipfs-utils",
"scripts": {
"test": "aegir test && npm run test:electron && npm run test:electron-renderer",
"test": "aegir test",
"test:browser": "aegir test -t browser",
"test:node": "aegir test -t node",
"test:electron": "electron-mocha 'test/**/*.spec.js'",
"test:electron-renderer": "electron-mocha --renderer 'test/**/*.spec.js'",
"test:electron": "aegir test -t electron-main",
"test:electron-renderer": "aegir test -t electron-renderer",
"lint": "aegir lint",
"release": "aegir release --docs",
"release-minor": "aegir release --type minor --docs",
Expand All @@ -39,13 +39,11 @@
"readable-stream": "^3.4.0"
},
"devDependencies": {
"aegir": "^20.0.0",
"aegir": "^20.3.0",
"async-iterator-all": "^1.0.0",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"electron": "^6.0.6",
"electron-mocha": "^8.0.3",
"pull-stream": "^3.6.13",
"readable-stream-2": "npm:readable-stream@^2.0.0"
},
Expand Down
2 changes: 2 additions & 0 deletions src/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const isElectron = require('is-electron')
const IS_ENV_WITH_DOM = typeof window === 'object' && typeof document === 'object' && document.nodeType === 9
const IS_ELECTRON = isElectron()
const IS_BROWSER = IS_ENV_WITH_DOM && !IS_ELECTRON
const IS_ELECTRON_MAIN = IS_ELECTRON && !IS_ENV_WITH_DOM
const IS_ELECTRON_RENDERER = IS_ELECTRON && IS_ENV_WITH_DOM
const IS_NODE = typeof require === 'function' && typeof process !== 'undefined' && typeof process.release !== 'undefined' && process.release.name === 'node' && !IS_ELECTRON
// eslint-disable-next-line no-undef
Expand All @@ -13,6 +14,7 @@ const IS_TEST = typeof process !== 'undefined' && typeof process.env !== 'undefi
module.exports = {
isTest: IS_TEST,
isElectron: IS_ELECTRON,
isElectronMain: IS_ELECTRON_MAIN,
isElectronRenderer: IS_ELECTRON_RENDERER,
isNode: IS_NODE,
/**
Expand Down
2 changes: 1 addition & 1 deletion src/files/glob-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module.exports = async function * globSource (paths, options) {
for await (const path of paths) {
if (typeof path !== 'string') {
throw errCode(
new Error(`Path must be a string`),
new Error('Path must be a string'),
'ERR_INVALID_PATH',
{ path }
)
Expand Down
186 changes: 117 additions & 69 deletions test/env.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,92 +10,140 @@ const expect = chai.expect

describe('env', function () {
it('isElectron should have the correct value in each env', function () {
if (env.isElectron) {
return expect(env.isElectron).to.be.true()
switch (process.env.AEGIR_RUNNER) {
case 'electron-main':
expect(env.isElectron).to.be.true()
break
case 'electron-renderer':
expect(env.isElectron).to.be.true()
break
case 'node':
expect(env.isElectron).to.be.false()
break
case 'browser':
expect(env.isElectron).to.be.false()
break
case 'webworker':
expect(env.isElectron).to.be.false()
break
default:
expect.fail(`Could not detect env. Current env is ${process.env.AEGIR_RUNNER}`)
break
}
if (env.isElectronRenderer) {
return expect(env.isElectron).to.be.true()
}
if (env.isBrowser) {
return expect(env.isElectron).to.be.false()
}
if (env.isNode) {
return expect(env.isElectron).to.be.false()
}
if (env.isWebWorker) {
return expect(env.isElectron).to.be.false()
})

it('isElectronMain should have the correct value in each env', function () {
switch (process.env.AEGIR_RUNNER) {
case 'electron-main':
expect(env.isElectronMain).to.be.true()
break
case 'electron-renderer':
expect(env.isElectronMain).to.be.false()
break
case 'node':
expect(env.isElectronMain).to.be.false()
break
case 'browser':
expect(env.isElectronMain).to.be.false()
break
case 'webworker':
expect(env.isElectronMain).to.be.false()
break
default:
expect.fail(`Could not detect env. Current env is ${process.env.AEGIR_RUNNER}`)
break
}
})

it('isElectronRenderer should have the correct value in each env', function () {
if (env.isElectron && !env.isElectronRenderer) {
return expect(env.isElectronRenderer).to.be.false()
}
if (env.isElectronRenderer) {
return expect(env.isElectronRenderer).to.be.true()
}
if (env.isBrowser) {
return expect(env.isElectronRenderer).to.be.false()
}
if (env.isNode) {
return expect(env.isElectronRenderer).to.be.false()
}
if (env.isWebWorker) {
return expect(env.isElectronRenderer).to.be.false()
switch (process.env.AEGIR_RUNNER) {
case 'electron-main':
expect(env.isElectronRenderer).to.be.false()
break
case 'electron-renderer':
expect(env.isElectronRenderer).to.be.true()
break
case 'node':
expect(env.isElectronRenderer).to.be.false()
break
case 'browser':
expect(env.isElectronRenderer).to.be.false()
break
case 'webworker':
expect(env.isElectronRenderer).to.be.false()
break
default:
expect.fail(`Could not detect env. Current env is ${process.env.AEGIR_RUNNER}`)
break
}
})

it('isNode should have the correct value in each env', function () {
if (env.isElectron) {
return expect(env.isNode).to.be.false()
}
if (env.isElectronRenderer) {
return expect(env.isNode).to.be.false()
}
if (env.isBrowser) {
return expect(env.isNode).to.be.false()
}
if (env.isNode) {
return expect(env.isNode).to.be.true()
}
if (env.isWebWorker) {
return expect(env.isNode).to.be.false()
switch (process.env.AEGIR_RUNNER) {
case 'electron-main':
expect(env.isNode).to.be.false()
break
case 'electron-renderer':
expect(env.isNode).to.be.false()
break
case 'node':
expect(env.isNode).to.be.true()
break
case 'browser':
expect(env.isNode).to.be.false()
break
case 'webworker':
expect(env.isNode).to.be.false()
break
default:
expect.fail(`Could not detect env. Current env is ${process.env.AEGIR_RUNNER}`)
break
}
})

it('isBrowser should have the correct value in each env', function () {
if (env.isElectron) {
return expect(env.isBrowser).to.be.false()
}
if (env.isElectronRenderer) {
return expect(env.isBrowser).to.be.false()
}
if (env.isBrowser) {
return expect(env.isBrowser).to.be.true()
}
if (env.isNode) {
return expect(env.isBrowser).to.be.false()
}
if (env.isWebWorker) {
return expect(env.isBrowser).to.be.false()
switch (process.env.AEGIR_RUNNER) {
case 'electron-main':
expect(env.isBrowser).to.be.false()
break
case 'electron-renderer':
expect(env.isBrowser).to.be.false()
break
case 'node':
expect(env.isBrowser).to.be.false()
break
case 'browser':
expect(env.isBrowser).to.be.true()
break
case 'webworker':
expect(env.isBrowser).to.be.false()
break
default:
expect.fail(`Could not detect env. Current env is ${process.env.AEGIR_RUNNER}`)
break
}
})

it('isWebWorker should have the correct value in each env', function () {
if (env.isElectron) {
return expect(env.isWebWorker).to.be.false()
}
if (env.isElectronRenderer) {
return expect(env.isWebWorker).to.be.false()
}
if (env.isBrowser) {
return expect(env.isWebWorker).to.be.false()
}
if (env.isNode) {
return expect(env.isWebWorker).to.be.false()
}
if (env.isWebWorker) {
return expect(env.isWebWorker).to.be.true()
switch (process.env.AEGIR_RUNNER) {
case 'electron-main':
expect(env.isWebWorker).to.be.false()
break
case 'electron-renderer':
expect(env.isWebWorker).to.be.false()
break
case 'node':
expect(env.isWebWorker).to.be.false()
break
case 'browser':
expect(env.isWebWorker).to.be.false()
break
case 'webworker':
expect(env.isWebWorker).to.be.true()
break
default:
expect.fail(`Could not detect env. Current env is ${process.env.AEGIR_RUNNER}`)
break
}
})
})
2 changes: 1 addition & 1 deletion test/files/normalise-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ async function verifyNormalisation (input) {
expect(input.length).to.equal(1)

if (!input[0].content[Symbol.asyncIterator] && !input[0].content[Symbol.iterator]) {
chai.assert.fail(`Content should have been an iterable or an async iterable`)
chai.assert.fail('Content should have been an iterable or an async iterable')
}

expect(await all(input[0].content)).to.deep.equal([BUFFER])
Expand Down