-
-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): add tests for updateElectronDependency and getElectronVer…
…sion Also refactor src/util/electron-version
- Loading branch information
Showing
7 changed files
with
105 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,40 @@ | ||
export default (packageJSON: any) => { | ||
if (!packageJSON.devDependencies) return null; | ||
return (packageJSON.devDependencies['electron-prebuilt-compile'] | ||
|| packageJSON.devDependencies['electron-prebuilt'] | ||
|| packageJSON.devDependencies.electron); | ||
import debug from 'debug'; | ||
|
||
const d = debug('electron-forge:electron-version'); | ||
|
||
const electronPackageNames = [ | ||
'electron-prebuilt-compile', | ||
'electron-prebuilt', | ||
'electron', | ||
]; | ||
|
||
function findElectronDep(dep: string): boolean { | ||
return electronPackageNames.includes(dep); | ||
} | ||
|
||
export function getElectronVersion (packageJSON: any) { | ||
if (!packageJSON.devDependencies) { | ||
throw new Error('package.json for app does not have any devDependencies'.red); | ||
} | ||
const packageName = electronPackageNames.find(pkg => packageJSON.devDependencies[pkg]); | ||
if (packageName === undefined) { | ||
throw new Error('Could not find any Electron packages in devDependencies'); | ||
} | ||
return packageJSON.devDependencies[packageName]; | ||
}; | ||
|
||
export function updateElectronDependency(packageJSON: any, dev: string[], exact: string[]): [string[], string[]] { | ||
if (Object.keys(packageJSON.devDependencies).find(findElectronDep)) { | ||
exact = exact.filter(dep => dep !== 'electron'); | ||
} else { | ||
const electronKey = Object.keys(packageJSON.dependencies).find(findElectronDep); | ||
if (electronKey) { | ||
exact = exact.filter(dep => dep !== 'electron'); | ||
d(`Moving ${electronKey} from dependencies to devDependencies`); | ||
dev.push(`${electronKey}@${packageJSON.dependencies[electronKey]}`); | ||
delete packageJSON.dependencies[electronKey]; | ||
} | ||
} | ||
|
||
return [dev, exact]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { expect } from 'chai'; | ||
import { getElectronVersion, updateElectronDependency } from '../../src/util/electron-version'; | ||
import { deps, devDeps, exactDevDeps } from '../../src/api/init-scripts/init-npm'; | ||
|
||
describe.only('updateElectronDependency', () => { | ||
it('adds an Electron dep if one does not already exist', () => { | ||
const packageJSON = { dependencies: {}, devDependencies: {} }; | ||
const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps); | ||
expect(dev).to.deep.equal(devDeps); | ||
expect(exact).to.deep.equal(exactDevDeps); | ||
}); | ||
it('does not add an Electron dep if one already exists', () => { | ||
const packageJSON = { | ||
dependencies: {}, | ||
devDependencies: { electron: '0.37.0' }, | ||
}; | ||
const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps); | ||
expect(dev).to.deep.equal(devDeps); | ||
expect(exact).to.deep.equal([]); | ||
}); | ||
it('moves an Electron dependency from dependencies to devDependencies', () => { | ||
const packageJSON = { | ||
dependencies: { electron: '0.37.0'}, | ||
devDependencies: { }, | ||
}; | ||
const [dev, exact] = updateElectronDependency(packageJSON, devDeps, exactDevDeps); | ||
expect(dev.includes('[email protected]')).to.equal(true); | ||
expect(exact).to.deep.equal([]); | ||
}); | ||
}); | ||
|
||
describe('getElectronVersion', () => { | ||
it('fails without devDependencies', () => { | ||
expect(() => getElectronVersion({})).to.throw('does not have any devDependencies'); | ||
}); | ||
|
||
it('fails without electron devDependencies', () => { | ||
expect(() => getElectronVersion({ devDependencies: {} })).to.throw('Electron packages in devDependencies'); | ||
}); | ||
|
||
it('works with electron-prebuilt-compile', () => { | ||
const packageJSON = { | ||
devDependencies: { 'electron-prebuilt-compile': '1.0.0' }, | ||
}; | ||
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0'); | ||
}); | ||
|
||
it('works with electron-prebuilt', () => { | ||
const packageJSON = { | ||
devDependencies: { 'electron-prebuilt': '1.0.0' }, | ||
}; | ||
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0'); | ||
}); | ||
|
||
it('works with electron', () => { | ||
const packageJSON = { | ||
devDependencies: { 'electron': '1.0.0' }, | ||
}; | ||
expect(getElectronVersion(packageJSON)).to.be.equal('1.0.0'); | ||
}); | ||
}); |