Skip to content

fix(prompt): prompt does not respect [body-leading-blank] setting #4066

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

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions @commitlint/prompt/src/input.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
/// <reference path="./inquirer/inquirer.d.ts" />

import {test, expect, vi} from 'vitest';
import {expect, test, vi} from 'vitest';
// @ts-expect-error -- no typings
import config from '@commitlint/config-angular';
import chalk from 'chalk';
import {Answers, DistinctQuestion, PromptModule} from 'inquirer';
import {
Answers,
DistinctQuestion,
InputCustomOptions,
PromptModule,
} from 'inquirer';

import {input} from './input.js';

const testConfig = {
parserPreset: config.parserPreset,
rules: {
...config.rules,
},
};

vi.mock('@commitlint/load', () => ({
default: () => config,
default: () => testConfig,
}));

test('should work with all fields filled', async () => {
const prompt = stub({
'input-custom': {
type: 'fix',
scope: 'test',
subject: 'subject',
body: 'body',
footer: 'footer',
},
});
const message = await input(prompt);
expect(message).toEqual('fix(test): subject\n' + '\nbody\n' + '\nfooter');
});

test('should not add leading blank line to body and footer if rules are disabled', async () => {
testConfig.rules['body-leading-blank'] = ['1', 'never'];
testConfig.rules['footer-leading-blank'] = ['1', 'never'];
const prompt = stub({
'input-custom': {
type: 'fix',
Expand All @@ -24,6 +52,10 @@ test('should work with all fields filled', async () => {
});
const message = await input(prompt);
expect(message).toEqual('fix(test): subject\n' + 'body\n' + 'footer');
// reset config mock
testConfig.rules['body-leading-blank'] = config.rules['body-leading-blank'];
testConfig.rules['footer-leading-blank'] =
config.rules['footer-leading-blank'];
});

test('should work without scope', async () => {
Expand All @@ -37,7 +69,7 @@ test('should work without scope', async () => {
},
});
const message = await input(prompt);
expect(message).toEqual('fix: subject\n' + 'body\n' + 'footer');
expect(message).toEqual('fix: subject\n' + '\nbody\n' + '\nfooter');
});

test('should fail without type', async () => {
Expand Down Expand Up @@ -72,7 +104,7 @@ function stub(config: Record<string, Record<string, unknown>>): PromptModule {
if (!questions) {
throw new Error(`Unexpected config type: ${configType}`);
}
const answer = questions[promptConfig.name!];
let answer = questions[promptConfig.name!];
if (answer == null) {
throw new Error(`Unexpected config name: ${promptConfig.name}`);
}
Expand All @@ -83,7 +115,11 @@ function stub(config: Record<string, Record<string, unknown>>): PromptModule {
throw new Error(validationResult || undefined);
}
}

const forceLeadingBlankFn = (promptConfig as InputCustomOptions)
.forceLeadingBlankFn;
if (forceLeadingBlankFn) {
answer = forceLeadingBlankFn(answer as string);
}
result[promptConfig.name!] = answer;
}
return result;
Expand Down
2 changes: 2 additions & 0 deletions @commitlint/prompt/src/inquirer/InputCustomPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export default class InputCustomPrompt<

onEnd(state: SuccessfulPromptStateData): void {
this.lineSubscription.unsubscribe();
// Add or remove leading blank if rule is active.
state.value = this.opt.forceLeadingBlankFn(state.value);
super.onEnd(state);
}

Expand Down
1 change: 1 addition & 0 deletions @commitlint/prompt/src/inquirer/inquirer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ declare module 'inquirer' {
log?(answers?: T): string;
tabCompletion?: InputCustomCompletionOption[];
maxLength(answers?: T): number;
forceLeadingBlankFn(input: string): string;
}

interface QuestionMap<T extends Answers = Answers> {
Expand Down
1 change: 1 addition & 0 deletions @commitlint/prompt/src/library/get-prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ export default function getPrompt(
transformer(value: string) {
return forceCaseFn(value);
},
forceLeadingBlankFn,
};
}