-
Notifications
You must be signed in to change notification settings - Fork 8
Centralize enable option handling in the factory
#324
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
Merged
gh-worker-dd-mergequeue-cf854d
merged 4 commits into
master
from
watson/DEBUG-5291/fix-enable
May 22, 2026
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,110 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2019-Present Datadog, Inc. | ||
|
|
||
| import type { Logger } from '@dd/core/types'; | ||
|
|
||
| import { resetEnableWarnings, resolveEnable } from './options'; | ||
|
|
||
| const mockLogger: Logger = { | ||
| getLogger: jest.fn(), | ||
| time: jest.fn() as unknown as Logger['time'], | ||
| error: jest.fn(), | ||
| warn: jest.fn(), | ||
| info: jest.fn(), | ||
| debug: jest.fn(), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| resetEnableWarnings(); | ||
| }); | ||
|
|
||
| describe('resolveEnable', () => { | ||
| describe('standard boolean / omitted values', () => { | ||
| const cases = [ | ||
| { | ||
| description: 'return false when the config key is undefined', | ||
| options: {}, | ||
| expected: false, | ||
| }, | ||
| { | ||
| description: 'return false when the config key is null', | ||
| options: { myPlugin: null }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| description: 'return true when the config key is a truthy object without enable', | ||
| options: { myPlugin: { someOther: 'val' } }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| description: 'return true when enable is true', | ||
| options: { myPlugin: { enable: true } }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| description: 'return false when enable is false', | ||
| options: { myPlugin: { enable: false } }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| description: 'return true when enable is undefined (object present)', | ||
| options: { myPlugin: { enable: undefined } }, | ||
| expected: true, | ||
| }, | ||
| ]; | ||
|
|
||
| test.each(cases)('should $description', ({ options, expected }) => { | ||
| expect(resolveEnable(options, 'myPlugin', mockLogger)).toBe(expected); | ||
| expect(mockLogger.warn).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('non-boolean coercion with deprecation warning', () => { | ||
| const cases = [ | ||
| { | ||
| description: 'coerce enable: 1 to true and warn', | ||
| options: { myPlugin: { enable: 1 } }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| description: 'coerce enable: 0 to false and warn', | ||
| options: { myPlugin: { enable: 0 } }, | ||
| expected: false, | ||
| }, | ||
| { | ||
| description: 'coerce enable: "true" to true and warn', | ||
| options: { myPlugin: { enable: 'true' } }, | ||
| expected: true, | ||
| }, | ||
| { | ||
| description: 'coerce enable: "" to false and warn', | ||
| options: { myPlugin: { enable: '' } }, | ||
| expected: false, | ||
| }, | ||
| ]; | ||
|
|
||
| test.each(cases)('should $description', ({ options, expected }) => { | ||
| expect(resolveEnable(options, 'myPlugin', mockLogger)).toBe(expected); | ||
| expect(mockLogger.warn).toHaveBeenCalledTimes(1); | ||
| expect(mockLogger.warn).toHaveBeenCalledWith( | ||
| expect.stringContaining('myPlugin.enable'), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('warn-once behavior', () => { | ||
| test('should only warn once per config key across multiple calls', () => { | ||
| resolveEnable({ myPlugin: { enable: 1 } }, 'myPlugin', mockLogger); | ||
| resolveEnable({ myPlugin: { enable: 'yes' } }, 'myPlugin', mockLogger); | ||
| expect(mockLogger.warn).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('should warn separately for different config keys', () => { | ||
| resolveEnable({ pluginA: { enable: 1 } }, 'pluginA', mockLogger); | ||
| resolveEnable({ pluginB: { enable: 1 } }, 'pluginB', mockLogger); | ||
| expect(mockLogger.warn).toHaveBeenCalledTimes(2); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,52 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed under the MIT License. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2019-Present Datadog, Inc. | ||
|
|
||
| import type { Logger } from '@dd/core/types'; | ||
|
|
||
| const warnedKeys = new Set<string>(); | ||
|
|
||
| /** | ||
| * Resolve the `enable` value for a plugin config key, emitting a deprecation | ||
| * warning when the caller passes a non-boolean truthy/falsy value. | ||
| * | ||
| * Semantics: | ||
| * - Config key absent / undefined / falsy → false (plugin disabled). | ||
| * - Config key is a truthy object without an `enable` property → true. | ||
| * - Config key is a truthy object with `enable` set → coerce to boolean, | ||
| * warning once per key if it isn't already a boolean. | ||
| */ | ||
| export const resolveEnable = <T extends { [K in C]?: unknown }, C extends string>( | ||
| options: T, | ||
| configKey: C, | ||
| log: Logger, | ||
| ): boolean => { | ||
| const pluginConfig = options[configKey]; | ||
|
|
||
| if (pluginConfig && typeof pluginConfig === 'object' && 'enable' in pluginConfig) { | ||
| const value = (pluginConfig as Record<string, unknown>).enable; | ||
|
watson marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (typeof value !== 'boolean' && value !== undefined) { | ||
| if (!warnedKeys.has(configKey)) { | ||
| warnedKeys.add(configKey); | ||
| log.warn( | ||
| `\`${configKey}.enable\` should be a boolean, got ${typeof value}. ` + | ||
| `Non-boolean values are coerced today but will be rejected in the next major.`, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| if (value !== undefined) { | ||
| // TODO(next major): drop this coercion and reject non-boolean `enable` | ||
| // outright. The warning above gives callers one major to migrate. | ||
| return !!value; | ||
| } | ||
| } | ||
|
|
||
| return !!pluginConfig; | ||
| }; | ||
|
|
||
| /** @internal Exposed only for tests to reset the warn-once set between cases. */ | ||
| export const resetEnableWarnings = (): void => { | ||
| warnedKeys.clear(); | ||
| }; | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.