diff --git a/samples/all-options.js b/examples/all-options.js similarity index 100% rename from samples/all-options.js rename to examples/all-options.js diff --git a/samples/log-to-file.js b/examples/log-to-file.js similarity index 100% rename from samples/log-to-file.js rename to examples/log-to-file.js diff --git a/samples/no-options.js b/examples/no-options.js similarity index 100% rename from samples/no-options.js rename to examples/no-options.js diff --git a/tests/index.test.js b/index.test.js similarity index 86% rename from tests/index.test.js rename to index.test.js index e6868f4223592782b350d109d5de8b9c796cdefe..e17f73542134c6484a2f55f51d7134e81ab8a627 100644 --- a/tests/index.test.js +++ b/index.test.js @@ -1,313 +1,313 @@ -/* 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' -let file = 'testfile' -const filePath = '/tmp' - -let orginialFile -let log -let line -let testline -describe('Log.js', () => { - describe(': No Options', () => { - beforeEach(() => { - console.log('create Object') - log = new LOG() - }) - afterEach(() => { - console.log('destroy Object') - log = undefined - }) - describe(': Info', () => { - it('should output empty line - standard log-level without options is warn', () => { - line = log.info('testline') - assert.equal(line, '') - }) - }) - describe(': Notice', () => { - it('should output empty line - standard log-level without options is warn', () => { - line = log.notice('testline') - assert.equal(line, '') - }) - }) - describe(': Warn', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tWARN\ttestline' - line = log.warn('testline') - assert.equal(line, testline) - }) - }) - describe(': Error', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tERROR\ttestline' - line = log.error('testline') - assert.equal(line, testline) - }) - }) - describe(': Fatal', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tFATAL\ttestline' - line = log.fatal('testline') - assert.equal(line, testline) - }) - }) - describe(': Debug', () => { - it('should output the given Line', () => { - line = log.debug('testline') - assert.equal(line, '') - }) - }) - }) - describe(': Hostname Set', () => { - beforeEach(() => { - console.log('create Object') - log = new LOG({ - hostname: manualhostname - }) - }) - afterEach(() => { - console.log('destroy Object') - log = undefined - }) - describe(': Info', () => { - it('should output the given Line', () => { - line = log.info('testline') - assert.equal(line, '') - }) - }) - describe(': Notice', () => { - it('should output the given Line', () => { - line = log.notice('testline') - assert.equal(line, '') - }) - }) - describe(': Warn', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + manualhostname + '\tlib-log\tWARN\ttestline' - line = log.warn('testline') - assert.equal(line, testline) - }) - }) - describe(': Error', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + manualhostname + '\tlib-log\tERROR\ttestline' - line = log.error('testline') - assert.equal(line, testline) - }) - }) - describe(': Fatal', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + manualhostname + '\tlib-log\tFATAL\ttestline' - line = log.fatal('testline') - assert.equal(line, testline) - }) - }) - describe(': Debug', () => { - it('should output the given Line', () => { - line = log.debug('testline') - assert.equal(line, '') - }) - }) - }) - describe(': Name Set', () => { - beforeEach(() => { - console.log('create Object') - log = new LOG({ - name: name - }) - }) - afterEach(() => { - console.log('destroy Object') - log = undefined - }) - describe(': Info', () => { - it('should output the given Line', () => { - line = log.info('testline') - assert.equal(line, '') - }) - }) - describe(': Notice', () => { - it('should output the given Line', () => { - line = log.notice('testline') - assert.equal(line, '') - }) - }) - describe(': Warn', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\t' + name + '\tWARN\ttestline' - line = log.warn('testline') - assert.equal(line, testline) - }) - }) - describe(': Error', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\t' + name + '\tERROR\ttestline' - line = log.error('testline') - assert.equal(line, testline) - }) - }) - describe(': Fatal', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\t' + name + '\tFATAL\ttestline' - line = log.fatal('testline') - assert.equal(line, testline) - }) - }) - describe(': Debug', () => { - it('should output the given Line', () => { - line = log.debug('testline') - assert.equal(line, '') - }) - }) - }) - describe(': File Set', () => { - beforeEach(() => { - console.log('create Object') - log = new LOG({ - path: filePath, - file: file - }) - orginialFile = file - file = path.join(filePath, file) - try { - fs.accessSync(file, fs.constants.R_OK | fs.constants.W_OK) - } catch (error) { - fs.writeFileSync(file, '', { flag: 'wx' }) // Create File if not exists - } - }) - afterEach(() => { - console.log('destroy Object') - log = undefined - fs.truncateSync(file) // Clean Up, clear contents - file = orginialFile - }) - describe(': Info', () => { - it('should output the given Line to the file', function (done) { - log.info('testline') - setTimeout(() => { - line = fs.readFileSync(file, 'utf-8') - assert.equal(line, '') - done() - }, 10) - }) - }) - describe(': Notice', () => { - it('should output the given Line to the file', function (done) { - log.notice('testline') - setTimeout(() => { - line = fs.readFileSync(file, 'utf-8') - assert.equal(line, '') - done() - }, 10) - }) - }) - describe(': Warn', () => { - it('should output the given Line to the file', function (done) { - testline = getDate() + '\t' + hostname + '\tlib-log\tWARN\ttestline\n' - log.warn('testline') - setTimeout(() => { - line = fs.readFileSync(file, 'utf-8') - assert.equal(line, testline) - done() - }, 10) - }) - }) - describe(': Error', () => { - it('should output the given Line to the file', function (done) { - testline = getDate() + '\t' + hostname + '\tlib-log\tERROR\ttestline\n' - log.error('testline') - setTimeout(() => { - line = fs.readFileSync(file, 'utf-8') - assert.equal(line, testline) - done() - }, 10) - }) - }) - describe(': Fatal', () => { - it('should output the given Line to the file', function (done) { - testline = getDate() + '\t' + hostname + '\tlib-log\tFATAL\ttestline\n' - log.fatal('testline') - setTimeout(() => { - line = fs.readFileSync(file, 'utf-8') - assert.equal(line, testline) - done() - }, 10) - }) - }) - describe(': Debug', () => { - it('should output the given Line to the file', function (done) { - log.debug('testline') - setTimeout(() => { - line = fs.readFileSync(file, 'utf-8') - assert.equal(line, '') - done() - }, 10) - }) - }) - }) - describe(': Loglevel DEBUG Set', () => { - beforeEach(() => { - console.log('create Object') - log = new LOG({ - loglevel: 'DEBUG' - }) - }) - afterEach(() => { - console.log('destroy Object') - log = undefined - }) - describe(': Info', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tINFO\ttestline' - line = log.info('testline') - assert.equal(line, testline) - }) - }) - describe(': Notice', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tNOTICE\ttestline' - line = log.notice('testline') - assert.equal(line, testline) - }) - }) - describe(': Warn', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tWARN\ttestline' - line = log.warn('testline') - assert.equal(line, testline) - }) - }) - describe(': Error', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tERROR\ttestline' - line = log.error('testline') - assert.equal(line, testline) - }) - }) - describe(': Fatal', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tFATAL\ttestline' - line = log.fatal('testline') - assert.equal(line, testline) - }) - }) - describe(': Debug', () => { - it('should output the given Line', () => { - testline = getDate() + '\t' + hostname + '\tlib-log\tDEBUG\ttestline' - line = log.debug('testline') - assert.equal(line, testline) - }) - }) - }) -}) +/* 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' +let file = 'testfile' +const filePath = '/tmp' + +let orginialFile +let log +let line +let testline +describe('@br/Log', () => { + describe(': No Options', () => { + beforeEach(() => { + console.log('create Object') + log = new LOG() + }) + afterEach(() => { + console.log('destroy Object') + log = undefined + }) + describe(': Info', () => { + it('should output empty line - standard log-level without options is warn', () => { + line = log.info('testline') + assert.equal(line, '') + }) + }) + describe(': Notice', () => { + it('should output empty line - standard log-level without options is warn', () => { + line = log.notice('testline') + assert.equal(line, '') + }) + }) + describe(': Warn', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tWARN\ttestline' + line = log.warn('testline') + assert.equal(line, testline) + }) + }) + describe(': Error', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tERROR\ttestline' + line = log.error('testline') + assert.equal(line, testline) + }) + }) + describe(': Fatal', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tFATAL\ttestline' + line = log.fatal('testline') + assert.equal(line, testline) + }) + }) + describe(': Debug', () => { + it('should output the given Line', () => { + line = log.debug('testline') + assert.equal(line, '') + }) + }) + }) + describe(': Hostname Set', () => { + beforeEach(() => { + console.log('create Object') + log = new LOG({ + hostname: manualhostname + }) + }) + afterEach(() => { + console.log('destroy Object') + log = undefined + }) + describe(': Info', () => { + it('should output the given Line', () => { + line = log.info('testline') + assert.equal(line, '') + }) + }) + describe(': Notice', () => { + it('should output the given Line', () => { + line = log.notice('testline') + assert.equal(line, '') + }) + }) + describe(': Warn', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + manualhostname + '\t@br/log\tWARN\ttestline' + line = log.warn('testline') + assert.equal(line, testline) + }) + }) + describe(': Error', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + manualhostname + '\t@br/log\tERROR\ttestline' + line = log.error('testline') + assert.equal(line, testline) + }) + }) + describe(': Fatal', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + manualhostname + '\t@br/log\tFATAL\ttestline' + line = log.fatal('testline') + assert.equal(line, testline) + }) + }) + describe(': Debug', () => { + it('should output the given Line', () => { + line = log.debug('testline') + assert.equal(line, '') + }) + }) + }) + describe(': Name Set', () => { + beforeEach(() => { + console.log('create Object') + log = new LOG({ + name: name + }) + }) + afterEach(() => { + console.log('destroy Object') + log = undefined + }) + describe(': Info', () => { + it('should output the given Line', () => { + line = log.info('testline') + assert.equal(line, '') + }) + }) + describe(': Notice', () => { + it('should output the given Line', () => { + line = log.notice('testline') + assert.equal(line, '') + }) + }) + describe(': Warn', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t' + name + '\tWARN\ttestline' + line = log.warn('testline') + assert.equal(line, testline) + }) + }) + describe(': Error', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t' + name + '\tERROR\ttestline' + line = log.error('testline') + assert.equal(line, testline) + }) + }) + describe(': Fatal', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t' + name + '\tFATAL\ttestline' + line = log.fatal('testline') + assert.equal(line, testline) + }) + }) + describe(': Debug', () => { + it('should output the given Line', () => { + line = log.debug('testline') + assert.equal(line, '') + }) + }) + }) + describe(': File Set', () => { + beforeEach(() => { + console.log('create Object') + log = new LOG({ + path: filePath, + file: file + }) + orginialFile = file + file = path.join(filePath, file) + try { + fs.accessSync(file, fs.constants.R_OK | fs.constants.W_OK) + } catch (error) { + fs.writeFileSync(file, '', { flag: 'wx' }) // Create File if not exists + } + }) + afterEach(() => { + console.log('destroy Object') + log = undefined + fs.truncateSync(file) // Clean Up, clear contents + file = orginialFile + }) + describe(': Info', () => { + it('should output the given Line to the file', function (done) { + log.info('testline') + setTimeout(() => { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, '') + done() + }, 10) + }) + }) + describe(': Notice', () => { + it('should output the given Line to the file', function (done) { + log.notice('testline') + setTimeout(() => { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, '') + done() + }, 10) + }) + }) + describe(': Warn', () => { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\t@br/log\tWARN\ttestline\n' + log.warn('testline') + setTimeout(() => { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + describe(': Error', () => { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\t@br/log\tERROR\ttestline\n' + log.error('testline') + setTimeout(() => { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + describe(': Fatal', () => { + it('should output the given Line to the file', function (done) { + testline = getDate() + '\t' + hostname + '\t@br/log\tFATAL\ttestline\n' + log.fatal('testline') + setTimeout(() => { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, testline) + done() + }, 10) + }) + }) + describe(': Debug', () => { + it('should output the given Line to the file', function (done) { + log.debug('testline') + setTimeout(() => { + line = fs.readFileSync(file, 'utf-8') + assert.equal(line, '') + done() + }, 10) + }) + }) + }) + describe(': Loglevel DEBUG Set', () => { + beforeEach(() => { + console.log('create Object') + log = new LOG({ + loglevel: 'DEBUG' + }) + }) + afterEach(() => { + console.log('destroy Object') + log = undefined + }) + describe(': Info', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tINFO\ttestline' + line = log.info('testline') + assert.equal(line, testline) + }) + }) + describe(': Notice', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tNOTICE\ttestline' + line = log.notice('testline') + assert.equal(line, testline) + }) + }) + describe(': Warn', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tWARN\ttestline' + line = log.warn('testline') + assert.equal(line, testline) + }) + }) + describe(': Error', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tERROR\ttestline' + line = log.error('testline') + assert.equal(line, testline) + }) + }) + describe(': Fatal', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tFATAL\ttestline' + line = log.fatal('testline') + assert.equal(line, testline) + }) + }) + describe(': Debug', () => { + it('should output the given Line', () => { + testline = getDate() + '\t' + hostname + '\t@br/log\tDEBUG\ttestline' + line = log.debug('testline') + assert.equal(line, testline) + }) + }) + }) +}) diff --git a/package.json b/package.json index d8cea4ff4fa2d1f0475a20f95adbb445a97005c9..7e3307e362281b7118bb0eb7c9e09e7bc3ee02e1 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "A simple Logger with Options!", "main": "index.js", "scripts": { - "test": "jest tests/index.test.js", + "test": "jest", "sonarqube": "sonar-scanner -Dsonar.projectKey=argos-log -Dsonar.sources=. -Dsonar.host.url=https://it-devops-01.br-edv.brnet.int:8999 -Dsonar.login=gitlab" }, "author": "Dominik Sigmund <sigmund.dominik@googlemail.com> (https://webdad.eu)",