-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make feature flags available to
@netlify/config
(#6691)
* refactor: construct API client earlier * refactor: move siteID up * fix: fetch featureflags in CLI * chore: add tests for feeding feature flags to config * chore: fix lint errors * chore: fix tests * chore: fix order * chore: logging * chore: conditionally get site info for config * chore: fix get site params * chore: analyse all flags before getting site * chore: only call getSite once * chore: add flags to site obj * chore: only set flags if site there * chore: fix undefined issue * chore: fix link test * chore: remove logs * chore: remove only * Revert changes to Link test * fix: only request API if there's a token * fix: fetch fresh siteData only in build and dev * chore: remove unneeded arg * fix: only fetch featureflags for build/deploy/dev * refactor: extract into variable * chore: rework test * fix: don't fetch when offline * fix: only make request if token exists * fix: prettier * fix: remove unneeded argument * fix: share featureflags across invocations by making it an instance variable * Revert changes to site-by-name fetching * refactor: remove siteId parameter * fix: wrap featureflag finding in try/catch * chore: add readme for help on PR and serve tests * fix: get site info from name or id, and do it just once * Revert "chore: add readme for help on PR and serve tests" This reverts commit 7c25a9f. * Revert "fix: get site info from name or id, and do it just once" This reverts commit fbb18b6. * fix: pass feature flag arg --------- Co-authored-by: Simon Knott <info@simonknott.de> Co-authored-by: Lewis Thorley <lewis.thorley@netlify.com> Co-authored-by: Netlify Bot <bot@netlify.com> Co-authored-by: Karin Hendrikse <30577427+khendrikse@users.noreply.github.com>
- Loading branch information
1 parent
0f69e06
commit 1706c8c
Showing
4 changed files
with
113 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import process from 'process' | ||
|
||
import { expect, beforeEach, afterAll, describe, test, vi } from 'vitest' | ||
|
||
import BaseCommand from '../../../../src/commands/base-command.ts' | ||
import { createBuildCommand } from '../../../../src/commands/build/index.ts' | ||
import { getEnvironmentVariables, withMockApi } from '../../utils/mock-api.js' | ||
import { withSiteBuilder } from '../../utils/site-builder.ts' | ||
|
||
let configOptions = {} | ||
|
||
vi.mock('@netlify/config', async (importOriginal) => { | ||
const original = await importOriginal() | ||
return { | ||
...original, | ||
resolveConfig: (options) => { | ||
configOptions = options | ||
return original.resolveConfig(options) | ||
}, | ||
} | ||
}) | ||
|
||
const siteInfo = { | ||
account_slug: 'test-account', | ||
id: 'site_id', | ||
name: 'site-name', | ||
feature_flags: { test_flag: true }, | ||
} | ||
const routes = [ | ||
{ path: 'sites/site_id', response: siteInfo }, | ||
{ path: 'sites/site_id/service-instances', response: [] }, | ||
{ | ||
path: 'accounts', | ||
response: [{ slug: siteInfo.account_slug }], | ||
}, | ||
] | ||
// eslint-disable-next-line workspace/no-process-cwd | ||
const originalCwd = process.cwd | ||
const originalConsoleLog = console.log | ||
const originalEnv = process.env | ||
|
||
describe('command/build', () => { | ||
beforeEach(() => { | ||
vi.resetModules() | ||
vi.clearAllMocks() | ||
configOptions = {} | ||
console.log = () => {} | ||
}) | ||
|
||
afterAll(() => { | ||
// eslint-disable-next-line workspace/no-process-cwd | ||
process.cwd = originalCwd | ||
console.log = originalConsoleLog | ||
process.env = originalEnv | ||
|
||
vi.resetModules() | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
test('should pass feature flags to @netlify/config', async (t) => { | ||
// this ensures that the process.exit does not exit the test process | ||
vi.spyOn(process, 'exit').mockImplementation((code) => { | ||
expect(code).toBe(0) | ||
}) | ||
await withSiteBuilder(t, async (builder) => { | ||
// eslint-disable-next-line workspace/no-process-cwd | ||
process.cwd = () => builder.directory | ||
await withMockApi(routes, async ({ apiUrl }) => { | ||
process.env = getEnvironmentVariables({ apiUrl }) | ||
|
||
await builder.withNetlifyToml({ config: {} }).withStateFile({ siteId: siteInfo.id }).build() | ||
|
||
await createBuildCommand(new BaseCommand('netlify')).parseAsync(['', '', 'build']) | ||
expect(configOptions.featureFlags).toEqual(siteInfo.feature_flags) | ||
|
||
await createBuildCommand(new BaseCommand('netlify')).parseAsync(['', '', 'build', '--offline']) | ||
expect(configOptions.featureFlags, 'should not call API in offline mode').toEqual({}) | ||
}) | ||
}) | ||
}) | ||
}) |
1706c8c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📊 Benchmark results
1706c8c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📊 Benchmark results
1706c8c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📊 Benchmark results