aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Mayer <[email protected]>2023-05-28 02:43:50 +0700
committerGitHub <[email protected]>2023-05-27 21:43:50 +0200
commita5e73f8a11baeefcf03f344cb5e4dde096a0b364 (patch)
tree94db06056940eacbe76186c6a8d3c43d3c72972b
parent012ba2c80924b7bb1b241f170bae9dad25dacc2e (diff)
downloadfaker-a5e73f8a11baeefcf03f344cb5e4dde096a0b364.tar.xz
faker-a5e73f8a11baeefcf03f344cb5e4dde096a0b364.zip
fix(git): limit need for Intl to specific method (#2172)
-rw-r--r--src/modules/git/index.ts50
-rw-r--r--test/git.spec.ts34
2 files changed, 66 insertions, 18 deletions
diff --git a/src/modules/git/index.ts b/src/modules/git/index.ts
index 05a80081..7528d9bb 100644
--- a/src/modules/git/index.ts
+++ b/src/modules/git/index.ts
@@ -1,23 +1,29 @@
import type { Faker } from '../..';
+import { FakerError } from '../../errors/faker-error';
import { deprecated } from '../../internal/deprecated';
-const GIT_DATE_FORMAT_BASE = new Intl.DateTimeFormat('en', {
- weekday: 'short',
- month: 'short',
- day: 'numeric',
- hour: '2-digit',
- hourCycle: 'h24',
- minute: '2-digit',
- second: '2-digit',
- year: 'numeric',
- timeZone: 'UTC',
-});
-const GIT_TIMEZONE_FORMAT = new Intl.NumberFormat('en', {
- minimumIntegerDigits: 4,
- maximumFractionDigits: 0,
- useGrouping: false,
- signDisplay: 'always',
-});
+const GIT_DATE_FORMAT_BASE = Intl?.DateTimeFormat
+ ? new Intl.DateTimeFormat('en', {
+ weekday: 'short',
+ month: 'short',
+ day: 'numeric',
+ hour: '2-digit',
+ hourCycle: 'h24',
+ minute: '2-digit',
+ second: '2-digit',
+ year: 'numeric',
+ timeZone: 'UTC',
+ })
+ : null;
+
+const GIT_TIMEZONE_FORMAT = Intl?.NumberFormat
+ ? new Intl.NumberFormat('en', {
+ minimumIntegerDigits: 4,
+ maximumFractionDigits: 0,
+ useGrouping: false,
+ signDisplay: 'always',
+ })
+ : null;
/**
* Module to generate git related entries.
@@ -64,6 +70,8 @@ export class GitModule {
* 'CRLF' = '\r\n'
* @param options.refDate The date to use as reference point for the commit. Defaults to `new Date()`.
*
+ * @throws When the environment does not support `Intl.NumberFormat` and `Intl.DateTimeFormat`.
+ *
* @example
* faker.git.commitEntry()
* // commit fe8c38a965d13d9794eb36918cb24cebe49a45c2
@@ -158,6 +166,8 @@ export class GitModule {
* @param options The optional options object.
* @param options.refDate The date to use as reference point for the commit. Defaults to `faker.defaultRefDate()`.
*
+ * @throws When the environment does not support `Intl.NumberFormat` and `Intl.DateTimeFormat`.
+ *
* @example
* faker.git.commitDate() // 'Mon Nov 7 14:40:58 2022 +0600'
* faker.git.commitDate({ refDate: '2020-01-01' }) // 'Tue Dec 31 05:40:59 2019 -0400'
@@ -175,6 +185,12 @@ export class GitModule {
} = {}
): string {
const { refDate = this.faker.defaultRefDate() } = options;
+ // We check if Intl support is missing rather than if GIT_DATE_FORMAT_BASE/GIT_TIMEZONE_FORMAT is null. This allows us to test the error case in environments that do have Intl support by temporarily removing Intl at runtime.
+ if (!Intl || !Intl.DateTimeFormat || !Intl.NumberFormat) {
+ throw new FakerError(
+ 'This method requires an environment which supports Intl.NumberFormat and Intl.DateTimeFormat'
+ );
+ }
const dateParts = GIT_DATE_FORMAT_BASE.format(
this.faker.date.recent({ days: 1, refDate })
diff --git a/test/git.spec.ts b/test/git.spec.ts
index 4000e71d..5926a0b7 100644
--- a/test/git.spec.ts
+++ b/test/git.spec.ts
@@ -1,6 +1,6 @@
import validator from 'validator';
import { describe, expect, it } from 'vitest';
-import { faker } from '../src';
+import { faker, FakerError } from '../src';
import { seededTests } from './support/seededRuns';
import { times } from './support/times';
@@ -114,6 +114,22 @@ describe('git', () => {
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', () => {
@@ -138,6 +154,22 @@ describe('git', () => {
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', () => {