Add commit message normalizer hook and install script
- scripts/normalize-commit-msg.js: capitalizes subject, strips trailing period, trims whitespace, warns when subject > 72 chars - scripts/commit-msg: shell wrapper symlinked into .git/hooks/commit-msg - scripts/install-hooks.sh: contributor setup script (sh scripts/install-hooks.sh) - scripts/package.json: test runner + hooks:install npm script - scripts/__tests__/normalize-commit-msg.test.js: 15 unit tests Nightshift-Task: commit-normalize Nightshift-Ref: https://github.com/marcus/nightshift
This commit is contained in:
90
scripts/__tests__/normalize-commit-msg.test.js
Normal file
90
scripts/__tests__/normalize-commit-msg.test.js
Normal file
@@ -0,0 +1,90 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
const { normalizeSubject, normalizeMessage } = require('../normalize-commit-msg.js');
|
||||
|
||||
describe('normalizeSubject', () => {
|
||||
it('passes an already-valid subject unchanged', () => {
|
||||
const { subject, warned } = normalizeSubject('Add feature flag support');
|
||||
expect(subject).toBe('Add feature flag support');
|
||||
expect(warned).toBe(false);
|
||||
});
|
||||
|
||||
it('capitalizes the first letter', () => {
|
||||
const { subject } = normalizeSubject('add feature flag support');
|
||||
expect(subject).toBe('Add feature flag support');
|
||||
});
|
||||
|
||||
it('strips a trailing period', () => {
|
||||
const { subject } = normalizeSubject('Add feature flag support.');
|
||||
expect(subject).toBe('Add feature flag support');
|
||||
});
|
||||
|
||||
it('trims leading whitespace', () => {
|
||||
const { subject } = normalizeSubject(' Fix the bug');
|
||||
expect(subject).toBe('Fix the bug');
|
||||
});
|
||||
|
||||
it('trims trailing whitespace', () => {
|
||||
const { subject } = normalizeSubject('Fix the bug ');
|
||||
expect(subject).toBe('Fix the bug');
|
||||
});
|
||||
|
||||
it('capitalizes and strips period together', () => {
|
||||
const { subject } = normalizeSubject('fix the bug.');
|
||||
expect(subject).toBe('Fix the bug');
|
||||
});
|
||||
|
||||
it('does not strip a period that is not trailing', () => {
|
||||
const { subject } = normalizeSubject('Fix bug in v1.0 release');
|
||||
expect(subject).toBe('Fix bug in v1.0 release');
|
||||
});
|
||||
|
||||
it('warns when subject exceeds 72 characters', () => {
|
||||
const long = 'A'.repeat(73);
|
||||
const { warned } = normalizeSubject(long);
|
||||
expect(warned).toBe(true);
|
||||
});
|
||||
|
||||
it('does not warn when subject is exactly 72 characters', () => {
|
||||
const exact = 'A'.repeat(72);
|
||||
const { warned } = normalizeSubject(exact);
|
||||
expect(warned).toBe(false);
|
||||
});
|
||||
|
||||
it('does not warn when subject is under 72 characters', () => {
|
||||
const { warned } = normalizeSubject('Short message');
|
||||
expect(warned).toBe(false);
|
||||
});
|
||||
|
||||
it('handles an empty subject gracefully', () => {
|
||||
const { subject, warned } = normalizeSubject('');
|
||||
expect(subject).toBe('');
|
||||
expect(warned).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeMessage', () => {
|
||||
it('normalizes only the subject line of a multi-line message', () => {
|
||||
const input = 'fix the bug.\n\nThis is the body paragraph.';
|
||||
const { message } = normalizeMessage(input);
|
||||
expect(message).toBe('Fix the bug\n\nThis is the body paragraph.');
|
||||
});
|
||||
|
||||
it('skips comment lines when finding the subject', () => {
|
||||
const input = '# Comment\nfix the bug.';
|
||||
const { message } = normalizeMessage(input);
|
||||
expect(message).toBe('# Comment\nFix the bug');
|
||||
});
|
||||
|
||||
it('returns warned true for long subject inside full message', () => {
|
||||
const longSubject = 'x'.repeat(73);
|
||||
const input = `${longSubject}\n\nBody.`;
|
||||
const { warned } = normalizeMessage(input);
|
||||
expect(warned).toBe(true);
|
||||
});
|
||||
|
||||
it('preserves body lines exactly as-is', () => {
|
||||
const input = 'Fix bug\n\n - detail one\n - detail two.';
|
||||
const { message } = normalizeMessage(input);
|
||||
expect(message).toBe('Fix bug\n\n - detail one\n - detail two.');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user