diff options
| author | Shinigami <[email protected]> | 2023-02-23 00:59:29 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-02-22 23:59:29 +0000 |
| commit | aa3e771dbef3d17cd90d9b8941cb091136087ed1 (patch) | |
| tree | a9a501aa766fb80652df5db85476d510a40db3eb | |
| parent | 25bd847545acb13291ac0a3704688793ca9a0933 (diff) | |
| download | faker-aa3e771dbef3d17cd90d9b8941cb091136087ed1.tar.xz faker-aa3e771dbef3d17cd90d9b8941cb091136087ed1.zip | |
refactor(git): length for commit sha (#1863)
| -rw-r--r-- | src/modules/git/index.ts | 46 | ||||
| -rw-r--r-- | test/__snapshots__/git.spec.ts.snap | 18 | ||||
| -rw-r--r-- | test/git.spec.ts | 36 |
3 files changed, 74 insertions, 26 deletions
diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts index 5a963840..bb0bca09 100644 --- a/src/modules/git/index.ts +++ b/src/modules/git/index.ts @@ -1,4 +1,5 @@ import type { Faker } from '../..'; +import { deprecated } from '../../internal/deprecated'; const GIT_DATE_FORMAT_BASE = new Intl.DateTimeFormat('en', { weekday: 'short', @@ -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(); @@ -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: '', }); @@ -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 }); } } diff --git a/test/__snapshots__/git.spec.ts.snap b/test/__snapshots__/git.spec.ts.snap index bdad5290..d1c74bd5 100644 --- a/test/__snapshots__/git.spec.ts.snap +++ b/test/__snapshots__/git.spec.ts.snap @@ -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"`; @@ -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"`; @@ -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"`; diff --git a/test/git.spec.ts b/test/git.spec.ts index a69b1baa..88c864ee 100644 --- a/test/git.spec.ts +++ b/test/git.spec.ts @@ -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', @@ -122,7 +130,7 @@ 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(); @@ -130,17 +138,21 @@ describe('git', () => { 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); + } + ); }); } }); |
