Skip to content
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

feat: configure eol character for git.commitEntry #681

Merged
merged 8 commits into from
Apr 23, 2022
30 changes: 23 additions & 7 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export class Git {
*
* @param options Options for the commit entry.
* @param options.merge Set to `true` to generate a merge message line.
* @param options.eol Choose the end of line character to use. Defaults to 'CRLF'.
* 'LF' = '\n',
* 'CRLF' = '\r\n'
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* faker.git.commitEntry()
Expand All @@ -59,17 +62,30 @@ export class Git {
* //
* // copy primary system
*/
commitEntry(options: { merge?: boolean } = {}): string {
// TODO @Shinigami92 2022-01-11: We may want to make it configurable to use just `\n` instead of `\r\n`
let entry = `commit ${this.commitSha()}\r\n`;
commitEntry(
options: {
merge?: boolean;
eol?: 'LF' | 'CRLF';
} = {}
): string {
const lines = [`commit ${this.faker.git.commitSha()}`];

if (options.merge || this.faker.datatype.number({ min: 0, max: 4 }) === 0) {
entry += `Merge: ${this.shortSha()}} ${this.shortSha()}\r\n`;
lines.push(`Merge: ${this.shortSha()} ${this.shortSha()}`);
}

entry += `Author: ${this.faker.name.firstName()} ${this.faker.name.lastName()} <${this.faker.internet.email()}>\r\n`;
entry += `Date: ${this.faker.date.recent().toString()}\r\n`;
entry += `\r\n\xa0\xa0\xa0\xa0${this.commitMessage()}\r\n`;
lines.push(
`Author: ${this.faker.name.firstName()} ${this.faker.name.lastName()} <${this.faker.internet.email()}>`,
`Date: ${this.faker.date.recent().toString()}`,
'',
`\xa0\xa0\xa0\xa0${this.commitMessage()}`,
// to end with a eol char
''
);

const eolOption = options.eol ?? 'CRLF';
const eolChar = eolOption === 'CRLF' ? '\r\n' : '\n';
const entry = lines.join(eolChar);

return entry;
}
Expand Down
32 changes: 31 additions & 1 deletion test/git.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('git', () => {
});

describe('commitEntry', () => {
it('should return a random commitEntry', () => {
it('should return a valid random commitEntry', () => {
const commitEntry = faker.git.commitEntry();

expect(commitEntry).toBeTruthy();
Expand All @@ -141,6 +141,36 @@ describe('git', () => {
expect(parts[4]).toMatch(/^\s{4}.+$/);
}
});

it('should return a random commitEntry with a default end of line charcter of "\r\n"', () => {
const commitEntry = faker.git.commitEntry();
const parts = commitEntry.split('\r\n');

expect(parts.length).toBeGreaterThanOrEqual(6);
expect(parts.length).toBeLessThanOrEqual(7);
});

it('should return a random commitEntry with a configured end of line charcter of "\r\n" with eol = CRLF', () => {
const commitEntry = faker.git.commitEntry({
eol: 'CRLF',
});
const parts = commitEntry.split('\r\n');

expect(parts.length).toBeGreaterThanOrEqual(6);
expect(parts.length).toBeLessThanOrEqual(7);
});

it('should return a random commitEntry with a configured end of line charcter of "\n" with eol = LF', () => {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
const commitEntry = faker.git.commitEntry({
eol: 'LF',
});
const parts = commitEntry.split('\n');

expect(parts.length).toBeGreaterThanOrEqual(6);
expect(parts.length).toBeLessThanOrEqual(7);

expect(commitEntry).not.contains('\r\n');
});
});

describe('commitMessage', () => {
Expand Down