@iobroker/legacy-testing 0.2.6 → 0.3.1

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.
Files changed (4) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +2 -2
  3. package/package.json +1 -1
  4. package/setup.js +176 -169
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 bluefox <dogafox@gmail.com>
3
+ Copyright (c) 2022-2023 bluefox <dogafox@gmail.com>
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -11,7 +11,7 @@ Legacy test modules for ioBroker
11
11
 
12
12
  ## Changelog
13
13
  <!-- ### **WORK IN PROGRESS** -->
14
- ### 0.2.6 (2023-05-07)
14
+ ### 0.3.1 (2023-05-08)
15
15
  * (bluefox) Added possibility to install additional adapters at start
16
16
  * (bluefox) Added setOfflineState/getOfflineState
17
17
 
@@ -30,7 +30,7 @@ Legacy test modules for ioBroker
30
30
  ## License
31
31
  MIT License
32
32
 
33
- Copyright (c) 2022 bluefox <dogafox@gmail.com>
33
+ Copyright (c) 2022-2023 bluefox <dogafox@gmail.com>
34
34
 
35
35
  Permission is hereby granted, free of charge, to any person obtaining a copy
36
36
  of this software and associated documentation files (the "Software"), to deal
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.6",
2
+ "version": "0.3.1",
3
3
  "name": "@iobroker/legacy-testing",
4
4
  "main": "setup.js",
