Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby): don't print out flag suggestions if none are enabled or opted-in (#31299) #31362

Merged
merged 1 commit into from
May 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions packages/gatsby/src/utils/__tests__/handle-flags.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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