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

fix(git): adjust commitEntry to match git log output #1539

Merged
merged 4 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
70 changes: 64 additions & 6 deletions src/modules/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import type { Faker } from '../..';

const gitDateFormatBase = new Intl.DateTimeFormat('en-US', {
weekday: 'short',
month: 'short',
day: 'numeric',
hour: '2-digit',
hourCycle: 'h24',
minute: '2-digit',
second: '2-digit',
year: 'numeric',
timeZone: 'UTC',
});

/**
* Module to generate git related entries.
*/
Expand Down Expand Up @@ -29,7 +41,7 @@ export class GitModule {
}

/**
* Generates a random commit entry.
* Generates a random commit entry as printed by `git log`.
*
* @param options Options for the commit entry.
* @param options.merge Set to `true` to generate a merge message line.
Expand All @@ -42,10 +54,10 @@ export class GitModule {
* @example
* faker.git.commitEntry()
* // commit fe8c38a965d13d9794eb36918cb24cebe49a45c2
* // Author: Mable Harvey <Cynthia_Quigley@yahoo.com>
* // Date: Sat Feb 05 2022 15:09:18 GMT+0100 (Mitteleuropäische Normalzeit)
* // Author: Marion Becker <Marion_Becker49@gmail.com>
* // Date: Mon Nov 7 05:38:37 2022 -0600
* //
* // copy primary system
* // generate open-source system
*
* @since 5.0.0
*/
Expand All @@ -68,9 +80,16 @@ export class GitModule {
lines.push(`Merge: ${this.shortSha()} ${this.shortSha()}`);
}

const firstName = this.faker.person.firstName();
const lastName = this.faker.person.lastName();
const fullName = this.faker.person.fullName({ firstName, lastName });
const username = this.faker.internet.userName(firstName, lastName);
const user = this.faker.helpers.arrayElement([fullName, username]);
const email = this.faker.internet.email(firstName, lastName);

