-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(build): ensure to check spawn result and assume files unchanged M…
…ONGOSH-521 (#599) * ensure to check spawn result and assume files unchanged - uses a helper function to ensure spawn.sync calls throw if not successful - before lerna publish marks known changed files as assume-unchanged and reverts after publish Signed-off-by: Michael Rose <michael_rose@gmx.de> * increase test coverage * fixup: increase test coverage
- Loading branch information
Showing
2 changed files
with
280 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,203 @@ | ||
import { expect } from 'chai'; | ||
import { listNpmPackages } from './npm-packages'; | ||
|
||
describe('listNpmPackages', () => { | ||
it('lists packages', () => { | ||
const packages = listNpmPackages(); | ||
expect(packages.length).to.be.greaterThan(1); | ||
for (const { name, version } of packages) { | ||
expect(name).to.be.a('string'); | ||
expect(version).to.be.a('string'); | ||
} | ||
import path from 'path'; | ||
import sinon, { SinonStub } from 'sinon'; | ||
import { | ||
bumpNpmPackages, | ||
listNpmPackages, | ||
markBumpedFilesAsAssumeUnchanged, | ||
publishNpmPackages, | ||
spawnSync | ||
} from './npm-packages'; | ||
|
||
|
||
describe('npm-packages', () => { | ||
describe('spawnSync', () => { | ||
it('works for a valid command', () => { | ||
const result = spawnSync('bash', ['-c', 'echo -n works'], { encoding: 'utf8' }); | ||
expect(result.status).to.equal(0); | ||
expect(result.stdout).to.equal('works'); | ||
}); | ||
|
||
it('throws on ENOENT error', () => { | ||
try { | ||
spawnSync('notaprogram', [], { encoding: 'utf8' }); | ||
} catch (e) { | ||
return expect(e).to.not.be.undefined; | ||
} | ||
expect.fail('Expected error'); | ||
}); | ||
|
||
it('throws on non-zero exit code', () => { | ||
try { | ||
spawnSync('bash', ['-c', 'exit 1'], { encoding: 'utf8' }); | ||
} catch (e) { | ||
return expect(e).to.not.be.undefined; | ||
} | ||
expect.fail('Expected error'); | ||
}); | ||
}); | ||
|
||
describe('bumpNpmPackages', () => { | ||
let spawnSync: SinonStub; | ||
|
||
beforeEach(() => { | ||
spawnSync = sinon.stub(); | ||
}); | ||
|
||
it('does not do anything if no version or placeholder version is specified', () => { | ||
bumpNpmPackages('', spawnSync); | ||
bumpNpmPackages('0.0.0-dev.0', spawnSync); | ||
expect(spawnSync).to.not.have.been.called; | ||
}); | ||
|
||
it('calls lerna to bump package version', () => { | ||
const lernaBin = path.resolve(__dirname, '..', '..', '..', 'node_modules', '.bin', 'lerna'); | ||
bumpNpmPackages('0.7.0', spawnSync); | ||
expect(spawnSync).to.have.been.calledWith( | ||
lernaBin, | ||
['version', '0.7.0', '--no-changelog', '--no-push', '--exact', '--no-git-tag-version', '--force-publish', '--yes'], | ||
sinon.match.any | ||
); | ||
}); | ||
}); | ||
|
||
describe('publishNpmPackages', () => { | ||
let listNpmPackages: SinonStub; | ||
let markBumpedFilesAsAssumeUnchanged: SinonStub; | ||
let spawnSync: SinonStub; | ||
|
||
beforeEach(() => { | ||
listNpmPackages = sinon.stub(); | ||
markBumpedFilesAsAssumeUnchanged = sinon.stub(); | ||
spawnSync = sinon.stub(); | ||
}); | ||
|
||
it('fails if packages have different versions', () => { | ||
listNpmPackages.returns([ | ||
{ name: 'packageA', version: '0.0.1' }, | ||
{ name: 'packageB', version: '0.0.2' } | ||
]); | ||
try { | ||
publishNpmPackages( | ||
listNpmPackages, | ||
markBumpedFilesAsAssumeUnchanged, | ||
spawnSync | ||
); | ||
} catch (e) { | ||
expect(markBumpedFilesAsAssumeUnchanged).to.not.have.been.called; | ||
expect(spawnSync).to.not.have.been.called; | ||
return; | ||
} | ||
expect.fail('Expected error'); | ||
}); | ||
|
||
it('fails if packages have placeholder versions', () => { | ||
listNpmPackages.returns([ | ||
{ name: 'packageA', version: '0.0.0-dev.0' }, | ||
{ name: 'packageB', version: '0.0.0-dev.0' } | ||
]); | ||
try { | ||
publishNpmPackages( | ||
listNpmPackages, | ||
markBumpedFilesAsAssumeUnchanged, | ||
spawnSync | ||
); | ||
} catch (e) { | ||
expect(markBumpedFilesAsAssumeUnchanged).to.not.have.been.called; | ||
expect(spawnSync).to.not.have.been.called; | ||
return; | ||
} | ||
expect.fail('Expected error'); | ||
}); | ||
|
||
it('calls lerna to publish packages for a real version', () => { | ||
const lernaBin = path.resolve(__dirname, '..', '..', '..', 'node_modules', '.bin', 'lerna'); | ||
const packages = [ | ||
{ name: 'packageA', version: '0.7.0' } | ||
]; | ||
listNpmPackages.returns(packages); | ||
|
||
publishNpmPackages( | ||
listNpmPackages, | ||
markBumpedFilesAsAssumeUnchanged, | ||
spawnSync | ||
); | ||
|
||
expect(markBumpedFilesAsAssumeUnchanged).to.have.been.calledWith(packages, true); | ||
expect(spawnSync).to.have.been.calledWith( | ||
lernaBin, | ||
['publish', 'from-package', '--no-changelog', '--no-push', '--exact', '--no-git-tag-version', '--force-publish', '--yes'], | ||
sinon.match.any | ||
); | ||
expect(markBumpedFilesAsAssumeUnchanged).to.have.been.calledWith(packages, false); | ||
}); | ||
|
||
it('reverts the assume unchanged even on spawn failure', () => { | ||
const packages = [ | ||
{ name: 'packageA', version: '0.7.0' } | ||
]; | ||
listNpmPackages.returns(packages); | ||
spawnSync.throws(new Error('meeep')); | ||
|
||
try { | ||
publishNpmPackages( | ||
listNpmPackages, | ||
markBumpedFilesAsAssumeUnchanged, | ||
spawnSync | ||
); | ||
} catch (e) { | ||
expect(markBumpedFilesAsAssumeUnchanged).to.have.been.calledWith(packages, true); | ||
expect(spawnSync).to.have.been.called; | ||
expect(markBumpedFilesAsAssumeUnchanged).to.have.been.calledWith(packages, false); | ||
return; | ||
} | ||
expect.fail('Expected error'); | ||
}); | ||
}); | ||
|
||
describe('listNpmPackages', () => { | ||
it('lists packages', () => { | ||
const packages = listNpmPackages(); | ||
expect(packages.length).to.be.greaterThan(1); | ||
for (const { name, version } of packages) { | ||
expect(name).to.be.a('string'); | ||
expect(version).to.be.a('string'); | ||
} | ||
}); | ||
}); | ||
|
||
describe('markBumpedFilesAsAssumeUnchanged', () => { | ||
let packages: { name: string; version: string }[]; | ||
let expectedFiles: string[]; | ||
let spawnSync: SinonStub; | ||
|
||
beforeEach(() => { | ||
expectedFiles = ['.npmrc', 'lerna.json']; | ||
packages = listNpmPackages(); | ||
packages.forEach(({ name }) => { | ||
expectedFiles.push(`packages/${name}/package.json`); | ||
expectedFiles.push(`packages/${name}/package-lock.json`); | ||
}); | ||
|
||
spawnSync = sinon.stub(); | ||
}); | ||
|
||
it('marks files with --assume-unchanged', () => { | ||
markBumpedFilesAsAssumeUnchanged(packages, true, spawnSync); | ||
expectedFiles.forEach(f => { | ||
expect(spawnSync).to.have.been.calledWith( | ||
'git', ['update-index', '--assume-unchanged', f], sinon.match.any | ||
); | ||
}); | ||
}); | ||
|
||
it('marks files with --no-assume-unchanged', () => { | ||
markBumpedFilesAsAssumeUnchanged(packages, false, spawnSync); | ||
expectedFiles.forEach(f => { | ||
expect(spawnSync).to.have.been.calledWith( | ||
'git', ['update-index', '--no-assume-unchanged', f], sinon.match.any | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |
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