Skip to content

Commit

Permalink
fix(changelog): regenerate changeling when release count = 0
Browse files Browse the repository at this point in the history
Improve documentation for release count flag and tests
  • Loading branch information
uglow committed Apr 7, 2017
1 parent 1704b5a commit 9bdeb6f
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ Of course you can change `corp-release` to any name you like.
## Options
* `-d` or `--dryrun`: it runs in non-destructive mode. No alteration should be done in your workspace.
* `--pre-commit [npm-script]`: Pre-commit hook [pre-commit]. Pass a string with the name of the npm script to run. it will run like this: `npm run [pre-commit]`. If you need more hooks to be implemented please open an issue.
* `-b [branch]` or `--branch [branch]`: Branch name allowed to run release. Default is `master`. If you want to release from another branch, you need to specify.
* `-b [branch]` or `--branch [branch]`: Branch name allowed to run release. Default is `master`. If you want to release from another branch, you need to specify.
* `-v` or `--verbose`: it prints extra info such as commit list from last tag and command details.
* `--changelogpreset [preset]`: The conventional-changelog preset to use. Default is `angular`. `angular-bitbucket` is available for [BitBucket repositories](https://github.com/uglow/conventional-changelog-angular-bitbucket). Other presets can be installed, e.g: `npm i conventional-changelog-jquery` then pass this flag to the command: `--changelogpreset jquery`.
* `-r [num]` or `--releasecount [num]`: How many releases of changelog you want to generate. It counts from the upcoming release. Useful when you forgot to generate any previous changelog. Set to 0 to regenerate all (will overwrite any existing changelog!).

**NOTE**: If you run via `npm`, you have to add `--` before the options so npm passes all arguments to node. Eg.: `npm run corp-release -- -v -d`

Expand Down
87 changes: 75 additions & 12 deletions spec/system.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ let fs = require('fs');
let writeFileSync = fs.writeFileSync;
let temp = require('temp').track();

let counter = 0; // Used to create unique content to trigger git changes, to trigger semver bumps.


describe('corp-semantic-release', function() {
// temp dir created for testing.
Expand Down Expand Up @@ -258,29 +260,90 @@ describe('corp-semantic-release', function() {
expect(changelog.indexOf('<a name="1.1.0"></a>')).to.equal(0);

// Old information is not regenerated, which means by default only 1 release is generated
expect(changelog.indexOf('<a name="1.0.0"></a>')).to.equal(-1);
expect(changelog).not.to.include('<a name="1.0.0"></a>');
});


it('should allow a changelog to be generated for all releases', function() {
commitFeat();
semanticRelease();
// expectedGitTag('1.0.0');

let changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog.indexOf('<a name="1.0.0"></a>')).to.equal(0); // First item in file

// Now clear the contents of the changelog, add another feature and re-generate all releases
shell.exec('echo > CHANGELOG.md');
changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog).to.equal('\n');

commitFeat();
semanticRelease(`-r 0`); // regenerate ALL releases (0 = all)
semanticRelease(`--releasecount 0`); // regenerate ALL releases (0 = all)
expectedGitTag('1.1.0');

let changelog = shell.exec('cat CHANGELOG.md').output;
changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog.indexOf('<a name="1.1.0"></a>')).to.equal(0); // First item in file

// Old information HAS been re-generated
expect(changelog).to.include('<a name="1.0.0"></a>');
});


it('should replace an existing changelog when re-generating it for all releases', function() {
shell.exec('echo foo bar > CHANGELOG.md');
let changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog).to.equal('foo bar\n');

// Don't clear the contents of the changelog, add another feature and re-generate all releases
commitFeat();
semanticRelease();
expectedGitTag('1.0.0');

changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog).to.include('<a name="1.0.0"></a>');
expect(changelog).to.include('foo bar');

commitFeat();
semanticRelease(`-r 0`); // regenerate ALL releases (0 = all)
expectedGitTag('1.1.0');

changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog).to.include('<a name="1.1.0"></a>');
expect(changelog).to.include('<a name="1.0.0"></a>');
expect(changelog).not.to.include('foo bar');
});


it('should generate the specified number of releases', function() {
// Add two features, clear the log, add another feature then generate 2 releases (this one and the previous one)
commitFeat();
semanticRelease();
expectedGitTag('1.0.0');

commitFeat();
semanticRelease();
expectedGitTag('1.1.0');

let changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog).to.include('<a name="1.1.0"></a>');
expect(changelog).to.include('<a name="1.0.0"></a>');

// Replace the log with junk, then append 2 releases
shell.exec('echo foo bar > CHANGELOG.md');
changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog).to.equal('foo bar\n');

commitFeat();
semanticRelease(`--releasecount 2`);
expectedGitTag('1.2.0');

changelog = shell.exec('cat CHANGELOG.md').output;
expect(changelog).to.include('<a name="1.2.0"></a>');
expect(changelog).to.include('<a name="1.1.0"></a>');
expect(changelog).not.to.include('<a name="1.0.0"></a>');
expect(changelog).to.include('foo bar');
});


// ####### Helpers ######

// function getBranchName() {
Expand All @@ -293,35 +356,35 @@ describe('corp-semantic-release', function() {
}

function commitFeat() {
writeFileSync('feat.txt', '');
commitWithMessage('feat: my first feature');
writeFileSync('feat.txt', counter++); // Produce a unique change to the file
return commitWithMessage('feat: my first feature');
}

function commitNonReleaseTypes() {
writeFileSync('docs.txt', '');
writeFileSync('docs.txt', counter++);
commitWithMessage('docs: commit 01');

writeFileSync('styles.txt', '');
writeFileSync('styles.txt', counter++);
commitWithMessage('styles: commit 02');

writeFileSync('chore.txt', '');
writeFileSync('chore.txt', counter++);
commitWithMessage('chore: commit 03');
}

function commitFixWithMessage(msg) {
writeFileSync('fix.txt', '');
writeFileSync('fix.txt', counter++);
commitWithMessageMultiline(`-m "${msg}"`);
}

function commitFixWithBreakingChange() {
writeFileSync('fix.txt', '');
writeFileSync('fix.txt', counter++);
const msg = '-m "fix: issue in the app" -m "BREAKING CHANGE:" -m "This should bump major"';

commitWithMessageMultiline(msg);
}

function commitWithMessage(msg) {
shell.exec(`git add --all && git commit -m "${msg}"`);
return shell.exec(`git add --all && git commit -m "${msg}"`).output;
}

function commitWithMessageMultiline(msg) {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function(err, results) {

// ### STEP 7 - Write or Append (DESTRUCTIVE OPERATION)
if (!program.dryrun) {
lib.writeChangelog(changes, program.verbose); // it has to run after the version has been bumped.
lib.writeChangelog(changes, program.releasecount === 0, program.verbose); // it has to run after the version has been bumped.
} else {
log.info('>>> Changelog contents would have been: \n\n', changes);

Expand Down
11 changes: 8 additions & 3 deletions src/lib/writeChangelog.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
'use strict';
const log = require('./log');
const fs = require('fs');
const prependFile = require('prepend-file');
const path = require('path');

module.exports = function writeChangelog(data, verbose) {
module.exports = function writeChangelog(data, replaceContents, verbose) {
if (verbose === undefined) verbose = false;

// var writeFileSync = fs.writeFileSync;
let fileName = path.join(process.cwd(), 'CHANGELOG.md');
if (verbose) log.info('>>> About to write/append contents to CHANGELOG.md... ');
prependFile.sync(fileName, data);

if (replaceContents) {
fs.writeFileSync(fileName, data);
} else {
prependFile.sync(fileName, data);
}
};

0 comments on commit 9bdeb6f

Please sign in to comment.