Skip to content

Commit 03bd86c

Browse files
committed
feat(tests): adds test suite, fixed several Node 0.10 issues along the way
1 parent 1f673c0 commit 03bd86c

File tree

6 files changed

+151
-39
lines changed

6 files changed

+151
-39
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules
2+
.nyc_output

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ node_js:
33
- "0.10"
44
- "0.12"
55
- "node"
6+
before_script:
7+
- git config --global user.name 'Travis-CI'
8+
- git config --global user.email '[email protected]'
9+
after_success: npm run coverage

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

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

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

3739
## Automating
3840

41+
Do this:
42+
43+
`npm i standard-version --save-dev`
44+
3945
Add this to your _package.json_
4046

4147
```json
@@ -46,6 +52,14 @@ Add this to your _package.json_
4652
}
4753
```
4854

55+
## Badges!
56+
57+
Tell your users that you adhere to the `standard-version` commit guidelines:
58+
59+
```markdown
60+
[![Standard Version](https://img.shields.io/badge/standard-version-brightgreen.svg)](https://github.com/conventional-changelog/standard-version)
61+
```
62+
4963
## License
5064

5165
ISC

index.js

+41-35
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,14 @@ var argv = require('yargs')
3737
.wrap(97)
3838
.argv
3939

40-
var addStream = require('add-stream')
4140
var chalk = require('chalk')
41+
var figures = require('figures')
4242
var exec = require('child_process').exec
4343
var fs = require('fs')
44+
var accessSync = require('fs-access').sync
4445
var pkgPath = path.resolve(process.cwd(), './package.json')
4546
var pkg = require(pkgPath)
4647
var semver = require('semver')
47-
var tempfile = require('tempfile')
48-
var rimraf = require('rimraf')
4948
var util = require('util')
5049

5150
conventionalRecommendedBump({
@@ -59,12 +58,11 @@ conventionalRecommendedBump({
5958
var newVersion = pkg.version
6059
if (!argv.firstRelease) {
6160
newVersion = semver.inc(pkg.version, release.releaseAs)
62-
63-
console.log(chalk.bold('1.') + ' bump version ' + chalk.bold(release.releaseAs) + ' in package.json (' + pkg.version + ' → ' + chalk.green(newVersion) + ')')
61+
checkpoint('bumping version in package.json from %s to %s', [pkg.version, newVersion])
6462
pkg.version = newVersion
6563
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2), 'utf-8')
6664
} else {
67-
console.log(chalk.yellow('skip package.json update on first release'))
65+
console.log(chalk.red(figures.cross) + ' skip version bump on first release')
6866
}
6967

7068
outputChangelog(argv, function () {
@@ -75,43 +73,45 @@ conventionalRecommendedBump({
7573
})
7674

7775
function outputChangelog (argv, cb) {
78-
console.log(chalk.bold('2.') + ' update changelog (' + chalk.bold(argv.infile) + ')')
79-
8076
createIfMissing(argv)
81-
82-
var readStream = fs.createReadStream(argv.infile)
83-
.on('error', function (err) {
84-
console.warn(chalk.yellow(err.message))
85-
})
86-
77+
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'
78+
var oldContent = fs.readFileSync(argv.infile, 'utf-8')
79+
// find the position of the last release and remove header:
80+
if (oldContent.indexOf('<a name=') !== -1) {
81+
oldContent = oldContent.substring(oldContent.indexOf('<a name='))
82+
}
83+
var content = ''
8784
var changelogStream = conventionalChangelog({
8885
preset: argv.preset,
8986
outputUnreleased: true,
9087
pkg: {
9188
path: path.resolve(process.cwd(), './package.json')
9289
}
9390
})
94-
.on('error', function (err) {
95-
console.error(chalk.red(err.message))
96-
process.exit(1)
97-
})
98-
var tmp = tempfile()
91+
.on('error', function (err) {
92+
console.error(chalk.red(err.message))
93+
process.exit(1)
94+
})
9995

100-
changelogStream
101-
.pipe(addStream(readStream))
102-
.pipe(fs.createWriteStream(tmp))
103-
.on('finish', function () {
104-
fs.createReadStream(tmp)
105-
.pipe(fs.createWriteStream(argv.infile))
106-
.on('finish', function () {
107-
rimraf.sync(tmp)
108-
return cb()
109-
})
110-
})
96+
changelogStream.on('data', function (buffer) {
97+
content += buffer.toString()
98+
})
99+
100+
changelogStream.on('end', function () {
101+
checkpoint('outputting changes to %s', [argv.infile])
102+
fs.writeFileSync(argv.infile, header + '\n' + content + oldContent, 'utf-8')
103+
return cb()
104+
})
111105
}
112106

113107
function commit (argv, newVersion, cb) {
114-
console.log(chalk.bold('3.') + ' commit ' + chalk.bold('package.json') + ' and ' + chalk.bold(argv.infile))
108+
var msg = 'committing %s'
109+
var args = [argv.infile]
110+
if (!argv.firstRelease) {
111+
msg += ' and %s'
112+
args.unshift('package.json')
113+
}
114+
checkpoint(msg, args)
115115
exec('git add package.json ' + argv.infile + ';git commit package.json ' + argv.infile + ' -m "' + formatCommitMessage(argv.message, newVersion) + '"', function (err, stdout, stderr) {
116116
var errMessage = null
117117
if (err) errMessage = err.message
@@ -129,7 +129,7 @@ function formatCommitMessage (msg, newVersion) {
129129
}
130130

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

144144
function createIfMissing (argv) {
145145
try {
146-
fs.accessSync(argv.infile, fs.F_OK)
146+
accessSync(argv.infile, fs.F_OK)
147147
} catch (err) {
148148
if (err.code === 'ENOENT') {
149-
console.log(chalk.green('creating ') + argv.infile)
149+
checkpoint('created %s', [argv.infile])
150150
argv.outputUnreleased = true
151-
fs.writeFileSync(argv.infile, '', 'utf-8')
151+
fs.writeFileSync(argv.infile, '\n', 'utf-8')
152152
}
153153
}
154154
}
155+
156+
function checkpoint (msg, args) {
157+
console.info(chalk.green(figures.tick) + ' ' + util.format.apply(util, [msg].concat(args.map(function (arg) {
158+
return chalk.bold(arg)
159+
}))))
160+
};

package.json

+10-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "replacement for `npm version` with automatic CHANGELOG generation",
55
"bin": "index.js",
66
"scripts": {
7-
"test": "standard"
7+
"pretest": "standard",
8+
"coverage": "nyc report --reporter=text-lcov | coveralls",
9+
"test": "nyc mocha --timeout=10000 test.js"
810
},
911
"repository": {
1012
"type": "git",
@@ -26,16 +28,20 @@
2628
},
2729
"homepage": "https://github.com/conventional-changelog/standard-version#readme",
2830
"dependencies": {
29-
"add-stream": "^1.0.0",
3031
"chalk": "^1.1.3",
3132
"conventional-changelog": "^1.1.0",
3233
"conventional-recommended-bump": "^0.2.0",
33-
"rimraf": "^2.5.2",
34+
"figures": "^1.5.0",
35+
"fs-access": "^1.0.0",
3436
"semver": "^5.1.0",
35-
"tempfile": "^1.1.1",
3637
"yargs": "^4.3.2"
3738
},
3839
"devDependencies": {
40+
"chai": "^3.5.0",
41+
"coveralls": "^2.11.9",
42+
"mocha": "^2.4.5",
43+
"nyc": "^6.2.1",
44+
"shelljs": "^0.6.0",
3945
"standard": "^6.0.8"
4046
}
4147
}

test.js

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* global describe it beforeEach afterEach */
2+
3+
'use strict'
4+
5+
var shell = require('shelljs')
6+
var fs = require('fs')
7+
var path = require('path')
8+
var cliPath = path.resolve(__dirname, './index.js')
9+
10+
require('chai').should()
11+
12+
function commit (msg) {
13+
shell.exec('git commit --allow-empty -m"' + msg + '"')
14+
}
15+
16+
describe('cli', function () {
17+
beforeEach(function () {
18+
shell.rm('-rf', 'tmp')
19+
shell.config.silent = true
20+
shell.mkdir('tmp')
21+
shell.cd('tmp')
22+
shell.exec('git init')
23+
commit('root-commit')
24+
})
25+
26+
afterEach(function () {
27+
shell.cd('../')
28+
shell.rm('-rf', 'tmp')
29+
})
30+
31+
describe('CHANGELOG.md does not exist', function () {
32+
it('populates changelog with commits since last tag by default', function () {
33+
fs.writeFileSync('package.json', JSON.stringify({
34+
version: '1.0.0'
35+
}), 'utf-8')
36+
37+
commit('feat: first commit')
38+
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
39+
commit('fix: patch release')
40+
41+
shell.exec(cliPath).code.should.equal(0)
42+
43+
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
44+
content.should.match(/patch release/)
45+
content.should.not.match(/first commit/)
46+
})
47+
48+
it('includes all commits if --first-release is true', function () {
49+
fs.writeFileSync('package.json', JSON.stringify({
50+
version: '1.0.1'
51+
}), 'utf-8')
52+
53+
commit('feat: first commit')
54+
commit('fix: patch release')
55+
shell.exec(cliPath + ' --first-release').code.should.equal(0)
56+
57+
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
58+
content.should.match(/patch release/)
59+
content.should.match(/first commit/)
60+
shell.exec('git tag').output.should.match(/1\.0\.1/)
61+
})
62+
})
63+
64+
describe('CHANGELOG.md exists', function () {
65+
it('appends the new release above the last release, removing the old header', function () {
66+
fs.writeFileSync('package.json', JSON.stringify({
67+
version: '1.0.0'
68+
}), 'utf-8')
69+
fs.writeFileSync('CHANGELOG.md', 'legacy header format<a name="1.0.0">\n', 'utf-8')
70+
71+
commit('feat: first commit')
72+
shell.exec('git tag -a v1.0.0 -m "my awesome first release"')
73+
commit('fix: patch release')
74+
75+
shell.exec(cliPath).code.should.equal(0)
76+
var content = fs.readFileSync('CHANGELOG.md', 'utf-8')
77+
content.should.match(/1\.0\.1/)
78+
content.should.not.match(/legacy header format/)
79+
})
80+
})
81+
})

0 commit comments

Comments
 (0)