How to use parseconfig method in tox

Best Python code snippet using tox_python

util_spec.js

Source:util_spec.js Github

copy

Full Screen

1/**2 * Copyright JS Foundation and other contributors, http://js.foundation3 *4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 **/16var should = require("should");17var sinon = require("sinon");18var when = require("when");19var clone = require("clone");20var NR_TEST_UTILS = require("nr-test-utils");21var flowUtil = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/flows/util");22var typeRegistry = NR_TEST_UTILS.require("@node-red/registry");23var redUtil = NR_TEST_UTILS.require("@node-red/util").util;24describe('flows/util', function() {25 var getType;26 before(function() {27 getType = sinon.stub(typeRegistry,"get",function(type) {28 return type!=='missing';29 });30 });31 after(function() {32 getType.restore();33 });34 describe('#mapEnvVarProperties',function() {35 before(function() {36 process.env.foo1 = "bar1";37 process.env.foo2 = "bar2";38 process.env.foo3 = "bar3";39 })40 after(function() {41 delete process.env.foo1;42 delete process.env.foo2;43 delete process.env.foo3;44 })45 it('handles ENV substitutions in an object - $()', function() {46 var foo = {a:"$(foo1)",b:"$(foo2)",c:{d:"$(foo3)"}};47 for (var p in foo) {48 if (foo.hasOwnProperty(p)) {49 flowUtil.mapEnvVarProperties(foo,p,{getSetting: p => process.env[p]});50 }51 }52 foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );53 });54 it('handles ENV substitutions in an object - ${}', function() {55 var foo = {a:"${foo1}",b:"${foo2}",c:{d:"${foo3}"}};56 for (var p in foo) {57 if (foo.hasOwnProperty(p)) {58 flowUtil.mapEnvVarProperties(foo,p,{getSetting: p => process.env[p]});59 }60 }61 foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );62 });63 it('gets ENV from parent flow', function() {64 var foo = {a:"$(unknown)",b:"$(foo2)",c:{d:"$(foo3)"}};65 for (var p in foo) {66 if (foo.hasOwnProperty(p)) {67 flowUtil.mapEnvVarProperties(foo,p,{68 getSetting: name => name[0]==='f'?name.toUpperCase():undefined69 });70 }71 }72 foo.should.eql({ a: '$(unknown)', b: 'FOO2', c: { d: 'FOO3' } } );73 });74 });75 describe('#getEnvVar',function() {76 before(function() {77 process.env.foo1 = "bar1";78 })79 after(function() {80 delete process.env.foo1;81 })82 it('returns a known env var', function() {83 flowUtil.init({settings:{}});84 flowUtil.getEnvVar("foo1").should.equal("bar1")85 })86 it('returns undefined for an unknown env var', function() {87 flowUtil.init({settings:{}});88 (flowUtil.getEnvVar("foo2") === undefined).should.be.true()89 })90 it('returns undefined for an excluded env var', function() {91 flowUtil.init({settings:{envVarExcludes:['foo1']}});92 (flowUtil.getEnvVar("foo1") === undefined).should.be.true()93 })94 });95 describe('#diffNodes',function() {96 it('handles a null old node', function() {97 flowUtil.diffNodes(null,{}).should.be.true();98 });99 it('ignores x/y changes', function() {100 flowUtil.diffNodes({x:10,y:10},{x:20,y:10}).should.be.false();101 flowUtil.diffNodes({x:10,y:10},{x:10,y:20}).should.be.false();102 });103 it('ignores wiring changes', function() {104 flowUtil.diffNodes({wires:[]},{wires:[1,2,3]}).should.be.false();105 });106 it('spots existing property change - string', function() {107 flowUtil.diffNodes({a:"foo"},{a:"bar"}).should.be.true();108 });109 it('spots existing property change - number', function() {110 flowUtil.diffNodes({a:0},{a:1}).should.be.true();111 });112 it('spots existing property change - boolean', function() {113 flowUtil.diffNodes({a:true},{a:false}).should.be.true();114 });115 it('spots existing property change - truthy', function() {116 flowUtil.diffNodes({a:true},{a:1}).should.be.true();117 });118 it('spots existing property change - falsey', function() {119 flowUtil.diffNodes({a:false},{a:0}).should.be.true();120 });121 it('spots existing property change - array', function() {122 flowUtil.diffNodes({a:[0,1,2]},{a:[0,2,3]}).should.be.true();123 });124 it('spots existing property change - object', function() {125 flowUtil.diffNodes({a:{a:[0,1,2]}},{a:{a:[0,2,3]}}).should.be.true();126 flowUtil.diffNodes({a:{a:[0,1,2]}},{a:{b:[0,1,2]}}).should.be.true();127 });128 it('spots added property', function() {129 flowUtil.diffNodes({a:"foo"},{a:"foo",b:"bar"}).should.be.true();130 });131 it('spots removed property', function() {132 flowUtil.diffNodes({a:"foo",b:"bar"},{a:"foo"}).should.be.true();133 });134 });135 describe('#parseConfig',function() {136 it('parses a single-tab flow', function() {137 var originalConfig = [138 {id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},139 {id:"t1",type:"tab"}140 ];141 var parsedConfig = flowUtil.parseConfig(originalConfig);142 var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]},"t1":{"id":"t1","type":"tab"}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]}}}},"missingTypes":[]};143 parsedConfig.should.eql(expectedConfig);144 });145 it('parses a single-tab flow with global config node', function() {146 var originalConfig = [147 {id:"t1-1",x:10,y:10,z:"t1",type:"test",foo:"cn", wires:[]},148 {id:"cn",type:"test"},149 {id:"t1",type:"tab"}150 ];151 var parsedConfig = flowUtil.parseConfig(originalConfig);152 var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]},"cn":{"id":"cn","type":"test"},"t1":{"id":"t1","type":"tab"}},"subflows":{},"configs":{"cn":{"id":"cn","type":"test","_users":["t1-1"]}},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]}}}},"missingTypes":[]};153 parsedConfig.should.eql(expectedConfig);154 });155 it('parses a multi-tab flow', function() {156 var originalConfig = [157 {id:"t1",type:"tab"},158 {id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},159 {id:"t2",type:"tab"},160 {id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]}161 ];162 var parsedConfig = flowUtil.parseConfig(originalConfig);163 var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]},"t2":{"id":"t2","type":"tab"},"t2-1":{"id":"t2-1","x":10,"y":10,"z":"t2","type":"test","wires":[]}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]}}},"t2":{"id":"t2","type":"tab","subflows":{},"configs":{},"nodes":{"t2-1":{"id":"t2-1","x":10,"y":10,"z":"t2","type":"test","wires":[]}}}},"missingTypes":[]};164 parsedConfig.should.eql(expectedConfig);165 });166 it('parses a subflow flow', function() {167 var originalConfig = [168 {id:"t1",type:"tab"},169 {id:"t1-1",x:10,y:10,z:"t1",type:"subflow:sf1",wires:[]},170 {id:"sf1",type:"subflow"},171 {id:"sf1-1",x:10,y:10,z:"sf1",type:"test",wires:[]}172 ];173 var parsedConfig = flowUtil.parseConfig(originalConfig);174 var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[]},"sf1":{"id":"sf1","type":"subflow"},"sf1-1":{"id":"sf1-1","x":10,"y":10,"z":"sf1","type":"test","wires":[]}},"subflows":{"sf1":{"id":"sf1","type":"subflow","configs":{},"nodes":{"sf1-1":{"id":"sf1-1","x":10,"y":10,"z":"sf1","type":"test","wires":[]}},"instances":[{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[],"subflow":"sf1"}]}},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[],"subflow":"sf1"}}}},"missingTypes":[]};175 parsedConfig.should.eql(expectedConfig);176 });177 it('parses a flow with a missing type', function() {178 var originalConfig = [179 {id:"t1",type:"tab"},180 {id:"t1-1",x:10,y:10,z:"t1",type:"sf1",wires:[]},181 {id:"t1-2",x:10,y:10,z:"t1",type:"missing",wires:[]},182 ];183 var parsedConfig = flowUtil.parseConfig(originalConfig);184 parsedConfig.missingTypes.should.eql(['missing']);185 var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"sf1","wires":[]},"t1-2":{"id":"t1-2","x":10,"y":10,"z":"t1","type":"missing","wires":[]}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"sf1","wires":[]},'t1-2': { id: 't1-2', x: 10, y: 10, z: 't1', type: 'missing', wires: [] }}}},"missingTypes":["missing"]};186 redUtil.compareObjects(parsedConfig,expectedConfig).should.be.true();187 });188 it('parses a flow with a missing flow', function() {189 var originalConfig = [190 {id:"t1-1",x:10,y:10,z:"t1",type:"test",foo:"cn", wires:[]},191 {id:"cn",type:"test"},192 ];193 var parsedConfig = flowUtil.parseConfig(originalConfig);194 var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]},"cn":{"id":"cn","type":"test"}},"subflows":{},"configs":{"cn":{"id":"cn","type":"test","_users":["t1-1"]}},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]}}}},"missingTypes":[]};195 parsedConfig.should.eql(expectedConfig);196 });197 });198 describe('#diffConfigs', function() {199 it('handles an identical configuration', function() {200 var config = [{id:"123",type:"test",foo:"a",wires:[]}];201 var originalConfig = flowUtil.parseConfig(clone(config));202 var changedConfig = flowUtil.parseConfig(clone(config));203 originalConfig.missingTypes.should.have.length(0);204 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);205 diffResult.added.should.have.length(0);206 diffResult.changed.should.have.length(0);207 diffResult.removed.should.have.length(0);208 diffResult.rewired.should.have.length(0);209 diffResult.linked.should.have.length(0);210 });211 it('identifies nodes with changed properties, including downstream linked', function() {212 var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[[1]]},{id:"3",type:"test",foo:"a",wires:[]}];213 var newConfig = clone(config);214 newConfig[0].foo = "b";215 var originalConfig = flowUtil.parseConfig(config);216 var changedConfig = flowUtil.parseConfig(newConfig);217 originalConfig.missingTypes.should.have.length(0);218 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);219 diffResult.added.should.have.length(0);220 diffResult.changed.should.eql(["1"]);221 diffResult.removed.should.have.length(0);222 diffResult.rewired.should.have.length(0);223 diffResult.linked.should.eql(["2"]);224 });225 it('identifies nodes with changed properties, including upstream linked', function() {226 var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];227 var newConfig = clone(config);228 newConfig[1].bar = "c";229 var originalConfig = flowUtil.parseConfig(config);230 var changedConfig = flowUtil.parseConfig(newConfig);231 originalConfig.missingTypes.should.have.length(0);232 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);233 diffResult.added.should.have.length(0);234 diffResult.changed.should.eql(["2"]);235 diffResult.removed.should.have.length(0);236 diffResult.rewired.should.have.length(0);237 diffResult.linked.should.eql(["1"]);238 });239 it('identifies nodes with changed credentials, including downstream linked', function() {240 var config = [{id:"1",type:"test",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];241 var newConfig = clone(config);242 newConfig[0].credentials = {};243 var originalConfig = flowUtil.parseConfig(config);244 var changedConfig = flowUtil.parseConfig(newConfig);245 originalConfig.missingTypes.should.have.length(0);246 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);247 diffResult.added.should.have.length(0);248 diffResult.changed.should.eql(["1"]);249 diffResult.removed.should.have.length(0);250 diffResult.rewired.should.have.length(0);251 diffResult.linked.should.eql(["2"]);252 });253 it('identifies nodes with changed wiring', function() {254 var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];255 var newConfig = clone(config);256 newConfig[1].wires[0][0] = "3";257 var originalConfig = flowUtil.parseConfig(config);258 var changedConfig = flowUtil.parseConfig(newConfig);259 originalConfig.missingTypes.should.have.length(0);260 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);261 diffResult.added.should.have.length(0);262 diffResult.changed.should.have.length(0);263 diffResult.removed.should.have.length(0);264 diffResult.rewired.should.eql(["2"]);265 diffResult.linked.sort().should.eql(["1","3"]);266 });267 it('identifies nodes with changed wiring - second connection added', function() {268 var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];269 var newConfig = clone(config);270 newConfig[1].wires[0].push("1");271 var originalConfig = flowUtil.parseConfig(config);272 var changedConfig = flowUtil.parseConfig(newConfig);273 originalConfig.missingTypes.should.have.length(0);274 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);275 diffResult.added.should.have.length(0);276 diffResult.changed.should.have.length(0);277 diffResult.removed.should.have.length(0);278 diffResult.rewired.should.eql(["2"]);279 diffResult.linked.sort().should.eql(["1"]);280 });281 it('identifies nodes with changed wiring - node connected', function() {282 var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[[]]},{id:"3",type:"test",foo:"a",wires:[]}];283 var newConfig = clone(config);284 newConfig[1].wires.push("3");285 var originalConfig = flowUtil.parseConfig(config);286 var changedConfig = flowUtil.parseConfig(newConfig);287 originalConfig.missingTypes.should.have.length(0);288 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);289 diffResult.added.should.have.length(0);290 diffResult.changed.should.have.length(0);291 diffResult.removed.should.have.length(0);292 diffResult.rewired.should.eql(["2"]);293 diffResult.linked.sort().should.eql(["1","3"]);294 });295 it('identifies new nodes', function() {296 var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"3",type:"test",foo:"a",wires:[]}];297 var newConfig = clone(config);298 newConfig.push({id:"2",type:"test",bar:"b",wires:[["1"]]});299 var originalConfig = flowUtil.parseConfig(config);300 var changedConfig = flowUtil.parseConfig(newConfig);301 originalConfig.missingTypes.should.have.length(0);302 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);303 diffResult.added.should.eql(["2"]);304 diffResult.changed.should.have.length(0);305 diffResult.removed.should.have.length(0);306 diffResult.rewired.should.have.length(0);307 diffResult.linked.sort().should.eql(["1"]);308 });309 it('identifies deleted nodes', function() {310 var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[["3"]]},{id:"3",type:"test",foo:"a",wires:[]}];311 var newConfig = clone(config);312 newConfig.splice(1,1);313 newConfig[0].wires = [];314 var originalConfig = flowUtil.parseConfig(config);315 var changedConfig = flowUtil.parseConfig(newConfig);316 originalConfig.missingTypes.should.have.length(0);317 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);318 diffResult.added.should.have.length(0);319 diffResult.changed.should.have.length(0);320 diffResult.removed.should.eql(["2"]);321 diffResult.rewired.should.eql(["1"]);322 diffResult.linked.sort().should.eql(["3"]);323 });324 it('identifies config nodes changes, node->config', function() {325 var config = [326 {id:"1",type:"test",foo:"configNode",wires:[["2"]]},327 {id:"2",type:"test",bar:"b",wires:[["3"]]},328 {id:"3",type:"test",foo:"a",wires:[]},329 {id:"configNode",type:"testConfig"}330 ];331 var newConfig = clone(config);332 newConfig[3].foo = "bar";333 var originalConfig = flowUtil.parseConfig(config);334 var changedConfig = flowUtil.parseConfig(newConfig);335 originalConfig.missingTypes.should.have.length(0);336 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);337 diffResult.added.should.have.length(0);338 diffResult.changed.sort().should.eql(["1","configNode"]);339 diffResult.removed.should.have.length(0);340 diffResult.rewired.should.have.length(0);341 diffResult.linked.sort().should.eql(["2","3"]);342 });343 it('identifies config nodes changes, node->config->config', function() {344 var config = [345 {id:"1",type:"test",foo:"configNode1",wires:[["2"]]},346 {id:"2",type:"test",bar:"b",wires:[["3"]]},347 {id:"3",type:"test",foo:"a",wires:[]},348 {id:"configNode1",foo:"configNode2",type:"testConfig"},349 {id:"configNode2",type:"testConfig"}350 ];351 var newConfig = clone(config);352 newConfig[4].foo = "bar";353 var originalConfig = flowUtil.parseConfig(config);354 var changedConfig = flowUtil.parseConfig(newConfig);355 originalConfig.missingTypes.should.have.length(0);356 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);357 diffResult.added.should.have.length(0);358 diffResult.changed.sort().should.eql(["1","configNode1","configNode2"]);359 diffResult.removed.should.have.length(0);360 diffResult.rewired.should.have.length(0);361 diffResult.linked.sort().should.eql(["2","3"]);362 });363 it('marks a parent subflow as changed for an internal property change', function() {364 var config = [365 {id:"1",type:"test",wires:[["2"]]},366 {id:"2",type:"subflow:sf1",wires:[["3"]]},367 {id:"3",type:"test",wires:[]},368 {id:"sf1",type:"subflow"},369 {id:"sf1-1",z:"sf1",type:"test",foo:"a",wires:[]},370 {id:"4",type:"subflow:sf1",wires:[]}371 ];372 var newConfig = clone(config);373 newConfig[4].foo = "b";374 var originalConfig = flowUtil.parseConfig(config);375 var changedConfig = flowUtil.parseConfig(newConfig);376 originalConfig.missingTypes.should.have.length(0);377 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);378 diffResult.added.should.have.length(0);379 diffResult.changed.sort().should.eql(['2', '4', 'sf1']);380 diffResult.removed.should.have.length(0);381 diffResult.rewired.should.have.length(0);382 diffResult.linked.sort().should.eql(["1","3"]);383 });384 it('marks a parent subflow as changed for an internal wiring change', function() {385 var config = [386 {id:"1",type:"test",wires:[["2"]]},387 {id:"2",type:"subflow:sf1",wires:[["3"]]},388 {id:"3",type:"test",wires:[]},389 {id:"sf1",type:"subflow"},390 {id:"sf1-1",z:"sf1",type:"test",wires:[]},391 {id:"sf1-2",z:"sf1",type:"test",wires:[]}392 ];393 var newConfig = clone(config);394 newConfig[4].wires = [["sf1-2"]];395 var originalConfig = flowUtil.parseConfig(config);396 var changedConfig = flowUtil.parseConfig(newConfig);397 originalConfig.missingTypes.should.have.length(0);398 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);399 diffResult.added.should.have.length(0);400 diffResult.changed.sort().should.eql(['2', 'sf1']);401 diffResult.removed.should.have.length(0);402 diffResult.rewired.should.have.length(0);403 diffResult.linked.sort().should.eql(["1","3"]);404 });405 it('marks a parent subflow as changed for an internal node add', function() {406 var config = [407 {id:"1",type:"test",wires:[["2"]]},408 {id:"2",type:"subflow:sf1",wires:[["3"]]},409 {id:"3",type:"test",wires:[]},410 {id:"sf1",type:"subflow"},411 {id:"sf1-1",z:"sf1",type:"test",wires:[]},412 {id:"sf1-2",z:"sf1",type:"test",wires:[]}413 ];414 var newConfig = clone(config);415 newConfig.push({id:"sf1-3",z:"sf1",type:"test",wires:[]});416 var originalConfig = flowUtil.parseConfig(config);417 var changedConfig = flowUtil.parseConfig(newConfig);418 originalConfig.missingTypes.should.have.length(0);419 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);420 diffResult.added.should.have.length(0);421 diffResult.changed.sort().should.eql(['2', 'sf1']);422 diffResult.removed.should.have.length(0);423 diffResult.rewired.should.have.length(0);424 diffResult.linked.sort().should.eql(["1","3"]);425 });426 it('marks a parent subflow as changed for an internal node delete', function() {427 var config = [428 {id:"1",type:"test",wires:[["2"]]},429 {id:"2",type:"subflow:sf1",wires:[["3"]]},430 {id:"3",type:"test",wires:[]},431 {id:"sf1",type:"subflow"},432 {id:"sf1-1",z:"sf1",type:"test",wires:[]},433 {id:"sf1-2",z:"sf1",type:"test",wires:[]}434 ];435 var newConfig = clone(config);436 newConfig.splice(5,1);437 var originalConfig = flowUtil.parseConfig(config);438 var changedConfig = flowUtil.parseConfig(newConfig);439 originalConfig.missingTypes.should.have.length(0);440 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);441 diffResult.added.should.have.length(0);442 diffResult.changed.sort().should.eql(['2', 'sf1']);443 diffResult.removed.should.have.length(1);444 diffResult.removed.sort().should.eql(['sf1-2']);445 diffResult.rewired.should.have.length(0);446 diffResult.linked.sort().should.eql(["1","3"]);447 });448 it('marks a parent subflow as changed for an internal subflow wiring change - input removed', function() {449 var config = [450 {id:"1",type:"test",wires:[["2"]]},451 {id:"2",type:"subflow:sf1",wires:[["3"]]},452 {id:"3",type:"test",wires:[]},453 {id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},454 {id:"sf1-1",z:"sf1",type:"test",wires:[]},455 {id:"sf1-2",z:"sf1",type:"test",wires:[]}456 ];457 var newConfig = clone(config);458 newConfig[3].in[0].wires = [];459 var originalConfig = flowUtil.parseConfig(config);460 var changedConfig = flowUtil.parseConfig(newConfig);461 originalConfig.missingTypes.should.have.length(0);462 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);463 diffResult.added.should.have.length(0);464 diffResult.changed.sort().should.eql(['2', 'sf1']);465 diffResult.removed.should.have.length(0);466 diffResult.rewired.should.have.length(0);467 diffResult.linked.sort().should.eql(["1","3"]);468 });469 it('marks a parent subflow as changed for an internal subflow wiring change - input added', function() {470 var config = [471 {id:"1",type:"test",wires:[["2"]]},472 {id:"2",type:"subflow:sf1",wires:[["3"]]},473 {id:"3",type:"test",wires:[]},474 {id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},475 {id:"sf1-1",z:"sf1",type:"test",wires:[]},476 {id:"sf1-2",z:"sf1",type:"test",wires:[]}477 ];478 var newConfig = clone(config);479 newConfig[3].in[0].wires.push({"id":"sf1-2"});480 var originalConfig = flowUtil.parseConfig(config);481 var changedConfig = flowUtil.parseConfig(newConfig);482 originalConfig.missingTypes.should.have.length(0);483 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);484 diffResult.added.should.have.length(0);485 diffResult.changed.sort().should.eql(['2', 'sf1']);486 diffResult.removed.should.have.length(0);487 diffResult.rewired.should.have.length(0);488 diffResult.linked.sort().should.eql(["1","3"]);489 });490 it('marks a parent subflow as changed for an internal subflow wiring change - output added', function() {491 var config = [492 {id:"1",type:"test",wires:[["2"]]},493 {id:"2",type:"subflow:sf1",wires:[["3"]]},494 {id:"3",type:"test",wires:[]},495 {id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},496 {id:"sf1-1",z:"sf1",type:"test",wires:[]},497 {id:"sf1-2",z:"sf1",type:"test",wires:[]}498 ];499 var newConfig = clone(config);500 newConfig[3].out[0].wires.push({"id":"sf1-2","port":0});501 var originalConfig = flowUtil.parseConfig(config);502 var changedConfig = flowUtil.parseConfig(newConfig);503 originalConfig.missingTypes.should.have.length(0);504 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);505 diffResult.added.should.have.length(0);506 diffResult.changed.sort().should.eql(['2', 'sf1']);507 diffResult.removed.should.have.length(0);508 diffResult.rewired.should.have.length(0);509 diffResult.linked.sort().should.eql(["1","3"]);510 });511 it('marks a parent subflow as changed for an internal subflow wiring change - output removed', function() {512 var config = [513 {id:"1",type:"test",wires:[["2"]]},514 {id:"2",type:"subflow:sf1",wires:[["3"]]},515 {id:"3",type:"test",wires:[]},516 {id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},517 {id:"sf1-1",z:"sf1",type:"test",wires:[]},518 {id:"sf1-2",z:"sf1",type:"test",wires:[]}519 ];520 var newConfig = clone(config);521 newConfig[3].out[0].wires = [];522 var originalConfig = flowUtil.parseConfig(config);523 var changedConfig = flowUtil.parseConfig(newConfig);524 originalConfig.missingTypes.should.have.length(0);525 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);526 diffResult.added.should.have.length(0);527 diffResult.changed.sort().should.eql(['2', 'sf1']);528 diffResult.removed.should.have.length(0);529 diffResult.rewired.should.have.length(0);530 diffResult.linked.sort().should.eql(["1","3"]);531 });532 it('marks a parent subflow as changed for a global config node change', function() {533 var config = [534 {id:"1",type:"test",wires:[["2"]]},535 {id:"2",type:"subflow:sf1",wires:[["3"]]},536 {id:"3",type:"test",wires:[]},537 {id:"sf1",type:"subflow"},538 {id:"sf1-1",z:"sf1",prop:"configNode",type:"test",wires:[]},539 {id:"sf1-2",z:"sf1",type:"test",wires:[]},540 {id:"configNode",a:"foo",type:"test",wires:[]}541 ];542 var newConfig = clone(config);543 newConfig[6].a = "bar";544 var originalConfig = flowUtil.parseConfig(config);545 var changedConfig = flowUtil.parseConfig(newConfig);546 originalConfig.missingTypes.should.have.length(0);547 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);548 diffResult.added.should.have.length(0);549 diffResult.changed.sort().should.eql(['2', "configNode", 'sf1']);550 diffResult.removed.should.have.length(0);551 diffResult.rewired.should.have.length(0);552 diffResult.linked.sort().should.eql(["1","3"]);553 });554 it('marks a parent subflow as changed for an internal subflow instance change', function() {555 var config = [556 {id:"1",type:"test",wires:[["2"]]},557 {id:"2",type:"subflow:sf1",wires:[["3"]]},558 {id:"3",type:"test",wires:[]},559 {id:"sf1",type:"subflow"},560 {id:"sf2",type:"subflow"},561 {id:"sf1-1",z:"sf1",type:"test",wires:[]},562 {id:"sf1-2",z:"sf1",type:"subflow:sf2",wires:[]},563 {id:"sf2-1",z:"sf2",type:"test",wires:[]},564 {id:"sf2-2",z:"sf2",type:"test",wires:[]},565 ];566 var newConfig = clone(config);567 newConfig[8].a = "bar";568 var originalConfig = flowUtil.parseConfig(config);569 var changedConfig = flowUtil.parseConfig(newConfig);570 originalConfig.missingTypes.should.have.length(0);571 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);572 diffResult.added.should.have.length(0);573 diffResult.changed.sort().should.eql(['2', 'sf1', 'sf2']);574 diffResult.removed.should.have.length(0);575 diffResult.rewired.should.have.length(0);576 diffResult.linked.sort().should.eql(["1","3"]);577 });578 it('ignores tab changes that are immaterial', function() {579 var config = [{id:"1",type:"tab",label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"}];580 var newConfig = clone(config);581 newConfig[0].label = "barney";582 var originalConfig = flowUtil.parseConfig(config);583 var changedConfig = flowUtil.parseConfig(newConfig);584 originalConfig.missingTypes.should.have.length(0);585 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);586 diffResult.added.should.have.length(0);587 diffResult.changed.should.have.length(0);588 diffResult.removed.should.have.length(0);589 diffResult.rewired.should.have.length(0);590 });591 it('marks a deleted tab as removed', function() {592 var config = [{id:"f1",type:"tab",label:"fred"},{id:"n1",type:"test",bar:"b",wires:[["1"]],z:"f1"},593 {id:"f2",type:"tab",label:"fred"},{id:"n2",type:"test",bar:"b",wires:[["1"]],z:"f2"}];594 var newConfig = clone(config);595 newConfig = newConfig.slice(0,2);596 var originalConfig = flowUtil.parseConfig(config);597 var changedConfig = flowUtil.parseConfig(newConfig);598 originalConfig.missingTypes.should.have.length(0);599 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);600 diffResult.added.should.have.length(0);601 diffResult.changed.should.have.length(0);602 diffResult.removed.sort().should.eql(['f2', 'n2']);603 diffResult.rewired.should.have.length(0);604 });605 it('marks all nodes as added when tab state changes disabled to enabled', function() {606 var config = [{id:"1",type:"tab",disabled:true,label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"},{id:"3",type:"test"}];607 var newConfig = clone(config);608 newConfig[0].disabled = false;609 var originalConfig = flowUtil.parseConfig(config);610 var changedConfig = flowUtil.parseConfig(newConfig);611 originalConfig.missingTypes.should.have.length(0);612 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);613 diffResult.added.should.have.length(2);614 diffResult.added.sort().should.eql(["1","2"]);615 diffResult.changed.should.have.length(0);616 diffResult.removed.should.have.length(0);617 diffResult.rewired.should.have.length(0);618 });619 it('marks all nodes as removed when tab state changes enabled to disabled', function() {620 var config = [{id:"1",type:"tab",disabled:false,label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"},{id:"3",type:"test"}];621 var newConfig = clone(config);622 newConfig[0].disabled = true;623 var originalConfig = flowUtil.parseConfig(config);624 var changedConfig = flowUtil.parseConfig(newConfig);625 originalConfig.missingTypes.should.have.length(0);626 var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);627 diffResult.added.should.have.length(0);628 diffResult.changed.should.have.length(0);629 diffResult.removed.should.have.length(2);630 diffResult.removed.sort().should.eql(["1","2"]);631 diffResult.rewired.should.have.length(0);632 });633 });...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1'use strict';2const {isEmpty} = require('lodash');3const parseConfig = require('lib/config');4const {config: configDefaults} = require('lib/constants/defaults');5const viewModes = require('lib/constants/view-modes');6const saveFormats = require('lib/constants/save-formats');7const SUPPORTED_CONTROL_TYPES = Object.values(require('lib/gui/constants/custom-gui-control-types'));8const {logger} = require('lib/common-utils');9describe('config', () => {10 const sandbox = sinon.createSandbox();11 beforeEach(function() {12 sandbox.stub(logger, 'warn');13 this.oldArgv = process.argv;14 });15 afterEach(function() {16 sandbox.restore();17 process.argv = this.oldArgv;18 delete process.env['html_reporter_enabled'];19 delete process.env['html_reporter_path'];20 delete process.env['html_reporter_default_view'];21 delete process.env['html_reporter_base_host'];22 delete process.env['html_reporter_scale_images'];23 delete process.env['html_reporter_lazy_load_offset'];24 delete process.env['html_reporter_meta_info_base_urls'];25 delete process.env['html_reporter_plugins_enabled'];26 });27 describe('"enabled" option', () => {28 it('should be true by default', () => {29 assert.isTrue(parseConfig({}).enabled);30 });31 it('should set from configuration file', () => {32 const config = parseConfig({enabled: false});33 assert.isFalse(config.enabled);34 });35 it('should set from environment variable', () => {36 process.env['html_reporter_enabled'] = 'false';37 assert.isFalse(parseConfig({}).enabled);38 });39 it('should set from cli', () => {40 process.argv = process.argv.concat('--html-reporter-enabled', 'false');41 assert.isFalse(parseConfig({}).enabled);42 });43 });44 describe('"path" option', () => {45 it('should be "html-report" by default', () => {46 assert.equal(parseConfig({}).path, 'html-report');47 });48 it('should set from configuration file', () => {49 const config = parseConfig({path: 'some/report/path'});50 assert.equal(config.path, 'some/report/path');51 });52 it('should set from environment variable', () => {53 process.env['html_reporter_path'] = 'env/report/path';54 assert.equal(parseConfig({}).path, 'env/report/path');55 });56 it('should set from cli', () => {57 process.argv = process.argv.concat('--html-reporter-path', 'cli/report/path');58 assert.equal(parseConfig({}).path, 'cli/report/path');59 });60 });61 describe('"saveFormat" option', () => {62 it(`should be ${saveFormats.SQLITE} by default`, () => {63 assert.equal(parseConfig({}).saveFormat, saveFormats.SQLITE);64 });65 });66 describe('"saveErrorDetails" option', () => {67 it('should be false by default', () => {68 assert.isFalse(parseConfig({}).saveErrorDetails);69 });70 it('should set from configuration file', () => {71 const config = parseConfig({saveErrorDetails: true});72 assert.isTrue(config.saveErrorDetails);73 });74 it('should set from environment variable', () => {75 process.env['html_reporter_save_error_details'] = 'true';76 assert.isTrue(parseConfig({}).saveErrorDetails);77 });78 it('should set from cli', () => {79 process.argv = process.argv.concat('--html-reporter-save-error-details', 'true');80 assert.isTrue(parseConfig({}).saveErrorDetails);81 });82 });83 describe('"commandsWithShortHistory" option', () => {84 it('should be empty array by default', () => {85 assert.deepEqual(parseConfig({}).commandsWithShortHistory, []);86 });87 it('should set from configuration file', () => {88 const config = parseConfig({commandsWithShortHistory: ['foo']});89 assert.deepEqual(config.commandsWithShortHistory, ['foo']);90 });91 describe('should throw an error if value', () => {92 it('is not an array', () => {93 assert.throws(94 () => parseConfig({commandsWithShortHistory: 100500}),95 '"commandsWithShortHistory" option must be an array, but got number'96 );97 });98 it('is not an array of strings', () => {99 assert.throws(100 () => parseConfig({commandsWithShortHistory: [100500]}),101 '"commandsWithShortHistory" option must be an array of strings but got number for one of items'102 );103 });104 });105 });106 describe('"defaultView" option', () => {107 it('should show all suites by default', () => {108 assert.equal(parseConfig({}).defaultView, viewModes.ALL);109 });110 it('should set from configuration file', () => {111 const config = parseConfig({defaultView: 'some-view'});112 assert.equal(config.defaultView, 'some-view');113 });114 it('should set from environment variable', () => {115 process.env['html_reporter_default_view'] = 'env/some-view';116 assert.equal(parseConfig({}).defaultView, 'env/some-view');117 });118 it('should set from cli', () => {119 process.argv = process.argv.concat('--html-reporter-default-view', 'cli/some-view');120 assert.equal(parseConfig({}).defaultView, 'cli/some-view');121 });122 });123 describe('"baseHost" option', () => {124 it('should be empty by default', () => {125 assert.equal(parseConfig({}).baseHost, '');126 });127 it('should set from configuration file', () => {128 const config = parseConfig({baseHost: 'some-host'});129 assert.equal(config.baseHost, 'some-host');130 });131 it('should set from environment variable', () => {132 process.env['html_reporter_base_host'] = 'env/some-host';133 assert.equal(parseConfig({}).baseHost, 'env/some-host');134 });135 it('should set from cli', () => {136 process.argv = process.argv.concat('--html-reporter-base-host', 'cli/some-host');137 assert.equal(parseConfig({}).baseHost, 'cli/some-host');138 });139 });140 describe('"scaleImages" option', () => {141 it('should be false by default', () => {142 assert.isFalse(parseConfig({}).scaleImages);143 });144 it('should set from configuration file', () => {145 const config = parseConfig({scaleImages: true});146 assert.isTrue(config.scaleImages);147 });148 it('should set from environment variable', () => {149 process.env['html_reporter_scale_images'] = 'true';150 assert.isTrue(parseConfig({}).scaleImages);151 });152 it('should set from cli', () => {153 process.argv = process.argv.concat('--html-reporter-scale-images', 'true');154 assert.isTrue(parseConfig({}).scaleImages);155 });156 });157 describe('"lazyLoadOffset" option', () => {158 it('should has default value', () => {159 assert.equal(parseConfig({}).lazyLoadOffset, configDefaults.lazyLoadOffset);160 });161 it('should warn about deprecation', () => {162 parseConfig({lazyLoadOffset: 700});163 assert.calledOnceWith(logger.warn, sinon.match('Warning: field "lazyLoadOffset" is deprecated'));164 });165 });166 describe('"errorPatterns", option', () => {167 describe('should throw an error if value', () => {168 it('is not an array', () => {169 assert.throws(170 () => parseConfig({errorPatterns: 100500}),171 '"errorPatterns" option must be array, but got number'172 );173 });174 it('is not a string or object', () => {175 assert.throws(176 () => parseConfig({errorPatterns: [100500]}),177 'Element of "errorPatterns" option must be plain object or string, but got number'178 );179 });180 it('is object but does not have "name" field', () => {181 assert.throws(182 () => parseConfig({errorPatterns: [{pattern: 'some-pattern'}]}),183 'Field "name" in element of "errorPatterns" option must be string, but got undefined'184 );185 });186 it('is object but does not have "pattern" field', () => {187 assert.throws(188 () => parseConfig({errorPatterns: [{name: 'some-err'}]}),189 'Field "pattern" in element of "errorPatterns" option must be string, but got undefined'190 );191 });192 });193 it('should have default value', () => {194 assert.deepEqual(parseConfig({}).errorPatterns, configDefaults.errorPatterns);195 });196 it('should modify string to object', () => {197 const config = parseConfig({errorPatterns: ['some-err']});198 assert.deepEqual(config.errorPatterns[0], {name: 'some-err', pattern: 'some-err'});199 });200 it('should set object', () => {201 const config = parseConfig({errorPatterns: [{name: 'some-err', pattern: 'some-pattern'}]});202 assert.deepEqual(203 config.errorPatterns[0],204 {name: 'some-err', pattern: 'some-pattern'}205 );206 });207 });208 describe('"metaInfoBaseUrls" option', () => {209 it('should set from configuration file', () => {210 const config = parseConfig({211 metaInfoBaseUrls: {212 file: 'base/path'213 }214 });215 assert.deepEqual(config.metaInfoBaseUrls, {file: 'base/path'});216 });217 it('should be set from environment variable', () => {218 process.env['html_reporter_meta_info_base_urls'] = '{"file": "base/path"}';219 assert.deepEqual(parseConfig({}).metaInfoBaseUrls, {file: 'base/path'});220 });221 it('should be set from cli', () => {222 process.argv = process.argv.concat('--html-reporter-meta-info-base-urls', '{"file":"base/path"}');223 assert.deepEqual(parseConfig({}).metaInfoBaseUrls, {file: 'base/path'});224 });225 it('should validate if passed value is string', () => {226 assert.throws(() => parseConfig({metaInfoBaseUrls: 'some/urls'}), /option must be object, but got string/);227 });228 it('should validate if passed to object value is number', () => {229 assert.throws(() => parseConfig({metaInfoBaseUrls: {file: 10}}), /option must be string, but got number/);230 });231 });232 describe('customGui', () => {233 const mkCustomGuiConfig = (sectionGroupValue) => parseConfig({234 customGui: {'section': [sectionGroupValue]}235 });236 it('should be empty by default', () => {237 assert.isTrue(isEmpty(parseConfig({}).customGui));238 });239 it('should be set from configuration file', () => {240 const initialize = () => {};241 const action = () => {};242 const config = mkCustomGuiConfig({243 type: 'button',244 controls: [{label: 'foo', value: 'bar'}],245 initialize,246 action247 });248 assert.deepEqual(config.customGui, {249 'section': [{250 type: 'button',251 controls: [{label: 'foo', value: 'bar'}],252 initialize,253 action254 }]255 });256 });257 it('should validate it is plain object', () => assert.throws(258 () => parseConfig({customGui: 'some-gui'}),259 /"customGui" option must be plain object, but got string/260 ));261 describe('section should validate it is', () => {262 it('array', () => assert.throws(263 () => parseConfig({customGui: {'section': 'foo'}}),264 /customGui\["section"\] must be an array, but got string/265 ));266 it('array of plain objects', () => assert.throws(267 () => parseConfig({customGui: {'section': ['foo']}}),268 /customGui\["section"\]\[0\] must be plain object, but got string/269 ));270 });271 describe('section-array object should validate it contains', () => {272 it('field "type"', () => assert.throws(273 () => mkCustomGuiConfig({}), /must contain field "type"/274 ));275 it('string in the field "type"', () => assert.throws(276 () => mkCustomGuiConfig({type: 100500}), /must contain string in the field "type"/277 ));278 it('only supported control in the field "type"', () => assert.throws(279 () => mkCustomGuiConfig({type: 'foo'}),280 new RegExp(`can contain in the field "type" only ${SUPPORTED_CONTROL_TYPES.join(', ')}`)281 ));282 it('field "controls"', () => assert.throws(283 () => mkCustomGuiConfig({type: 'button'}), /must contain field "controls"/284 ));285 it('array in the field "controls"', () => assert.throws(286 () => mkCustomGuiConfig({type: 'button', controls: 'foo'}),287 /must contain array in the field "controls"/288 ));289 it('non-empty array in the field "controls"', () => assert.throws(290 () => mkCustomGuiConfig({type: 'button', controls: []}),291 /must contain non-empty array in the field "controls"/292 ));293 it('array of plain objects in the field "controls"', () => assert.throws(294 () => mkCustomGuiConfig({type: 'button', controls: ['foo', 'bar']}),295 /must contain objects in the array "controls"/296 ));297 });298 });299 describe('customScripts', () => {300 it('should have default value', () => {301 assert.deepEqual(parseConfig({}).customScripts, configDefaults.customScripts);302 });303 it('should validate for Array type', () => {304 assert.throws(305 () => parseConfig({customScripts: 'foo'}),306 /"customScripts" option must be an array, but got string/307 );308 });309 it('should validate for Array items', () => {310 assert.throws(311 () => parseConfig({customScripts: ['foo']}),312 /"customScripts" option must be an array of functions but got string for one of items/313 );314 });315 it('should not throw with correct values', () => {316 const scripts = [function() {}];317 const config = parseConfig({customScripts: scripts});318 assert.deepEqual(config.customScripts, scripts);319 });320 });321 describe('yandexMetrika', () => {322 it('should have default value', () => {323 assert.deepEqual(parseConfig({}).yandexMetrika, configDefaults.yandexMetrika);324 });325 describe('counterNumber', () => {326 it('should throw error if option is not a null or number', () => {327 assert.throws(() => {328 parseConfig({329 yandexMetrika: {330 counterNumber: 'string'331 }332 }),333 Error,334 /option must be number, but got string/;335 });336 });337 it('should set value from config file', () => {338 const config = parseConfig({339 yandexMetrika: {340 counterNumber: 100500341 }342 });343 assert.equal(config.yandexMetrika.counterNumber, 100500);344 });345 });346 });347 describe('"pluginsEnabled" option', () => {348 it('should be false by default', () => {349 assert.isFalse(parseConfig({}).pluginsEnabled);350 });351 it('should set from configuration file', () => {352 const config = parseConfig({pluginsEnabled: true});353 assert.isTrue(config.pluginsEnabled);354 });355 it('should set from environment variable', () => {356 process.env['html_reporter_plugins_enabled'] = 'true';357 assert.isTrue(parseConfig({}).pluginsEnabled);358 });359 it('should set from cli', () => {360 process.argv = process.argv.concat('--html-reporter-plugins-enabled', 'true');361 assert.isTrue(parseConfig({}).pluginsEnabled);362 });363 });364 describe('plugins', () => {365 it('should have default value', () => {366 assert.deepEqual(parseConfig({}).plugins, configDefaults.plugins);367 assert.deepEqual(parseConfig({}).plugins, []);368 });369 it('should validate for Array type', () => {370 assert.throws(371 () => parseConfig({plugins: 'foo'}),372 /"plugins" option must be an array, but got string/373 );374 });375 it('should validate for Array items', () => {376 assert.throws(377 () => parseConfig({plugins: ['foo']}),378 /plugin description expected to be an object but got string/379 );380 });381 it('should validate for Array item required fields', () => {382 assert.throws(383 () => parseConfig({plugins: [{name: 'test-plugin'}]}),384 /"plugins.component" option must be non-empty string but got undefined/385 );386 });387 it('should validate for Array item point field when present', () => {388 assert.throws(389 () => parseConfig({plugins: [{name: 'test-plugin', component: 'TestComponent', point: 10}]}),390 /"plugins.point" option must be string but got number/391 );392 });393 it('should validate for Array item point field when present', () => {394 assert.throws(395 () => parseConfig({plugins: [396 {name: 'test-plugin', component: 'TestComponent', point: 'result', position: 'red'}397 ]}),398 /"plugins.position" option got an unexpected value "red"/399 );400 });401 it('should validate for Array item unexpected fields', () => {402 assert.throws(403 () => parseConfig({plugins: [404 {name: 'test-plugin', component: 'TestComponent', unexpected: 'field'}405 ]}),406 /a "plugins" item has unexpected field "unexpected" of type string/407 );408 });409 it('should not throw with correct values', () => {410 const plugins = [411 {412 name: 'test-plugin',413 component: 'TestComponent',414 point: 'result',415 position: 'wrap'416 }417 ];418 const config = parseConfig({plugins: plugins});419 assert.deepEqual(config.plugins, plugins);420 });421 it('should set from environment variable', () => {422 const plugins = [423 {424 name: 'test-plugin',425 component: 'TestComponent',426 point: 'result',427 position: 'wrap'428 }429 ];430 process.env['html_reporter_plugins'] = JSON.stringify(plugins);431 assert.deepEqual(parseConfig({}).plugins, plugins);432 });433 it('should set from cli', () => {434 const plugins = [435 {436 name: 'test-plugin',437 component: 'TestComponent',438 point: 'result',439 position: 'wrap'440 }441 ];442 process.argv = process.argv.concat('--html-reporter-plugins', JSON.stringify(plugins));443 assert.deepEqual(parseConfig({}).plugins, plugins);444 });445 });...

Full Screen

Full Screen

parseConfig.js

Source:parseConfig.js Github

copy

Full Screen

1'use strict';2describe('utils/parseConfig()', function () {3 const { parseConfig } = require('../../../lib/utils');4 it('string', function () {5 const schema = { type: 'string' }6 expect(parseConfig(schema, "This is a string")).toBe("This is a string")7 expect(parseConfig(schema, '')).toBe('')8 expect(parseConfig(schema, 1)).toBeUndefined()9 });10 it('string/length', function () {11 const schema = { type: 'string', minLength: 1, maxLength: 3 }12 expect(parseConfig(schema, '')).toBeUndefined()13 expect(parseConfig(schema, '1')).toBe('1')14 expect(parseConfig(schema, '1234')).toBeUndefined()15 })16 it('string/pattern', function () {17 const schema = { type: 'string', pattern: "^(\\([0-9]{3}\\))?[0-9]{3}-[0-9]{4}$" }18 expect(parseConfig(schema, '555-1212')).toBe('555-1212')19 expect(parseConfig(schema, '(888)555-1212')).toBe('(888)555-1212')20 expect(parseConfig(schema, '(800)FLOWERS')).toBeUndefined()21 })22 it('string/format', function () {23 // support date, email, uri, uri-reference, regex24 const date = { type: 'string', format: 'date' }25 const email = { type: 'string', format: 'email' }26 expect(parseConfig(date, '2018-11-13')).toBe('2018-11-13')27 expect(parseConfig(email, 'a@a.a')).toBe('a@a.a')28 expect(parseConfig(date, '2018-11-13-')).toBeUndefined()29 expect(parseConfig(email, 'a@a.a.')).toBeUndefined()30 })31 it('number', function () {32 const schema = { type: 'number' }33 expect(parseConfig(schema, 0)).toBe(0)34 expect(parseConfig(schema, '0')).toBeUndefined()35 });36 it('number/range', function () {37 const schema = { type: 'number', minimum: 1, maximum: 4 }38 expect(parseConfig(schema, 0)).toBeUndefined()39 expect(parseConfig(schema, 1)).toBe(1)40 expect(parseConfig(schema, 5)).toBeUndefined()41 });42 it('boolean', function () {43 const schema = { type: 'boolean' }44 expect(parseConfig(schema, 0)).toBeUndefined()45 expect(parseConfig(schema, false)).toBe(false)46 expect(parseConfig(schema, true)).toBe(true)47 });48 it('enum', function () {49 expect(parseConfig({ enum: [1, 3, 5] }, 0)).toBeUndefined()50 expect(parseConfig({ enum: [1, 3, false] }, false)).toBe(false)51 expect(parseConfig({ enum: ['1', 3, '5'] }, 3)).toBe(3)52 expect(parseConfig({ enum: ['1', '3', '5'] }, '3')).toBe('3')53 });54 it('array', function () {55 const schema = { type: 'array' }56 expect(parseConfig(schema, [1, 2, '3'])).toEqual([1, 2, '3'])57 expect(parseConfig(schema, { "Not": "an array" })).toBeUndefined()58 });59 it('array/contains', function () {60 const schema = { type: 'array', contains: { type: 'string' } }61 expect(parseConfig(schema, [1, 2, 'contains a string'])).toEqual([1, 2, 'contains a string'])62 expect(parseConfig(schema, [1, 2, 3])).toBeUndefined()63 });64 it('array/items', function () {65 const schema = { type: 'array', items: { type: 'string' } }66 expect(parseConfig(schema, [1, 2, 3])).toBeUndefined()67 expect(parseConfig(schema, ['1', '2', '3'])).toEqual(['1', '2', '3'])68 });69 it('array/tuple', function () {70 const schema = { type: 'array', items: [{ type: 'number' }, { type: 'string' }], additionalItems: { type: 'boolean' } }71 expect(parseConfig(schema, [1, 2, 3])).toBeUndefined()72 expect(parseConfig(schema, [1, '2', '3'])).toBeUndefined()73 expect(parseConfig(schema, [1, '2', false])).toEqual([1, '2', false])74 schema.additionalItems = false75 expect(parseConfig(schema, [1, '2', false])).toBeUndefined()76 delete schema.additionalItems77 expect(parseConfig(schema, [1, '2', false])).toEqual([1, '2', false])78 });79 it('array/length', function () {80 const schema = { type: 'array', minItems: 2, maxItems: 2 }81 expect(parseConfig(schema, [1, 2, 3])).toBeUndefined()82 expect(parseConfig(schema, [1, 2])).toEqual([1, 2])83 });84 it('array/uniqueItems', function () {85 const schema = { type: 'array', uniqueItems: true }86 expect(parseConfig(schema, [1, 2, 1])).toBeUndefined()87 expect(parseConfig(schema, [1, { two: 2 }, { two: 2 }])).toBeUndefined()88 expect(parseConfig(schema, [1, { two: 2 }, { three: 3 }])).toEqual([1, { two: 2 }, { three: 3 }])89 });90 it('array/objectItems', function () {91 const schema = { type: 'array', items: { type: 'object', properties: { name: 'string', age: 'number' }, required: ['name', 'age'] } }92 expect(parseConfig(schema, [1, 2, 1])).toBeUndefined()93 expect(parseConfig(schema, [{ name: '1' }, { age: 2 }])).toBeUndefined()94 expect(parseConfig(schema, [{ name: '1', age: 2 }, { name: '1', age: 2 }])).toEqual([{ name: '1', age: 2 }, { name: '1', age: 2 }])95 });96 it('object', function () {97 const schema = { type: 'object' }98 expect(parseConfig(schema, {})).toEqual({})99 expect(parseConfig(schema, { a: 1 })).toEqual({ a: 1 })100 expect(parseConfig(schema, ["An", "array", "not", "an", "object"])).toBe(undefined);101 });102 it('object/properties', function () {103 const schema = {104 type: 'object',105 "properties": {106 "number": { "type": "number" },107 "string": { "type": "string" },108 "enum": {109 "type": "string",110 "enum": ["a", "b", "c"]111 }112 }113 }114 expect(parseConfig(schema, { "number": 1, "string": "string", "enum": "a" })).toEqual({ "number": 1, "string": "string", "enum": "a" })115 // a standard implementation should fail116 expect(parseConfig(schema, { "number": "1", "string": "string", "enum": "d" })).toEqual({ "string": "string" })117 expect(parseConfig(schema, {})).toEqual({})118 });119 it('object/additionalProperties', function () {120 const schema = {121 type: 'object',122 "properties": {123 "number": { "type": "number" }124 },125 additionalProperties: false126 }127 expect(parseConfig(schema, { "number": '1', "string": "string" })).toBe(undefined);128 expect(parseConfig(schema, { "number": 1, "string": "string" })).toEqual({ "number": 1 })129 schema.additionalProperties = { "type": "string" };130 expect(parseConfig(schema, { "number": 1, "string": "string" })).toEqual({ "number": 1, "string": "string" })131 expect(parseConfig(schema, { "number": 1, "string": false })).toEqual({ "number": 1 })132 // a standard implementation should fail133 // expect(parseConfig(schema, { "number": "1", "string": "string", "enum": "d" })).toEqual({ "string": "string" })134 // expect(parseConfig(schema, {})).toEqual({})135 });136 it('object/size', function () {137 const schema = {138 "type": "object",139 "minProperties": 2,140 "maxProperties": 3141 }142 expect(parseConfig(schema, { "a": 0 })).toBe(undefined);143 expect(parseConfig(schema, { "a": 0, "b": 1 })).toEqual({ "a": 0, "b": 1 })144 expect(parseConfig(schema, { "a": 0, "b": 1, "c": 2, "d": 3 })).toBe(undefined);145 });146 it('object/patternProperties', function () {147 let schema = {148 "type": "object",149 "patternProperties": {150 "^S_": { "type": "string" },151 "^I_": { "type": "number" }152 },153 "additionalProperties": false154 }155 expect(parseConfig(schema, { "S_25": "This is a string" })).toEqual({ "S_25": "This is a string" })156 expect(parseConfig(schema, { "I_0": 42 })).toEqual({ "I_0": 42 })157 expect(parseConfig(schema, { "S_0": 42 })).toBe(undefined);158 expect(parseConfig(schema, { "I_42": "This is a string" })).toBe(undefined);159 schema = Object.assign(schema, {160 properties: {161 builtin: { "type": "number" },162 },163 additionalProperties: { "type": "string" }164 })165 expect(parseConfig(schema, { "builtin": 42 })).toEqual({ "builtin": 42 })166 expect(parseConfig(schema, { "keyword": "value" })).toEqual({ "keyword": "value" })167 expect(parseConfig(schema, { "keyword": 42 })).toBe(undefined);168 });169 it('object/required', function () {170 const schema = {171 "type": "object",172 "properties": {173 "name": { "type": "string" },174 "email": { "type": "string" },175 "address": { "type": "string" },176 "telephone": { "type": "string" }177 },178 "required": ["name", "email"]179 };180 expect(parseConfig(schema, {181 "name": "William Shakespeare",182 "email": "bill@stratford-upon-avon.co.uk"183 }))184 .toEqual({185 "name": "William Shakespeare",186 "email": "bill@stratford-upon-avon.co.uk"187 });188 expect(parseConfig(schema, {189 "name": "William Shakespeare",190 "email": "bill@stratford-upon-avon.co.uk",191 "address": "Henley Street, Stratford-upon-Avon, Warwickshire, England",192 "authorship": "in question"193 }))194 .toEqual({195 "name": "William Shakespeare",196 "email": "bill@stratford-upon-avon.co.uk",197 "address": "Henley Street, Stratford-upon-Avon, Warwickshire, England",198 "authorship": "in question"199 });200 expect(parseConfig(schema, {201 "name": "William Shakespeare",202 "address": "Henley Street, Stratford-upon-Avon, Warwickshire, England",203 }))204 .toBe(undefined);205 // fallback to default206 schema.properties.email.default = 'a@a.a';207 expect(parseConfig(schema, {208 "name": "William Shakespeare",209 "address": "Henley Street, Stratford-upon-Avon, Warwickshire, England",210 }))211 .toEqual({212 "name": "William Shakespeare",213 "email": "a@a.a",214 "address": "Henley Street, Stratford-upon-Avon, Warwickshire, England",215 });216 })217 it('object/dependencies', function () {218 const schema = {219 "type": "object",220 "properties": {221 "name": { "type": "string" },222 "credit_card": { "type": "number" },223 "billing_address": { "type": "string" }224 },225 "required": ["name"],226 "dependencies": {227 "credit_card": ["billing_address"]228 }229 };230 // Property dependencies231 expect(parseConfig(schema, {232 "name": "John Doe",233 "credit_card": 5555555555555555,234 "billing_address": "555 Debtor's Lane"235 }))236 .toEqual({237 "name": "John Doe",238 "credit_card": 5555555555555555,239 "billing_address": "555 Debtor's Lane"240 });241 expect(parseConfig(schema, {242 "name": "John Doe",243 "credit_card": 5555555555555555244 }))245 .toBe(undefined);246 expect(parseConfig(schema, {247 "name": "John Doe"248 }))249 .toEqual({250 "name": "John Doe"251 });252 expect(parseConfig(schema, {253 "name": "John Doe",254 "billing_address": "555 Debtor's Lane"255 }))256 .toEqual({257 "name": "John Doe",258 "billing_address": "555 Debtor's Lane"259 });260 // bidirectional261 schema.dependencies = {262 "credit_card": ["billing_address"],263 "billing_address": ["credit_card"]264 };265 expect(parseConfig(schema, {266 "name": "John Doe",267 "credit_card": 5555555555555555268 }))269 .toBe(undefined);270 expect(parseConfig(schema, {271 "name": "John Doe",272 "billing_address": "555 Debtor's Lane"273 }))274 .toBe(undefined);275 // Schema dependencies276 schema.dependencies = {277 "credit_card": {278 "properties": {279 "billing_address": { "type": "string" }280 },281 "required": ["billing_address"]282 }283 };284 expect(parseConfig(schema, {285 "name": "John Doe",286 "credit_card": 5555555555555555,287 "billing_address": "555 Debtor's Lane"288 }))289 .toEqual({290 "name": "John Doe",291 "credit_card": 5555555555555555,292 "billing_address": "555 Debtor's Lane"293 });294 expect(parseConfig(schema, {295 "name": "John Doe",296 "credit_card": 5555555555555555297 }))298 .toBe(undefined);299 expect(parseConfig(schema, {300 "name": "John Doe",301 "billing_address": "555 Debtor's Lane"302 }))303 .toEqual({304 "name": "John Doe",305 "billing_address": "555 Debtor's Lane"306 });307 });308 it('definitions', function () {309 const schema = {310 definitions: {311 age: { type: 'number' },312 gender: { $id: '#gender', enum: ['male', 'female', 'other'] }313 },314 type: 'object',315 properties: {316 age: { $ref: '#/definitions/age' },317 gender: { $ref: '#gender' },318 class: {319 definitions: {320 color: { type: 'string' }321 },322 type: 'object',323 properties: {324 color: { $ref: '#/properties/class/definitions/color' }325 }326 }327 }328 };329 expect(parseConfig(schema, {330 age: 1,331 gender: 'other',332 class: { color: 0 }333 }))334 .toEqual({335 age: 1,336 gender: 'other'337 });338 expect(parseConfig(schema, {339 age: 1,340 gender: 'other',341 class: { color: 'this is a string' }342 }))343 .toEqual({344 age: 1,345 gender: 'other',346 class: { color: 'this is a string' }347 });348 });349 it('anyOf', function () {350 const schema = {351 "anyOf": [352 { "type": "string", "maxLength": 5 },353 { "type": "number", "minimum": 0 }354 ]355 };356 expect(parseConfig(schema, "short")).toBe("short")357 expect(parseConfig(schema, "too long")).toBeUndefined()358 expect(parseConfig(schema, 12)).toBe(12)359 expect(parseConfig(schema, -5)).toBeUndefined()360 });361 it('allOf', function () {362 let schema = {363 "allOf": [364 { "type": "string" },365 { "type": "string", "maxLength": 5 }366 ]367 };368 expect(parseConfig(schema, "short")).toBe("short")369 expect(parseConfig(schema, "too long")).toBeUndefined()370 schema = {371 "definitions": {372 "address": {373 "type": "object",374 "properties": {375 "street_address": { "type": "string" },376 "city": { "type": "string" },377 "state": { "type": "string" }378 },379 "required": ["street_address", "city", "state"]380 }381 },382 "allOf": [383 { "$ref": "#/definitions/address" },384 {385 "properties": {386 "type": { "enum": ["residential", "business"] }387 }388 }389 ]390 };391 expect(parseConfig(schema, {392 "street_address": "1600 Pennsylvania Avenue NW",393 "city": "Washington",394 "state": "DC",395 "type": "business"396 }))397 .toEqual({398 "street_address": "1600 Pennsylvania Avenue NW",399 "city": "Washington",400 "state": "DC",401 "type": "business"402 });403 schema.additionalProperties = false404 expect(parseConfig(schema, {405 "street_address": "1600 Pennsylvania Avenue NW",406 "city": "Washington",407 "state": "DC",408 "type": "business"409 })).toBe(undefined)410 });...

Full Screen

Full Screen

parse.test.js

Source:parse.test.js Github

copy

Full Screen

1import path from 'path'2import makeParseConfig from '../parse'3import normalizeConfig from '../normalize'4import validateConfig from '../validation'5const parseConfig = makeParseConfig(normalizeConfig, validateConfig)6describe('given that the user did not provide any custom configuration', () => {7 const userConfig = {}8 it('should be able to return the default configuration', () => {9 expect(parseConfig(userConfig)).toEqual({10 paths: {11 components: path.resolve(process.cwd(), 'components'),12 containers: path.resolve(process.cwd(), 'containers'),13 templates: '',14 },15 extensions: {16 js: {17 main: 'js',18 index: 'js',19 tests: 'test.js',20 },21 stylesheet: {22 main: 'css',23 },24 },25 rules: {26 'component-name-root-dir': true,27 'es6class-container-component': true,28 },29 formatting: {30 prettier: true,31 },32 })33 })34})35describe('given that the user provided a custom relative components path', () => {36 it('should be able to return a configuration object that includes a custom components path as an absolute path', () => {37 const userConfig = {38 paths: {39 components: 'src/components',40 },41 }42 expect(parseConfig(userConfig)).toHaveProperty(43 'paths.components',44 path.resolve(process.cwd(), 'src', 'components')45 )46 expect(parseConfig(userConfig)).toHaveProperty(47 'paths.containers',48 path.resolve(process.cwd(), 'containers')49 )50 })51 it('should be able to handle wrongly formatted paths', () => {52 const userConfig = {53 paths: {54 components: ' ./src/components ',55 },56 }57 expect(parseConfig(userConfig)).toHaveProperty(58 'paths.components',59 path.resolve(process.cwd(), 'src', 'components')60 )61 })62})63describe('given that the user provided an absolute components path', () => {64 it('should be able to return a configuration object that includes a custom components path', () => {65 const userConfig = {66 paths: {67 components: path.resolve(__dirname, 'src', 'components'),68 },69 }70 expect(parseConfig(userConfig)).toHaveProperty(71 'paths.components',72 path.resolve(__dirname, 'src', 'components')73 )74 expect(parseConfig(userConfig)).toHaveProperty(75 'paths.containers',76 path.resolve(process.cwd(), 'containers')77 )78 })79})80describe('given that the user provided a custom relative containers path', () => {81 it('should be able to return a configuration object that includes a custom containers absolute path', () => {82 const userConfig = {83 paths: {84 containers: '../src/containers',85 },86 }87 expect(parseConfig(userConfig)).toHaveProperty(88 'paths.containers',89 path.resolve(process.cwd(), '..', 'src', 'containers')90 )91 })92})93describe('given that the user provided a custom JS extension', () => {94 it('should be able to return a configuration object includes a custom JS main extension', () => {95 const userConfig = {96 extensions: {97 js: {98 main: 'jsx',99 },100 },101 }102 expect(parseConfig(userConfig)).toHaveProperty('extensions.js', {103 main: 'jsx',104 index: 'js',105 tests: 'test.js',106 })107 })108 it('should be able to return a configuration object that includes a custom JS index extension', () => {109 const userConfig = {110 extensions: {111 js: {112 index: 'ts',113 },114 },115 }116 expect(parseConfig(userConfig)).toHaveProperty('extensions.js', {117 main: 'js',118 index: 'ts',119 tests: 'test.js',120 })121 })122 it('should be able to return a configuration object that includes a custom JS index extension', () => {123 const userConfig = {124 extensions: {125 js: {126 index: 'ts',127 },128 },129 }130 expect(parseConfig(userConfig)).toHaveProperty('extensions.js', {131 main: 'js',132 index: 'ts',133 tests: 'test.js',134 })135 })136 it('should be able to normalize extensions with extra spaces', () => {137 const userConfig = {138 extensions: {139 js: {140 index: 'ts ',141 },142 },143 }144 expect(parseConfig(userConfig)).toHaveProperty('extensions.js.index', 'ts')145 })146 it('should be able to normalize extensions with extra initial dots', () => {147 const userConfig = {148 extensions: {149 js: {150 main: '.tsx',151 index: '.ts',152 tests: '.js',153 },154 },155 }156 expect(parseConfig(userConfig)).toHaveProperty('extensions.js', {157 main: 'tsx',158 index: 'ts',159 tests: 'js',160 })161 })162 it('should be able to distinguish initial dots from latter ones', () => {163 const userConfig = {164 extensions: {165 js: {166 main: '.tsx',167 index: '.ts',168 tests: 'spec.js',169 },170 },171 }172 expect(parseConfig(userConfig)).toHaveProperty('extensions.js', {173 main: 'tsx',174 index: 'ts',175 tests: 'spec.js',176 })177 })178})179describe('given that the user provided a custom stylesheet extension', () => {180 it('should be able to return a configuration object that includes a custom extension for main stylesheet', () => {181 const userConfig = {182 extensions: {183 stylesheet: {184 main: 'scss',185 },186 },187 }188 expect(parseConfig(userConfig)).toHaveProperty(189 'extensions.stylesheet.main',190 'scss'191 )192 })193})194describe('given that the user provided a custom templates path', () => {195 it('should be able to return a configuration object that includes a custom path for templates', () => {196 const userConfig = {197 paths: {198 templates: './src/templates',199 },200 }201 expect(parseConfig(userConfig)).toHaveProperty(202 'paths.templates',203 path.resolve(process.cwd(), 'src', 'templates')204 )205 })206})207describe('given that the user provided an invalid config argument', () => {208 it('should throw a TypeError', () => {209 expect.assertions(1)210 const userConfig = []211 expect(() => {212 parseConfig(userConfig)213 }).toThrowError(TypeError)214 })...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run tox automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful