@iobroker/legacy-testing 0.2.6 → 0.3.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.
Files changed (3) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/setup.js +146 -163
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.0 (2023-05-08)
15
15
  * (bluefox) Added possibility to install additional adapters at start
16
16
  * (bluefox) Added setOfflineState/getOfflineState
17
17
 
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.2.6",
2
+ "version": "0.3.0",
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,20 +173,19 @@ 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 {
@@ -212,9 +210,20 @@ async function checkIsAdapterInstalled(cb, counter, customAdapterName, customIns
212
210
  }
213
211
  }
214
212
 
213
+ function checkIsAdapterInstalledAsync(counter, customAdapterName, customInstance) {
214
+ return new Promise((resolve, reject) => {
215
+ checkIsAdapterInstalled(err => {
216
+ if (err) {
217
+ reject(err);
218
+ } else {
219
+ resolve();
220
+ }
221
+ }, counter, customAdapterName, customInstance);
222
+ });
223
+ }
224
+
215
225
  async function checkIsControllerInstalled(cb, counter) {
216
226
  counter = counter || 0;
217
- const dataDir = `${rootDir}tmp/${appName}-data/`;
218
227
 
219
228
  console.log('checkIsControllerInstalled...');
220
229
  try {
@@ -237,12 +246,24 @@ async function checkIsControllerInstalled(cb, counter) {
237
246
  }
238
247
  }
239
248
 
240
- function installAdapter(customAdapterName, cb) {
249
+ function checkIsControllerInstalledAsync(counter) {
250
+ return new Promise((resolve, reject) => {
251
+ checkIsControllerInstalled(err => {
252
+ if (err) {
253
+ reject(err);
254
+ } else {
255
+ resolve();
256
+ }
257
+ }, counter);
258
+ });
259
+ }
260
+
261
+ async function installAdapter(customAdapterName, cb) {
241
262
  if (typeof customAdapterName === 'function') {
242
263
  cb = customAdapterName;
243
264
  customAdapterName = null;
244
265
  }
245
- customAdapterName = customAdapterName || pkg.name.split('.').pop();
266
+ customAdapterName = customAdapterName || pkg.name;
246
267
  console.log(`[${customAdapterName}] Install adapter...`);
247
268
 
248
269
  if (customAdapterName.includes('@')) {
@@ -252,29 +273,23 @@ function installAdapter(customAdapterName, cb) {
252
273
  const startFile = `node_modules/${appName}.js-controller/${appName}.js`;
253
274
  // make first install
254
275
  if (debug) {
255
- child_process.execSync(`node ${startFile} add ${customAdapterName} --enabled false`, {
276
+ cp.execSync(`node ${startFile} add ${customAdapterName} --enabled false`, {
256
277
  cwd: `${rootDir}tmp`,
257
278
  stdio: [0, 1, 2],
258
279
  });
259
- checkIsAdapterInstalled(error => {
260
- error && console.error(error);
261
- console.log(`[${customAdapterName}] Adapter installed.`);
262
- cb && cb();
263
- }, 0, customAdapterName);
264
280
  } else {
265
281
  // add controller
266
- const _pid = child_process.fork(startFile, ['add', customAdapterName, '--enabled', 'false'], {
282
+ const _pid = cp.fork(startFile, ['add', customAdapterName, '--enabled', 'false'], {
267
283
  cwd: `${rootDir}tmp`,
268
284
  stdio: [0, 1, 2, 'ipc'],
269
285
  });
270
286
 
271
- waitForEnd(_pid, () =>
272
- checkIsAdapterInstalled(error => {
273
- error && console.error(error);
274
- console.log(`[${customAdapterName}] Adapter installed.`);
275
- cb && cb();
276
- }, 0, customAdapterName));
287
+ await waitForEndAsync(_pid);
277
288
  }
289
+
290
+ await checkIsAdapterInstalledAsync(null, customAdapterName);
291
+ console.log(`[${customAdapterName}] Adapter installed.`);
292
+ cb && cb();
278
293
  }
279
294
 
280
295
  function installAdapterAsync(customAdapterName) {
@@ -301,7 +316,28 @@ function waitForEnd(_pid, cb) {
301
316
  }
302
317
  }
303
318
 
304
- function installJsController(preInstalledAdapters, cb) {
319
+ function waitForEndAsync(_pid) {
320
+ if (!_pid) {
321
+ return {code: -1, signal: -1};
322
+ } else {
323
+ return new Promise(resolve => {
324
+ _pid.on('exit', (code, signal) => {
325
+ if (_pid) {
326
+ _pid = null;
327
+ resolve({code, signal});
328
+ }
329
+ });
330
+ _pid.on('close', (code, signal) => {
331
+ if (_pid) {
332
+ _pid = null;
333
+ resolve({code, signal});
334
+ }
335
+ });
336
+ });
337
+ }
338
+ }
339
+
340
+ async function installJsController(preInstalledAdapters, cb) {
305
341
  if (typeof preInstalledAdapters === 'function') {
306
342
  cb = preInstalledAdapters;
307
343
  preInstalledAdapters = null;
@@ -320,137 +356,114 @@ function installJsController(preInstalledAdapters, cb) {
320
356
  let _pid;
321
357
  if (debug) {
322
358
  // start controller
323
- _pid = child_process.exec(`node ${appName}.js stop`, {
359
+ _pid = cp.exec(`node ${appName}.js stop`, {
324
360
  cwd: `${rootDir}node_modules/${appName}.js-controller`,
325
361
  stdio: [0, 1, 2],
326
362
  });
327
363
  } else {
328
- _pid = child_process.fork(`${appName}.js`, ['stop'], {
364
+ _pid = cp.fork(`${appName}.js`, ['stop'], {
329
365
  cwd: `${rootDir}node_modules/${appName}.js-controller`,
330
366
  stdio: [0, 1, 2, 'ipc'],
331
367
  });
332
368
  }
333
369
 
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
- }
370
+ await waitForEndAsync(_pid);
371
+ // copy all files into
372
+ !fs.existsSync(`${rootDir}tmp`) && fs.mkdirSync(`${rootDir}tmp`);
373
+ !fs.existsSync(`${rootDir}tmp/node_modules`) && fs.mkdirSync(`${rootDir}tmp/node_modules`);
343
374
 
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
- }
375
+ if (!fs.existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`)){
376
+ console.log('Copy js-controller...');
377
+ copyFolderRecursiveSync(`${rootDir}node_modules/${appName}.js-controller`, `${rootDir}tmp/node_modules/`);
378
+ }
377
379
 
378
- await installAdapterAsync();
379
- await storeOriginalFiles();
380
- cb && cb(true);
381
- }));
382
- });
380
+ console.log('Setup js-controller...');
381
+ let __pid;
382
+ if (debug) {
383
+ // start controller
384
+ __pid = cp.exec(`node ${appName}.js setup first --console`, {
385
+ cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
386
+ stdio: [0, 1, 2]
387
+ });
388
+ } else {
389
+ __pid = cp.fork(`${appName}.js`, ['setup', 'first', '--console'], {
390
+ cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
391
+ stdio: [0, 1, 2, 'ipc']
392
+ });
393
+ }
394
+ await waitForEnd(__pid);
395
+ await checkIsControllerInstalledAsync();
383
396
  } else {
384
397
  // check if port 9000 is free, else admin adapter will be added to running instance
385
- const { Socket } = require('net')
398
+ const {Socket} = require('net')
386
399
  const client = new Socket();
387
- client.on('error', () => {});
400
+ client.on('error', () => {
401
+ });
388
402
  client.connect(9000, '127.0.0.1', () => {
389
403
  console.error('Cannot initiate the first run of test, because one instance of application is running on this PC. Stop it and repeat.');
390
404
  process.exit(0);
391
405
  });
392
406
 
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');
407
+ await new Promise(resolve => setTimeout(resolve, 1000));
408
+ client.destroy();
397
409
 
398
- child_process.execSync(`npm install ${appName}.js-controller@dev --prefix ./ --production`, {
399
- cwd: `${rootDir}tmp/`,
410
+ if (!fs.existsSync(`${rootDir}tmp/node_modules/${appName}.js-controller`)) {
411
+ console.log('installJsController: no js-controller => install dev build from npm');
412
+
413
+ cp.execSync(`npm install ${appName}.js-controller@dev --prefix ./ --production`, {
414
+ cwd: `${rootDir}tmp/`,
415
+ stdio: [0, 1, 2],
416
+ });
417
+ } else {
418
+ console.log('Setup js-controller...');
419
+ let __pid;
420
+ if (debug) {
421
+ // start controller
422
+ __pid = cp.exec(`node ${appName}.js setup first`, {
423
+ cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
400
424
  stdio: [0, 1, 2],
401
425
  });
402
426
  } 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
- }
427
+ __pid = cp.fork(`${appName}.js`, ['setup', 'first'], {
428
+ cwd: `${rootDir}tmp/node_modules/${appName}.js-controller`,
429
+ stdio: [0, 1, 2, 'ipc'],
430
+ });
416
431
  }
432
+ await waitForEnd(__pid);
433
+ }
417
434
 
418
- // let npm install admin and run setup
419
- checkIsControllerInstalled(() => {
420
- let _pid;
435
+ // let npm install admin and run setup
436
+ await checkIsControllerInstalledAsync();
437
+ let _pid;
421
438
 
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
- }
439
+ if (fs.existsSync(`${rootDir}node_modules/${appName}.js-controller/${appName}.js`)) {
440
+ _pid = cp.fork(`${appName}.js`, ['stop'], {
441
+ cwd: `${rootDir}node_modules/${appName}.js-controller`,
442
+ stdio: [0, 1, 2, 'ipc']
443
+ });
444
+ }
428
445
 
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
- }
446
+ await waitForEndAsync(_pid);
447
+ }
446
448
 
447
- await installAdapterAsync();
448
- await storeOriginalFiles();
449
- cb && cb(true);
450
- });
451
- });
452
- }, 1000);
449
+ // change ports for the object and state DBs
450
+ const config = require(`${rootDir}tmp/${appName}-data/${appName}.json`);
451
+ config.objects.port = 19001;
452
+ config.states.port = 19000;
453
+
454
+ fs.writeFileSync(`${rootDir}tmp/${appName}-data/${appName}.json`, JSON.stringify(config, null, 2));
455
+ console.log('Setup finished.');
456
+
457
+ copyAdapterToController();
458
+ if (preInstalledAdapters) {
459
+ for (let p = 0; p < preInstalledAdapters.length; p++) {
460
+ await installAdapterAsync(preInstalledAdapters[p]);
461
+ }
453
462
  }
463
+
464
+ await installAdapterAsync();
465
+ await storeOriginalFiles();
466
+ cb && cb(true);
454
467
  } else {
455
468
  setTimeout(() => {
456
469
  console.log('installJsController: js-controller installed');
@@ -606,8 +619,6 @@ async function setObject(id, obj){
606
619
  }
607
620
 
608
621
  async function getSecret() {
609
- const dataDir = `${rootDir}tmp/${appName}-data/`;
610
-
611
622
  if (systemConfig) {
612
623
  return systemConfig.native.secret;
613
624
  }
@@ -624,59 +635,31 @@ function encrypt (key, value) {
624
635
  }
625
636
 
626
637
  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
- }
638
+ startCustomAdapter();
656
639
  callback && callback(objects, states);
657
640
  }
658
641
 
659
642
  function startCustomAdapter(adapterName, adapterInstance) {
660
643
  adapterInstance = adapterInstance || 0;
661
- const id = `${adapterName}.${adapterInstance}`;
644
+ const id = `${adapterName || pkg.name}.${adapterInstance}`;
662
645
  if (adaptersStarted[id]) {
663
646
  console.log(`Adapter ${id} already started ...`);
664
647
  return;
665
648
  }
666
649
  adaptersStarted[id] = true;
667
650
  console.log(`startAdapter ${id} ...`);
668
- const _pkg = require(`${rootDir}tmp/node_modules/iobroker.${adapterName}/package.json`);
651
+ const _pkg = adapterName ? require(`${rootDir}tmp/node_modules/iobroker.${adapterName}/package.json`) : pkg;
669
652
  if (fs.existsSync(`${rootDir}tmp/node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`)) {
670
653
  try {
671
654
  if (debug) {
672
655
  // start controller
673
- pids[id] = child_process.exec(`node node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'} ${adapterInstance} --debug --console silly`, {
656
+ pids[id] = cp.exec(`node node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'} ${adapterInstance} --debug --console silly`, {
674
657
  cwd: `${rootDir}tmp`,
675
658
  stdio: [0, 1, 2],
676
659
  });
677
660
  } else {
678
661
  // start controller
679
- pids[id] = child_process.fork(`node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`, [adapterInstance, '--debug ', '--console', 'silly'], {
662
+ pids[id] = cp.fork(`node_modules/iobroker.${adapterName}/${_pkg.main || 'main.js'}`, [adapterInstance, '--debug ', '--console', 'silly'], {
680
663
  cwd: `${rootDir}tmp`,
681
664
  stdio: [0, 1, 2, 'ipc'],
682
665
  });