Skip to content

Commit

Permalink
Log template and context if rendering throws (fixes #562)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 8, 2019
1 parent d89487c commit 341ce73
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 13 additions & 4 deletions lib/util.js
Expand Up @@ -3,13 +3,22 @@ const { EOL } = require('os');
const _ = require('lodash');
const gitUrlParse = require('git-url-parse');
const semver = require('semver');
const Log = require('./log');
const { TimeoutError } = require('./errors');

const log = new Log();

const format = (template = '', context = {}) => {
template = template.startsWith('git log')
? template.replace('[REV_RANGE]', '${latestTag}...HEAD')
: template.replace(/%s/g, '${version}');
return _.template(template)(context);
try {
template = template.startsWith('git log')
? template.replace('[REV_RANGE]', '${latestTag}...HEAD')
: template.replace(/%s/g, '${version}');
return _.template(template)(context);
} catch (error) {
log.error(`Unable to render template with context:\n${template}\n${JSON.stringify(context)}`);
log.error(error);
throw error;
}
};

const truncateLines = (input, maxLines = 10, surplusText = null) => {
Expand Down
13 changes: 13 additions & 0 deletions test/utils.js
@@ -1,5 +1,7 @@
const { EOL } = require('os');
const test = require('ava');
const mockStdIo = require('mock-stdio');
const stripAnsi = require('strip-ansi');
const { format, truncateLines, parseGitUrl } = require('../lib/util');

test('format', t => {
Expand All @@ -8,6 +10,17 @@ test('format', t => {
t.is(format('release v${version} (${name})', { version: '1.0.0', name: 'foo' }), 'release v1.0.0 (foo)');
});

test('format (throw)', t => {
mockStdIo.start();
t.throws(() => format('release v${foo}', { version: '1.0.0' }), /foo is not defined/);
const { stdout, stderr } = mockStdIo.end();
t.is(stdout, '');
t.regex(
stripAnsi(stderr),
/ERROR Unable to render template with context:\s+release v\${foo}\s+{"version":"1\.0\.0"}\s+ERROR ReferenceError: foo is not defined/
);
});

test('format (back-compat)', t => {
t.is(
format('git log --pretty=format:"* %s (%h)" ${latestTag}...HEAD', { version: '1.0.0', latestTag: '2.0.0' }),
Expand Down

0 comments on commit 341ce73

Please sign in to comment.