From d58b5f71b5b95ba1bce7028ebd067bc7830022ce Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Fri, 7 May 2021 00:22:40 +0200 Subject: [PATCH] fix(gatsby): don't print out flag suggestions if none are enabled or opted-in --- .../src/utils/__tests__/handle-flags.ts | 151 ++++++++++++++++++ packages/gatsby/src/utils/handle-flags.ts | 45 +++--- 2 files changed, 175 insertions(+), 21 deletions(-) diff --git a/packages/gatsby/src/utils/__tests__/handle-flags.ts b/packages/gatsby/src/utils/__tests__/handle-flags.ts index 8c7b58d1ad35c..a4c74f2091fdd 100644 --- a/packages/gatsby/src/utils/__tests__/handle-flags.ts +++ b/packages/gatsby/src/utils/__tests__/handle-flags.ts @@ -557,4 +557,155 @@ describe(`handle flags`, () => { `) }) }) + + describe(`other flag suggestions`, () => { + it(`suggest other flags when there is flag explicitly enabled`, () => { + const response = handleFlags( + [ + { + name: `ENABLED_FLAG`, + env: `GATSBY_ENABLED_FLAG`, + command: `all`, + description: `test`, + umbrellaIssue: `test`, + telemetryId: `test`, + experimental: false, + testFitness: (): fitnessEnum => true, + }, + { + name: `OTHER_FLAG`, + env: `GATSBY_OTHER_FLAG`, + command: `all`, + description: `test`, + umbrellaIssue: `test`, + telemetryId: `test`, + experimental: false, + testFitness: (): fitnessEnum => true, + }, + ], + { + ENABLED_FLAG: true, + }, + `build` + ) + + expect(response.message).toMatchInlineSnapshot(` + "The following flags are active: + - ENABLED_FLAG · (Umbrella Issue (test)) · test + + There is one other flag available that you might be interested in: + - OTHER_FLAG · (Umbrella Issue (test)) · test + " + `) + }) + + it(`suggest other flags when there is opted-in flag`, () => { + const response = handleFlags( + [ + { + name: `OPTED_IN_FLAG`, + env: `GATSBY_OPTED_IN_FLAG`, + command: `all`, + description: `test`, + umbrellaIssue: `test`, + telemetryId: `test`, + experimental: false, + testFitness: (): fitnessEnum => `OPT_IN`, + }, + { + name: `OTHER_FLAG`, + env: `GATSBY_OTHER_FLAG`, + command: `all`, + description: `test`, + umbrellaIssue: `test`, + telemetryId: `test`, + experimental: false, + testFitness: (): fitnessEnum => true, + }, + ], + {}, + `build` + ) + + expect(response.message).toMatchInlineSnapshot(` + "We're shipping new features! For final testing, we're rolling them out first to a small % of Gatsby users + and your site was automatically chosen as one of them. With your help, we'll then release them to everyone in the next minor release. + + We greatly appreciate your help testing the change. Please report any feedback good or bad in the umbrella issue. If you do encounter problems, please disable the flag by setting it to false in your gatsby-config.js like: + + flags: { + THE_FLAG: false + } + + The following flags were automatically enabled on your site: + - OPTED_IN_FLAG · (Umbrella Issue (test)) · test + + There is one other flag available that you might be interested in: + - OTHER_FLAG · (Umbrella Issue (test)) · test + " + `) + }) + + it(`doesn't suggest other flags if there are no enabled or opted in flags (no locked-in flags)`, () => { + const response = handleFlags( + [ + { + name: `SOME_FLAG`, + env: `GATSBY_SOME_FLAG`, + command: `all`, + description: `test`, + umbrellaIssue: `test`, + telemetryId: `test`, + experimental: false, + testFitness: (): fitnessEnum => true, + }, + { + name: `OTHER_FLAG`, + env: `GATSBY_OTHER_FLAG`, + command: `all`, + description: `test`, + umbrellaIssue: `test`, + telemetryId: `test`, + experimental: false, + testFitness: (): fitnessEnum => true, + }, + ], + {}, + `build` + ) + + expect(response.message).toMatchInlineSnapshot(`""`) + }) + + it(`doesn't suggest other flags if there are no enabled or opted in flags (with locked-in flag)`, () => { + const response = handleFlags( + [ + { + name: `LOCKED_IN_FLAG`, + env: `GATSBY_LOCKED_IN_FLAG`, + command: `all`, + description: `test`, + umbrellaIssue: `test`, + telemetryId: `test`, + experimental: false, + testFitness: (): fitnessEnum => `LOCKED_IN`, + }, + { + name: `OTHER_FLAG`, + env: `GATSBY_OTHER_FLAG`, + command: `all`, + description: `test`, + umbrellaIssue: `test`, + telemetryId: `test`, + experimental: false, + testFitness: (): fitnessEnum => true, + }, + ], + {}, + `build` + ) + + expect(response.message).toMatchInlineSnapshot(`""`) + }) + }) }) diff --git a/packages/gatsby/src/utils/handle-flags.ts b/packages/gatsby/src/utils/handle-flags.ts index 3f167587e2b39..5621a1fc0f41e 100644 --- a/packages/gatsby/src/utils/handle-flags.ts +++ b/packages/gatsby/src/utils/handle-flags.ts @@ -203,28 +203,31 @@ The following flags were automatically enabled on your site:` }) } - const otherFlagSuggestionLines: Array = [] - const enabledFlagsSet = new Set() - enabledConfigFlags.forEach(f => enabledFlagsSet.add(f.name)) - applicableFlags.forEach(flag => { - if ( - !enabledFlagsSet.has(flag.name) && - typeof configFlags[flag.name] === `undefined` - ) { - // we want to suggest flag when it's not enabled and user specifically didn't use it in config - // we don't want to suggest flag user specifically wanted to disable - otherFlagSuggestionLines.push(generateFlagLine(flag)) + if (message.length > 0) { + // if we will print anything about flags, let's try to suggest other available ones + const otherFlagSuggestionLines: Array = [] + const enabledFlagsSet = new Set() + enabledConfigFlags.forEach(f => enabledFlagsSet.add(f.name)) + applicableFlags.forEach(flag => { + if ( + !enabledFlagsSet.has(flag.name) && + typeof configFlags[flag.name] === `undefined` + ) { + // we want to suggest flag when it's not enabled and user specifically didn't use it in config + // we don't want to suggest flag user specifically wanted to disable + otherFlagSuggestionLines.push(generateFlagLine(flag)) + } + }) + + if (otherFlagSuggestionLines.length > 0) { + message += `\n\nThere ${ + otherFlagSuggestionLines.length === 1 + ? `is one other flag` + : `are ${otherFlagSuggestionLines.length} other flags` + } available that you might be interested in:${otherFlagSuggestionLines.join( + `` + )}` } - }) - - if (otherFlagSuggestionLines.length > 0) { - message += `\n\nThere ${ - otherFlagSuggestionLines.length === 1 - ? `is one other flag` - : `are ${otherFlagSuggestionLines.length} other flags` - } available that you might be interested in:${otherFlagSuggestionLines.join( - `` - )}` } if (message.length > 0) {