Skip to content

Commit

Permalink
fix(gatsby): don't print out flag suggestions if none are enabled or …
Browse files Browse the repository at this point in the history
…opted-in (#31299) (#31362)

(cherry picked from commit 48eea6f)

Co-authored-by: Michal Piechowiak <misiek.piechowiak@gmail.com>
  • Loading branch information
GatsbyJS Bot and pieh committed May 11, 2021
1 parent 1a4a3a7 commit 01de613
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 21 deletions.
151 changes: 151 additions & 0 deletions packages/gatsby/src/utils/__tests__/handle-flags.ts
Expand Up @@ -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(`""`)
})
})
})
45 changes: 24 additions & 21 deletions packages/gatsby/src/utils/handle-flags.ts
Expand Up @@ -203,28 +203,31 @@ The following flags were automatically enabled on your site:`
})
}

const otherFlagSuggestionLines: Array<string> = []
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<string> = []
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) {
Expand Down

0 comments on commit 01de613

Please sign in to comment.