Skip to content

Commit

Permalink
key --> path, add a test for invalid command
Browse files Browse the repository at this point in the history
  • Loading branch information
mshustov committed May 11, 2021
1 parent da506e7 commit 03532f9
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 28 deletions.
40 changes: 38 additions & 2 deletions packages/kbn-config/src/deprecation/apply_deprecations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('applyDeprecations', () => {
const createAddDeprecation = jest.fn().mockReturnValue(addDeprecation);
const initialConfig = { foo: 'bar', deprecated: 'deprecated' };

const handlerA = jest.fn().mockReturnValue({ unset: [{ key: 'deprecated' }] });
const handlerA = jest.fn().mockReturnValue({ unset: [{ path: 'deprecated' }] });
const handlerB = jest.fn().mockReturnValue(undefined);

applyDeprecations(
Expand All @@ -61,7 +61,7 @@ describe('applyDeprecations', () => {
const handlerA = jest.fn().mockImplementation((config) => {
// the first argument is mutated between calls, we store a copy of it
configs.push({ fn: 'handlerA', config: { ...config } });
return { unset: [{ key: 'deprecated' }] };
return { unset: [{ path: 'deprecated' }] };
});
const handlerB = jest.fn().mockImplementation((config) => {
configs.push({ fn: 'handlerB', config: { ...config } });
Expand Down Expand Up @@ -100,4 +100,40 @@ describe('applyDeprecations', () => {
expect(initialConfig).toEqual({ foo: 'bar', deprecated: 'deprecated' });
expect(migrated).toEqual({ foo: 'bar' });
});

it('ignores a command for unknown path', () => {
const addDeprecation = jest.fn();
const createAddDeprecation = jest.fn().mockReturnValue(addDeprecation);
const initialConfig = { foo: 'bar', deprecated: 'deprecated' };

const handler = jest.fn().mockImplementation((config) => {
return { unset: [{ path: 'unknown' }] };
});

const migrated = applyDeprecations(
initialConfig,
[wrapHandler(handler, 'pathA')],
createAddDeprecation
);

expect(migrated).toEqual(initialConfig);
});

it('ignores an unknown command', () => {
const addDeprecation = jest.fn();
const createAddDeprecation = jest.fn().mockReturnValue(addDeprecation);
const initialConfig = { foo: 'bar', deprecated: 'deprecated' };

const handler = jest.fn().mockImplementation((config) => {
return { rewrite: [{ path: 'foo' }] };
});

const migrated = applyDeprecations(
initialConfig,
[wrapHandler(handler, 'pathA')],
createAddDeprecation
);

expect(migrated).toEqual(initialConfig);
});
});
8 changes: 4 additions & 4 deletions packages/kbn-config/src/deprecation/apply_deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export const applyDeprecations = (
const commands = deprecation(result, path, createAddDeprecation(path));
if (commands) {
if (commands.set) {
commands.set.forEach(function ({ key, value }) {
set(result, key, value);
commands.set.forEach(function ({ path: commandPath, value }) {
set(result, commandPath, value);
});
}
if (commands.unset) {
commands.unset.forEach(function ({ key }) {
unset(result, key);
commands.unset.forEach(function ({ path: commandPath }) {
unset(result, commandPath);
});
}
}
Expand Down
26 changes: 13 additions & 13 deletions packages/kbn-config/src/deprecation/deprecation_factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ describe('DeprecationFactory', () => {
expect(commands).toEqual({
set: [
{
key: 'myplugin.renamed',
path: 'myplugin.renamed',
value: 'toberenamed',
},
],
unset: [{ key: 'myplugin.deprecated' }],
unset: [{ path: 'myplugin.deprecated' }],
});
expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -88,11 +88,11 @@ describe('DeprecationFactory', () => {
expect(commands).toEqual({
set: [
{
key: 'myplugin.newsection.renamed',
path: 'myplugin.newsection.renamed',
value: 'toberenamed',
},
],
unset: [{ key: 'myplugin.oldsection.deprecated' }],
unset: [{ path: 'myplugin.oldsection.deprecated' }],
});
expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Array [
Expand All @@ -118,7 +118,7 @@ describe('DeprecationFactory', () => {
};
const commands = rename('deprecated', 'renamed')(rawConfig, 'myplugin', addDeprecation);
expect(commands).toEqual({
unset: [{ key: 'myplugin.deprecated' }],
unset: [{ path: 'myplugin.deprecated' }],
});
expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -157,11 +157,11 @@ describe('DeprecationFactory', () => {
expect(commands).toEqual({
set: [
{
key: 'myplugin.renamed',
path: 'myplugin.renamed',
value: 'toberenamed',
},
],
unset: [{ key: 'myplugin.deprecated' }],
unset: [{ path: 'myplugin.deprecated' }],
});
expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -197,11 +197,11 @@ describe('DeprecationFactory', () => {
expect(commands).toEqual({
set: [
{
key: 'newplugin.renamed',
path: 'newplugin.renamed',
value: 'toberenamed',
},
],
unset: [{ key: 'oldplugin.deprecated' }],
unset: [{ path: 'oldplugin.deprecated' }],
});
expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -251,7 +251,7 @@ describe('DeprecationFactory', () => {
addDeprecation
);
expect(commands).toEqual({
unset: [{ key: 'myplugin.deprecated' }],
unset: [{ path: 'myplugin.deprecated' }],
});

expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -285,7 +285,7 @@ describe('DeprecationFactory', () => {
};
const commands = unused('deprecated')(rawConfig, 'myplugin', addDeprecation);
expect(commands).toEqual({
unset: [{ key: 'myplugin.deprecated' }],
unset: [{ path: 'myplugin.deprecated' }],
});
expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -317,7 +317,7 @@ describe('DeprecationFactory', () => {
};
const commands = unused('section.deprecated')(rawConfig, 'myplugin', addDeprecation);
expect(commands).toEqual({
unset: [{ key: 'myplugin.section.deprecated' }],
unset: [{ path: 'myplugin.section.deprecated' }],
});
expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -367,7 +367,7 @@ describe('DeprecationFactory', () => {
addDeprecation
);
expect(commands).toEqual({
unset: [{ key: 'myplugin.deprecated' }],
unset: [{ path: 'myplugin.deprecated' }],
});
expect(addDeprecation.mock.calls).toMatchInlineSnapshot(`
Array [
Expand Down
8 changes: 4 additions & 4 deletions packages/kbn-config/src/deprecation/deprecation_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const _rename = (
...details,
});
return {
set: [{ key: fullNewPath, value: oldValue }],
unset: [{ key: fullOldPath }],
set: [{ path: fullNewPath, value: oldValue }],
unset: [{ path: fullOldPath }],
};
} else {
addDeprecation({
Expand All @@ -59,7 +59,7 @@ const _rename = (
}

return {
unset: [{ key: fullOldPath }],
unset: [{ path: fullOldPath }],
};
};

Expand All @@ -84,7 +84,7 @@ const _unused = (
...details,
});
return {
unset: [{ key: fullPath }],
unset: [{ path: fullPath }],
};
};

Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-config/src/deprecation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ export type ConfigDeprecation = (
* @public
*/
export interface ConfigDeprecationCommand {
set?: Array<{ key: string; value: any }>;
unset?: Array<{ key: string }>;
set?: Array<{ path: string; value: any }>;
unset?: Array<{ path: string }>;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/config/deprecation/core_deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const rewriteCorsSettings: ConfigDeprecation = (settings, fromPath, addDeprecati
});

return {
set: [{ key: 'server.cors', value: { enabled: corsSettings } }],
set: [{ path: 'server.cors', value: { enabled: corsSettings } }],
};
}
};
Expand All @@ -67,7 +67,7 @@ const cspRulesDeprecation: ConfigDeprecation = (settings, fromPath, addDeprecati
return {
set: [
{
key: 'csp.rules',
path: 'csp.rules',
value: [...parsed].map(([policy, sourceList]) => {
if (sourceList.find((source) => source.includes(NONCE_STRING))) {
addDeprecation({
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/banners/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const config: PluginConfigDescriptor<BannersConfigType> = {
message: 'The `header` value for xpack.banners.placement has been replaced by `top`',
});
return {
set: [{ key: `${fromPath}.placement`, value: 'top' }],
set: [{ path: `${fromPath}.placement`, value: 'top' }],
};
}
},
Expand Down

0 comments on commit 03532f9

Please sign in to comment.