diff --git a/test.js b/test.js index a801441de..ff810695d 100644 --- a/test.js +++ b/test.js @@ -6,6 +6,7 @@ var shell = require('shelljs') var fs = require('fs') var path = require('path') var cliPath = path.resolve(__dirname, './index.js') +var PATH require('chai').should() @@ -13,6 +14,19 @@ function commit (msg) { shell.exec('git commit --allow-empty -m"' + msg + '"') } +function writePackageJson (version) { + fs.writeFileSync('package.json', JSON.stringify({ + version: version + }), 'utf-8') +} + +function mockGit (logic) { + fs.writeFileSync('git', '#!/usr/bin/env node\n' + logic, 'utf8') + shell.chmod('+x', 'git') + PATH = shell.env.PATH + shell.env.PATH = shell.pwd() + ':' + PATH +} + describe('cli', function () { beforeEach(function () { shell.rm('-rf', 'tmp') @@ -24,15 +38,17 @@ describe('cli', function () { }) afterEach(function () { + if (PATH) { + shell.env['PATH'] = PATH + PATH = undefined + } shell.cd('../') shell.rm('-rf', 'tmp') }) describe('CHANGELOG.md does not exist', function () { it('populates changelog with commits since last tag by default', function () { - fs.writeFileSync('package.json', JSON.stringify({ - version: '1.0.0' - }), 'utf-8') + writePackageJson('1.0.0') commit('feat: first commit') shell.exec('git tag -a v1.0.0 -m "my awesome first release"') @@ -46,9 +62,7 @@ describe('cli', function () { }) it('includes all commits if --first-release is true', function () { - fs.writeFileSync('package.json', JSON.stringify({ - version: '1.0.1' - }), 'utf-8') + writePackageJson('1.0.1') commit('feat: first commit') commit('fix: patch release') @@ -63,9 +77,7 @@ describe('cli', function () { describe('CHANGELOG.md exists', function () { it('appends the new release above the last release, removing the old header', function () { - fs.writeFileSync('package.json', JSON.stringify({ - version: '1.0.0' - }), 'utf-8') + writePackageJson('1.0.0') fs.writeFileSync('CHANGELOG.md', 'legacy header format\n', 'utf-8') commit('feat: first commit') @@ -79,24 +91,44 @@ describe('cli', function () { }) }) - it('respects the --sign option', function () { - fs.writeFileSync('package.json', JSON.stringify({ - version: '1.0.0' - }), 'utf-8') + describe('with mocked git', function () { + it('--sign signs the commit and tag', function () { + // mock git with file that writes args to gitcapture.log + mockGit('require("fs").appendFileSync("gitcapture.log", JSON.stringify(process.argv.splice(2)) + "\\n", "utf8")') + writePackageJson('1.0.0') - commit('feat: first commit') + shell.exec(cliPath + ' --sign').code.should.equal(0) + + var captured = shell.cat('gitcapture.log').stdout.split('\n').map(function (line) { + return line ? JSON.parse(line) : line + }) + captured[captured.length - 3].should.deep.equal(['commit', '-S', 'package.json', 'CHANGELOG.md', '-m', 'chore(release): 1.0.1']) + captured[captured.length - 2].should.deep.equal(['tag', '-s', 'v1.0.1', '-m', 'chore(release): 1.0.1']) + }) + + it('exits with error code if git commit fails', function () { + // mock git by throwing on attempt to commit + mockGit('if (process.argv[2] === "commit") { console.error("commit yourself"); process.exit(128); }') + writePackageJson('1.0.0') + + var result = shell.exec(cliPath) + result.code.should.equal(1) + result.stdout.should.match(/commit yourself/) + }) + + it('exits with error code if git tag fails', function () { + // mock git by throwing on attempt to commit + mockGit('if (process.argv[2] === "tag") { console.error("tag, you\'re it"); process.exit(128); }') + writePackageJson('1.0.0') - // this should fail without a GPG key - var result = shell.exec(cliPath + ' --sign') - result.code.should.equal(1) - result.stdout.should.match(/gpg\: signing failed\: secret key not available/) - result.stdout.should.match(/error\: gpg failed to sign the data/) + var result = shell.exec(cliPath) + result.code.should.equal(1) + result.stdout.should.match(/tag, you're it/) + }) }) it('handles commit messages longer than 80 characters', function () { - fs.writeFileSync('package.json', JSON.stringify({ - version: '1.0.0' - }), 'utf-8') + writePackageJson('1.0.0') commit('feat: first commit') shell.exec('git tag -a v1.0.0 -m "my awesome first release"') @@ -109,9 +141,7 @@ describe('cli', function () { }) it('formats the commit and tag messages appropriately', function () { - fs.writeFileSync('package.json', JSON.stringify({ - version: '1.0.0' - }), 'utf-8') + writePackageJson('1.0.0') commit('feat: first commit') shell.exec('git tag -a v1.0.0 -m "my awesome first release"')