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

refactor(git): length for commit sha #1863

Merged
merged 4 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
46 changes: 38 additions & 8 deletions src/modules/git/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Faker } from '../..';
import { deprecated } from '../../internal/deprecated';

const GIT_DATE_FORMAT_BASE = new Intl.DateTimeFormat('en', {
weekday: 'short',
Expand Down Expand Up @@ -104,7 +105,11 @@ export class GitModule {
const lines = [`commit ${this.faker.git.commitSha()}`];

if (merge) {
lines.push(`Merge: ${this.shortSha()} ${this.shortSha()}`);
lines.push(
`Merge: ${this.commitSha({ length: 7 })} ${this.commitSha({
length: 7,
})}`
);
}

const firstName = this.faker.person.firstName();
Expand Down Expand Up @@ -186,16 +191,37 @@ export class GitModule {
}

/**
* Generates a random commit sha (full).
* Generates a random commit sha.
*
* By default, the length of the commit sha is 40 characters.
*
* For a shorter commit sha, use the `length` option.
*
* Usual short commit sha length is:
* - 7 for GitHub
* - 8 for GitLab
*
* @param options Options for the commit sha.
* @param options.length The length of the commit sha. Defaults to 40.
*
* @example
* faker.git.commitSha() // '2c6e3880fd94ddb7ef72d34e683cdc0c47bec6e6'
*
* @since 5.0.0
*/
commitSha(): string {
commitSha(
options: {
/**
* The length of the commit sha.
*
* @default 40
*/
length?: number;
} = {}
): string {
const { length = 40 } = options;
return this.faker.string.hexadecimal({
length: 40,
length,
casing: 'lower',
prefix: '',
});
Expand All @@ -208,12 +234,16 @@ export class GitModule {
* faker.git.shortSha() // '6155732'
*
* @since 5.0.0
*
* @deprecated Use `faker.git.commitSha({ length: 7 })` instead.
*/
shortSha(): string {
return this.faker.string.hexadecimal({
length: 7,
casing: 'lower',
prefix: '',
deprecated({
deprecated: 'faker.git.shortSha()',
proposed: 'faker.git.commitSha({ length: 7 })',
since: '8.0',
until: '9.0',
});
return this.commitSha({ length: 7 });
}
}
18 changes: 12 additions & 6 deletions test/__snapshots__/git.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ Date: Tue Dec 31 14:49:14 2019 +0100

exports[`git > 42 > commitMessage 1`] = `"navigate neural capacitor"`;

exports[`git > 42 > commitSha 1`] = `"8be4abdd39321ad7d3fe01ffce404f4d6db0906b"`;
exports[`git > 42 > commitSha > noArgs 1`] = `"8be4abdd39321ad7d3fe01ffce404f4d6db0906b"`;

exports[`git > 42 > shortSha 1`] = `"8be4abd"`;
exports[`git > 42 > commitSha > with length 7 1`] = `"8be4abd"`;

exports[`git > 42 > commitSha > with length 8 1`] = `"8be4abdd"`;

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

Expand Down Expand Up @@ -78,9 +80,11 @@ Date: Tue Dec 31 20:11:06 2019 -0600

exports[`git > 1211 > commitMessage 1`] = `"reboot online circuit"`;

exports[`git > 1211 > commitSha 1`] = `"eadb42f0e3f4a973fab0aeefce96dfcf49cd438d"`;
exports[`git > 1211 > commitSha > noArgs 1`] = `"eadb42f0e3f4a973fab0aeefce96dfcf49cd438d"`;

exports[`git > 1211 > commitSha > with length 7 1`] = `"eadb42f"`;

exports[`git > 1211 > shortSha 1`] = `"eadb42f"`;
exports[`git > 1211 > commitSha > with length 8 1`] = `"eadb42f0"`;

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

Expand Down Expand Up @@ -119,6 +123,8 @@ Date: Tue Dec 31 14:05:04 2019 +0800

exports[`git > 1337 > commitMessage 1`] = `"compress multi-byte panel"`;

exports[`git > 1337 > commitSha 1`] = `"5c346ba075bd57f5a62b82d72af39cbbb07a98cb"`;
exports[`git > 1337 > commitSha > noArgs 1`] = `"5c346ba075bd57f5a62b82d72af39cbbb07a98cb"`;

exports[`git > 1337 > commitSha > with length 7 1`] = `"5c346ba"`;

exports[`git > 1337 > shortSha 1`] = `"5c346ba"`;
exports[`git > 1337 > commitSha > with length 8 1`] = `"5c346ba0"`;
36 changes: 24 additions & 12 deletions test/git.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@ describe('git', () => {
});

seededTests(faker, 'git', (t) => {
t.itEach('branch', 'commitMessage', 'commitSha', 'shortSha');
t.itEach('branch', 'commitMessage');

t.describe('commitSha', (t) => {
t.it('noArgs')
.it('with length 7', { length: 7 })
.it('with length 8', { length: 8 });
});

t.skip('shortSha');

t.describeEach(
'commitEntry',
Expand Down Expand Up @@ -122,25 +130,29 @@ describe('git', () => {
});

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

expect(commitSha).toBeTruthy();
expect(commitSha).toBeTypeOf('string');
expect(commitSha).toSatisfy(validator.isHexadecimal);
expect(commitSha).toHaveLength(40);
});
});

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

expect(shortSha).toBeTruthy();
expect(shortSha).toBeTypeOf('string');
expect(shortSha).toSatisfy(validator.isHexadecimal);
expect(shortSha).toHaveLength(7);
});
it.each([
['GitHub', 7],
['GitLab', 8],
])(
'should return a random short commitSha for %s',
(_provider, length) => {
const commitSha = faker.git.commitSha({ length });

expect(commitSha).toBeTruthy();
expect(commitSha).toBeTypeOf('string');
expect(commitSha).toSatisfy(validator.isHexadecimal);
expect(commitSha).toHaveLength(length);
}
);
});
}
});
Expand Down