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

Commit 2ccf478

Browse files
committed
Add core options unit tests
1 parent b1d65f0 commit 2ccf478

6 files changed

Lines changed: 172 additions & 151 deletions

File tree

jest.config.js

Lines changed: 7 additions & 6 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: 70,
21-
functions: 75,
22-
lines: 75,
23-
statements: 75
20+
branches: 80,
21+
functions: 78,
22+
lines: 80,
23+
statements: 80
2424
}
2525
},
2626

@@ -43,9 +43,10 @@ module.exports = {
4343
"**/test/unit/core/mocks/FilesHandler.spec.js",
4444
"**/test/unit/core/mocks/Mocks.spec.js",
4545
"**/test/unit/core/server/Server.spec.js",
46-
"**/test/unit/core/settings/CommandLineArguments.spec.js"
46+
"**/test/unit/core/settings/CommandLineArguments.spec.js",
47+
"**/test/unit/core/settings/Options.spec.js"
4748
],
48-
//testMatch: ["**/test/unit/core/settings/CommandLineArguments.spec.js"],
49+
//testMatch: ["**/test/unit/core/settings/CommandLineArguments.spec.js", "**/test/unit/core/settings/Options.spec.js"],
4950

5051
// The test environment that will be used for testing
5152
testEnvironment: "node"

lib/core/settings/CommandLineArguments.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ Unless required by applicable law or agreed to in writing, software distributed
1111

1212
"use strict";
1313

14-
const commander = require("commander");
1514
const { isUndefined } = require("lodash");
1615

