aboutsummaryrefslogtreecommitdiff
path: root/test/git.spec.ts
blob: 5926a0b76f436b33dfefe5818baeeb417963f402 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import validator from 'validator';
import { describe, expect, it } from 'vitest';
import { faker, FakerError } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';

const NON_SEEDED_BASED_RUN = 5;

const refDate = '2020-01-01T00:00:00.000Z';

describe('git', () => {
  seededTests(faker, 'git', (t) => {
    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',
      'commitDate'
    )((t) => {
      t.it('with only string refDate', { refDate })
        .it('with only Date refDate', { refDate: new Date(refDate) })
        .it('with only number refDate', {
          refDate: new Date(refDate).getTime(),
        });
    });
  });

  describe.each(times(NON_SEEDED_BASED_RUN).map(() => faker.seed()))(
    'random seeded tests for seed %i',
    () => {
      describe('branch()', () => {
        it('should return a random branch', () => {
          const branch = faker.git.branch();

          expect(branch).toBeTruthy();
          expect(branch).toBeTypeOf('string');
          expect(branch).toSatisfy(validator.isSlug);
        });
      });

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

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

          const parts = commitEntry.split(/\r?\n/);

          expect(parts.length).toBeGreaterThanOrEqual(6);
          expect(parts.length).toBeLessThanOrEqual(7);

          expect(parts[0]).toMatch(/^commit [a-f0-9]+$/);
          const isValidAuthor = (email: string) => {
            // `validator.isEmail()` does not support display names
            // that contain unquoted characters like . output by Git so we need
            // to quote the display name
            const quotedEmail = email.replace(/^(.*) </, '"$1" <');
            return validator.isEmail(quotedEmail, {
              require_display_name: true,
            });
          };

          const authorRegex = /^Author: .*$/;
          if (parts.length === 7) {
            expect(parts[1]).toMatch(/^Merge: [a-f0-9]+ [a-f0-9]+$/);
            expect(parts[2]).toMatch(authorRegex);
            expect(parts[2].substring(8)).toSatisfy(isValidAuthor);
            expect(parts[3]).toMatch(/^Date: .+$/);
            expect(parts[4]).toBe('');
            expect(parts[5]).toMatch(/^\s{4}.+$/);
          } else {
            expect(parts[1]).toMatch(authorRegex);
            expect(parts[1].substring(8)).toSatisfy(isValidAuthor);
            expect(parts[2]).toMatch(/^Date: .+$/);
            expect(parts[3]).toBe('');
            expect(parts[4]).toMatch(/^\s{4}.+$/);
          }
        });

        it('should return a random commitEntry with a default end of line character of "\r\n"', () => {
          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 a configured end of line character of "\r\n" 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 character 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);

          expect(commitEntry).not.contains('\r\n');
        });

        it('should throw if Intl is unavailable', () => {
          const backup = globalThis.Intl.DateTimeFormat;
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          (globalThis as any).Intl.DateTimeFormat = undefined;

          expect(() => {
            faker.git.commitEntry();
          }).toThrow(
            new FakerError(
              'This method requires an environment which supports Intl.NumberFormat and Intl.DateTimeFormat'
            )
          );

          globalThis.Intl.DateTimeFormat = backup;
        });
      });

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

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

          const parts = commitMessage.split(' ');
          expect(parts.length).toBeGreaterThanOrEqual(3);
        });
      });

      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);
        });

        it('should throw if Intl is unavailable', () => {
          const backup = globalThis.Intl.DateTimeFormat;
          // eslint-disable-next-line @typescript-eslint/no-explicit-any
          (globalThis as any).Intl.DateTimeFormat = undefined;

          expect(() => {
            faker.git.commitDate();
          }).toThrow(
            new FakerError(
              'This method requires an environment which supports Intl.NumberFormat and Intl.DateTimeFormat'
            )
          );

          globalThis.Intl.DateTimeFormat = backup;
        });
      });

      describe('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);
        });

        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);
          }
        );
      });
    }
  );
});