From d11dc61c1f9023de721d96984f97d2cca2fad4e3 Mon Sep 17 00:00:00 2001 From: Domink Sigmund <sigmund.dominik@googlemail.com> Date: Fri, 5 Oct 2018 10:44:07 +0200 Subject: [PATCH] Added Tests --- .gitignore | 4 +- index.js | 23 +-- package.json | 3 +- tests/test.js | 468 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 485 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index d92c337..eaf59a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ node_modules -samples/file.log \ No newline at end of file +samples/file.log +coverage/ +mochawesome-report/ \ No newline at end of file diff --git a/index.js b/index.js index 7409786..c206a96 100644 --- a/index.js +++ b/index.js @@ -15,37 +15,37 @@ function Log (options) { this.fs = require('fs') } this.info = function (message, tags) { - this.log('INFO', message, tags) + return this.log('INFO', message, tags) } this.notice = function (message, tags) { - this.log('NOTICE', message, tags) + return this.log('NOTICE', message, tags) } this.fatal = function (message, tags) { - this.log('FATAL', message, tags) + return this.log('FATAL', message, tags) } this.warn = function (message, tags) { - this.log('WARN', message, tags) + return this.log('WARN', message, tags) } this.error = function (message, tags) { - this.log('ERROR', message, tags) + return this.log('ERROR', message, tags) } this.debug = function (message, tags) { - this.log('DEBUG', message, tags) + return this.log('DEBUG', message, tags) } this.log = function (tag, message, tags) { let tadd = '' if (typeof tags !== 'undefined') { if (Array.isArray(tags)) { - tadd = '#' + tags.join(' #') + tadd = ' #' + tags.join(' #') } else if (tags.includes(' ')) { - tadd = '#' + tags.split(' ').join(' #') + tadd = ' #' + tags.split(' ').join(' #') } else if (tags.includes(',')) { - tadd = '#' + tags.split(',').join(' #') + tadd = ' #' + tags.split(',').join(' #') } else { - tadd = '#' + tags + tadd = ' #' + tags } } - let msg = this.getDate() + '\t' + this.hostname + '\t' + this.name + '\t' + tag + '\t' + message + '\t' + tadd + let msg = this.getDate() + '\t' + this.hostname + '\t' + this.name + '\t' + tag + '\t' + message + tadd switch (tag) { case 'INFO': console.info(msg) @@ -75,6 +75,7 @@ function Log (options) { } }) } + return msg } this.getDate = function () { var tzoffset = (new Date()).getTimezoneOffset() * 60000 // offset in milliseconds diff --git a/package.json b/package.json index 0f663d8..77969c4 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "A simple Logger with Options!", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "istanbul cover _mocha -- tests/test.js && rsync --remove-source-files -av --progress ./coverage ./docs/ && rm -Rf ./coverage", + "test-graphics":"istanbul cover _mocha -- tests/test.js -R mochawesome && rsync --remove-source-files -av --progress ./coverage ./docs/ && rm -Rf ./coverage && rsync --remove-source-files -av --progress ./mochawesome-report ./docs/ && rm -Rf ./mochawesome-report" }, "author": "Dominik Sigmund", "license": "ISC", diff --git a/tests/test.js b/tests/test.js index e69de29..aa8b745 100644 --- a/tests/test.js +++ b/tests/test.js @@ -0,0 +1,468 @@ +/* global it, describe, beforeEach, afterEach */ +const assert = require('assert') +const os = require('os') +const path = require('path') +const fs = require('fs') +const LOG = require(path.join(__dirname, '../index.js')) +const hostname = os.hostname() +function getDate () { + var tzoffset = (new Date()).getTimezoneOffset() * 60000 // offset in milliseconds + var localISOTime = (new Date(Date.now() - tzoffset)).toISOString().slice(0, -1) + return localISOTime.split('.')[0].trim() +} +const manualhostname = 'testhost' +const name = 'testapp' +const file = '/tmp/testfile' +let log +let line +let testline +describe('Log.js', function () { + describe(': No Options', function () { + beforeEach('create Object', function () { + log = new LOG() + }) + afterEach('destroy Object', function () { + log = undefined + }) + describe(': Info', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\tlog\tINFO\ttestline' + line = log.info('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\tlog\tINFO\ttestline #tag1 #tag2 #tag3' + + line = log.info('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.info('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.info('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Notice', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\tlog\tNOTICE\ttestline' + line = log.notice('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\tlog\tNOTICE\ttestline #tag1 #tag2 #tag3' + + line = log.notice('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.notice('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.notice('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Warn', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\tlog\tWARN\ttestline' + line = log.warn('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\tlog\tWARN\ttestline #tag1 #tag2 #tag3' + + line = log.warn('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.warn('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.warn('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Error', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\tlog\tERROR\ttestline' + line = log.error('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\tlog\tERROR\ttestline #tag1 #tag2 #tag3' + + line = log.error('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.error('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.error('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Fatal', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\tlog\tFATAL\ttestline' + line = log.fatal('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\tlog\tFATAL\ttestline #tag1 #tag2 #tag3' + + line = log.fatal('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.fatal('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.fatal('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Debug', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\tlog\tDEBUG\ttestline' + line = log.debug('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\tlog\tDEBUG\ttestline #tag1 #tag2 #tag3' + + line = log.debug('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.debug('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.debug('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + }) + describe(': Hostname Set', function () { + beforeEach('create Object', function () { + log = new LOG({ + hostname: manualhostname + }) + }) + afterEach('destroy Object', function () { + log = undefined + }) + describe(': Info', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tINFO\ttestline' + line = log.info('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tINFO\ttestline #tag1 #tag2 #tag3' + + line = log.info('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.info('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.info('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Notice', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tNOTICE\ttestline' + line = log.notice('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tNOTICE\ttestline #tag1 #tag2 #tag3' + + line = log.notice('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.notice('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.notice('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Warn', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tWARN\ttestline' + line = log.warn('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tWARN\ttestline #tag1 #tag2 #tag3' + + line = log.warn('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.warn('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.warn('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Error', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tERROR\ttestline' + line = log.error('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tERROR\ttestline #tag1 #tag2 #tag3' + + line = log.error('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.error('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.error('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Fatal', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tFATAL\ttestline' + line = log.fatal('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tFATAL\ttestline #tag1 #tag2 #tag3' + + line = log.fatal('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.fatal('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.fatal('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Debug', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tDEBUG\ttestline' + line = log.debug('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + manualhostname + '\tlog\tDEBUG\ttestline #tag1 #tag2 #tag3' + + line = log.debug('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.debug('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.debug('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + }) + describe(': Name Set', function () { + beforeEach('create Object', function () { + log = new LOG({ + name: name + }) + }) + afterEach('destroy Object', function () { + log = undefined + }) + describe(': Info', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tINFO\ttestline' + line = log.info('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tINFO\ttestline #tag1 #tag2 #tag3' + + line = log.info('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.info('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.info('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Notice', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tNOTICE\ttestline' + line = log.notice('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tNOTICE\ttestline #tag1 #tag2 #tag3' + + line = log.notice('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.notice('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.notice('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Warn', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tWARN\ttestline' + line = log.warn('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tWARN\ttestline #tag1 #tag2 #tag3' + + line = log.warn('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.warn('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.warn('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Error', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tERROR\ttestline' + line = log.error('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tERROR\ttestline #tag1 #tag2 #tag3' + + line = log.error('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.error('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.error('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Fatal', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tFATAL\ttestline' + line = log.fatal('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tFATAL\ttestline #tag1 #tag2 #tag3' + + line = log.fatal('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.fatal('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.fatal('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + describe(': Debug', function () { + it('should output the given Line', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tDEBUG\ttestline' + line = log.debug('testline') + assert.equal(line, testline) + }) + it('should output the given Line with Tags', function () { + testline = getDate() + '\t' + hostname + '\t' + name + '\tDEBUG\ttestline #tag1 #tag2 #tag3' + + line = log.debug('testline', 'tag1 tag2 tag3') + assert.equal(line, testline) + + line = log.debug('testline', 'tag1,tag2,tag3') + assert.equal(line, testline) + + line = log.debug('testline', ['tag1', 'tag2', 'tag3']) + assert.equal(line, testline) + }) + }) + }) + describe(': File Set', function () { + beforeEach('create Object', function () { + log = new LOG({ + file: file + }) + fs.writeFileSync(file, '') // Create File if not exists + }) + afterEach('destroy Object', function () { + log = undefined + fs.truncateSync(file) // Clean Up, clear contents + }) + describe(': Info', function () { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\tlog\tINFO\ttestline\n' + log.info('testline') + setTimeout(function () { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + describe(': Notice', function () { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\tlog\tNOTICE\ttestline\n' + log.notice('testline') + setTimeout(function () { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + describe(': Warn', function () { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\tlog\tWARN\ttestline\n' + log.warn('testline') + setTimeout(function () { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + describe(': Error', function () { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\tlog\tERROR\ttestline\n' + log.error('testline') + setTimeout(function () { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + describe(': Fatal', function () { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\tlog\tFATAL\ttestline\n' + log.fatal('testline') + setTimeout(function () { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + describe(': Debug', function () { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\tlog\tDEBUG\ttestline\n' + log.debug('testline') + setTimeout(function () { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + }) +}) -- GitLab