lines.push(
`Author: ${this.faker.person.firstName()} ${this.faker.person.lastName()} <${this.faker.internet.email()}>`,
`Date: ${this.faker.date.recent(1, refDate).toString()}`,
`Author: ${user} <${email}>`,
`Date: ${this.commitDate({ refDate })}`,
'',
`\xa0\xa0\xa0\xa0${this.commitMessage()}`,
// to end with a eol char
Expand All @@ -95,6 +114,45 @@ export class GitModule {
return `${this.faker.hacker.verb()} ${this.faker.hacker.adjective()} ${this.faker.hacker.noun()}`;
}

/**
* Generates a date string for a git commit using the same format as `git log`.
*
* @param options The optional options object.
* @param options.refDate The date to use as reference point for the commit. Defaults to now.
*
* @example
* faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600'
* faker.git.commitDate({ refDate: '2020-01-01' }) // 'Tue Dec 31 05:40:59 2019 -0400'
*
* @since 8.0.0
*/
commitDate(options: { refDate?: string | Date | number } = {}): string {
const { refDate } = options;

const dateParts = gitDateFormatBase
.format(this.faker.date.recent(1, refDate))
.replace(/,/g, '')
.split(' ');
[dateParts[3], dateParts[4]] = [dateParts[4], dateParts[3]];

// Timezone offset
dateParts.push(
this.faker.helpers.arrayElement([
'+0000',
`+${this.faker.datatype
.number({ min: 1, max: 11 })
.toString()
.padStart(2, '0')}00`,
`-${this.faker.datatype
.number({ min: 1, max: 11 })
.toString()
.padStart(2, '0')}00`,
])
);

return dateParts.join(' ');
}

/**
* Generates a random commit sha (full).
*
Expand Down
72 changes: 45 additions & 27 deletions test/__snapshots__/git.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@

exports[`git > 42 > branch 1`] = `"array-transmit"`;

exports[`git > 42 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 15:00:39 2019 +0000"`;

exports[`git > 42 > commitDate > with only number refDate 1`] = `"Tue Dec 31 15:00:39 2019 +0000"`;

exports[`git > 42 > commitDate > with only string refDate 1`] = `"Tue Dec 31 15:00:39 2019 +0000"`;

exports[`git > 42 > commitEntry > with only Date refDate 1`] = `
"commit be4abdd39321ad7d3fe01ffce404f4d6db0906bd
Author: Gregg Conn <Eladio.Wiza@gmail.com>
Date: Tue Dec 31 2019 13:03:15 GMT+0000 (Coordinated Universal Time)
Author: Gregg Conn <Gregg_Conn19@gmail.com>
Date: Tue Dec 31 14:49:14 2019 +0600

    bypass neural pixel
    connect auxiliary bus
"
`;

exports[`git > 42 > commitEntry > with only number refDate 1`] = `
"commit be4abdd39321ad7d3fe01ffce404f4d6db0906bd
Author: Gregg Conn <Eladio.Wiza@gmail.com>
Date: Tue Dec 31 2019 13:03:15 GMT+0000 (Coordinated Universal Time)
Author: Gregg Conn <Gregg_Conn19@gmail.com>
Date: Tue Dec 31 14:49:14 2019 +0600

    bypass neural pixel
    connect auxiliary bus
"
`;

exports[`git > 42 > commitEntry > with only string refDate 1`] = `
"commit be4abdd39321ad7d3fe01ffce404f4d6db0906bd
Author: Gregg Conn <Eladio.Wiza@gmail.com>
Date: Tue Dec 31 2019 13:03:15 GMT+0000 (Coordinated Universal Time)
Author: Gregg Conn <Gregg_Conn19@gmail.com>
Date: Tue Dec 31 14:49:14 2019 +0600

    bypass neural pixel
    connect auxiliary bus
"
`;

Expand All @@ -37,30 +43,36 @@ exports[`git > 42 > shortSha 1`] = `"8be4abd"`;

exports[`git > 1211 > branch 1`] = `"capacitor-connect"`;

exports[`git > 1211 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 01:42:55 2019 -1000"`;

exports[`git > 1211 > commitDate > with only number refDate 1`] = `"Tue Dec 31 01:42:55 2019 -1000"`;

exports[`git > 1211 > commitDate > with only string refDate 1`] = `"Tue Dec 31 01:42:55 2019 -1000"`;

exports[`git > 1211 > commitEntry > with only Date refDate 1`] = `
"commit adb42f0e3f4a973fab0aeefce96dfcf49cd438df
Author: Imani Runolfsson <Gracie.Gutmann53@gmail.com>
Date: Tue Dec 31 2019 13:53:17 GMT+0000 (Coordinated Universal Time)
Author: Imani Runolfsson <Imani22@hotmail.com>
Date: Tue Dec 31 10:07:32 2019 -0200

    parse back-end program
    navigate haptic circuit
"
`;

exports[`git > 1211 > commitEntry > with only number refDate 1`] = `
"commit adb42f0e3f4a973fab0aeefce96dfcf49cd438df
Author: Imani Runolfsson <Gracie.Gutmann53@gmail.com>
Date: Tue Dec 31 2019 13:53:17 GMT+0000 (Coordinated Universal Time)
Author: Imani Runolfsson <Imani22@hotmail.com>
Date: Tue Dec 31 10:07:32 2019 -0200

    parse back-end program
    navigate haptic circuit
"
`;

exports[`git > 1211 > commitEntry > with only string refDate 1`] = `
"commit adb42f0e3f4a973fab0aeefce96dfcf49cd438df
Author: Imani Runolfsson <Gracie.Gutmann53@gmail.com>
Date: Tue Dec 31 2019 13:53:17 GMT+0000 (Coordinated Universal Time)
Author: Imani Runolfsson <Imani22@hotmail.com>
Date: Tue Dec 31 10:07:32 2019 -0200

    parse back-end program
    navigate haptic circuit
"
`;

Expand All @@ -72,30 +84,36 @@ exports[`git > 1211 > shortSha 1`] = `"eadb42f"`;

exports[`git > 1337 > branch 1`] = `"port-quantify"`;

exports[`git > 1337 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 17:42:40 2019 +0000"`;

exports[`git > 1337 > commitDate > with only number refDate 1`] = `"Tue Dec 31 17:42:40 2019 +0000"`;

exports[`git > 1337 > commitDate > with only string refDate 1`] = `"Tue Dec 31 17:42:40 2019 +0000"`;

exports[`git > 1337 > commitEntry > with only Date refDate 1`] = `
"commit c346ba075bd57f5a62b82d72af39cbbb07a98cba
Author: Friedrich Dibbert <Edward.Stracke49@yahoo.com>
Date: Tue Dec 31 2019 13:25:40 GMT+0000 (Coordinated Universal Time)
Author: Friedrich Dibbert <Friedrich41@gmail.com>
Date: Tue Dec 31 04:18:56 2019 +0300

    override back-end interface
    reboot neural pixel
"
`;

exports[`git > 1337 > commitEntry > with only number refDate 1`] = `
"commit c346ba075bd57f5a62b82d72af39cbbb07a98cba
Author: Friedrich Dibbert <Edward.Stracke49@yahoo.com>
Date: Tue Dec 31 2019 13:25:40 GMT+0000 (Coordinated Universal Time)
Author: Friedrich Dibbert <Friedrich41@gmail.com>
Date: Tue Dec 31 04:18:56 2019 +0300

    override back-end interface
    reboot neural pixel
"
`;

exports[`git > 1337 > commitEntry > with only string refDate 1`] = `
"commit c346ba075bd57f5a62b82d72af39cbbb07a98cba
Author: Friedrich Dibbert <Edward.Stracke49@yahoo.com>
Date: Tue Dec 31 2019 13:25:40 GMT+0000 (Coordinated Universal Time)
Author: Friedrich Dibbert <Friedrich41@gmail.com>
Date: Tue Dec 31 04:18:56 2019 +0300

    override back-end interface
    reboot neural pixel
"
`;

Expand Down
21 changes: 18 additions & 3 deletions test/git.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ describe('git', () => {
seededTests(faker, 'git', (t) => {
t.itEach('branch', 'commitMessage', 'commitSha', 'shortSha');

t.describe('commitEntry', (t) => {
t.describeEach(
'commitEntry',
'commitDate'
)((t) => {
t.it('with only string refDate', { refDate })
.it('with only Date refDate', { refDate: new Date(refDate) })
.it('with only number refDate', {
Expand Down Expand Up @@ -51,12 +54,12 @@ describe('git', () => {
expect(parts[0]).toMatch(/^commit [a-f0-9]+$/);
if (parts.length === 7) {
expect(parts[1]).toMatch(/^Merge: [a-f0-9]+ [a-f0-9]+$/);
expect(parts[2]).toMatch(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/);
expect(parts[2]).toMatch(/^Author: [\w_\. ]+ \<[\w\.]+@[\w\.]+\>$/);
expect(parts[3]).toMatch(/^Date: .+$/);
expect(parts[4]).toBe('');
expect(parts[5]).toMatch(/^\s{4}.+$/);
} else {
expect(parts[1]).toMatch(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/);
expect(parts[1]).toMatch(/^Author: [\w_\. ]+ \<[\w\.]+@[\w\.]+\>$/);
expect(parts[2]).toMatch(/^Date: .+$/);
expect(parts[3]).toBe('');
expect(parts[4]).toMatch(/^\s{4}.+$/);
Expand Down Expand Up @@ -106,6 +109,18 @@ describe('git', () => {
});
});

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

expect(commitDate).toBeTruthy();
expect(commitDate).toBeTypeOf('string');

const parts = commitDate.split(' ');
expect(parts.length).toBe(6);
});
});

describe('commitSha', () => {
it('should return a random commitSha', () => {
const commitSha = faker.git.commitSha();
Expand Down