From aabab3804e6480c5640c78c0a0846c8838adc8f1 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Sat, 30 Apr 2022 19:22:43 +0100 Subject: [PATCH 01/16] feat: Add cron expression generator --- src/modules/system/index.ts | 42 +++++++++++++++++++++++++++++++++++++ test/system.spec.ts | 27 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 46a12b19156..2676a7f67f2 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -264,4 +264,46 @@ export class System { return `${prefix}${interfaceType}${commonInterfaceSchemas[interfaceSchema]}${suffix}`; } + + /** + * Returns a cron expression + * + * @param options The optional options to use. + * @param options.includeYear Whether to include a year in the generated expression. Default to `false` + * + * @example + * faker.system.cron() // '45 23 * * 6' + * faker.system.cron({ includeYear: true }) // '45 23 * * 6 2067' + * faker.system.cron({ includeYear: false }) // '45 23 * * 6' + */ + cron(options?: { includeYear: boolean }): string { + // create the arrays to hold the available values for each component of the expression + const minutes = [this.faker.datatype.number({ min: 0, max: 59 }), '*']; + const hours = [this.faker.datatype.number({ min: 0, max: 23 }), '*']; + const days = [this.faker.datatype.number({ min: 1, max: 31 }), '*', '?']; + const months = [this.faker.datatype.number({ min: 1, max: 12 }), '*']; + const daysOfWeek = [ + this.faker.datatype.number({ min: 0, max: 6 }), + // cron days are only in English + ...['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'], + '*', + '?', + ]; + const years = [this.faker.datatype.number({ min: 1970, max: 2099 }), '*']; + + const minute = this.faker.random.arrayElement(minutes); + const hour = this.faker.random.arrayElement(hours); + const day = this.faker.random.arrayElement(days); + const month = this.faker.random.arrayElement(months); + const dayOfWeek = this.faker.random.arrayElement(daysOfWeek); + const year = this.faker.random.arrayElement(years); + + // create and return the cron expression string + let expression = `${minute} ${hour} ${day} ${month} ${dayOfWeek}`; + if (options?.includeYear) { + expression += ` ${year}`; + } + + return expression; + } } diff --git a/test/system.spec.ts b/test/system.spec.ts index 05e2c53daba..e0d26d07713 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -10,6 +10,7 @@ const functionNames = [ 'commonFileExt', 'commonFileName', 'commonFileType', + 'cron', 'directoryPath', 'fileExt', 'fileName', @@ -385,6 +386,32 @@ describe('system', () => { ).toMatch(/^enx[a-f\d]{12}$/); }); }); + + describe('cron()', () => { + const regex = new RegExp( + /^([1-9]|[1-5]\d|\*) ([1-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3})/ + ); + it('should return cron expression with 5 elements', () => { + expect( + faker.system.cron(), + `generated cron, string should contain 5 space-separated values` + ).toMatch(regex); + }); + + it('should return expression with 5 elements with includeYear false', () => { + expect( + faker.system.cron({ includeYear: false }), + `generated cron, string should contain 5 space-separated values` + ).toMatch(regex); + }); + + it('should return cron expression with 6 elements with includeYear true', () => { + expect( + faker.system.cron({ includeYear: true }), + `generated cron, string should contain 6 space-separated values` + ).toMatch(new RegExp(`${regex.source} ?((19[7-9]\d)|20\\d{2}|\\*)?`)); + }); + }); } }); From ee90563b9e70d08941f3b9bd7a5036edd5468798 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 23 May 2022 21:20:00 +0200 Subject: [PATCH 02/16] chore: minor changes --- src/modules/system/index.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 2676a7f67f2..83c474ee4e0 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -291,12 +291,12 @@ export class System { ]; const years = [this.faker.datatype.number({ min: 1970, max: 2099 }), '*']; - const minute = this.faker.random.arrayElement(minutes); - const hour = this.faker.random.arrayElement(hours); - const day = this.faker.random.arrayElement(days); - const month = this.faker.random.arrayElement(months); - const dayOfWeek = this.faker.random.arrayElement(daysOfWeek); - const year = this.faker.random.arrayElement(years); + const minute = this.faker.helpers.arrayElement(minutes); + const hour = this.faker.helpers.arrayElement(hours); + const day = this.faker.helpers.arrayElement(days); + const month = this.faker.helpers.arrayElement(months); + const dayOfWeek = this.faker.helpers.arrayElement(daysOfWeek); + const year = this.faker.helpers.arrayElement(years); // create and return the cron expression string let expression = `${minute} ${hour} ${day} ${month} ${dayOfWeek}`; From 5f0061769f3dbfc84c34033597a949b51a676465 Mon Sep 17 00:00:00 2001 From: Shinigami92 Date: Mon, 23 May 2022 21:31:49 +0200 Subject: [PATCH 03/16] chore: little improvments --- src/modules/system/index.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 83c474ee4e0..7d0692e4371 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -22,6 +22,19 @@ const commonInterfaceSchemas = { pci: 'p', } as const; +/** + * cron days are only in English + */ +const CRON_DAY_OF_WEEK = [ + 'SUN', + 'MON', + 'TUE', + 'WED', + 'THU', + 'FRI', + 'SAT', +] as const; + /** * Generates fake data for many computer systems properties. */ @@ -284,8 +297,7 @@ export class System { const months = [this.faker.datatype.number({ min: 1, max: 12 }), '*']; const daysOfWeek = [ this.faker.datatype.number({ min: 0, max: 6 }), - // cron days are only in English - ...['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'], + this.faker.helpers.arrayElement(CRON_DAY_OF_WEEK), '*', '?', ]; From b7d93861cd24203f2de958c3c717e0c803551e89 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Tue, 23 Aug 2022 22:32:43 +0100 Subject: [PATCH 04/16] test: Update test to allow 12am cron hour expression --- test/system.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/system.spec.ts b/test/system.spec.ts index e0d26d07713..be1ab99b676 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -389,7 +389,7 @@ describe('system', () => { describe('cron()', () => { const regex = new RegExp( - /^([1-9]|[1-5]\d|\*) ([1-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3})/ + /^([1-9]|[1-5]\d|\*) ([0-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3})/ ); it('should return cron expression with 5 elements', () => { expect( From 80a4ff5689ac01c04505d1cfb52468333a651d5c Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Wed, 24 Aug 2022 07:48:15 +0100 Subject: [PATCH 05/16] test: Update test snapshots --- test/__snapshots__/system.spec.ts.snap | 24 ++++++++++++++++++++++++ test/system.spec.ts | 6 ++++++ 2 files changed, 30 insertions(+) diff --git a/test/__snapshots__/system.spec.ts.snap b/test/__snapshots__/system.spec.ts.snap index f3eda41ad97..346ab1a7c29 100644 --- a/test/__snapshots__/system.spec.ts.snap +++ b/test/__snapshots__/system.spec.ts.snap @@ -8,6 +8,12 @@ exports[`system > 42 > commonFileName > with extension 1`] = `"mobile_fish.ext"` exports[`system > 42 > commonFileType 1`] = `"audio"`; +exports[`system > 42 > cron > noArgs 1`] = `"* 19 * 3 5"`; + +exports[`system > 42 > cron > with explicitly excluded year 1`] = `"* 19 * 3 5"`; + +exports[`system > 42 > cron > with includeYear 1`] = `"* 19 * 3 5 2047"`; + exports[`system > 42 > directoryPath 1`] = `"/opt/bin"`; exports[`system > 42 > fileExt > noArgs 1`] = `"lrm"`; @@ -76,6 +82,12 @@ exports[`system > 1211 > commonFileName > with extension 1`] = `"functionalities exports[`system > 1211 > commonFileType 1`] = `"application"`; +exports[`system > 1211 > cron > noArgs 1`] = `"55 * 28 * 1"`; + +exports[`system > 1211 > cron > with explicitly excluded year 1`] = `"55 * 28 * 1"`; + +exports[`system > 1211 > cron > with includeYear 1`] = `"55 * 28 * 1 *"`; + exports[`system > 1211 > directoryPath 1`] = `"/var/log"`; exports[`system > 1211 > fileExt > noArgs 1`] = `"dic"`; @@ -144,6 +156,12 @@ exports[`system > 1337 > commonFileName > with extension 1`] = `"delaware.ext"`; exports[`system > 1337 > commonFileType 1`] = `"audio"`; +exports[`system > 1337 > cron > noArgs 1`] = `"15 13 5 * *"`; + +exports[`system > 1337 > cron > with explicitly excluded year 1`] = `"15 13 5 * *"`; + +exports[`system > 1337 > cron > with includeYear 1`] = `"15 13 5 * * 2029"`; + exports[`system > 1337 > directoryPath 1`] = `"/Library"`; exports[`system > 1337 > fileExt > noArgs 1`] = `"oa3"`; @@ -210,6 +228,8 @@ exports[`system > seed: 42 > commonFileName() 1`] = `"mobile_fish.mpe"`; exports[`system > seed: 42 > commonFileType() 1`] = `"audio"`; +exports[`system > seed: 42 > cron() 1`] = `"* 19 * 3 5"`; + exports[`system > seed: 42 > directoryPath() 1`] = `"/opt/bin"`; exports[`system > seed: 42 > fileExt() 1`] = `"lrm"`; @@ -232,6 +252,8 @@ exports[`system > seed: 1211 > commonFileName() 1`] = `"functionalities_frozen_b exports[`system > seed: 1211 > commonFileType() 1`] = `"application"`; +exports[`system > seed: 1211 > cron() 1`] = `"55 * 28 * 1"`; + exports[`system > seed: 1211 > directoryPath() 1`] = `"/var/log"`; exports[`system > seed: 1211 > fileExt() 1`] = `"dic"`; @@ -254,6 +276,8 @@ exports[`system > seed: 1337 > commonFileName() 1`] = `"delaware.mp2"`; exports[`system > seed: 1337 > commonFileType() 1`] = `"audio"`; +exports[`system > seed: 1337 > cron() 1`] = `"15 13 5 * *"`; + exports[`system > seed: 1337 > directoryPath() 1`] = `"/Library"`; exports[`system > seed: 1337 > fileExt() 1`] = `"oa3"`; diff --git a/test/system.spec.ts b/test/system.spec.ts index be1ab99b676..f7d0dfccd9e 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -66,6 +66,12 @@ describe('system', () => { } } }); + + t.describe('cron', (t) => { + t.it('noArgs') + .it('with includeYear', { includeYear: true }) + .it('with explicitly excluded year', { includeYear: false }); + }); }); for (const seed of seededRuns) { From 075e614299f76beea86c0f77e21a649e6d2895c0 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Wed, 24 Aug 2022 09:29:48 +0100 Subject: [PATCH 06/16] feat: Support non-standard cron expressions --- src/modules/system/index.ts | 28 ++++++++++++++++++++++++---- test/system.spec.ts | 16 ++++++++++++++-- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 7d0692e4371..4f4c04880f2 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -283,13 +283,19 @@ export class System { * * @param options The optional options to use. * @param options.includeYear Whether to include a year in the generated expression. Default to `false` + * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the generated expression. Default to `false` * * @example * faker.system.cron() // '45 23 * * 6' * faker.system.cron({ includeYear: true }) // '45 23 * * 6 2067' * faker.system.cron({ includeYear: false }) // '45 23 * * 6' + * faker.system.cron({ includeNonStandard: false }) // '45 23 * * 6' + * faker.system.cron({ includeNonStandard: true }) // '@yearly' */ - cron(options?: { includeYear: boolean }): string { + cron(options?: { + includeYear?: boolean; + includeNonStandard?: boolean; + }): string { // create the arrays to hold the available values for each component of the expression const minutes = [this.faker.datatype.number({ min: 0, max: 59 }), '*']; const hours = [this.faker.datatype.number({ min: 0, max: 23 }), '*']; @@ -311,11 +317,25 @@ export class System { const year = this.faker.helpers.arrayElement(years); // create and return the cron expression string - let expression = `${minute} ${hour} ${day} ${month} ${dayOfWeek}`; + let standardExpression = `${minute} ${hour} ${day} ${month} ${dayOfWeek}`; if (options?.includeYear) { - expression += ` ${year}`; + standardExpression += ` ${year}`; } - return expression; + const availableExpressions = [standardExpression]; + if (options?.includeNonStandard) { + const nonStandardExpressions = [ + '@annually', + '@daily', + '@hourly', + '@monthly', + '@reboot', + '@weekly', + '@yearly', + ]; + availableExpressions.push(...nonStandardExpressions); + } + + return this.faker.helpers.arrayElement(availableExpressions); } } diff --git a/test/system.spec.ts b/test/system.spec.ts index f7d0dfccd9e..74e15a61abf 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -69,8 +69,10 @@ describe('system', () => { t.describe('cron', (t) => { t.it('noArgs') - .it('with includeYear', { includeYear: true }) - .it('with explicitly excluded year', { includeYear: false }); + .it('with includeYear true', { includeYear: true }) + .it('with includeYear false', { includeYear: false }) + .it('with includeNonStandard true', { includeNonStandard: true }) + .it('with includeNonStandard false', { includeNonStandard: true }); }); }); @@ -417,6 +419,16 @@ describe('system', () => { `generated cron, string should contain 6 space-separated values` ).toMatch(new RegExp(`${regex.source} ?((19[7-9]\d)|20\\d{2}|\\*)?`)); }); + + it('should return non-standard cron expressions', () => { + const validResults = ['*', '@']; + expect( + faker.system.cron({ includeNonStandard: true })[0], + 'generated cron, string should contain non-standard cron labels' + ).toSatisfy( + (value) => !!validResults.find((result) => value === result) + ); + }); }); } }); From d6da4e2f3e8ec8a44e51028b0f43f8fda1da36ca Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Wed, 24 Aug 2022 09:31:46 +0100 Subject: [PATCH 07/16] refactor: Update capitalisation, punctuation Co-authored-by: ST-DDT --- src/modules/system/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 4f4c04880f2..9280bb2014d 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -23,7 +23,7 @@ const commonInterfaceSchemas = { } as const; /** - * cron days are only in English + * Cron days are only in English. */ const CRON_DAY_OF_WEEK = [ 'SUN', From 7434655cb45d9565e955be5f34f1bd64dabd6ef0 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Thu, 25 Aug 2022 11:45:14 +0100 Subject: [PATCH 08/16] refactor: Improve comment readability Co-authored-by: Shinigami --- src/modules/system/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 9280bb2014d..2ac0429437c 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -282,7 +282,7 @@ export class System { * Returns a cron expression * * @param options The optional options to use. - * @param options.includeYear Whether to include a year in the generated expression. Default to `false` + * @param options.includeYear Whether to include a year in the generated expression. Defaults to `false`. * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the generated expression. Default to `false` * * @example From ec675a91eabac736b0f80466c66d6480b8cedb0b Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Thu, 25 Aug 2022 11:42:50 +0100 Subject: [PATCH 09/16] refactor: Remove unnecessary comment --- src/modules/system/index.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 2ac0429437c..c470d88b097 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -22,9 +22,6 @@ const commonInterfaceSchemas = { pci: 'p', } as const; -/** - * Cron days are only in English. - */ const CRON_DAY_OF_WEEK = [ 'SUN', 'MON', From 93741dd66c750020805f8e128dc7cc2207e66b34 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Thu, 25 Aug 2022 11:49:50 +0100 Subject: [PATCH 10/16] refactor: Convert regex to literal --- test/__snapshots__/system.spec.ts.snap | 24 ++++++++++++++++++++++++ test/system.spec.ts | 5 ++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/test/__snapshots__/system.spec.ts.snap b/test/__snapshots__/system.spec.ts.snap index 346ab1a7c29..b05262bbeaf 100644 --- a/test/__snapshots__/system.spec.ts.snap +++ b/test/__snapshots__/system.spec.ts.snap @@ -12,8 +12,16 @@ exports[`system > 42 > cron > noArgs 1`] = `"* 19 * 3 5"`; exports[`system > 42 > cron > with explicitly excluded year 1`] = `"* 19 * 3 5"`; +exports[`system > 42 > cron > with includeNonStandard false 1`] = `"@hourly"`; + +exports[`system > 42 > cron > with includeNonStandard true 1`] = `"@hourly"`; + exports[`system > 42 > cron > with includeYear 1`] = `"* 19 * 3 5 2047"`; +exports[`system > 42 > cron > with includeYear false 1`] = `"* 19 * 3 5"`; + +exports[`system > 42 > cron > with includeYear true 1`] = `"* 19 * 3 5 2047"`; + exports[`system > 42 > directoryPath 1`] = `"/opt/bin"`; exports[`system > 42 > fileExt > noArgs 1`] = `"lrm"`; @@ -86,8 +94,16 @@ exports[`system > 1211 > cron > noArgs 1`] = `"55 * 28 * 1"`; exports[`system > 1211 > cron > with explicitly excluded year 1`] = `"55 * 28 * 1"`; +exports[`system > 1211 > cron > with includeNonStandard false 1`] = `"@hourly"`; + +exports[`system > 1211 > cron > with includeNonStandard true 1`] = `"@hourly"`; + exports[`system > 1211 > cron > with includeYear 1`] = `"55 * 28 * 1 *"`; +exports[`system > 1211 > cron > with includeYear false 1`] = `"55 * 28 * 1"`; + +exports[`system > 1211 > cron > with includeYear true 1`] = `"55 * 28 * 1 *"`; + exports[`system > 1211 > directoryPath 1`] = `"/var/log"`; exports[`system > 1211 > fileExt > noArgs 1`] = `"dic"`; @@ -160,8 +176,16 @@ exports[`system > 1337 > cron > noArgs 1`] = `"15 13 5 * *"`; exports[`system > 1337 > cron > with explicitly excluded year 1`] = `"15 13 5 * *"`; +exports[`system > 1337 > cron > with includeNonStandard false 1`] = `"@daily"`; + +exports[`system > 1337 > cron > with includeNonStandard true 1`] = `"@daily"`; + exports[`system > 1337 > cron > with includeYear 1`] = `"15 13 5 * * 2029"`; +exports[`system > 1337 > cron > with includeYear false 1`] = `"15 13 5 * *"`; + +exports[`system > 1337 > cron > with includeYear true 1`] = `"15 13 5 * * 2029"`; + exports[`system > 1337 > directoryPath 1`] = `"/Library"`; exports[`system > 1337 > fileExt > noArgs 1`] = `"oa3"`; diff --git a/test/system.spec.ts b/test/system.spec.ts index 74e15a61abf..bd0c1733fed 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -396,9 +396,8 @@ describe('system', () => { }); describe('cron()', () => { - const regex = new RegExp( - /^([1-9]|[1-5]\d|\*) ([0-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3})/ - ); + const regex = + /^([1-9]|[1-5]\d|\*) ([0-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3})/; it('should return cron expression with 5 elements', () => { expect( faker.system.cron(), From 13173ca3d79ef382454f748e2948d2b0a4276843 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Thu, 25 Aug 2022 20:24:05 +0100 Subject: [PATCH 11/16] refactor: Extract options from parameters --- src/modules/system/index.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index c470d88b097..92c047d4a0c 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -289,10 +289,14 @@ export class System { * faker.system.cron({ includeNonStandard: false }) // '45 23 * * 6' * faker.system.cron({ includeNonStandard: true }) // '@yearly' */ - cron(options?: { - includeYear?: boolean; - includeNonStandard?: boolean; - }): string { + cron( + options: { + includeYear?: boolean; + includeNonStandard?: boolean; + } = {} + ): string { + const { includeYear = false, includeNonStandard = false } = options; + // create the arrays to hold the available values for each component of the expression const minutes = [this.faker.datatype.number({ min: 0, max: 59 }), '*']; const hours = [this.faker.datatype.number({ min: 0, max: 23 }), '*']; @@ -315,12 +319,12 @@ export class System { // create and return the cron expression string let standardExpression = `${minute} ${hour} ${day} ${month} ${dayOfWeek}`; - if (options?.includeYear) { + if (includeYear) { standardExpression += ` ${year}`; } const availableExpressions = [standardExpression]; - if (options?.includeNonStandard) { + if (includeNonStandard) { const nonStandardExpressions = [ '@annually', '@daily', From 39a2932844d554615da8be70eacb47b39c9e2176 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Thu, 25 Aug 2022 20:57:27 +0100 Subject: [PATCH 12/16] refactor: Change chance of standard expression being chosen --- src/modules/system/index.ts | 26 ++++++++++++-------------- test/__snapshots__/system.spec.ts.snap | 22 +++++----------------- test/system.spec.ts | 4 ++-- 3 files changed, 19 insertions(+), 33 deletions(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 92c047d4a0c..64186a1946c 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -323,20 +323,18 @@ export class System { standardExpression += ` ${year}`; } - const availableExpressions = [standardExpression]; - if (includeNonStandard) { - const nonStandardExpressions = [ - '@annually', - '@daily', - '@hourly', - '@monthly', - '@reboot', - '@weekly', - '@yearly', - ]; - availableExpressions.push(...nonStandardExpressions); - } + const nonStandardExpressions = [ + '@annually', + '@daily', + '@hourly', + '@monthly', + '@reboot', + '@weekly', + '@yearly', + ]; - return this.faker.helpers.arrayElement(availableExpressions); + return !includeNonStandard || this.faker.datatype.boolean() + ? standardExpression + : this.faker.helpers.arrayElement(nonStandardExpressions); } } diff --git a/test/__snapshots__/system.spec.ts.snap b/test/__snapshots__/system.spec.ts.snap index b05262bbeaf..7821ced20de 100644 --- a/test/__snapshots__/system.spec.ts.snap +++ b/test/__snapshots__/system.spec.ts.snap @@ -10,13 +10,9 @@ exports[`system > 42 > commonFileType 1`] = `"audio"`; exports[`system > 42 > cron > noArgs 1`] = `"* 19 * 3 5"`; -exports[`system > 42 > cron > with explicitly excluded year 1`] = `"* 19 * 3 5"`; +exports[`system > 42 > cron > with includeNonStandard false 1`] = `"* 19 * 3 5"`; -exports[`system > 42 > cron > with includeNonStandard false 1`] = `"@hourly"`; - -exports[`system > 42 > cron > with includeNonStandard true 1`] = `"@hourly"`; - -exports[`system > 42 > cron > with includeYear 1`] = `"* 19 * 3 5 2047"`; +exports[`system > 42 > cron > with includeNonStandard true 1`] = `"@yearly"`; exports[`system > 42 > cron > with includeYear false 1`] = `"* 19 * 3 5"`; @@ -92,14 +88,10 @@ exports[`system > 1211 > commonFileType 1`] = `"application"`; exports[`system > 1211 > cron > noArgs 1`] = `"55 * 28 * 1"`; -exports[`system > 1211 > cron > with explicitly excluded year 1`] = `"55 * 28 * 1"`; - -exports[`system > 1211 > cron > with includeNonStandard false 1`] = `"@hourly"`; +exports[`system > 1211 > cron > with includeNonStandard false 1`] = `"55 * 28 * 1"`; exports[`system > 1211 > cron > with includeNonStandard true 1`] = `"@hourly"`; -exports[`system > 1211 > cron > with includeYear 1`] = `"55 * 28 * 1 *"`; - exports[`system > 1211 > cron > with includeYear false 1`] = `"55 * 28 * 1"`; exports[`system > 1211 > cron > with includeYear true 1`] = `"55 * 28 * 1 *"`; @@ -174,13 +166,9 @@ exports[`system > 1337 > commonFileType 1`] = `"audio"`; exports[`system > 1337 > cron > noArgs 1`] = `"15 13 5 * *"`; -exports[`system > 1337 > cron > with explicitly excluded year 1`] = `"15 13 5 * *"`; - -exports[`system > 1337 > cron > with includeNonStandard false 1`] = `"@daily"`; - -exports[`system > 1337 > cron > with includeNonStandard true 1`] = `"@daily"`; +exports[`system > 1337 > cron > with includeNonStandard false 1`] = `"15 13 5 * *"`; -exports[`system > 1337 > cron > with includeYear 1`] = `"15 13 5 * * 2029"`; +exports[`system > 1337 > cron > with includeNonStandard true 1`] = `"@yearly"`; exports[`system > 1337 > cron > with includeYear false 1`] = `"15 13 5 * *"`; diff --git a/test/system.spec.ts b/test/system.spec.ts index bd0c1733fed..44a2d1e04c5 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -72,7 +72,7 @@ describe('system', () => { .it('with includeYear true', { includeYear: true }) .it('with includeYear false', { includeYear: false }) .it('with includeNonStandard true', { includeNonStandard: true }) - .it('with includeNonStandard false', { includeNonStandard: true }); + .it('with includeNonStandard false', { includeNonStandard: false }); }); }); @@ -420,7 +420,7 @@ describe('system', () => { }); it('should return non-standard cron expressions', () => { - const validResults = ['*', '@']; + const validResults = ['2', '5', '*', '@']; expect( faker.system.cron({ includeNonStandard: true })[0], 'generated cron, string should contain non-standard cron labels' From 5550a137b6e73aea02ae20176c5d3d3183355855 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Thu, 25 Aug 2022 22:16:15 +0100 Subject: [PATCH 13/16] refactor: Add punctation to comment Co-authored-by: ST-DDT --- src/modules/system/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 64186a1946c..bca3af51cfc 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -280,7 +280,7 @@ export class System { * * @param options The optional options to use. * @param options.includeYear Whether to include a year in the generated expression. Defaults to `false`. - * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the generated expression. Default to `false` + * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the generated expression. Default to `false`. * * @example * faker.system.cron() // '45 23 * * 6' From 8cea37a69d2302f82c8908b306c872df726ffb5d Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Fri, 26 Aug 2022 14:12:44 +0100 Subject: [PATCH 14/16] refactor: Update comment Co-authored-by: Shinigami --- src/modules/system/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index bca3af51cfc..4b2df543cc7 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -280,7 +280,7 @@ export class System { * * @param options The optional options to use. * @param options.includeYear Whether to include a year in the generated expression. Defaults to `false`. - * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the generated expression. Default to `false`. + * @param options.includeNonStandard Whether to include a @yearly, @monthly, @daily, etc text labels in the generated expression. Defaults to `false`. * * @example * faker.system.cron() // '45 23 * * 6' From 7a529acd629438e939e5b9a6e29de47b15ad7577 Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Fri, 26 Aug 2022 14:52:41 +0100 Subject: [PATCH 15/16] test: Improved failure messages --- test/system.spec.ts | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/test/system.spec.ts b/test/system.spec.ts index 44a2d1e04c5..fb0ef209a2e 100644 --- a/test/system.spec.ts +++ b/test/system.spec.ts @@ -397,30 +397,30 @@ describe('system', () => { describe('cron()', () => { const regex = - /^([1-9]|[1-5]\d|\*) ([0-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3})/; - it('should return cron expression with 5 elements', () => { - expect( - faker.system.cron(), - `generated cron, string should contain 5 space-separated values` - ).toMatch(regex); - }); - - it('should return expression with 5 elements with includeYear false', () => { - expect( - faker.system.cron({ includeYear: false }), - `generated cron, string should contain 5 space-separated values` - ).toMatch(regex); - }); - - it('should return cron expression with 6 elements with includeYear true', () => { - expect( - faker.system.cron({ includeYear: true }), - `generated cron, string should contain 6 space-separated values` - ).toMatch(new RegExp(`${regex.source} ?((19[7-9]\d)|20\\d{2}|\\*)?`)); - }); + /^([1-9]|[1-5]\d|\*) ([0-9]|1\d|2[0-3]|\*) ([1-9]|[12]\d|3[01]|\*|\?) ([1-9]|1[0-2]|\*) ([0-6]|\*|\?|[A-Z]{3}) ((19[7-9]d)|20\d{2}|\*)?/; + + const regexElements = regex.toString().replace(/\//g, '').split(' '); + + it.each([ + [{}, 5], + [{ includeYear: false }, 5], + [{ includeYear: true }, 6], + ])( + 'should return cron expression with correct number of valid elements - %o, %d', + (options, count: number) => { + const cron = faker.system.cron(options).split(' '); + expect(cron).toHaveLength(count); + cron.forEach((cronElement, i) => + expect( + cronElement, + `generated cron, ${cronElement} should match regex ${regexElements[i]}` + ).toMatch(new RegExp(regexElements[i])) + ); + } + ); it('should return non-standard cron expressions', () => { - const validResults = ['2', '5', '*', '@']; + const validResults = ['1', '2', '5', '*', '@']; expect( faker.system.cron({ includeNonStandard: true })[0], 'generated cron, string should contain non-standard cron labels' From eb4a84e02c6a415511732784c005ed7a811110fc Mon Sep 17 00:00:00 2001 From: Nick Hammond Date: Fri, 26 Aug 2022 14:54:03 +0100 Subject: [PATCH 16/16] refactor: Update function description Co-authored-by: ST-DDT --- src/modules/system/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/system/index.ts b/src/modules/system/index.ts index 4b2df543cc7..6d2acca083f 100644 --- a/src/modules/system/index.ts +++ b/src/modules/system/index.ts @@ -276,7 +276,7 @@ export class System { } /** - * Returns a cron expression + * Returns a random cron expression. * * @param options The optional options to use. * @param options.includeYear Whether to include a year in the generated expression. Defaults to `false`.