git.delta.rocks / unique-network / refs/commits / a68b33eb81c5

difftreelog

test SilentConsole cls instead of multiple decorations

Andrey2022-09-04parent: #c9f3344.patch.diff
in: master

4 files changed

modifiedtests/src/eth/util/playgrounds/index.tsdiffbeforeafterboth
--- a/tests/src/eth/util/playgrounds/index.ts
+++ b/tests/src/eth/util/playgrounds/index.ts
@@ -6,7 +6,7 @@
 import config from '../../../config';
 
 import {EthUniqueHelper} from './unique.dev';
-import {SilentLogger} from '../../../util/playgrounds/unique.dev';
+import {SilentLogger, SilentConsole} from '../../../util/playgrounds/unique.dev';
 
 export {EthUniqueHelper} from './unique.dev';
 
@@ -16,25 +16,9 @@
 export const expect = chai.expect;
 
 export const usingEthPlaygrounds = async (code: (helper: EthUniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {
-  // TODO: Remove, this is temporary: Filter unneeded API output
-  // (Jaco promised it will be removed in the next version)
-  const consoleErr = console.error;
-  const consoleLog = console.log;
-  const consoleWarn = console.warn;
-
-  const outFn = (printer: any) => (...args: any[]) => {
-    for (const arg of args) {
-      if (typeof arg !== 'string')
-        continue;
-      if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis: UniqueApi/2, RmrkApi/1') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')
-        return;
-    }
-    printer(...args);
-  };
+  const silentConsole = new SilentConsole();
+  silentConsole.enable();
 
-  console.error = outFn(consoleErr.bind(console));
-  console.log = outFn(consoleLog.bind(console));
-  console.warn = outFn(consoleWarn.bind(console));
   const helper = new EthUniqueHelper(new SilentLogger());
 
   try {
@@ -47,9 +31,7 @@
   finally {
     await helper.disconnect();
     await helper.disconnectWeb3();
-    console.error = consoleErr;
-    console.log = consoleLog;
-    console.warn = consoleWarn;
+    silentConsole.disable();
   }
 }
   
modifiedtests/src/substrate/substrate-api.tsdiffbeforeafterboth
--- a/tests/src/substrate/substrate-api.ts
+++ b/tests/src/substrate/substrate-api.ts
@@ -25,6 +25,8 @@
 import privateKey from './privateKey';
 import promisifySubstrate from './promisify-substrate';
 
+import {SilentConsole} from '../util/playgrounds/unique.dev';
+
 
 
 function defaultApiOptions(): ApiOptions {
@@ -74,26 +76,9 @@
   settings = settings || defaultApiOptions();
   const api: ApiPromise = new ApiPromise(settings);
   let result: T = null as unknown as T;
-
-  // TODO: Remove, this is temporary: Filter unneeded API output
-  // (Jaco promised it will be removed in the next version)
-  const consoleErr = console.error;
-  const consoleLog = console.log;
-  const consoleWarn = console.warn;
 
-  const outFn = (printer: any) => (...args: any[]) => {
-    for (const arg of args) {
-      if (typeof arg !== 'string')
-        continue;
-      if (arg.includes('1000:: Normal connection closure' || arg === 'Normal connection closure'))
-        return;
-    }
-    printer(...args);
-  };
-
-  console.error = outFn(consoleErr.bind(console));
-  console.log = outFn(consoleLog.bind(console));
-  console.warn = outFn(consoleWarn.bind(console));
+  const silentConsole = new SilentConsole();
+  silentConsole.enable();
 
   try {
     await promisifySubstrate(api, async () => {
@@ -106,9 +91,7 @@
     })();
   } finally {
     await api.disconnect();
-    console.error = consoleErr;
-    console.log = consoleLog;
-    console.warn = consoleWarn;
+    silentConsole.disable();
   }
   return result as T;
 }
modifiedtests/src/util/playgrounds/index.tsdiffbeforeafterboth
before · tests/src/util/playgrounds/index.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {IKeyringPair} from '@polkadot/types/types';5import config from '../../config';6import '../../interfaces/augment-api-events';7import {DevUniqueHelper} from './unique.dev';89class SilentLogger {10  log(msg: any, level: any): void { }11  level = {12    ERROR: 'ERROR' as const,13    WARNING: 'WARNING' as const,14    INFO: 'INFO' as const,15  };16}1718export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {19  // TODO: Remove, this is temporary: Filter unneeded API output20  // (Jaco promised it will be removed in the next version)21  const consoleErr = console.error;22  const consoleLog = console.log;23  const consoleWarn = console.warn;2425  const outFn = (printer: any) => (...args: any[]) => {26    for (const arg of args) {27      if (typeof arg !== 'string')28        continue;29      if (arg.includes('1000:: Normal connection closure' || arg === 'Normal connection closure'))30        return;31    }32    printer(...args);33  };3435  console.error = outFn(consoleErr.bind(console));36  console.log = outFn(consoleLog.bind(console));37  console.warn = outFn(consoleWarn.bind(console));38  const helper = new DevUniqueHelper(new SilentLogger());3940  try {41    await helper.connect(config.substrateUrl);42    const ss58Format = helper.chain.getChainProperties().ss58Format;43    const privateKey = (seed: string) => helper.util.fromSeed(seed, ss58Format);44    await code(helper, privateKey);45  }46  finally {47    await helper.disconnect();48    console.error = consoleErr;49    console.log = consoleLog;50    console.warn = consoleWarn;51  }52};
after · tests/src/util/playgrounds/index.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {IKeyringPair} from '@polkadot/types/types';5import config from '../../config';6import '../../interfaces/augment-api-events';7import {DevUniqueHelper, SilentLogger, SilentConsole} from './unique.dev';8910export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string) => IKeyringPair) => Promise<void>) => {11  const silentConsole = new SilentConsole();12  silentConsole.enable();1314  const helper = new DevUniqueHelper(new SilentLogger());1516  try {17    await helper.connect(config.substrateUrl);18    const ss58Format = helper.chain.getChainProperties().ss58Format;19    const privateKey = (seed: string) => helper.util.fromSeed(seed, ss58Format);20    await code(helper, privateKey);21  }22  finally {23    await helper.disconnect();24    silentConsole.disable();25  }26};
modifiedtests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/unique.dev.ts
+++ b/tests/src/util/playgrounds/unique.dev.ts
@@ -5,10 +5,58 @@
 import {UniqueHelper} from './unique';
 import {ApiPromise, WsProvider} from '@polkadot/api';
 import * as defs from '../../interfaces/definitions';
-import {TSigner} from './types';
 import {IKeyringPair} from '@polkadot/types/types';
 
 
+export class SilentLogger {
+  log(msg: any, level: any): void { }
+  level = {
+    ERROR: 'ERROR' as const,
+    WARNING: 'WARNING' as const,
+    INFO: 'INFO' as const,
+  };
+}
+
+
+export class SilentConsole {
+  // TODO: Remove, this is temporary: Filter unneeded API output
+  // (Jaco promised it will be removed in the next version)
+  consoleErr: any;
+  consoleLog: any;
+  consoleWarn: any;
+
+  constructor() {
+    this.consoleErr = console.error;
+    this.consoleLog = console.log;
+    this.consoleWarn = console.warn;
+  }
+
+  enable() {  
+    const outFn = (printer: any) => (...args: any[]) => {
+      for (const arg of args) {
+        for (const arg of args) {
+          if (typeof arg !== 'string')
+            continue;
+          if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis: UniqueApi/2, RmrkApi/1') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')
+            return;
+        }
+      }
+      printer(...args);
+    };
+  
+    console.error = outFn(this.consoleErr.bind(console));
+    console.log = outFn(this.consoleLog.bind(console));
+    console.warn = outFn(this.consoleWarn.bind(console));
+  }
+
+  disable() {
+    console.error = this.consoleErr;
+    console.log = this.consoleLog;
+    console.warn = this.consoleWarn;
+  }
+}
+
+
 export class DevUniqueHelper extends UniqueHelper {
   /**
    * Arrange methods for tests