From 8d9f363ed973ae23d331bed98b5b42e88de03088 Mon Sep 17 00:00:00 2001 From: Steve Mao Date: Mon, 3 Aug 2015 23:06:22 +1000 Subject: [PATCH] fix(previousTag): incase there is no commits in the first release --- index.js | 4 ++-- test/test.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 84b5fc049..70442346b 100644 --- a/index.js +++ b/index.js @@ -184,9 +184,9 @@ function conventinalChangelog(options, context, gitRawCommitsOpts, parserOpts, w if (!previousTag) { if (options.append) { - context.previousTag = context.previousTag || commits[0].hash; + context.previousTag = context.previousTag || commits[0] ? commits[0].hash : null; } else { - context.previousTag = context.previousTag || commits[commits.length - 1].hash; + context.previousTag = context.previousTag || commits[commits.length - 1] ? commits[commits.length - 1].hash : null; } } } else { diff --git a/test/test.js b/test/test.js index 4c2eecf93..e48bb9bd7 100644 --- a/test/test.js +++ b/test/test.js @@ -399,6 +399,73 @@ describe('conventionalChangelog', function() { done(); })); }); + + it('should still work if first release has no commits (prepend)', function(done) { + shell.exec('git tag v0.0.1 ' + tail); + var i = 0; + + conventionalChangelog({ + releaseCount: 0 + }, { + version: '3.0.0' + }, {}, {}, { + mainTemplate: '{{previousTag}}...{{currentTag}}', + transform: function() { + return null; + } + }) + .pipe(through(function(chunk, enc, cb) { + chunk = chunk.toString(); + + if (i === 0) { + expect(chunk).to.equal('v2.0.0...v3.0.0'); + } else if (i === 1) { + expect(chunk).to.equal('v0.0.1...v2.0.0'); + } else if (i === 2) { + expect(chunk).to.equal('...v0.0.1'); + } + + i++; + cb(); + }, function() { + expect(i).to.equal(3); + done(); + })); + }); + + it('should still work if first release has no commits (append)', function(done) { + shell.exec('git tag v0.0.1 ' + tail); + var i = 0; + + conventionalChangelog({ + releaseCount: 0, + append: true + }, { + version: '3.0.0' + }, {}, {}, { + mainTemplate: '{{previousTag}}...{{currentTag}}', + transform: function() { + return null; + } + }) + .pipe(through(function(chunk, enc, cb) { + chunk = chunk.toString(); + + if (i === 0) { + expect(chunk).to.equal('...v0.0.1'); + } else if (i === 1) { + expect(chunk).to.equal('v0.0.1...v2.0.0'); + } else if (i === 2) { + expect(chunk).to.equal('v2.0.0...v3.0.0'); + } + + i++; + cb(); + }, function() { + expect(i).to.equal(3); + done(); + })); + }); }); it('should warn if preset is not found', function(done) {