16+
const commander = require("commander");
17+
1718
class CommandLineArguments {
1819
constructor(defaultOptions) {
1920
this._options = {};
@@ -33,10 +34,7 @@ class CommandLineArguments {
3334
}
3435

3536
init() {
36-
this._options = this._getDefinedOptions(
37-
this._commander.parse(process.argv),
38-
this._optionsNames
39-
);
37+
this._options = this._commander.parse(process.argv);
4038
return Promise.resolve();
4139
}
4240

@@ -50,15 +48,6 @@ class CommandLineArguments {
5048
throw new Error("Invalid boolean value");
5149
}
5250

53-
_getDefinedOptions(options, keys) {
54-
return keys.reduce((cleanObject, currentKey) => {
55-
if (!isUndefined(options[currentKey])) {
56-
cleanObject[currentKey] = options[currentKey];
57-
}
58-
return cleanObject;
59-
}, {});
60-
}
61-
6251
addCustom(optionDetails) {
6352
const optionPrefix =
6453
optionDetails.type === "boolean" && optionDetails.default === true ? "--no-" : "--";

lib/core/settings/Options.js

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ http://www.apache.org/licenses/LICENSE-2.0
99
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
1010
*/
1111

12+
const { isUndefined } = require("lodash");
13+
1214
const tracer = require("../tracer");
1315

1416
const CommandLineArguments = require("./CommandLineArguments");
@@ -44,12 +46,14 @@ class Options {
4446
};
4547
if (!this._onlyProgrammaticOptions) {
4648
await this._commandLineArguments.init();
47-
this._options = this._removeDeprecatedOptions({
48-
...baseOptions,
49-
...this._commandLineArguments.options
50-
});
49+
this._options = this._getDefinedOptions(
50+
this._removeDeprecatedOptions({
51+
...baseOptions,
52+
...this._commandLineArguments.options
53+
})
54+
);
5155
} else {
52-
this._options = this._removeDeprecatedOptions(baseOptions);
56+
this._options = this._getDefinedOptions(this._removeDeprecatedOptions(baseOptions));
5357
}
5458
return Promise.resolve();
5559
}
@@ -75,44 +79,54 @@ class Options {
7579
tracer.error(`Option with name ${optionDetails.name} is already registered`);
7680
return;
7781
}
78-
if (!optionDetails.description) {
79-
tracer.error("Please provide option description when adding a new option");
80-
return;
81-
}
8282
if (
8383
!optionDetails.type ||
8484
!["string", "number", "boolean", "booleanString"].includes(optionDetails.type)
8585
) {
8686
tracer.error("Please provide a valid option type between: string, number, boolean");
8787
return;
8888
}
89+
if (!optionDetails.description) {
90+
tracer.warn("Please provide option description when adding a new option");
91+
optionDetails.description = "";
92+
}
8993

9094
this._optionsNames.push(optionDetails.name);
9195
this._customDefaults[optionDetails.name] = optionDetails.default;
9296
this._commandLineArguments.addCustom(optionDetails);
9397
}
9498

99+
_getDefinedOptions(options) {
100+
return this._optionsNames.reduce((cleanObject, currentKey) => {
101+
if (!isUndefined(options[currentKey])) {
102+
cleanObject[currentKey] = options[currentKey];
103+
}
104+
return cleanObject;
105+
}, {});
106+
}
107+
95108
// TODO, remove deprecated options
96109
_removeDeprecatedOptions(options) {
97-
if (options.feature) {
110+
if (options.feature !== DEFAULT_OPTIONS.feature) {
98111
tracer.warn(
99112
"Deprecation warning: --feature option will be deprecated. Use --behavior instead"
100113
);
101114
if (!options.behavior) {
102115
options.behavior = options.feature;
103116
}
104-
delete options.feature;
105117
}
106118

107-
if (options.features) {
119+
if (options.features !== DEFAULT_OPTIONS.features) {
108120
tracer.warn(
109121
"Deprecation warning: --features option will be deprecated. Use --behaviors instead"
110122
);
111123
if (!options.behaviors) {
112124
options.behaviors = options.features;
113125
}
114-
delete options.features;
115126
}
127+
128+
delete options.feature;
129+
delete options.features;
116130
return options;
117131
}
118132
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Copyright 2019 Javier Brea
3+
Copyright 2019 XbyOrange
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10+
*/
11+
12+
const sinon = require("sinon");
13+
14+
jest.mock("../../../../lib/core/settings/CommandLineArguments");
15+
16+
const CommandLineArguments = require("../../../../lib/core/settings/CommandLineArguments");
17+
18+
class Mock {
19+
constructor() {
20+
this._sandbox = sinon.createSandbox();
21+
22+
this._stubs = {
23+
init: this._sandbox.stub(),
24+
addCustom: this._sandbox.stub(),
25+
options: {}
26+
};
27+
28+
CommandLineArguments.mockImplementation(() => this._stubs);
29+
}
30+
31+
get stubs() {
32+
return {
33+
Constructor: CommandLineArguments,
34+
instance: this._stubs
35+
};
36+
}
37+
38+
restore() {
39+
this._sandbox.restore();
40+
}
41+
}
42+
43+
module.exports = Mock;

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

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,6 @@ describe("options", () => {
7575
await commandLineArguments.init();
7676
});
7777

78-
it("should only get values from keys defined in default values", async () => {
79-
const options = {
80-
behavior: "foo-behavior",
81-
behaviors: "foo/behaviors/path"
82-
};
83-
parseStub.returns({
84-
...options,
85-
foo: "foo-2",
86-
cli: true
87-
});
88-
await commandLineArguments.init();
89-
expect(commandLineArguments.options).toEqual(options);
90-
});
91-
9278
it("should omit undefined values", async () => {
9379
const options = {
9480
behavior: "foo-behavior",
@@ -169,29 +155,6 @@ describe("options", () => {
169155
});
170156

171157
describe("when adding custom option", () => {
172-
describe("when initializing", () => {
173-
it("should get values from keys defined in new options", async () => {
174-
const options = {
175-
behavior: "foo-behavior",
176-
behaviors: "foo/behaviors/path"
177-
};
178-
parseStub.returns({
179-
...options,
180-
foo: "foo-2",
181-
cli: true
182-
});
183-
commandLineArguments.addCustom({
184-
name: "cli",
185-
type: "boolean"
186-
});
187-
await commandLineArguments.init();
188-
expect(commandLineArguments.options).toEqual({
189-
...options,
190-
cli: true
191-
});
192-
});
193-
});
194-
195158
describe("when it is string type", () => {
196159
it("should add commander option with mandatory value", async () => {
197160
expect.assertions(2);

0 commit comments

Comments
 (0)