Skip to content

Commit

Permalink
Prevent error when no tags exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Mar 25, 2020
1 parent d209e46 commit d9dbb4b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ module.exports = class LernaChangelogGeneratorPlugin extends Plugin {
}

async getTagForHEAD() {
let tag = await this.exec('git describe --tags --abbrev=0', { options: { write: false } });

return tag;
try {
return await this.exec('git describe --tags --abbrev=0', { options: { write: false } });
} catch(error) {
return null;
}
}

async getFirstCommit() {
Expand Down
35 changes: 34 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ class TestPlugin extends Plugin {
this.shell.execFormattedCommand = async (command, options) => {
this.commands.push([command, options]);
if (this.responses[command]) {
return Promise.resolve(this.responses[command]);
let response = this.responses[command];

if (typeof response === 'string') {
return Promise.resolve(response);
} else if (typeof response === 'object' && response !== null && response.reject === true) {
return Promise.reject(response.value);
}
}
};
}
Expand Down Expand Up @@ -102,6 +108,33 @@ test('it sets the changelog without version information onto the config', async
t.is(changelog, 'The changelog');
});

test('it uses the first commit when no tags exist', async t => {
let infile = tmp.fileSync().name;

let plugin = buildPlugin({ infile });
plugin.config.setContext({ git: { tagName: 'v${version}' } });

Object.assign(plugin.responses, {
'git describe --tags --abbrev=0': {
reject: true,
value: 'hahahahaah, does not exist',
},
'git rev-list --max-parents=0 HEAD': 'aabc',
[`${LERNA_PATH} --next-version=Unreleased --from=aabc`]: `### Unreleased\n\nThe changelog\n### v1.0.0\n\nThe old changelog`,
});

await runTasks(plugin);

t.deepEqual(plugin.commands, [
['git describe --tags --abbrev=0', { write: false }],
['git rev-list --max-parents=0 HEAD', { write: false }],
[`${LERNA_PATH} --next-version=Unreleased --from=aabc`, { write: false }],
]);

const changelog = fs.readFileSync(infile, { encoding: 'utf8' });
t.is(changelog.trim(), '### v1.0.1\n\nThe changelog\n### v1.0.0\n\nThe old changelog');
});

test('it writes the changelog to the specified file when it did not exist', async t => {
let infile = tmp.fileSync().name;
fs.unlinkSync(infile);
Expand Down

0 comments on commit d9dbb4b

Please sign in to comment.