5
5
  "files": [
package/setup.js CHANGED
@@ -5,10 +5,9 @@
5
5
  // check if tmp directory exists
6
6
  const fs = require('fs');
7
7
  const path = require('path');
8
- const child_process = require('child_process');
8
+ const cp = require('child_process');
9
9
  const rootDir = path.normalize(`${__dirname}/../../../`);
10
10
  const pkg = require(`${rootDir}package.json`);
11
- const cp = require('child_process');
12
11
  const debug = typeof v8debug === 'object';
13
12
  pkg.main = pkg.main || 'main.js';
14
13
 
@@ -174,31 +173,29 @@ function restoreOriginalFiles() {
174
173
  fs.writeFileSync(`${dataDir}states.json`, f);
175
174
  }
176
175
 
177
- if (fs.existsSync(dataDir + 'objects.jsonl.original')) {
178
- const f = fs.readFileSync(dataDir + 'objects.jsonl.original');
179
- fs.writeFileSync(dataDir + 'objects.jsonl', f);
176
+ if (fs.existsSync(`${dataDir}objects.jsonl.original`)) {
177
+ const f = fs.readFileSync(`${dataDir}objects.jsonl.original`);
178
+ fs.writeFileSync(`${dataDir}objects.jsonl`, f);
180
179
  }
181
- if (fs.existsSync(dataDir + 'objects.jsonl.original')) {
182
- const f = fs.readFileSync(dataDir + 'states.jsonl.original');
183
- fs.writeFileSync(dataDir + 'states.jsonl', f);
180
+ if (fs.existsSync(`${dataDir}objects.jsonl.original`)) {
181
+ const f = fs.readFileSync(`${dataDir}states.jsonl.original`);
182
+ fs.writeFileSync(`${dataDir}states.jsonl`, f);
184
183
  }
185
184
  }
186
185
 
187
186
  async function checkIsAdapterInstalled(cb, counter, customAdapterName, customInstance) {
188
187
  customAdapterName = customAdapterName || pkg.name.split('.').pop();
189
188
  counter = counter || 0;
190
- const dataDir = `${rootDir}tmp/${appName}-data/`;
191
189
  console.log(`[${customAdapterName}] checkIsAdapterInstalled...`);
192
190
 
193
191
  try {
194
- const obj = await getObject(`system.adapter.${customAdapterName}.${customInstance || 0}`)
195
- if (obj) {
196
- console.log(`[${customAdapterName}] checkIsAdapterInstalled: ready!`);
192
+ const obj = await getObject(`system.adapter.${customAdapterName}.${customInstance || 0}`);
193
+ if (obj && obj.common) {
194
+ console.log(`[${customAdapterName}] checkIsAdapterInstalled: ready! ${JSON.stringify(obj)}`);
197
195
  setTimeout(() => cb && cb(), 100);
198
196
  return;
199
- } else {
200
- console.warn(`[${customAdapterName}] checkIsAdapterInstalled: still not ready`);
201
197
  }
198
+ console.warn(`[${customAdapterName}] checkIsAdapterInstalled: still not ready`);
202
199
  } catch (err) {
203
200
  console.log(`[${customAdapterName}] checkIsAdapterInstalled: catch ${err}`);
204
201
  }
@@ -212,14 +209,25 @@ async function checkIsAdapterInstalled(cb, counter, customAdapterName, customIns
212
209
  }
213
210
  }
214
211
 
212
+ function checkIsAdapterInstalledAsync(counter, customAdapterName, customInstance) {
213
+ return new Promise((resolve, reject) => {
214
+ checkIsAdapterInstalled(err => {
215
+ if (err) {
216
+ reject(err);
217
+ } else {
218
+ resolve();
219
+ }
220
+ }, counter, customAdapterName, customInstance);
221
+ });
222
+ }
223
+
215
224
  async function checkIsControllerInstalled(cb, counter) {
216
225
  counter = counter || 0;
217
- const dataDir = `${rootDir}tmp/${appName}-data/`;
218
226
 
219
227
  console.log('checkIsControllerInstalled...');
220
228
  try {
221
229
  const obj = await getObject('system.certificates');
222
- if (obj) {
230
+ if (obj && obj.common) {
223
231
  console.log('checkIsControllerInstalled: installed!');
224
232
  setTimeout(() => cb && cb(), 100);
225
233
  return;
@@ -237,12 +245,24 @@ async function checkIsControllerInstalled(cb, counter) {
237
245
  }
238
246
  }
239
247
 
240
- function installAdapter(customAdapterName, cb) {
248
+ function checkIsControllerInstalledAsync(counter) {
249
+ return new Promise((resolve, reject) => {
250
+ checkIsControllerInstalled(err => {
251
+ if (err) {
252
+ reject(err);
253
+ } else {
254
+ resolve();
255
+ }
256
+ }, counter);
257
+ });
258
+ }
259
+
260
+ async function installAdapter(customAdapterName, cb) {
241
261
  if (typeof customAdapterName === 'function') {
242
262
  cb = customAdapterName;
243
263
  customAdapterName = null;
244
264
  }
245
- customAdapterName = customAdapterName || pkg.name.split('.').pop();
265
+ customAdapterName = customAdapterName || pkg.name;
246
266
  console.log(`[${customAdapterName}] Install adapter...`);
247
267
 
248
268
  if (customAdapterName.includes('@')) {
@@ -252,29 +272,48 @@ function installAdapter(customAdapterName, cb) {
252
272
  const startFile = `node_modules/${appName}.js-controller/${appName}.js`;
253
273
  // make first install
254
274
  if (debug) {
255
- child_process.execSync(`node ${startFile} add ${customAdapterName} --enabled false`, {
275
+ cp.execSync(`node ${startFile} add ${customAdapterName} --enabled false`, {
256
276
  cwd: `${rootDir}tmp`,
257
277
  stdio: [0, 1, 2],
258
278
  });
259
- checkIsAdapterInstalled(error => {
260
- error && console.error(error);
261
- console.log(`[${customAdapterName}] Adapter installed.`);
262
- cb && cb();
263
- }, 0, customAdapterName);
264
279
  } else {
265
280
  // add controller
266
- const _pid = child_process.fork(startFile, ['add', customAdapterName, '--enabled', 'false'], {
281
+ const _pid = cp.fork(startFile, ['add', customAdapterName, '--enabled', 'false'], {
267
282
  cwd: `${rootDir}tmp`,
268
283
  stdio: [0, 1, 2, 'ipc'],
269
284
  });
270
285
 
271
- waitForEnd(_pid, () =>
272
- checkIsAdapterInstalled(error => {
273
- error && console.error(error);
274
- console.log(`[${customAdapterName}] Adapter installed.`);
275
- cb && cb();
276
- }, 0, customAdapterName));
286
+ await waitForEndAsync(_pid);
287
+ }
288
+
289
+ const name = (customAdapterName.split('@')[0]).split('.').pop(); // extract from iobroker.adaptername@version => adaptername
290
+
291
+ try {
292
+ await checkIsAdapterInstalledAsync(null, name);
293
+ } catch (err) {
294
+ console.warn(`[${customAdapterName}] Adapter not installed: ${err}`);
295
+ // try workaround
296
+ installCustomAdapter(customAdapterName);
297
+ const startFile = `node_modules/${appName}.js-controller/${appName}.js`;
298
+ // make first install
299
+ if (debug) {
300
+ cp.execSync(`node ${startFile} add ${customAdapterName} --enabled false`, {
301
+ cwd: `${rootDir}tmp`,
302
+ stdio: [0, 1, 2],
303
+ });
304
+ } else {
305
+ // add controller
306
+ const _pid = cp.fork(startFile, ['add', customAdapterName, '--enabled', 'false'], {
307
+ cwd: `${rootDir}tmp`,
308
+ stdio: [0, 1, 2, 'ipc'],
309
+ });
310
+
311
+ await waitForEndAsync(_pid);
312
+ }
313
+ await checkIsAdapterInstalledAsync(null, name);
277
314
  }
315
+ console.log(`[${customAdapterName}] Adapter installed.`);
316
+ cb && cb();
278
317
  }
279
318
 
280
319
  function installAdapterAsync(customAdapterName) {
@@ -301,7 +340,28 @@ function waitForEnd(_pid, cb) {
301
340
  }
302
341
  }
303
342
 
304
- function installJsController(preInstalledAdapters, cb) {
343
+ function waitForEndAsync(_pid) {
344
+ if (!_pid) {
345
+ return {code: -1, signal: -1};
346
+ } else {
347
+ return new Promise(resolve => {
348
+ _pid.on('exit', (code, signal) => {
349
+ if (_pid) {
350
+ _pid = null;
351
+ resolve({code, signal});
352
+ }
353
+ });
354
+ _pid.on('close', (code, signal) => {
355
+ if (_pid) {
356
+ _pid = null;
357
+ resolve({code, signal});
358
+ }
359
+ });
360
+ });
361
+ }
362
+ }
363
+
364
+ async function installJsController(preInstalledAdapters, cb) {
305
365
  if (typeof preInstalledAdapters === 'function') {
306
366
  cb = preInstalledAdapters;
307
367
  preInstalledAdapters = null;
@@ -320,137 +380,114 @@ function installJsController(preInstalledAdapters, cb) {
320
380
  let _pid;
321
381
  if (debug) {
322
382
  // start controller
323
- _pid = child_process.exec(`node ${appName}.js stop`, {
383
+ _pid = cp.exec(`node ${appName}.js stop`, {
324
384
  cwd: `${rootDir}node_modules/${appName}.js-controller`,
325
385
  stdio: [0, 1, 2],
326
386
  });
327
387
  } else {
328
- _pid = child_process.fork(`${appName}.js`, ['stop'], {
388
+ _pid = cp.fork(`${appName}.js`, ['stop'], {
329
389
  cwd: `${rootDir}node_modules/${appName}.js-controller`,
330
390
  stdio: [0, 1, 2, 'ipc'],
331
391
  });
332
392
  }
333
393
 
334
- waitForEnd(_pid, () => {
335
- // copy all files into
336
- !fs.existsSync(`${rootDir}tmp`) && fs.mkdirSync(`${rootDir}tmp`);
337
- !fs.existsSync(`${rootDir}tmp/node_modules`) && fs.mkdirSync(`${rootDir}tmp/node_modules`);
338
-
339
- if (!fs.existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`)){
340
- console.log('Copy js-controller...');
341
- copyFolderRecursiveSync(`${rootDir}node_modules/${appName}.js-controller`, `${rootDir}tmp/node_modules/`);
342
- }
394
+ await waitForEndAsync(_pid);
395
+ // copy all files into
396
+ !fs.existsSync(`${rootDir}tmp`) && fs.mkdirSync(`${rootDir}tmp`);
397
+ !fs.existsSync(`${rootDir}tmp/node_modules`) && fs.mkdirSync(`${rootDir}tmp/node_modules`);
343
398
 
344
- console.log('Setup js-controller...');
345
- let __pid;
346
- if (debug) {
347
- // start controller
348
- _pid = child_process.exec(`node ${appName}.js setup first --console`, {
349
- cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
350
- stdio: [0, 1, 2]
351
- });
352
- } else {
353
- __pid = child_process.fork(`${appName}.js`, ['setup', 'first', '--console'], {
354
- cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
355
- stdio: [0, 1, 2, 'ipc']
356
- });
357
- }
358
- waitForEnd(__pid, () =>
359
- checkIsControllerInstalled(async () => {
360
- // change ports for the object and state DBs
361
- const config = require(`${rootDir}tmp/${appName}-data/${appName}.json`);
362
- config.objects.port = 19001;
363
- config.states.port = 19000;
364
-
365
- // TEST WISE!
366
- //config.objects.type = 'jsonl';
367
- //config.states.type = 'jsonl';
368
- fs.writeFileSync(`${rootDir}tmp/${appName}-data/${appName}.json`, JSON.stringify(config, null, 2));
369
- console.log('Setup finished.');
370
-
371
- copyAdapterToController();
372
- if (preInstalledAdapters) {
373
- for (let p = 0; p < preInstalledAdapters.length; p++) {
374
- await installAdapterAsync(preInstalledAdapters[p]);
375
- }
376
- }
399
+ if (!fs.existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`)){
400
+ console.log('Copy js-controller...');
401
+ copyFolderRecursiveSync(`${rootDir}node_modules/${appName}.js-controller`, `${rootDir}tmp/node_modules/`);
402
+ }
377
403
 
378
- await installAdapterAsync();
379
- await storeOriginalFiles();
380
- cb && cb(true);
381
- }));
382
- });
404
+ console.log('Setup js-controller...');
405
+ let __pid;
406
+ if (debug) {
407
+ // start controller
408
+ __pid = cp.exec(`node ${appName}.js setup first --console`, {
409
+ cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
410
+ stdio: [0, 1, 2]
411
+ });
412
+ } else {
413
+ __pid = cp.fork(`${appName}.js`, ['setup', 'first', '--console'], {
414
+ cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
415
+ stdio: [0, 1, 2, 'ipc']
416
+ });
417
+ }
418
+ await waitForEnd(__pid);
419
+ await checkIsControllerInstalledAsync();
383
420
  } else {
384
421
  // check if port 9000 is free, else admin adapter will be added to running instance
385
- const { Socket } = require('net')
422
+ const {Socket} = require('net')
386
423
  const client = new Socket();
387
- client.on('error', () => {});
424
+ client.on('error', () => {
425
+ });
388
426
  client.connect(9000, '127.0.0.1', () => {
389
427
  console.error('Cannot initiate the first run of test, because one instance of application is running on this PC. Stop it and repeat.');
390
428
  process.exit(0);
391
429
  });
392
430
 
393
- setTimeout(() => {
394
- client.destroy();
395
- if (!fs.existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`)) {
396
- console.log('installJsController: no js-controller => install dev build from npm');
431
+ await new Promise(resolve => setTimeout(resolve, 1000));
432
+ client.destroy();
433
+
434
+ if (!fs.existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`)) {
435
+ console.log('installJsController: no js-controller => install dev build from npm');
397
436
 
398
- child_process.execSync(`npm install ${appName}.js-controller@dev --prefix ./ --production`, {
399
- cwd: `${rootDir}tmp/`,
437
+ cp.execSync(`npm install ${appName}.js-controller@dev --prefix ./ --production`, {
438
+ cwd: `${rootDir}tmp/`,
439
+ stdio: [0, 1, 2],
440
+ });
441
+ } else {
442
+ console.log('Setup js-controller...');
443
+ let __pid;
444
+ if (debug) {
445
+ // start controller
446
+ __pid = cp.exec(`node ${appName}.js setup first`, {
447
+ cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
400
448
  stdio: [0, 1, 2],
401
449
  });
402
450
  } else {
403
- console.log('Setup js-controller...');
404
- if (debug) {
405
- // start controller
406
- child_process.exec(`node ${appName}.js setup first`, {
407
- cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
408
- stdio: [0, 1, 2],
409
- });
410
- } else {
411
- child_process.fork(`${appName}.js`, ['setup', 'first'], {
412
- cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
413
- stdio: [0, 1, 2, 'ipc'],
414
- });
415
- }
451
+ __pid = cp.fork(`${appName}.js`, ['setup', 'first'], {
452
+ cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
453
+ stdio: [0, 1, 2, 'ipc'],
454
+ });
416
455
  }
456
+ await waitForEnd(__pid);
457
+ }
417
458
 
418
- // let npm install admin and run setup
419
- checkIsControllerInstalled(() => {
420
- let _pid;
459
+ // let npm install admin and run setup
460
+ await checkIsControllerInstalledAsync();
461
+ let _pid;
421
462
 
422
- if (fs.existsSync(`${rootDir}node_modules/${appName}.js-controller/${appName}.js`)) {
423
- _pid = child_process.fork(`${appName}.js`, ['stop'], {
424
- cwd: `${rootDir}node_modules/${appName}.js-controller`,
425
- stdio: [0, 1, 2, 'ipc']
426
- });
427
- }
463
+ if (fs.existsSync(`${rootDir}node_modules/${appName}.js-controller/${appName}.js`)) {
464
+ _pid = cp.fork(`${appName}.js`, ['stop'], {
465
+ cwd: `${rootDir}node_modules/${appName}.js-controller`,
466
+ stdio: [0, 1, 2, 'ipc']
467
+ });
468
+ }
428
469
 
429
- waitForEnd(_pid, async () => {
430
- // change ports for the object and state DBs
431
- const config = require(`${rootDir}tmp/${appName}-data/${appName}.json`);
432
- config.objects.port = 19001;
433
- config.states.port = 19000;
434
-
435
- // TEST WISE!
436
- //config.objects.type = 'jsonl';
437
- //config.states.type = 'jsonl';
438
- fs.writeFileSync(`${rootDir}tmp/${appName}-data/${appName}.json`, JSON.stringify(config, null, 2));
439
-
440
- copyAdapterToController();
441
- if (preInstalledAdapters) {
442
- for (let p = 0; p < preInstalledAdapters.length; p++) {
443
- await installAdapterAsync(preInstalledAdapters[p]);
444
- }
445
- }
470
+ await waitForEndAsync(_pid);
471
+ }
446
472
 
447
- await installAdapterAsync();
448
- await storeOriginalFiles();
449
- cb && cb(true);
450
- });
451
- });
452
- }, 1000);
473
+ // change ports for the object and state DBs
474
+ const config = require(`${rootDir}tmp/${appName}-data/${appName}.json`);
475
+ config.objects.port = 19001;
476
+ config.states.port = 19000;
477
+
478
+ fs.writeFileSync(`${rootDir}tmp/${appName}-data/${appName}.json`, JSON.stringify(config, null, 2));
479
+ console.log('Setup finished.');
480
+
481
+ copyAdapterToController();
482
+ if (preInstalledAdapters) {
483
+ for (let p = 0; p < preInstalledAdapters.length; p++) {
484
+ await installAdapterAsync(preInstalledAdapters[p]);
485
+ }
453
486
  }
487
+
488
+ await installAdapterAsync();
489
+ await storeOriginalFiles();
490
+ cb && cb(true);
454
491
  } else {
455
492
  setTimeout(() => {
456
493
  console.log('installJsController: js-controller installed');
@@ -606,8 +643,6 @@ async function setObject(id, obj){
606
643
  }
607
644
 
608
645
  async function getSecret() {
609
- const dataDir = `${rootDir}tmp/${appName}-data/`;
610
-
611
646
  if (systemConfig) {
612
647
  return systemConfig.native.secret;
613
648
  }
@@ -624,59 +659,31 @@ function encrypt (key, value) {
624
659
  }
625
660
 
626
661
  function startAdapter(objects, states, callback) {
627
- const id = `${pkg.name}.0`;
628
- if (adaptersStarted[id]) {
629
- console.log('Adapter already started ...');
630
- callback && callback(objects, states);
631
- return;
632
- }
633
- adaptersStarted[id] = true;
634
- console.log('startAdapter...');
635
- if (fs.existsSync(`${rootDir}tmp/node_modules/${pkg.name}/${pkg.main}`)) {
636
- try {
637
- if (debug) {
638
- // start controller
639
- pids[id] = child_process.exec(`node node_modules/${pkg.name}/${pkg.main} --console silly`, {
640
- cwd: `${rootDir}tmp`,
641
- stdio: [0, 1, 2]
642
- });
643
- } else {
644
- // start controller
645
- pids[id] = child_process.fork(`node_modules/${pkg.name}/${pkg.main}`, ['--console', 'silly'], {
646
- cwd: `${rootDir}tmp`,
647
- stdio: [0, 1, 2, 'ipc']
648
- });
649
- }
650
- } catch (error) {
651
- console.error(JSON.stringify(error));
652
- }
653
- } else {
654
- console.error(`Cannot find: ${rootDir}tmp/node_modules/${pkg.name}/${pkg.main}`);
655
- }
662
+ startCustomAdapter();
656
663
  callback && callback(objects, states);
657
664
  }
658
665
 
659
666
  function startCustomAdapter(adapterName, adapterInstance) {
660
667
  adapterInstance = adapterInstance || 0;
661
- const id = `${adapterName}.${adapterInstance}`;
668
+ const id = `${adapterName || pkg.name}.${adapterInstance}`;
662
669
  if (adaptersStarted[id]) {
663
670
  console.log(`Adapter ${id} already started ...`);
664
671
  return;
665
672
  }
666
673
  adaptersStarted[id] = true;
667
674
  console.log(`startAdapter ${id} ...`);
668
- const _pkg = require(`${rootDir}tmp/node_modules/iobroker.${adapterName}/package.json`);
675
+ const _pkg = adapterName ? require(`${rootDir}tmp/node_modules/iobroker.${adapterName}/package.json`) : pkg;
669
676
  if (fs.existsSync(`${rootDir}tmp/node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`)) {
670
677
  try {
671
678
  if (debug) {
672
679
  // start controller
673
- pids[id] = child_process.exec(`node node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'} ${adapterInstance} --debug --console silly`, {
680
+ pids[id] = cp.exec(`node node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'} ${adapterInstance} --debug --console silly`, {
674
681
  cwd: `${rootDir}tmp`,
675
682
  stdio: [0, 1, 2],
676
683
  });
677
684
  } else {
678
685
  // start controller
679
- pids[id] = child_process.fork(`node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`, [adapterInstance, '--debug ', '--console', 'silly'], {
686
+ pids[id] = cp.fork(`node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`, [adapterInstance, '--debug ', '--console', 'silly'], {
680
687
  cwd: `${rootDir}tmp`,
681
688
  stdio: [0, 1, 2, 'ipc'],
682
689
  });