Skip to content

Commit

Permalink
test: mock git for some tests to improve code coverage (conventional-…
Browse files Browse the repository at this point in the history
  • Loading branch information
nexdrew authored and bcoe committed May 6, 2016
1 parent b6b2a75 commit 7378ded
Showing 1 changed file with 55 additions and 25 deletions.
80 changes: 55 additions & 25 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@ var shell = require('shelljs')
var fs = require('fs')
var path = require('path')
var cliPath = path.resolve(__dirname, './index.js')
var PATH

require('chai').should()

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')
Expand All @@ -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"')
Expand All @@ -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')
Expand All @@ -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<a name="1.0.0">\n', 'utf-8')

commit('feat: first commit')
Expand All @@ -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"')
Expand All @@ -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"')
Expand Down

0 comments on commit 7378ded

Please sign in to comment.