Skip to content

Commit 01de613

Browse files
GatsbyJS Botpieh
andauthored
fix(gatsby): don't print out flag suggestions if none are enabled or opted-in (#31299) (#31362)
(cherry picked from commit 48eea6f) Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
1 parent 1a4a3a7 commit 01de613

2 files changed

Lines changed: 175 additions & 21 deletions

File tree

packages/gatsby/src/utils/__tests__/handle-flags.ts

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,4 +557,155 @@ describe(`handle flags`, () => {
557557
`)
558558
})
559559
})
560+
561+
describe(`other flag suggestions`, () => {
562+
it(`suggest other flags when there is flag explicitly enabled`, () => {
563+
const response = handleFlags(
564+
[
565+
{
566+
name: `ENABLED_FLAG`,
567+
env: `GATSBY_ENABLED_FLAG`,
568+
command: `all`,
569+
description: `test`,
570+
umbrellaIssue: `test`,
571+
telemetryId: `test`,
572+
experimental: false,
573+
testFitness: (): fitnessEnum => true,
574+
},
575+
{
576+
name: `OTHER_FLAG`,
577+
env: `GATSBY_OTHER_FLAG`,
578+
command: `all`,
579+
description: `test`,
580+
umbrellaIssue: `test`,
581+
telemetryId: `test`,
582+
experimental: false,
583+
testFitness: (): fitnessEnum => true,
584+
},
585+
],
586+
{
587+
ENABLED_FLAG: true,
588+
},
589+
`build`
590+
)
591+
592+
expect(response.message).toMatchInlineSnapshot(`
593+
"The following flags are active:
594+
- ENABLED_FLAG · (Umbrella Issue (test)) · test
595+
596+
There is one other flag available that you might be interested in:
597+
- OTHER_FLAG · (Umbrella Issue (test)) · test
598+
"
599+
`)
600+
})
601+
602+
it(`suggest other flags when there is opted-in flag`, () => {
603+
const response = handleFlags(
604+
[
605+
{
606+
name: `OPTED_IN_FLAG`,
607+
env: `GATSBY_OPTED_IN_FLAG`,
608+
command: `all`,
609+
description: `test`,
610+
umbrellaIssue: `test`,
611+
telemetryId: `test`,
612+
experimental: false,
613+
testFitness: (): fitnessEnum => `OPT_IN`,
614+
},
615+
{
616+
name: `OTHER_FLAG`,
617+
env: `GATSBY_OTHER_FLAG`,
618+
command: `all`,
619+
description: `test`,
620+
umbrellaIssue: `test`,
621+
telemetryId: `test`,
622+
experimental: false,
623+
testFitness: (): fitnessEnum => true,
624+
},
625+
],
626+
{},
627+
`build`
628+
)
629+
630+
expect(response.message).toMatchInlineSnapshot(`
631+
"We're shipping new features! For final testing, we're rolling them out first to a small % of Gatsby users
632+
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.
633+
634+
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:
635+
636+
flags: {
637+
THE_FLAG: false
638+
}
639+
640+
The following flags were automatically enabled on your site:
641+
- OPTED_IN_FLAG · (Umbrella Issue (test)) · test
642+
643+
There is one other flag available that you might be interested in:
644+
- OTHER_FLAG · (Umbrella Issue (test)) · test
645+
"
646+
`)
647+
})
648+
649+
it(`doesn't suggest other flags if there are no enabled or opted in flags (no locked-in flags)`, () => {
650+
const response = handleFlags(
651+
[
652+
{
653+
name: `SOME_FLAG`,
654+
env: `GATSBY_SOME_FLAG`,
655+
command: `all`,
656+
description: `test`,
657+
umbrellaIssue: `test`,
658+
telemetryId: `test`,
659+
experimental: false,
660+
testFitness: (): fitnessEnum => true,
661+
},
662+
{
663+
name: `OTHER_FLAG`,
664+
env: `GATSBY_OTHER_FLAG`,
665+
command: `all`,
666+
description: `test`,
667+
umbrellaIssue: `test`,
668+
telemetryId: `test`,
669+
experimental: false,
670+
testFitness: (): fitnessEnum => true,
671+
},
672+
],
673+
{},
674+
`build`
675+
)
676+
677+
expect(response.message).toMatchInlineSnapshot(`""`)
678+
})
679+
680+
it(`doesn't suggest other flags if there are no enabled or opted in flags (with locked-in flag)`, () => {
681+
const response = handleFlags(
682+
[
683+
{
684+
name: `LOCKED_IN_FLAG`,
685+
env: `GATSBY_LOCKED_IN_FLAG`,
686+
command: `all`,
687+
description: `test`,
688+
umbrellaIssue: `test`,
689+
telemetryId: `test`,
690+
experimental: false,
691+
testFitness: (): fitnessEnum => `LOCKED_IN`,
692+
},
693+
{
694+
name: `OTHER_FLAG`,
695+
env: `GATSBY_OTHER_FLAG`,
696+
command: `all`,
697+
description: `test`,
698+
umbrellaIssue: `test`,
699+
telemetryId: `test`,
700+
experimental: false,
701+
testFitness: (): fitnessEnum => true,
702+
},
703+
],
704+
{},
705+
`build`
706+
)
707+
708+
expect(response.message).toMatchInlineSnapshot(`""`)
709+
})
710+
})
560711
})

packages/gatsby/src/utils/handle-flags.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,28 +203,31 @@ The following flags were automatically enabled on your site:`
203203
})
204204
}
205205

206-
const otherFlagSuggestionLines: Array<string> = []
207-
const enabledFlagsSet = new Set()
208-
enabledConfigFlags.forEach(f => enabledFlagsSet.add(f.name))
209-
applicableFlags.forEach(flag => {
210-
if (
211-
!enabledFlagsSet.has(flag.name) &&
212-
typeof configFlags[flag.name] === `undefined`
213-
) {
214-
// we want to suggest flag when it's not enabled and user specifically didn't use it in config
215-
// we don't want to suggest flag user specifically wanted to disable
216-
otherFlagSuggestionLines.push(generateFlagLine(flag))
206+
if (message.length > 0) {
207+
// if we will print anything about flags, let's try to suggest other available ones
208+
const otherFlagSuggestionLines: Array<string> = []
209+
const enabledFlagsSet = new Set()
210+
enabledConfigFlags.forEach(f => enabledFlagsSet.add(f.name))
211+
applicableFlags.forEach(flag => {
212+
if (
213+
!enabledFlagsSet.has(flag.name) &&
214+
typeof configFlags[flag.name] === `undefined`
215+
) {
216+
// we want to suggest flag when it's not enabled and user specifically didn't use it in config
217+
// we don't want to suggest flag user specifically wanted to disable
218+
otherFlagSuggestionLines.push(generateFlagLine(flag))
219+
}
220+
})
221+
222+
if (otherFlagSuggestionLines.length > 0) {
223+
message += `\n\nThere ${
224+
otherFlagSuggestionLines.length === 1
225+
? `is one other flag`
226+
: `are ${otherFlagSuggestionLines.length} other flags`
227+
} available that you might be interested in:${otherFlagSuggestionLines.join(
228+
``
229+
)}`
217230
}
218-
})
219-
220-
if (otherFlagSuggestionLines.length > 0) {
221-
message += `\n\nThere ${
222-
otherFlagSuggestionLines.length === 1
223-
? `is one other flag`
224-
: `are ${otherFlagSuggestionLines.length} other flags`
225-
} available that you might be interested in:${otherFlagSuggestionLines.join(
226-
``
227-
)}`
228231
}
229232

230233
if (message.length > 0) {

0 commit comments

Comments
 (0)