From e7d016b53689f6e741eca4bb61d6f7c4dd3473f6 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 26 Mar 2022 14:14:24 +0100 Subject: [PATCH 1/7] feat(git.commitEntry): configure eol charcter --- src/git.ts | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/git.ts b/src/git.ts index 94144043cae..daf1efc2f7a 100644 --- a/src/git.ts +++ b/src/git.ts @@ -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' * * @example * faker.git.commitEntry() @@ -59,17 +62,28 @@ 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()}`, + '', + ); + + const eolChar = options.eol === 'CRLF' ? '\r\n' : '\n'; + const entry = lines.join(eolChar); return entry; } From f832649b792be830113d85dc3385bcb0900ef433 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 26 Mar 2022 14:24:58 +0100 Subject: [PATCH 2/7] refactor(git.commitEntry): make entry building more readable --- src/git.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/git.ts b/src/git.ts index daf1efc2f7a..e1c8c321709 100644 --- a/src/git.ts +++ b/src/git.ts @@ -79,6 +79,7 @@ export class Git { `Date: ${this.faker.date.recent().toString()}`, '', `\xa0\xa0\xa0\xa0${this.commitMessage()}`, + // to end with a eol char '', ); From 3326634f60738bc6b35cd565a3ce413701b7c2bc Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Thu, 31 Mar 2022 23:08:34 +0200 Subject: [PATCH 3/7] fix(git.commitEntry): linting --- src/git.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/git.ts b/src/git.ts index e1c8c321709..be74ef5b1c2 100644 --- a/src/git.ts +++ b/src/git.ts @@ -80,7 +80,7 @@ export class Git { '', `\xa0\xa0\xa0\xa0${this.commitMessage()}`, // to end with a eol char - '', + '' ); const eolChar = options.eol === 'CRLF' ? '\r\n' : '\n'; From febd6ea379e4d1e7d6a9f11c45d3b0011070b2b5 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 23 Apr 2022 11:23:29 +0200 Subject: [PATCH 4/7] fix(git.commitEntry): default eol option --- src/git.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/git.ts b/src/git.ts index be74ef5b1c2..c1d02147202 100644 --- a/src/git.ts +++ b/src/git.ts @@ -83,7 +83,8 @@ export class Git { '' ); - const eolChar = options.eol === 'CRLF' ? '\r\n' : '\n'; + const eolOption = options.eol ?? 'CRLF'; + const eolChar = eolOption === 'CRLF' ? '\r\n' : '\n'; const entry = lines.join(eolChar); return entry; From e64a951570d0f9e0159a26957570f3f44369e48f Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 23 Apr 2022 11:25:09 +0200 Subject: [PATCH 5/7] test(git.commitEntry): check eol option --- test/git.spec.ts | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/test/git.spec.ts b/test/git.spec.ts index 5da5deb5561..fe200aa1809 100644 --- a/test/git.spec.ts +++ b/test/git.spec.ts @@ -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(); @@ -141,6 +141,34 @@ describe('git', () => { expect(parts[4]).toMatch(/^\s{4}.+$/); } }); + + it('should return a random commitEntry with a default end of line charcter of "\n\r"', () => { + 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 an explicit end of line charcter of "\n\r" 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', () => { + const commitEntry = faker.git.commitEntry({ + eol: 'LF', + }); + const parts = commitEntry.split('\n'); + + expect(parts.length).toBeGreaterThanOrEqual(6); + expect(parts.length).toBeLessThanOrEqual(7); + }); }); describe('commitMessage', () => { From 7130cdbf2034dc75fcce451748ecfa0001d81c12 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 23 Apr 2022 12:09:09 +0200 Subject: [PATCH 6/7] test(git.commitEntry): update test labels --- test/git.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/git.spec.ts b/test/git.spec.ts index fe200aa1809..d5b526c2b6f 100644 --- a/test/git.spec.ts +++ b/test/git.spec.ts @@ -142,7 +142,7 @@ describe('git', () => { } }); - it('should return a random commitEntry with a default end of line charcter of "\n\r"', () => { + 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'); @@ -150,7 +150,7 @@ describe('git', () => { expect(parts.length).toBeLessThanOrEqual(7); }); - it('should return a random commitEntry with an explicit end of line charcter of "\n\r" with eol = CRLF', () => { + 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', }); From 0e049608e6fe6f3967ae15942f6cda121e03cc81 Mon Sep 17 00:00:00 2001 From: xDivisionByZerox Date: Sat, 23 Apr 2022 12:11:57 +0200 Subject: [PATCH 7/7] test(git.commitEntry): stricten LF option validation --- test/git.spec.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/git.spec.ts b/test/git.spec.ts index d5b526c2b6f..18451abd440 100644 --- a/test/git.spec.ts +++ b/test/git.spec.ts @@ -168,6 +168,8 @@ describe('git', () => { expect(parts.length).toBeGreaterThanOrEqual(6); expect(parts.length).toBeLessThanOrEqual(7); + + expect(commitEntry).not.contains('\r\n'); }); });