From 7e6989ee4b503b28117bd1c462e4fee9b16806ec Mon Sep 17 00:00:00 2001 From: Joey Perrott Date: Tue, 6 Apr 2021 12:37:55 -0700 Subject: [PATCH] test(dev-infra): extract commit message build function into testing util function (#41476) Creates a testing utility function to build commit message strings. PR Close #41476 --- dev-infra/commit-message/parse.spec.ts | 11 +++------- dev-infra/commit-message/test-util.ts | 30 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 dev-infra/commit-message/test-util.ts diff --git a/dev-infra/commit-message/parse.spec.ts b/dev-infra/commit-message/parse.spec.ts index b1ec355a81..4fa8b623cd 100644 --- a/dev-infra/commit-message/parse.spec.ts +++ b/dev-infra/commit-message/parse.spec.ts @@ -7,9 +7,9 @@ */ import {parseCommitMessage} from './parse'; +import {commitMessageBuilder, CommitMessageParts} from './test-util'; - -const commitValues = { +const commitValues: CommitMessageParts = { prefix: '', type: 'fix', npmScope: '', @@ -19,12 +19,7 @@ const commitValues = { footer: 'Closes #1', }; -function buildCommitMessage(params: Partial = {}) { - const {prefix, npmScope, type, scope, summary, body, footer} = {...commitValues, ...params}; - const scopeSlug = npmScope ? `${npmScope}/${scope}` : scope; - return `${prefix}${type}${scopeSlug ? '(' + scopeSlug + ')' : ''}: ${summary}\n\n${body}\n\n${ - footer}`; -} +const buildCommitMessage = commitMessageBuilder(commitValues); describe('commit message parsing:', () => { diff --git a/dev-infra/commit-message/test-util.ts b/dev-infra/commit-message/test-util.ts new file mode 100644 index 0000000000..0371cf0ec3 --- /dev/null +++ b/dev-infra/commit-message/test-util.ts @@ -0,0 +1,30 @@ +/** + * @license + * Copyright Google LLC All Rights Reserved. + * + * Use of this source code is governed by an MIT-style license that can be + * found in the LICENSE file at https://angular.io/license + */ + +/** The parts that make up a commit message for creating a commit message string. */ +export interface CommitMessageParts { + prefix: string; + type: string; + npmScope: string; + scope: string; + summary: string; + body: string; + footer: string; +} + +/** + * Generate a commit message builder function, using the provided defaults. + */ +export function commitMessageBuilder(defaults: CommitMessageParts) { + return (params: Partial = {}) => { + const {prefix, type, npmScope, scope, summary, body, footer} = {...defaults, ...params}; + const scopeSlug = npmScope ? `${npmScope}/${scope}` : scope; + return `${prefix}${type}${scopeSlug ? '(' + scopeSlug + ')' : ''}: ${summary}\n\n${body}\n\n${ + footer}`; + }; +}