Skip to content

Commit

Permalink
feat(tests): adds test suite, fixed several Node 0.10 issues along th…
Browse files Browse the repository at this point in the history
…e way
  • Loading branch information
bcoe committed Apr 8, 2016
1 parent 1f673c0 commit 03bd86c
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.nyc_output
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ node_js:
- "0.10"
- "0.12"
- "node"
before_script:
- git config --global user.name 'Travis-CI'
- git config --global user.email 'dummy@example.org'
after_success: npm run coverage
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

[![Build Status](https://travis-ci.org/conventional-changelog/standard-version.svg)](https://travis-ci.org/conventional-changelog/standard-version)
[![NPM version](https://img.shields.io/npm/v/standard-version.svg)](https://www.npmjs.com/package/standard-version)
[![Coverage Status](https://coveralls.io/repos/conventional-changelog/standard-version/badge.svg?branch=)](https://coveralls.io/r/conventional-changelog/standard-version?branch=master)
[![Standard Version](https://img.shields.io/badge/standard-version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)

> stop using `npm version`, use `standard-version` it does so much more:
Expand Down Expand Up @@ -36,6 +38,10 @@ When you're generating your changelog for the first time, simply do:

## Automating

Do this:

`npm i standard-version --save-dev`

Add this to your _package.json_

```json
Expand All @@ -46,6 +52,14 @@ Add this to your _package.json_
}
```

## Badges!

Tell your users that you adhere to the `standard-version` commit guidelines:

```markdown
[![Standard Version](https://img.shields.io/badge/standard-version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
```

## License

ISC
76 changes: 41 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ var argv = require('yargs')
.wrap(97)
.argv

var addStream = require('add-stream')
var chalk = require('chalk')
var figures = require('figures')
var exec = require('child_process').exec
var fs = require('fs')
var accessSync = require('fs-access').sync
var pkgPath = path.resolve(process.cwd(), './package.json')
var pkg = require(pkgPath)
var semver = require('semver')
var tempfile = require('tempfile')
var rimraf = require('rimraf')
var util = require('util')

conventionalRecommendedBump({
Expand All @@ -59,12 +58,11 @@ conventionalRecommendedBump({
var newVersion = pkg.version
if (!argv.firstRelease) {
newVersion = semver.inc(pkg.version, release.releaseAs)

console.log(chalk.bold('1.') + ' bump version ' + chalk.bold(release.releaseAs) + ' in package.json (' + pkg.version + ' → ' + chalk.green(newVersion) + ')')
checkpoint('bumping version in package.json from %s to %s', [pkg.version, newVersion])
pkg.version = newVersion
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2), 'utf-8')
} else {
console.log(chalk.yellow('skip package.json update on first release'))
console.log(chalk.red(figures.cross) + ' skip version bump on first release')
}

outputChangelog(argv, function () {
Expand All @@ -75,43 +73,45 @@ conventionalRecommendedBump({
})

function outputChangelog (argv, cb) {
console.log(chalk.bold('2.') + ' update changelog (' + chalk.bold(argv.infile) + ')')

createIfMissing(argv)

var readStream = fs.createReadStream(argv.infile)
.on('error', function (err) {
console.warn(chalk.yellow(err.message))
})

var header = '# Change Log\n\nAll notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.\n'
var oldContent = fs.readFileSync(argv.infile, 'utf-8')
// find the position of the last release and remove header:
if (oldContent.indexOf('<a name=') !== -1) {
oldContent = oldContent.substring(oldContent.indexOf('<a name='))
}
var content = ''
var changelogStream = conventionalChangelog({
preset: argv.preset,
outputUnreleased: true,
pkg: {
path: path.resolve(process.cwd(), './package.json')
}
})
.on('error', function (err) {
console.error(chalk.red(err.message))
process.exit(1)
})
var tmp = tempfile()
.on('error', function (err) {
console.error(chalk.red(err.message))
process.exit(1)
})

changelogStream
.pipe(addStream(readStream))
.pipe(fs.createWriteStream(tmp))
.on('finish', function () {
fs.createReadStream(tmp)
.pipe(fs.createWriteStream(argv.infile))
.on('finish', function () {
rimraf.sync(tmp)
return cb()
})
})
changelogStream.on('data', function (buffer) {
content += buffer.toString()
})

changelogStream.on('end', function () {
checkpoint('outputting changes to %s', [argv.infile])
fs.writeFileSync(argv.infile, header + '\n' + content + oldContent, 'utf-8')
return cb()
})
}

function commit (argv, newVersion, cb) {
console.log(chalk.bold('3.') + ' commit ' + chalk.bold('package.json') + ' and ' + chalk.bold(argv.infile))
var msg = 'committing %s'
var args = [argv.infile]
if (!argv.firstRelease) {
msg += ' and %s'
args.unshift('package.json')
}
checkpoint(msg, args)
exec('git add package.json ' + argv.infile + ';git commit package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
var errMessage = null
if (err) errMessage = err.message
Expand All @@ -129,7 +129,7 @@ function formatCommitMessage (msg, newVersion) {
}

function tag (newVersion, argv) {
console.log(chalk.bold('4.') + ' tag release (' + chalk.green(newVersion) + ')')
checkpoint('tagging release %s', [newVersion])
exec('git tag -a v' + newVersion + ' -m "' + argv.message + '"', function (err, stdout, stderr) {
var errMessage = null
if (err) errMessage = err.message
Expand All @@ -143,12 +143,18 @@ function tag (newVersion, argv) {

function createIfMissing (argv) {
try {
fs.accessSync(argv.infile, fs.F_OK)
accessSync(argv.infile, fs.F_OK)
} catch (err) {
if (err.code === 'ENOENT') {
console.log(chalk.green('creating ') + argv.infile)
checkpoint('created %s', [argv.infile])
argv.outputUnreleased = true
fs.writeFileSync(argv.infile, '', 'utf-8')
fs.writeFileSync(argv.infile, '\n', 'utf-8')
}
}
}

function checkpoint (msg, args) {
console.info(chalk.green(figures.tick) + ' ' + util.format.apply(util, [msg].concat(args.map(function (arg) {
return chalk.bold(arg)
}))))
};
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"description": "replacement for `npm version` with automatic CHANGELOG generation",
"bin": "index.js",
"scripts": {
"test": "standard"
"pretest": "standard",
"coverage": "nyc report --reporter=text-lcov | coveralls",
"test": "nyc mocha --timeout=10000 test.js"
},
"repository": {
"type": "git",
Expand All @@ -26,16 +28,20 @@
},
"homepage": "https://github.com/conventional-changelog/standard-version#readme",
"dependencies": {
"add-stream": "^1.0.0",
"chalk": "^1.1.3",
"conventional-changelog": "^1.1.0",
"conventional-recommended-bump": "^0.2.0",
"rimraf": "^2.5.2",
"figures": "^1.5.0",
"fs-access": "^1.0.0",
"semver": "^5.1.0",
"tempfile": "^1.1.1",
"yargs": "^4.3.2"
},
"devDependencies": {
"chai": "^3.5.0",
"coveralls": "^2.11.9",
"mocha": "^2.4.5",
"nyc": "^6.2.1",
"shelljs": "^0.6.0",
"standard": "^6.0.8"
}
}
81 changes: 81 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* global describe it beforeEach afterEach */

'use strict'

var shell = require('shelljs')
var fs = require('fs')
var path = require('path')
var cliPath = path.resolve(__dirname, './index.js')

require('chai').should()

function commit (msg) {
shell.exec('git commit --allow-empty -m"' + msg + '"')
}

describe('cli', function () {
beforeEach(function () {
shell.rm('-rf', 'tmp')
shell.config.silent = true
shell.mkdir('tmp')
shell.cd('tmp')
shell.exec('git init')
commit('root-commit')
})

afterEach(function () {
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')

commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('fix: patch release')

shell.exec(cliPath).code.should.equal(0)

var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/patch release/)
content.should.not.match(/first commit/)
})

it('includes all commits if --first-release is true', function () {
fs.writeFileSync('package.json', JSON.stringify({
version: '1.0.1'
}), 'utf-8')

commit('feat: first commit')
commit('fix: patch release')
shell.exec(cliPath + ' --first-release').code.should.equal(0)

var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/patch release/)
content.should.match(/first commit/)
shell.exec('git tag').output.should.match(/1\.0\.1/)
})
})

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')
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')

commit('feat: first commit')
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
commit('fix: patch release')

shell.exec(cliPath).code.should.equal(0)
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
content.should.match(/1\.0\.1/)
content.should.not.match(/legacy header format/)
})
})
})

0 comments on commit 03bd86c

Please sign in to comment.