Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit a93b2ef

Browse files
committed
Add compatibility with deprecated options to Settings
1 parent 5e7bdfe commit a93b2ef

6 files changed

Lines changed: 143 additions & 32 deletions

File tree

jest.config.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ module.exports = {
1717
// An object that configures minimum threshold enforcement for coverage results
1818
coverageThreshold: {
1919
global: {
20-
branches: 80,
21-
functions: 78,
22-
lines: 80,
23-
statements: 80
20+
branches: 85,
21+
functions: 82,
22+
lines: 85,
23+
statements: 85
2424
}
2525
},
2626

@@ -47,7 +47,7 @@ module.exports = {
4747
"**/test/unit/core/settings/Options.spec.js",
4848
"**/test/unit/core/settings/Settings.spec.js"
4949
],
50-
//testMatch: ["**/test/unit/core/settings/Settings.spec.js"],
50+
//testMatch: ["**/test/unit/core/settings/Settings.spec.js", "**/test/unit/core/settings/Options.spec.js"],
5151

5252
// The test environment that will be used for testing
5353
testEnvironment: "node"

lib/core/settings/Options.js

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,19 @@ const DEFAULT_OPTIONS = {
2828
features: null
2929
};
3030

31+
const DEPRECATED_OPTIONS = {
32+
feature: "behavior",
33+
features: "behaviors"
34+
};
35+
3136
class Options {
3237
constructor(coreOptions = {}) {
3338
this._onlyProgrammaticOptions = coreOptions.onlyProgrammaticOptions;
3439
this._options = {};
3540
this._optionsNames = Object.keys(DEFAULT_OPTIONS);
3641
this._customDefaults = {};
3742
this._initialized = false;
43+
this._removeDeprecatedOption = this._removeDeprecatedOption.bind(this);
3844
this._commandLineArguments = new CommandLineArguments(DEFAULT_OPTIONS);
3945
}
4046

@@ -48,14 +54,14 @@ class Options {
4854
};
4955
if (!this._onlyProgrammaticOptions) {
5056
await this._commandLineArguments.init();
51-
this._options = this._getDefinedOptions(
57+
this._options = this._getValidOptions(
5258
this._removeDeprecatedOptions({
5359
...baseOptions,
5460
...this._commandLineArguments.options
5561
})
5662
);
5763
} else {
58-
this._options = this._getDefinedOptions(this._removeDeprecatedOptions(baseOptions));
64+
this._options = this._getValidOptions(this._removeDeprecatedOptions(baseOptions));
5965
}
6066
}
6167
return Promise.resolve();
@@ -101,7 +107,20 @@ class Options {
101107
this._commandLineArguments.addCustom(optionDetails);
102108
}
103109

104-
_getDefinedOptions(options) {
110+
getValidOptionName(optionName) {
111+
if (this._optionsNames.includes(optionName) && !DEPRECATED_OPTIONS[optionName]) {
112+
return optionName;
113+
}
114+
if (DEPRECATED_OPTIONS[optionName]) {
115+
tracer.warn(
116+
`Deprecation warning: ${optionName} option will be deprecated. Use ${DEPRECATED_OPTIONS[optionName]} instead`
117+
);
118+
return DEPRECATED_OPTIONS[optionName];
119+
}
120+
throw new Error("Not valid option");
121+
}
122+
123+
_getValidOptions(options) {
105124
return this._optionsNames.reduce((cleanObject, currentKey) => {
106125
if (!isUndefined(options[currentKey])) {
107126
cleanObject[currentKey] = options[currentKey];
@@ -110,30 +129,28 @@ class Options {
110129
}, {});
111130
}
112131

113-
// TODO, remove deprecated options
114-
_removeDeprecatedOptions(options) {
115-
if (options.feature !== DEFAULT_OPTIONS.feature) {
116-
tracer.warn(
117-
"Deprecation warning: --feature option will be deprecated. Use --behavior instead"
118-
);
119-
if (!options.behavior) {
120-
options.behavior = options.feature;
132+
_removeDeprecatedOption(options, optionName) {
133+
if (options[optionName] !== DEFAULT_OPTIONS[optionName]) {
134+
const newOption = DEPRECATED_OPTIONS[optionName];
135+
if (!options[newOption]) {
136+
options[newOption] = options[optionName];
121137
}
122-
}
123-
124-
if (options.features !== DEFAULT_OPTIONS.features) {
125138
tracer.warn(
126-
"Deprecation warning: --features option will be deprecated. Use --behaviors instead"
139+
`Deprecation warning: --${optionName} option will be deprecated. Use --${DEPRECATED_OPTIONS[optionName]} instead`
127140
);
128-
if (!options.behaviors) {
129-
options.behaviors = options.features;
130-
}
131141
}
132-
133-
delete options.feature;
134-
delete options.features;
142+
delete options[optionName];
135143
return options;
136144
}
145+
146+
_removeDeprecatedOptions(options) {
147+
let newOptions = options;
148+
Object.keys(DEPRECATED_OPTIONS).forEach(optionName => {
149+
newOptions = this._removeDeprecatedOption(newOptions, optionName);
150+
});
151+
152+
return newOptions;
153+
}
137154
}
138155

139156
module.exports = Options;

lib/core/settings/Settings.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,19 @@ class Settings {
4141
}
4242

4343
set(option, value) {
44-
if (this._settings[option] !== value) {
45-
this._settings[option] = value;
46-
this._newSettings[option] = value;
44+
const optionName = this._optionsHandler.getValidOptionName(option);
45+
if (this._settings[optionName] !== value) {
46+
this._settings[optionName] = value;
47+
this._newSettings[optionName] = value;
4748
this._emitChange();
48-
if (option === "log") {
49+
if (optionName === "log") {
4950
this._setTracerLevel();
5051
}
5152
}
5253
}
5354

5455
get(option) {
55-
return this._settings[option];
56+
return this._settings[this._optionsHandler.getValidOptionName(option)];
5657
}
5758

5859
addCustom(option) {

test/unit/core/settings/Options.mocks.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ class Mock {
2222
this._stubs = {
2323
init: this._sandbox.stub(),
2424
addCustom: this._sandbox.stub(),
25+
getValidOptionName: this._sandbox.stub(),
2526
options: {
26-
log: "foo-log-level"
27+
log: "foo-log-level",
28+
behavior: "foo-behavior"
2729
}
2830
};
2931

test/unit/core/settings/Options.spec.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ describe("options", () => {
199199
});
200200
});
201201

202+
it("should remove deprecated options", async () => {
203+
commandLineArgumentsMocks.stubs.instance.options = {
204+
behavior: "foo-behavior",
205+
cli: true,
206+
behaviors: "foo/behaviors/path",
207+
foo: undefined,
208+
foo2: "foooo",
209+
recursive: false
210+
};
211+
await options.init();
212+
expect(options.options).toEqual({
213+
port: 3100,
214+
host: "0.0.0.0",
215+
log: "info",
216+
delay: 0,
217+
watch: true,
218+
behavior: "foo-behavior",
219+
behaviors: "foo/behaviors/path"
220+
});
221+
});
222+
202223
it("should get values from keys defined in new options", async () => {
203224
commandLineArgumentsMocks.stubs.instance.options = {
204225
behavior: "foo-behavior",
@@ -312,6 +333,26 @@ describe("options", () => {
312333
});
313334
});
314335

336+
it("should remove deprecated options", async () => {
337+
await options.init({
338+
behavior: "foo-behavior",
339+
cli: true,
340+
behaviors: "foo/behaviors/path",
341+
foo: undefined,
342+
foo2: "foooo",
343+
recursive: false
344+
});
345+
expect(options.options).toEqual({
346+
port: 3100,
347+
host: "0.0.0.0",
348+
log: "info",
349+
delay: 0,
350+
watch: true,
351+
behavior: "foo-behavior",
352+
behaviors: "foo/behaviors/path"
353+
});
354+
});
355+
315356
it("should get values from keys defined in new options", async () => {
316357
options.addCustom({
317358
name: "cli",
@@ -394,4 +435,42 @@ describe("options", () => {
394435
});
395436
});
396437
});
438+
439+
describe("getValidOptionName method", () => {
440+
it("should throw an error if option is not valid", async () => {
441+
expect.assertions(1);
442+
await options.init();
443+
try {
444+
options.getValidOptionName("foo");
445+
} catch (error) {
446+
expect(error.message).toEqual(expect.stringContaining("Not valid option"));
447+
}
448+
});
449+
450+
it("should return option name if option is valid", async () => {
451+
await options.init();
452+
expect(options.getValidOptionName("behavior")).toEqual("behavior");
453+
});
454+
455+
it("should return new option name if option is deprecated", async () => {
456+
expect.assertions(2);
457+
await options.init();
458+
const option = options.getValidOptionName("feature");
459+
expect(
460+
tracer.warn.calledWith(
461+
"Deprecation warning: feature option will be deprecated. Use behavior instead"
462+
)
463+
).toEqual(true);
464+
expect(option).toEqual("behavior");
465+
});
466+
467+
it("should return true if option is custom option", async () => {
468+
options.addCustom({
469+
name: "foo",
470+
type: "boolean"
471+
});
472+
await options.init();
473+
expect(options.getValidOptionName("foo")).toEqual("foo");
474+
});
475+
});
397476
});

test/unit/core/settings/Settings.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ describe("Settings", () => {
2929
sandbox = sinon.createSandbox();
3030
optionsMocks = new OptionsMocks();
3131
optionsInstance = optionsMocks.stubs.instance;
32+
optionsInstance.getValidOptionName.callsFake(name => name);
3233
coreMocks = new CoreMocks();
3334
coreInstance = coreMocks.stubs.instance;
3435
sandbox.stub(tracer, "set");
@@ -58,6 +59,11 @@ describe("Settings", () => {
5859
expect(settings.get("log")).toEqual("foo-log-level");
5960
});
6061

62+
it("should return current deprecated option value", () => {
63+
optionsInstance.getValidOptionName.returns("behavior");
64+
expect(settings.get("log")).toEqual("foo-behavior");
65+
});
66+
6167
it("should return new value if set is called", () => {
6268
settings.set("log", "foo-new-value");
6369
expect(settings.get("log")).toEqual("foo-new-value");
@@ -70,6 +76,12 @@ describe("Settings", () => {
7076
expect(tracer.set.calledWith("console", "foo-new-value")).toEqual(true);
7177
});
7278

79+
it("should set new option if provided one is deprecated", () => {
80+
optionsInstance.getValidOptionName.returns("behavior");
81+
settings.set("log", "foo");
82+
expect(settings.get("behavior")).toEqual("foo");
83+
});
84+
7385
it("should emit change if setting has changed value", () => {
7486
settings.set("foo", "foo-new-value");
7587
expect(

0 commit comments

Comments
 (0)