feat: add transformations

Fixes #9
This commit is contained in:
Karl-Philipp Wulfert
2019-07-10 16:12:21 +02:00
parent d05fd8a2a5
commit ddbe00d2a5
17 changed files with 415 additions and 175 deletions

View File

@@ -13,7 +13,7 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {slow, suite, test, timeout} from 'mocha-typescript';
import {suite, test} from 'mocha-typescript';
import {
deleteUndefinedProperties,
isNodeEnvironment,
@@ -21,7 +21,7 @@ import {
isProductiveNodeEnvironment,
} from '../src/common';
@suite(timeout(2000), slow(1000))
@suite()
export class CommonSpec {
@test
deleteUndefinedProperties1() {

View File

@@ -16,23 +16,30 @@ import * as chai from 'chai';
import {expect} from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as chaiSpies from 'chai-spies';
import {slow, suite, timeout} from 'mocha-typescript';
import {suite} from 'mocha-typescript';
import {Logger} from '../src/logger';
import {AddLogLevel} from '../src/transformations/add-log-level';
import {DummyTransport} from './dummyTransport';
chai.should();
chai.use(chaiSpies);
chai.use(chaiAsPromised);
@suite(timeout(2000), slow(1000))
@suite()
export class LoggerSpec {
static sandbox: any;
static sandbox: ChaiSpies.Sandbox;
static before() {
LoggerSpec.sandbox = chai.spy.sandbox();
}
before() {
Logger.setTransformations([
new AddLogLevel(),
]);
}
after() {
LoggerSpec.sandbox.restore();
}
@@ -44,6 +51,7 @@ export class LoggerSpec {
@test
async error() {
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
// noop
});
await Logger.error('Foobar');
@@ -56,6 +64,7 @@ export class LoggerSpec {
@test
async 'error in productive environment'() {
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
// noop
});
const nodeEnv = process.env.NODE_ENV;
@@ -84,6 +93,7 @@ export class LoggerSpec {
@test
async 'error without output'() {
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
// noop
});
process.env.STAPPS_LOG_LEVEL = '0';
@@ -98,6 +108,7 @@ export class LoggerSpec {
@test
async 'error with Error'() {
const spy = LoggerSpec.sandbox.on(console, 'error', () => {
// noop
});
await Logger.error(new Error());
@@ -110,6 +121,7 @@ export class LoggerSpec {
@test
info() {
const spy = LoggerSpec.sandbox.on(console, 'info', () => {
// noop
});
Logger.info('Foobar');
@@ -122,6 +134,7 @@ export class LoggerSpec {
@test
'info without output'() {
const spy = LoggerSpec.sandbox.on(console, 'info', () => {
// noop
});
process.env.STAPPS_LOG_LEVEL = '0';
@@ -162,6 +175,7 @@ export class LoggerSpec {
}).to.throw();
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
// noop
});
process.env.ALLOW_NO_TRANSPORT = 'true';
@@ -180,6 +194,7 @@ export class LoggerSpec {
@test
log() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
// noop
});
Logger.log('Foobar');
@@ -192,6 +207,7 @@ export class LoggerSpec {
@test
'log without output'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
// noop
});
process.env.STAPPS_LOG_LEVEL = '0';
@@ -206,6 +222,7 @@ export class LoggerSpec {
@test
ok() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
// noop
});
Logger.ok('Foobar');
@@ -218,6 +235,7 @@ export class LoggerSpec {
@test
'ok without output'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
// noop
});
process.env.STAPPS_LOG_LEVEL = '0';
@@ -226,7 +244,7 @@ export class LoggerSpec {
delete process.env.STAPPS_LOG_LEVEL;
expect(spy).not.to.have.been.called()
expect(spy).not.to.have.been.called();
}
@test
@@ -240,6 +258,7 @@ export class LoggerSpec {
@test
'stringify'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
// noop
});
Logger.log('foo', 'bar');
@@ -252,10 +271,11 @@ export class LoggerSpec {
@test
'stringify object'() {
const spy = LoggerSpec.sandbox.on(console, 'log', () => {
// noop
});
Logger.log({
foo: 'bar'
foo: 'bar',
});
expect(spy).to.have.been.called();
@@ -266,6 +286,7 @@ export class LoggerSpec {
@test
warn() {
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
// noop
});
Logger.warn('Foobar');
@@ -278,6 +299,7 @@ export class LoggerSpec {
@test
'warn without output'() {
const spy = LoggerSpec.sandbox.on(console, 'warn', () => {
// noop
});
process.env.STAPPS_LOG_LEVEL = '0';
@@ -301,6 +323,7 @@ export class LoggerSpec {
};
const stub = LoggerSpec.sandbox.on(console, 'info', () => {
// noop
});
Logger.info('Foobar');
@@ -311,4 +334,19 @@ export class LoggerSpec {
expect(stub).not.to.have.been.called();
}
@test
'output without transformations'() {
Logger.setTransformations([]);
const stub = LoggerSpec.sandbox.on(console, 'log', () => {
// noop
});
const applyTransformationsSpy = LoggerSpec.sandbox.on(Logger, 'applyTransformations');
Logger.log('Foobar');
expect(stub).to.have.been.called.with('Foobar');
expect(applyTransformationsSpy).not.to.have.been.called;
}
}

View File

@@ -13,10 +13,10 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {slow, suite, test, timeout} from 'mocha-typescript';
import {suite, test} from 'mocha-typescript';
import {SMTP} from '../src/smtp';
@suite(timeout(2000), slow(1000))
@suite()
export class SMTPSpec {
/* tslint:disable:member-ordering */
@test
@@ -103,5 +103,6 @@ export class SMTPSpec {
mailValidation17() {
expect(SMTP.isValidEmailAddress('stordeur@campus')).to.be.false;
}
/* tslint:enable:member-ordering */
}

View File

@@ -0,0 +1,27 @@
/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from 'mocha-typescript';
import {AddLogLevel} from '../../src/transformations/add-log-level';
@suite()
export class AddLogLevelSpec {
@test
'transform'() {
const transformation = new AddLogLevel();
expect(transformation.transform('ERROR', 'Foobar')).to.be.equal('[ERROR] Foobar');
}
}

View File

@@ -0,0 +1,28 @@
/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from 'mocha-typescript';
import {Colorize} from '../../src/transformations/colorize';
@suite()
export class ColorizeSpec {
@test
'transform'() {
const transformation = new Colorize();
expect(transformation.transform('ERROR', 'Foobar')).to.be.equal('\u001b[1m\u001b[31mFoobar\u001b[39m\u001b[22m');
expect(transformation.transform('LOG', 'Foobar')).to.be.equal('\u001b[37mFoobar\u001b[39m');
}
}

View File

@@ -0,0 +1,35 @@
/*
* Copyright (C) 2019 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {suite, test} from 'mocha-typescript';
import {Timestamp} from '../../src/transformations/timestamp';
import moment = require('moment');
@suite()
export class ColorizeSpec {
@test
'default'() {
const transformation = new Timestamp();
expect(transformation.transform('ERROR', 'Foobar')).to.be.equal(`[${moment().format('LLLL')}] Foobar`);
}
@test
'different format'() {
const transformation = new Timestamp('DD.MM.YYYY');
expect(transformation.transform('ERROR', 'Foobar')).to.be.equal(`[${moment().format('DD.MM.YYYY')}] Foobar`);
}
}

View File

@@ -13,11 +13,11 @@
* this program. If not, see <https://www.gnu.org/licenses/>.
*/
import {expect} from 'chai';
import {slow, suite, test, timeout} from 'mocha-typescript';
import {suite, test} from 'mocha-typescript';
import {isTransportWithVerification} from '../src/common';
import {DummyTransport, VerifiableDummyTransport} from './dummyTransport';
@suite(timeout(2000), slow(1000))
@suite()
export class TransportSpec {
@test
isNotTransportWithVerification() {