@iobroker/legacy-testing 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,12 +5,18 @@ Legacy test modules for ioBroker
5
5
 
6
6
  ## How to replace existing tests
7
7
  1. add to package.json=>devDependencies: `"@iobroker/legacy-testing": "^0.0.4"`
8
- 2. Remove from package.json=>devDependencies: `chai` and `mocha`
8
+ 2. Remove from package.json=>devDependencies: `chai`
9
9
  3. Replace in `tests/testAdapter.js` the code `const setup = require('./lib/setup');` with `const setup = require('@iobroker/legacy-testing');`
10
10
  4. Replace whole file `tests/testPackageFiles.js` with `require('@iobroker/legacy-testing/tests/testPackageFiles');`
11
11
 
12
12
  ## Changelog
13
13
  <!-- ### **WORK IN PROGRESS** -->
14
+ ### 0.2.0 (2023-05-07)
15
+ * (bluefox) Added possibility to install additional adapters at start
16
+
17
+ ### 0.1.1 (2022-12-22)
18
+ * (bluefox) Extended testPackageFiles.js with checks
19
+
14
20
  ### 0.1.0 (2022-12-22)
15
21
  * (bluefox) Corrected testPackageFiles.js
16
22
 
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.1.0",
2
+ "version": "0.2.0",
3
3
  "name": "@iobroker/legacy-testing",
4
4
  "main": "setup.js",
5
5
  "files": [
@@ -24,7 +24,7 @@
24
24
  "email": "dogafox@gmail.com"
25
25
  },
26
26
  "dependencies": {
27
- "mocha": "^10.1.0",
27
+ "mocha": "^10.2.0",
28
28
  "chai": "^4.3.7"
29
29
  },
30
30
  "devDependencies": {
package/setup.js CHANGED
@@ -48,11 +48,10 @@ let pid = null;
48
48
  let systemConfig = null;
49
49
 
50
50
  function copyFileSync(source, target) {
51
-
52
51
  let targetFile = target;
53
52
 
54
53
  //if target is a directory a new file with the same name will be created
55
- if (fs.existsSync(target) && fs.lstatSync( target ).isDirectory() ) {
54
+ if (fs.existsSync(target) && fs.lstatSync(target).isDirectory() ) {
56
55
  targetFile = path.join(target, path.basename(source));
57
56
  }
58
57
 
@@ -101,12 +100,11 @@ function copyFolderRecursiveSync(source, target, ignore) {
101
100
 
102
101
  !fs.existsSync(`${rootDir}tmp`) && fs.mkdirSync(`${rootDir}tmp`);
103
102
 
104
-
105
103
  async function storeOriginalFiles() {
106
104
  console.log('Store original files...');
107
105
  const dataDir = `${rootDir}tmp/${appName}-data/`;
108
106
 
109
- if (fs.existsSync(dataDir + 'objects.json')) {
107
+ if (fs.existsSync(`${dataDir}objects.json`)) {
110
108
  const f = fs.readFileSync(`${dataDir}objects.json`);
111
109
  const objects = JSON.parse(f.toString());
112
110
  if (objects['system.adapter.admin.0'] && objects['system.adapter.admin.0'].common) {
@@ -152,7 +150,7 @@ async function storeOriginalFiles() {
152
150
  }
153
151
  await db.close();
154
152
 
155
- const f = fs.readFileSync(dataDir + 'objects.jsonl');
153
+ const f = fs.readFileSync(`${dataDir}objects.jsonl`);
156
154
  fs.writeFileSync(`${dataDir}objects.jsonl.original`, f);
157
155
  console.log('Store original objects.jsonl');
158
156
  }
@@ -187,8 +185,8 @@ function restoreOriginalFiles() {
187
185
  }
188
186
  }
189
187
 
190
- async function checkIsAdapterInstalled(cb, counter, customName) {
191
- customName = customName || pkg.name.split('.').pop();
188
+ async function checkIsAdapterInstalled(cb, counter, customAdapterName, customInstance) {
189
+ customAdapterName = customAdapterName || pkg.name.split('.').pop();
192
190
  counter = counter || 0;
193
191
  const dataDir = `${rootDir}tmp/${appName}-data/`;
194
192
  console.log('checkIsAdapterInstalled...');
@@ -197,11 +195,9 @@ async function checkIsAdapterInstalled(cb, counter, customName) {
197
195
  if (fs.existsSync(`${dataDir}objects.json`)) {
198
196
  const f = fs.readFileSync(`${dataDir}objects.json`);
199
197
  const objects = JSON.parse(f.toString());
200
- if (objects[`system.adapter.${customName}.0`]) {
198
+ if (objects[`system.adapter.${customAdapterName}.${customInstance || 0}`]) {
201
199
  console.log('checkIsAdapterInstalled: ready!');
202
- setTimeout(function () {
203
- cb && cb();
204
- }, 100);
200
+ setTimeout(() => cb && cb(), 100);
205
201
  return;
206
202
  } else {
207
203
  console.warn('checkIsAdapterInstalled: still not ready');
@@ -218,7 +214,7 @@ async function checkIsAdapterInstalled(cb, counter, customName) {
218
214
  throw err;
219
215
  }
220
216
 
221
- const obj = db.get(`system.adapter.${customName}.0`);
217
+ const obj = db.get(`system.adapter.${customAdapterName}.${customInstance || 0}`);
222
218
  await db.close();
223
219
 
224
220
  if (obj) {
@@ -241,9 +237,7 @@ async function checkIsAdapterInstalled(cb, counter, customName) {
241
237
  cb && cb('Cannot install');
242
238
  } else {
243
239
  console.log('checkIsAdapterInstalled: wait...');
244
- setTimeout(function() {
245
- checkIsAdapterInstalled(cb, counter + 1);
246
- }, 1000);
240
+ setTimeout(() => checkIsAdapterInstalled(cb, counter + 1, customAdapterName, customInstance), 1000);
247
241
  }
248
242
  }
249
243
 
@@ -253,8 +247,8 @@ async function checkIsControllerInstalled(cb, counter) {
253
247
 
254
248
  console.log('checkIsControllerInstalled...');
255
249
  try {
256
- if (fs.existsSync(dataDir + 'objects.json')) {
257
- const f = fs.readFileSync(dataDir + 'objects.json');
250
+ if (fs.existsSync(`${dataDir}objects.json`)) {
251
+ const f = fs.readFileSync(`${dataDir}objects.json`);
258
252
  const objects = JSON.parse(f.toString());
259
253
  if (objects['system.certificates']) {
260
254
  console.log('checkIsControllerInstalled: installed!');
@@ -298,30 +292,30 @@ async function checkIsControllerInstalled(cb, counter) {
298
292
  }
299
293
  }
300
294
 
301
- function installAdapter(customName, cb) {
302
- if (typeof customName === 'function') {
303
- cb = customName;
304
- customName = null;
295
+ function installAdapter(customAdapterName, cb) {
296
+ if (typeof customAdapterName === 'function') {
297
+ cb = customAdapterName;
298
+ customAdapterName = null;
305
299
  }
306
- customName = customName || pkg.name.split('.').pop();
300
+ customAdapterName = customAdapterName || pkg.name.split('.').pop();
307
301
  console.log('Install adapter...');
308
302
  const startFile = `node_modules/${appName}.js-controller/${appName}.js`;
309
303
  // make first install
310
304
  if (debug) {
311
- child_process.execSync(`node ${startFile} add ${customName} --enabled false`, {
312
- cwd: rootDir + 'tmp',
313
- stdio: [0, 1, 2]
305
+ child_process.execSync(`node ${startFile} add ${customAdapterName} --enabled false`, {
306
+ cwd: `${rootDir}tmp`,
307
+ stdio: [0, 1, 2],
314
308
  });
315
309
  checkIsAdapterInstalled(error => {
316
310
  error && console.error(error);
317
311
  console.log('Adapter installed.');
318
312
  cb && cb();
319
- });
313
+ }, 0, customAdapterName);
320
314
  } else {
321
315
  // add controller
322
- const _pid = child_process.fork(startFile, ['add', customName, '--enabled', 'false'], {
316
+ const _pid = child_process.fork(startFile, ['add', customAdapterName, '--enabled', 'false'], {
323
317
  cwd: `${rootDir}tmp`,
324
- stdio: [0, 1, 2, 'ipc']
318
+ stdio: [0, 1, 2, 'ipc'],
325
319
  });
326
320
 
327
321
  waitForEnd(_pid, () =>
@@ -329,30 +323,40 @@ function installAdapter(customName, cb) {
329
323
  error && console.error(error);
330
324
  console.log('Adapter installed.');
331
325
  cb && cb();
332
- }));
326
+ }, 0, customAdapterName));
333
327
  }
334
328
  }
335
329
 
330
+ function installAdapterAsync(customAdapterName) {
331
+ return new Promise(resolve =>
332
+ installAdapter(customAdapterName, () => resolve()));
333
+ }
334
+
336
335
  function waitForEnd(_pid, cb) {
337
336
  if (!_pid) {
338
337
  cb(-1, -1);
339
- return;
338
+ } else {
339
+ _pid.on('exit', (code, signal) => {
340
+ if (_pid) {
341
+ _pid = null;
342
+ cb(code, signal);
343
+ }
344
+ });
345
+ _pid.on('close', (code, signal) => {
346
+ if (_pid) {
347
+ _pid = null;
348
+ cb(code, signal);
349
+ }
350
+ });
340
351
  }
341
- _pid.on('exit', (code, signal) => {
342
- if (_pid) {
343
- _pid = null;
344
- cb(code, signal);
345
- }
346
- });
347
- _pid.on('close', (code, signal) => {
348
- if (_pid) {
349
- _pid = null;
350
- cb(code, signal);
351
- }
352
- });
353
352
  }
354
353
 
355
- function installJsController(cb) {
354
+ function installJsController(preInstalledAdapters, cb) {
355
+ if (typeof preInstalledAdapters === 'function') {
356
+ cb = preInstalledAdapters;
357
+ preInstalledAdapters = null;
358
+ }
359
+
356
360
  console.log('installJsController...');
357
361
  if (!fs.existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`) ||
358
362
  !fs.existsSync(`${rootDir}tmp/${appName}-data`)) {
@@ -361,19 +365,19 @@ function installJsController(cb) {
361
365
  if (fs.existsSync(`${rootDir}node_modules/${appName}.js-controller`)) {
362
366
  console.log(`installJsController: no js-controller => copy it from "${rootDir}node_modules/${appName}.js-controller"`);
363
367
  // copy all
364
- // stop controller
368
+ // stop the controller
365
369
  console.log('Stop controller if running...');
366
370
  let _pid;
367
371
  if (debug) {
368
372
  // start controller
369
373
  _pid = child_process.exec(`node ${appName}.js stop`, {
370
374
  cwd: `${rootDir}node_modules/${appName}.js-controller`,
371
- stdio: [0, 1, 2]
375
+ stdio: [0, 1, 2],
372
376
  });
373
377
  } else {
374
378
  _pid = child_process.fork(`${appName}.js`, ['stop'], {
375
379
  cwd: `${rootDir}node_modules/${appName}.js-controller`,
376
- stdio: [0, 1, 2, 'ipc']
380
+ stdio: [0, 1, 2, 'ipc'],
377
381
  });
378
382
  }
379
383
 
@@ -402,8 +406,8 @@ function installJsController(cb) {
402
406
  });
403
407
  }
404
408
  waitForEnd(__pid, () =>
405
- checkIsControllerInstalled(() => {
406
- // change ports for object and state DBs
409
+ checkIsControllerInstalled(async () => {
410
+ // change ports for the object and state DBs
407
411
  const config = require(`${rootDir}tmp/${appName}-data/${appName}.json`);
408
412
  config.objects.port = 19001;
409
413
  config.states.port = 19000;
@@ -415,11 +419,15 @@ function installJsController(cb) {
415
419
  console.log('Setup finished.');
416
420
 
417
421
  copyAdapterToController();
422
+ if (preInstalledAdapters) {
423
+ for (let p = 0; p < preInstalledAdapters.length; p++) {
424
+ await installAdapterAsync(preInstalledAdapters[p]);
425
+ }
426
+ }
418
427
 
419
- installAdapter(async () => {
420
- await storeOriginalFiles();
421
- cb && cb(true);
422
- });
428
+ await installAdapterAsync();
429
+ await storeOriginalFiles();
430
+ cb && cb(true);
423
431
  }));
424
432
  });
425
433
  } else {
@@ -427,7 +435,7 @@ function installJsController(cb) {
427
435
  const client = new require('net').Socket();
428
436
  client.on('error', () => {});
429
437
  client.connect(9000, '127.0.0.1', () => {
430
- console.error('Cannot initiate fisrt run of test, because one instance of application is running on this PC. Stop it and repeat.');
438
+ console.error('Cannot initiate the first run of test, because one instance of application is running on this PC. Stop it and repeat.');
431
439
  process.exit(0);
432
440
  });
433
441
 
@@ -437,8 +445,8 @@ function installJsController(cb) {
437
445
  console.log('installJsController: no js-controller => install dev build from npm');
438
446
 
439
447
  child_process.execSync(`npm install ${appName}.js-controller@dev --prefix ./ --production`, {
440
- cwd: rootDir + 'tmp/',
441
- stdio: [0, 1, 2]
448
+ cwd: `${rootDir}tmp/`,
449
+ stdio: [0, 1, 2],
442
450
  });
443
451
  } else {
444
452
  console.log('Setup js-controller...');
@@ -446,12 +454,12 @@ function installJsController(cb) {
446
454
  // start controller
447
455
  child_process.exec(`node ${appName}.js setup first`, {
448
456
  cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
449
- stdio: [0, 1, 2]
457
+ stdio: [0, 1, 2],
450
458
  });
451
459
  } else {
452
460
  child_process.fork(`${appName}.js`, ['setup', 'first'], {
453
461
  cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
454
- stdio: [0, 1, 2, 'ipc']
462
+ stdio: [0, 1, 2, 'ipc'],
455
463
  });
456
464
  }
457
465
  }
@@ -467,8 +475,8 @@ function installJsController(cb) {
467
475
  });
468
476
  }
469
477
 
470
- waitForEnd(_pid, () => {
471
- // change ports for object and state DBs
478
+ waitForEnd(_pid, async () => {
479
+ // change ports for the object and state DBs
472
480
  const config = require(`${rootDir}tmp/${appName}-data/${appName}.json`);
473
481
  config.objects.port = 19001;
474
482
  config.states.port = 19000;
@@ -479,11 +487,15 @@ function installJsController(cb) {
479
487
  fs.writeFileSync(`${rootDir}tmp/${appName}-data/${appName}.json`, JSON.stringify(config, null, 2));
480
488
 
481
489
  copyAdapterToController();
490
+ if (preInstalledAdapters) {
491
+ for (let p = 0; p < preInstalledAdapters.length; p++) {
492
+ await installAdapterAsync(preInstalledAdapters[p]);
493
+ }
494
+ }
482
495
 
483
- installAdapter(async () => {
484
- await storeOriginalFiles();
485
- cb && cb(true);
486
- });
496
+ await installAdapterAsync();
497
+ await storeOriginalFiles();
498
+ cb && cb(true);
487
499
  });
488
500
  });
489
501
  }, 1000);
@@ -499,12 +511,12 @@ function installJsController(cb) {
499
511
  function copyAdapterToController() {
500
512
  console.log('Copy adapter...');
501
513
  // Copy adapter to tmp/node_modules/appName.adapter
502
- copyFolderRecursiveSync(rootDir, `${rootDir}tmp/node_modules/`, ['.idea', 'test', 'tmp', '.git', appName + '.js-controller']);
514
+ copyFolderRecursiveSync(rootDir, `${rootDir}tmp/node_modules/`, ['.github', '.idea', 'test', 'tmp', '.git', 'src', 'src-widgets', `${appName}.js-controller`]);
503
515
  console.log('Adapter copied.');
504
516
  }
505
517
 
506
518
  function clearControllerLog() {
507
- const dirPath = rootDir + 'tmp/log';
519
+ const dirPath = `${rootDir}tmp/log`;
508
520
  let files;
509
521
  try {
510
522
  if (fs.existsSync(dirPath)) {
@@ -562,12 +574,12 @@ function clearDB() {
562
574
  }
563
575
 
564
576
  function setupController(cb) {
565
- installJsController(async isInited => {
577
+ installJsController(async isInitialized => {
566
578
  try {
567
579
  clearControllerLog();
568
580
  clearDB();
569
581
 
570
- if (!isInited) {
582
+ if (!isInitialized) {
571
583
  restoreOriginalFiles();
572
584
  copyAdapterToController();
573
585
  }
@@ -667,13 +679,13 @@ function startAdapter(objects, states, callback) {
667
679
  if (debug) {
668
680
  // start controller
669
681
  pid = child_process.exec(`node node_modules/${pkg.name}/${pkg.main} --console silly`, {
670
- cwd: rootDir + 'tmp',
682
+ cwd: `${rootDir}tmp`,
671
683
  stdio: [0, 1, 2]
672
684
  });
673
685
  } else {
674
686
  // start controller
675
687
  pid = child_process.fork(`node_modules/${pkg.name}/${pkg.main}`, ['--console', 'silly'], {
676
- cwd: rootDir + 'tmp',
688
+ cwd: `${rootDir}tmp`,
677
689
  stdio: [0, 1, 2, 'ipc']
678
690
  });
679
691
  }
@@ -712,7 +724,7 @@ function startController(isStartAdapter, onObjectChange, onStateChange, callback
712
724
 
713
725
  // rootDir + 'tmp/node_modules
714
726
  const objPath = require.resolve(`@iobroker/db-objects-${config.objects.type}`, {
715
- paths: [ rootDir + 'tmp/node_modules', rootDir, `${rootDir}tmp/node_modules/${appName}.js-controller`]
727
+ paths: [`${rootDir}tmp/node_modules`, rootDir, `${rootDir}tmp/node_modules/${appName}.js-controller`]
716
728
  });
717
729
  console.log(`Objects Path: ${objPath}`);
718
730
  const Objects = require(objPath).Server;
@@ -880,9 +892,9 @@ function stopController(cb) {
880
892
  }, 5000);
881
893
  }
882
894
 
883
- // Setup the adapter
884
- async function setAdapterConfig(common, native, instance) {
885
- const id = `system.adapter.${adapterName.split('.').pop()}.${instance || 0}`;
895
+ // Set up the adapter
896
+ async function setAdapterConfig(common, native, instance, customAdapterName, customInstance) {
897
+ const id = `system.adapter.${(adapterName || customAdapterName).split('.').pop()}.${customInstance === undefined ? instance || 0 : customInstance}`;
886
898
  if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.json`)) {
887
899
  const objects = JSON.parse(fs.readFileSync(`${rootDir}tmp/${appName}-data/objects.json`).toString());
888
900
  if (common) {
@@ -913,8 +925,8 @@ async function setAdapterConfig(common, native, instance) {
913
925
  }
914
926
 
915
927
  // Read config of the adapter
916
- async function getAdapterConfig(instance) {
917
- const id = `system.adapter.${adapterName.split('.').pop()}.${instance || 0}`;
928
+ async function getAdapterConfig(instance, customAdapterName, customInstance) {
929
+ const id = `system.adapter.${(adapterName || customAdapterName).split('.').pop()}.${customInstance === undefined ? instance || 0 : customInstance}`;
918
930
  if (fs.existsSync(`${rootDir}tmp/${appName}-data/objects.json`)) {
919
931
  const objects = JSON.parse(fs.readFileSync(`${rootDir}tmp/${appName}-data/objects.json`).toString());
920
932
  return objects[id];
@@ -73,6 +73,15 @@ describe('Test package.json and io-package.json', () => {
73
73
  if (ioPackage.common.materialize || (ioPackage.common.adminUI && ioPackage.common.adminUI.conifg === 'materialize')) {
74
74
  expect(fs.existsSync(`${adapterDir}/admin/index_m.html`), 'Admin3 support is enabled in io-package.json, but index_m.html is missing!').to.be.true;
75
75
  }
76
+ if (ioPackage.common.jsonConfig || (ioPackage.common.adminUI && ioPackage.common.adminUI.conifg === 'json')) {
77
+ expect(fs.existsSync(`${adapterDir}/admin/jsonConfig.json`) || fs.existsSync(`${adapterDir}/admin/jsonConfig.json5`), 'Admin3 support is enabled in io-package.json, but jsonConfig.json(5) is missing!').to.be.true;
78
+ }
79
+ if (ioPackage.common.adminUI && ioPackage.common.adminUI.custom === 'json') {
80
+ expect(fs.existsSync(`${adapterDir}/admin/jsonCustom.json`) || fs.existsSync(`${adapterDir}/admin/jsonCustom.json5`), 'Custom config support is enabled in io-package.json, but jsonCustom.json(5) is missing!').to.be.true;
81
+ }
82
+ if (ioPackage.common.adminUI && ioPackage.common.adminUI.tab === 'html') {
83
+ expect(fs.existsSync(`${adapterDir}/admin/tab.html`) || fs.existsSync(`${adapterDir}/admin/tab_m.html`), 'HTML-Tab support is enabled in io-package.json, but tab(_m).html is missing!').to.be.true;
84
+ }
76
85
  }
77
86
 
78
87
  const licenseFileExists = fs.existsSync(`${adapterDir}/LICENSE`);