Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent error when no tags exist. #19

Merged
merged 1 commit into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions index.js
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
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