Skip to content

Commit 99b6fb2

Browse files
authored
fix(git): adjust commitEntry to match git log output (#1539)
1 parent 7be3724 commit 99b6fb2

3 files changed

Lines changed: 126 additions & 36 deletions

File tree

src/modules/git/index.ts

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
import type { Faker } from '../..';
22

3+
const GIT_DATE_FORMAT_BASE = new Intl.DateTimeFormat('en', {
4+
weekday: 'short',
5+
month: 'short',
6+
day: 'numeric',
7+
hour: '2-digit',
8+
hourCycle: 'h24',
9+
minute: '2-digit',
10+
second: '2-digit',
11+
year: 'numeric',
12+
timeZone: 'UTC',
13+
});
14+
const GIT_TIMEZONE_FORMAT = new Intl.NumberFormat('en', {
15+
minimumIntegerDigits: 4,
16+
maximumFractionDigits: 0,
17+
useGrouping: false,
18+
signDisplay: 'always',
19+
});
20+
321
/**
422
* Module to generate git related entries.
523
*/
@@ -29,7 +47,7 @@ export class GitModule {
2947
}
3048

3149
/**
32-
* Generates a random commit entry.
50+
* Generates a random commit entry as printed by `git log`.
3351
*
3452
* @param options Options for the commit entry.
3553
* @param options.merge Set to `true` to generate a merge message line.
@@ -42,10 +60,10 @@ export class GitModule {
4260
* @example
4361
* faker.git.commitEntry()
4462
* // commit fe8c38a965d13d9794eb36918cb24cebe49a45c2
45-
* // Author: Mable Harvey <Cynthia_Quigley@yahoo.com>
46-
* // Date: Sat Feb 05 2022 15:09:18 GMT+0100 (Mitteleuropäische Normalzeit)
63+
* // Author: Marion Becker <Marion_Becker49@gmail.com>
64+
* // Date: Mon Nov 7 05:38:37 2022 -0600
4765
* //
48-
* // copy primary system
66+
* // generate open-source system
4967
*
5068
* @since 5.0.0
5169
*/
@@ -68,9 +86,16 @@ export class GitModule {
6886
lines.push(`Merge: ${this.shortSha()} ${this.shortSha()}`);
6987
}
7088

89+
const firstName = this.faker.person.firstName();
90+
const lastName = this.faker.person.lastName();
91+
const fullName = this.faker.person.fullName({ firstName, lastName });
92+
const username = this.faker.internet.userName(firstName, lastName);
93+
const user = this.faker.helpers.arrayElement([fullName, username]);
94+
const email = this.faker.internet.email(firstName, lastName);
95+
7196
lines.push(
72-
`Author: ${this.faker.person.firstName()} ${this.faker.person.lastName()} <${this.faker.internet.email()}>`,
73-
`Date: ${this.faker.date.recent(1, refDate).toString()}`,
97+
`Author: ${user} <${email}>`,
98+
`Date: ${this.commitDate({ refDate })}`,
7499
'',
75100
`\xa0\xa0\xa0\xa0${this.commitMessage()}`,
76101
// to end with a eol char
@@ -95,6 +120,38 @@ export class GitModule {
95120
return `${this.faker.hacker.verb()} ${this.faker.hacker.adjective()} ${this.faker.hacker.noun()}`;
96121
}
97122

123+
/**
124+
* Generates a date string for a git commit using the same format as `git log`.
125+
*
126+
* @param options The optional options object.
127+
* @param options.refDate The date to use as reference point for the commit. Defaults to now.
128+
*
129+
* @example
130+
* faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600'
131+
* faker.git.commitDate({ refDate: '2020-01-01' }) // 'Tue Dec 31 05:40:59 2019 -0400'
132+
*
133+
* @since 8.0.0
134+
*/
135+
commitDate(options: { refDate?: string | Date | number } = {}): string {
136+
const { refDate } = options;
137+
138+
const dateParts = GIT_DATE_FORMAT_BASE.format(
139+
this.faker.date.recent(1, refDate)
140+
)
141+
.replace(/,/g, '')
142+
.split(' ');
143+
[dateParts[3], dateParts[4]] = [dateParts[4], dateParts[3]];
144+
145+
// Timezone offset
146+
dateParts.push(
147+
GIT_TIMEZONE_FORMAT.format(
148+
this.faker.datatype.number({ min: -11, max: 12 }) * 100
149+
)
150+
);
151+
152+
return dateParts.join(' ');
153+
}
154+
98155
/**
99156
* Generates a random commit sha (full).
100157
*

test/__snapshots__/git.spec.ts.snap

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,36 @@
22

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

5+
exports[`git > 42 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 15:00:39 2019 +0800"`;
6+
7+
exports[`git > 42 > commitDate > with only number refDate 1`] = `"Tue Dec 31 15:00:39 2019 +0800"`;
8+
9+
exports[`git > 42 > commitDate > with only string refDate 1`] = `"Tue Dec 31 15:00:39 2019 +0800"`;
10+
511
exports[`git > 42 > commitEntry > with only Date refDate 1`] = `
612
"commit be4abdd39321ad7d3fe01ffce404f4d6db0906bd
7-
Author: Gregg Conn <Eladio.Wiza@gmail.com>
8-
Date: Tue Dec 31 2019 13:03:15 GMT+0000 (Coordinated Universal Time)
13+
Author: Gregg Conn <Gregg_Conn19@gmail.com>
14+
Date: Tue Dec 31 14:49:14 2019 +0100
915
10-
    bypass neural pixel
16+
    parse multi-byte sensor
1117
"
1218
`;
1319

1420
exports[`git > 42 > commitEntry > with only number refDate 1`] = `
1521
"commit be4abdd39321ad7d3fe01ffce404f4d6db0906bd
16-
Author: Gregg Conn <Eladio.Wiza@gmail.com>
17-
Date: Tue Dec 31 2019 13:03:15 GMT+0000 (Coordinated Universal Time)
22+
Author: Gregg Conn <Gregg_Conn19@gmail.com>
23+
Date: Tue Dec 31 14:49:14 2019 +0100
1824
19-
    bypass neural pixel
25+
    parse multi-byte sensor
2026
"
2127
`;
2228

2329
exports[`git > 42 > commitEntry > with only string refDate 1`] = `
2430
"commit be4abdd39321ad7d3fe01ffce404f4d6db0906bd
25-
Author: Gregg Conn <Eladio.Wiza@gmail.com>
26-
Date: Tue Dec 31 2019 13:03:15 GMT+0000 (Coordinated Universal Time)
31+
Author: Gregg Conn <Gregg_Conn19@gmail.com>
32+
Date: Tue Dec 31 14:49:14 2019 +0100
2733
28-
    bypass neural pixel
34+
    parse multi-byte sensor
2935
"
3036
`;
3137

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

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

46+
exports[`git > 1211 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 01:42:55 2019 +0000"`;
47+
48+
exports[`git > 1211 > commitDate > with only number refDate 1`] = `"Tue Dec 31 01:42:55 2019 +0000"`;
49+
50+
exports[`git > 1211 > commitDate > with only string refDate 1`] = `"Tue Dec 31 01:42:55 2019 +0000"`;
51+
4052
exports[`git > 1211 > commitEntry > with only Date refDate 1`] = `
4153
"commit adb42f0e3f4a973fab0aeefce96dfcf49cd438df
42-
Author: Imani Runolfsson <Gracie.Gutmann53@gmail.com>
43-
Date: Tue Dec 31 2019 13:53:17 GMT+0000 (Coordinated Universal Time)
54+
Author: Imani Runolfsson <Imani22@hotmail.com>
55+
Date: Tue Dec 31 10:07:32 2019 -0400
4456
45-
    parse back-end program
57+
    override wireless interface
4658
"
4759
`;
4860

4961
exports[`git > 1211 > commitEntry > with only number refDate 1`] = `
5062
"commit adb42f0e3f4a973fab0aeefce96dfcf49cd438df
51-
Author: Imani Runolfsson <Gracie.Gutmann53@gmail.com>
52-
Date: Tue Dec 31 2019 13:53:17 GMT+0000 (Coordinated Universal Time)
63+
Author: Imani Runolfsson <Imani22@hotmail.com>
64+
Date: Tue Dec 31 10:07:32 2019 -0400
5365
54-
    parse back-end program
66+
    override wireless interface
5567
"
5668
`;
5769

5870
exports[`git > 1211 > commitEntry > with only string refDate 1`] = `
5971
"commit adb42f0e3f4a973fab0aeefce96dfcf49cd438df
60-
Author: Imani Runolfsson <Gracie.Gutmann53@gmail.com>
61-
Date: Tue Dec 31 2019 13:53:17 GMT+0000 (Coordinated Universal Time)
72+
Author: Imani Runolfsson <Imani22@hotmail.com>
73+
Date: Tue Dec 31 10:07:32 2019 -0400
6274
63-
    parse back-end program
75+
    override wireless interface
6476
"
6577
`;
6678

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

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

87+
exports[`git > 1337 > commitDate > with only Date refDate 1`] = `"Tue Dec 31 17:42:40 2019 +0200"`;
88+
89+
exports[`git > 1337 > commitDate > with only number refDate 1`] = `"Tue Dec 31 17:42:40 2019 +0200"`;
90+
91+
exports[`git > 1337 > commitDate > with only string refDate 1`] = `"Tue Dec 31 17:42:40 2019 +0200"`;
92+
7593
exports[`git > 1337 > commitEntry > with only Date refDate 1`] = `
7694
"commit c346ba075bd57f5a62b82d72af39cbbb07a98cba
77-
Author: Friedrich Dibbert <Edward.Stracke49@yahoo.com>
78-
Date: Tue Dec 31 2019 13:25:40 GMT+0000 (Coordinated Universal Time)
95+
Author: Friedrich Dibbert <Friedrich41@gmail.com>
96+
Date: Tue Dec 31 04:18:56 2019 -0700
7997
80-
    override back-end interface
98+
    reboot haptic capacitor
8199
"
82100
`;
83101

84102
exports[`git > 1337 > commitEntry > with only number refDate 1`] = `
85103
"commit c346ba075bd57f5a62b82d72af39cbbb07a98cba
86-
Author: Friedrich Dibbert <Edward.Stracke49@yahoo.com>
87-
Date: Tue Dec 31 2019 13:25:40 GMT+0000 (Coordinated Universal Time)
104+
Author: Friedrich Dibbert <Friedrich41@gmail.com>
105+
Date: Tue Dec 31 04:18:56 2019 -0700
88106
89-
    override back-end interface
107+
    reboot haptic capacitor
90108
"
91109
`;
92110

93111
exports[`git > 1337 > commitEntry > with only string refDate 1`] = `
94112
"commit c346ba075bd57f5a62b82d72af39cbbb07a98cba
95-
Author: Friedrich Dibbert <Edward.Stracke49@yahoo.com>
96-
Date: Tue Dec 31 2019 13:25:40 GMT+0000 (Coordinated Universal Time)
113+
Author: Friedrich Dibbert <Friedrich41@gmail.com>
114+
Date: Tue Dec 31 04:18:56 2019 -0700
97115
98-
    override back-end interface
116+
    reboot haptic capacitor
99117
"
100118
`;
101119

test/git.spec.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ describe('git', () => {
1515
seededTests(faker, 'git', (t) => {
1616
t.itEach('branch', 'commitMessage', 'commitSha', 'shortSha');
1717

18-
t.describe('commitEntry', (t) => {
18+
t.describeEach(
19+
'commitEntry',
20+
'commitDate'
21+
)((t) => {
1922
t.it('with only string refDate', { refDate })
2023
.it('with only Date refDate', { refDate: new Date(refDate) })
2124
.it('with only number refDate', {
@@ -51,12 +54,12 @@ describe('git', () => {
5154
expect(parts[0]).toMatch(/^commit [a-f0-9]+$/);
5255
if (parts.length === 7) {
5356
expect(parts[1]).toMatch(/^Merge: [a-f0-9]+ [a-f0-9]+$/);
54-
expect(parts[2]).toMatch(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/);
57+
expect(parts[2]).toMatch(/^Author: [\w_\. ]+ \<[\w\.]+@[\w\.]+\>$/);
5558
expect(parts[3]).toMatch(/^Date: .+$/);
5659
expect(parts[4]).toBe('');
5760
expect(parts[5]).toMatch(/^\s{4}.+$/);
5861
} else {
59-
expect(parts[1]).toMatch(/^Author: \w+ \w+ \<[\w\.]+@[\w\.]+\>$/);
62+
expect(parts[1]).toMatch(/^Author: [\w_\. ]+ \<[\w\.]+@[\w\.]+\>$/);
6063
expect(parts[2]).toMatch(/^Date: .+$/);
6164
expect(parts[3]).toBe('');
6265
expect(parts[4]).toMatch(/^\s{4}.+$/);
@@ -106,6 +109,18 @@ describe('git', () => {
106109
});
107110
});
108111

112+
describe('commitDate', () => {
113+
it('should return a random commitDate', () => {
114+
const commitDate = faker.git.commitDate();
115+
116+
expect(commitDate).toBeTruthy();
117+
expect(commitDate).toBeTypeOf('string');
118+
119+
const parts = commitDate.split(' ');
120+
expect(parts.length).toBe(6);
121+
});
122+
});
123+
109124
describe('commitSha', () => {
110125
it('should return a random commitSha', () => {
111126
const commitSha = faker.git.commitSha();

0 commit comments

Comments
 (0)