| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Imports
 | 
					
						
							| 
									
										
										
										
											2020-05-08 14:51:29 -07:00
										 |  |  | import * as validateConfig from './config'; | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  | import {validateCommitMessage, ValidateCommitMessageResult} from './validate'; | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 17:39:55 -07:00
										 |  |  | type CommitMessageConfig = validateConfig.CommitMessageConfig; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | // Constants
 | 
					
						
							| 
									
										
										
										
											2020-06-25 17:39:55 -07:00
										 |  |  | const config: {commitMessage: CommitMessageConfig} = { | 
					
						
							|  |  |  |   commitMessage: { | 
					
						
							|  |  |  |     maxLineLength: 120, | 
					
						
							|  |  |  |     minBodyLength: 0, | 
					
						
							|  |  |  |     scopes: [ | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |       'common', | 
					
						
							|  |  |  |       'compiler', | 
					
						
							|  |  |  |       'core', | 
					
						
							|  |  |  |       'packaging', | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-08-12 09:36:59 -07:00
										 |  |  | const TYPES = Object.keys(validateConfig.COMMIT_TYPES).join(', '); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | const SCOPES = config.commitMessage.scopes.join(', '); | 
					
						
							|  |  |  | const INVALID = false; | 
					
						
							|  |  |  | const VALID = true; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  | function expectValidationResult( | 
					
						
							|  |  |  |     validationResult: ValidateCommitMessageResult, valid: boolean, errors: string[] = []) { | 
					
						
							|  |  |  |   expect(validationResult).toEqual(jasmine.objectContaining({valid, errors})); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | // TODO(josephperrott): Clean up tests to test script rather than for
 | 
					
						
							|  |  |  | // specific commit messages we want to use.
 | 
					
						
							|  |  |  | describe('validate-commit-message.js', () => { | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2020-08-12 09:36:59 -07:00
										 |  |  |     spyOn(validateConfig, 'getCommitMessageConfig') | 
					
						
							|  |  |  |         .and.returnValue(config as ReturnType<typeof validateConfig.getCommitMessageConfig>); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('validateMessage()', () => { | 
					
						
							|  |  |  |     it('should be valid', () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult(validateCommitMessage('feat(packaging): something'), VALID); | 
					
						
							|  |  |  |       expectValidationResult(validateCommitMessage('fix(packaging): something'), VALID); | 
					
						
							|  |  |  |       expectValidationResult(validateCommitMessage('fixup! fix(packaging): something'), VALID); | 
					
						
							|  |  |  |       expectValidationResult(validateCommitMessage('squash! fix(packaging): something'), VALID); | 
					
						
							|  |  |  |       expectValidationResult(validateCommitMessage('Revert: "fix(packaging): something"'), VALID); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should validate max length', () => { | 
					
						
							|  |  |  |       const msg = | 
					
						
							|  |  |  |           'fix(compiler): something super mega extra giga tera long, maybe even longer and longer and longer and longer and longer and longer...'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult(validateCommitMessage(msg), INVALID, [ | 
					
						
							|  |  |  |         `The commit message header is longer than ${config.commitMessage.maxLineLength} characters` | 
					
						
							|  |  |  |       ]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-07 15:58:11 +02:00
										 |  |  |     it('should skip max length limit for URLs', () => { | 
					
						
							|  |  |  |       const msg = 'fix(compiler): this is just an usual commit message tile\n\n' + | 
					
						
							|  |  |  |           'This is a normal commit message body which does not exceed the max length\n' + | 
					
						
							|  |  |  |           'limit. For more details see the following super long URL:\n\n' + | 
					
						
							|  |  |  |           'https://github.com/angular/components/commit/e2ace018ddfad10608e0e32932c43dcfef4095d7#diff-9879d6db96fd29134fc802214163b95a'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult(validateCommitMessage(msg), VALID); | 
					
						
							| 
									
										
										
										
											2020-07-07 15:58:11 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |     it('should validate "<type>(<scope>): <subject>" format', () => { | 
					
						
							|  |  |  |       const msg = 'not correct format'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage(msg), INVALID, | 
					
						
							|  |  |  |           [`The commit message header does not match the expected format.`]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should fail when type is invalid', () => { | 
					
						
							|  |  |  |       const msg = 'weird(core): something'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage(msg), INVALID, | 
					
						
							|  |  |  |           [`'weird' is not an allowed type.\n => TYPES: ${TYPES}`]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should fail when scope is invalid', () => { | 
					
						
							|  |  |  |       const errorMessageFor = (scope: string, header: string) => | 
					
						
							|  |  |  |           `'${scope}' is not an allowed scope.\n => SCOPES: ${SCOPES}`; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage('fix(Compiler): something'), INVALID, | 
					
						
							|  |  |  |           [errorMessageFor('Compiler', 'fix(Compiler): something')]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage('feat(bah): something'), INVALID, | 
					
						
							|  |  |  |           [errorMessageFor('bah', 'feat(bah): something')]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage('fix(webworker): something'), INVALID, | 
					
						
							|  |  |  |           [errorMessageFor('webworker', 'fix(webworker): something')]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage('refactor(security): something'), INVALID, | 
					
						
							|  |  |  |           [errorMessageFor('security', 'refactor(security): something')]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage('refactor(docs): something'), INVALID, | 
					
						
							|  |  |  |           [errorMessageFor('docs', 'refactor(docs): something')]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage('feat(angular): something'), INVALID, | 
					
						
							|  |  |  |           [errorMessageFor('angular', 'feat(angular): something')]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should allow empty scope', () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult(validateCommitMessage('build: blablabla'), VALID); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // We do not want to allow WIP. It is OK to fail the PR build in this case to show that there is
 | 
					
						
							|  |  |  |     // work still to be done (i.e. fixing the commit message).
 | 
					
						
							|  |  |  |     it('should not allow "WIP: ..." syntax', () => { | 
					
						
							|  |  |  |       const msg = 'WIP: fix: something'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |       expectValidationResult( | 
					
						
							|  |  |  |           validateCommitMessage(msg), INVALID, | 
					
						
							|  |  |  |           [`'WIP' is not an allowed type.\n => TYPES: ${TYPES}`]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('(revert)', () => { | 
					
						
							|  |  |  |       it('should allow valid "revert: ..." syntaxes', () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |         expectValidationResult(validateCommitMessage('revert: anything'), VALID); | 
					
						
							|  |  |  |         expectValidationResult(validateCommitMessage('Revert: "anything"'), VALID); | 
					
						
							|  |  |  |         expectValidationResult(validateCommitMessage('revert anything'), VALID); | 
					
						
							|  |  |  |         expectValidationResult(validateCommitMessage('rEvErT anything'), VALID); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not allow "revert(scope): ..." syntax', () => { | 
					
						
							|  |  |  |         const msg = 'revert(compiler): reduce generated code payload size by 65%'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |         expectValidationResult( | 
					
						
							|  |  |  |             validateCommitMessage(msg), INVALID, | 
					
						
							|  |  |  |             [`'revert' is not an allowed type.\n => TYPES: ${TYPES}`]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // https://github.com/angular/angular/issues/23479
 | 
					
						
							|  |  |  |       it('should allow typical Angular messages generated by git', () => { | 
					
						
							|  |  |  |         const msg = | 
					
						
							|  |  |  |             'Revert "fix(compiler): Pretty print object instead of [Object object] (#22689)" (#23442)'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |         expectValidationResult(validateCommitMessage(msg), VALID); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('(squash)', () => { | 
					
						
							| 
									
										
										
										
											2020-04-06 13:18:59 -07:00
										 |  |  |       describe('without `disallowSquash`', () => { | 
					
						
							|  |  |  |         it('should return commits as valid', () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |           expectValidationResult(validateCommitMessage('squash! feat(core): add feature'), VALID); | 
					
						
							|  |  |  |           expectValidationResult(validateCommitMessage('squash! fix: a bug'), VALID); | 
					
						
							|  |  |  |           expectValidationResult(validateCommitMessage('squash! fix a typo'), VALID); | 
					
						
							| 
									
										
										
										
											2020-04-06 13:18:59 -07:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe('with `disallowSquash`', () => { | 
					
						
							|  |  |  |         it('should fail', () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |           expectValidationResult( | 
					
						
							|  |  |  |               validateCommitMessage('fix(core): something', {disallowSquash: true}), VALID); | 
					
						
							|  |  |  |           expectValidationResult( | 
					
						
							|  |  |  |               validateCommitMessage('squash! fix(core): something', {disallowSquash: true}), | 
					
						
							|  |  |  |               INVALID, ['The commit must be manually squashed into the target commit']); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('(fixup)', () => { | 
					
						
							|  |  |  |       describe('without `nonFixupCommitHeaders`', () => { | 
					
						
							| 
									
										
										
										
											2020-04-06 13:18:59 -07:00
										 |  |  |         it('should return commits as valid', () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |           expectValidationResult(validateCommitMessage('fixup! feat(core): add feature'), VALID); | 
					
						
							|  |  |  |           expectValidationResult(validateCommitMessage('fixup! fix: a bug'), VALID); | 
					
						
							|  |  |  |           expectValidationResult(validateCommitMessage('fixup! fixup! fix: a bug'), VALID); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe('with `nonFixupCommitHeaders`', () => { | 
					
						
							|  |  |  |         it('should check that the fixup commit matches a non-fixup one', () => { | 
					
						
							|  |  |  |           const msg = 'fixup! foo'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |           expectValidationResult( | 
					
						
							|  |  |  |               validateCommitMessage( | 
					
						
							|  |  |  |                   msg, {disallowSquash: false, nonFixupCommitHeaders: ['foo', 'bar', 'baz']}), | 
					
						
							|  |  |  |               VALID); | 
					
						
							|  |  |  |           expectValidationResult( | 
					
						
							|  |  |  |               validateCommitMessage( | 
					
						
							|  |  |  |                   msg, {disallowSquash: false, nonFixupCommitHeaders: ['bar', 'baz', 'foo']}), | 
					
						
							|  |  |  |               VALID); | 
					
						
							|  |  |  |           expectValidationResult( | 
					
						
							|  |  |  |               validateCommitMessage( | 
					
						
							|  |  |  |                   msg, {disallowSquash: false, nonFixupCommitHeaders: ['baz', 'foo', 'bar']}), | 
					
						
							|  |  |  |               VALID); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           expectValidationResult( | 
					
						
							|  |  |  |               validateCommitMessage( | 
					
						
							|  |  |  |                   msg, {disallowSquash: false, nonFixupCommitHeaders: ['qux', 'quux', 'quuux']}), | 
					
						
							|  |  |  |               INVALID, | 
					
						
							|  |  |  |               ['Unable to find match for fixup commit among prior commits: \n' + | 
					
						
							|  |  |  |                '      qux\n' + | 
					
						
							|  |  |  |                '      quux\n' + | 
					
						
							|  |  |  |                '      quuux']); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should fail if `nonFixupCommitHeaders` is empty', () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |           expectValidationResult( | 
					
						
							|  |  |  |               validateCommitMessage( | 
					
						
							|  |  |  |                   'refactor(core): make reactive', | 
					
						
							|  |  |  |                   {disallowSquash: false, nonFixupCommitHeaders: []}), | 
					
						
							|  |  |  |               VALID); | 
					
						
							|  |  |  |           expectValidationResult( | 
					
						
							|  |  |  |               validateCommitMessage( | 
					
						
							|  |  |  |                   'fixup! foo', {disallowSquash: false, nonFixupCommitHeaders: []}), | 
					
						
							|  |  |  |               INVALID, [`Unable to find match for fixup commit among prior commits: -`]); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:39:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     describe('minBodyLength', () => { | 
					
						
							|  |  |  |       const minBodyLengthConfig: {commitMessage: CommitMessageConfig} = { | 
					
						
							|  |  |  |         commitMessage: { | 
					
						
							|  |  |  |           maxLineLength: 120, | 
					
						
							|  |  |  |           minBodyLength: 30, | 
					
						
							|  |  |  |           minBodyLengthTypeExcludes: ['docs'], | 
					
						
							|  |  |  |           scopes: ['core'] | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       beforeEach(() => { | 
					
						
							|  |  |  |         (validateConfig.getCommitMessageConfig as jasmine.Spy).and.returnValue(minBodyLengthConfig); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should fail validation if the body is shorter than `minBodyLength`', () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |         expectValidationResult( | 
					
						
							|  |  |  |             validateCommitMessage( | 
					
						
							|  |  |  |                 'fix(core): something\n\n Explanation of the motivation behind this change'), | 
					
						
							|  |  |  |             VALID); | 
					
						
							|  |  |  |         expectValidationResult( | 
					
						
							|  |  |  |             validateCommitMessage('fix(core): something\n\n too short'), INVALID, | 
					
						
							|  |  |  |             ['The commit message body does not meet the minimum length of 30 characters']); | 
					
						
							|  |  |  |         expectValidationResult(validateCommitMessage('fix(core): something'), INVALID, [ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           'The commit message body does not meet the minimum length of 30 characters' | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:39:55 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should pass validation if the body is shorter than `minBodyLength` but the commit type is in the `minBodyLengthTypeExclusions` list', | 
					
						
							|  |  |  |          () => { | 
					
						
							| 
									
										
										
										
											2020-09-03 14:54:31 -07:00
										 |  |  |            expectValidationResult(validateCommitMessage('docs: just fixing a typo'), VALID); | 
					
						
							|  |  |  |            expectValidationResult(validateCommitMessage('docs(core): just fixing a typo'), VALID); | 
					
						
							|  |  |  |            expectValidationResult( | 
					
						
							|  |  |  |                validateCommitMessage( | 
					
						
							|  |  |  |                    'docs(core): just fixing a typo\n\nThis was just a silly typo.'), | 
					
						
							|  |  |  |                VALID); | 
					
						
							| 
									
										
										
										
											2020-06-25 17:39:55 -07:00
										 |  |  |          }); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-10 10:29:44 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |