Skip to content

Commit

Permalink
测试通过率达到100%
Browse files Browse the repository at this point in the history
  • Loading branch information
CaroseKYS committed Oct 9, 2017
1 parent 081847f commit dc99035
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 26 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

## 配置信息

/*配置开发和生产环境下的日志级别*/
'swa-middleware-logger': {
// 日志的存放目录,默认为/logs目录
'logs-path': 'logs',
Expand All @@ -25,6 +24,6 @@
'morgan-production': 'swa-pro-2',
// morgan 的immediate配置
'morgan-immediate': false,
// 生产模式下是否记录请求日志
'morgan-under-production': true
// 是否记录请求日志
'enabled': true
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getLogger(){
/*配置内容*/
var loggerConf;

loggerConf = utils.getJsonProp('swa-middleware-logger') || {};
loggerConf = utils.getJsonProp(configFile, 'swa-middleware-logger') || {};

/*获取设置缺省状态下的默认日志级别以及存放路径*/
levelDev = utils.getJsonProp(loggerConf, 'morgan-development') || 'dev';
Expand Down
1 change: 0 additions & 1 deletion test/conf-1/swa-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = {
*/
'morgan-development': 'dev',
'morgan-production': 'combined',
'morgan-under-production': true /*生产模式下是否记录请求日志*/,
'morgan-immediate': false
}
}
2 changes: 1 addition & 1 deletion test/conf-2/swa-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = {
*/
'morgan-development': 'dev',
'morgan-production': 'combined',
'morgan-under-production': false /*生产模式下是否记录请求日志*/,
'enabled': false,
'morgan-immediate': false
}
}
1 change: 0 additions & 1 deletion test/conf-3/swa-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = {
*/
'morgan-development': 'swa-dev-1',
'morgan-production': 'swa-pro-1',
'morgan-under-production': true /*生产模式下是否记录请求日志*/,
'morgan-immediate': false
}
}
1 change: 0 additions & 1 deletion test/conf-4/swa-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = {
*/
'morgan-development': ':query',
'morgan-production': 'swa-pro-2',
'morgan-under-production': true /*生产模式下是否记录请求日志*/,
'morgan-immediate': true
}
}
1 change: 0 additions & 1 deletion test/conf-5/swa-conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = {
*/
'morgan-development': ':query :body :query[test1] :body[test1] :real-ip',
'morgan-production' : ':query :body :query[test1] :body[test1] :real-ip',
'morgan-under-production': true /*生产模式下是否记录请求日志*/,
'morgan-immediate': true
}
}
14 changes: 14 additions & 0 deletions test/conf-6/swa-conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
/*配置开发和生产环境下的日志级别*/
'swa-middleware-logger': {
'logs-path': 'logs',/*日志的存放目录,默认为/logs目录*/
/**
* 请求日志的记录级别,所有级别如下:
* combined, common, dev, short, tiny
* 详情请查看: https://www.npmjs.com/package/morgan
*/
'morgan-development': ':query :body :query[test1] :body[test1] :real-ip',
'morgan-production' : ':query :body :query[test1] :body[test1] :real-ip',
'morgan-immediate': true
}
}
61 changes: 44 additions & 17 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ var rimraf = require('rimraf');
var rewire = require('rewire');
var muk = require('muk');
var httpmock = require('node-mocks-http');
var mkdirp = require('mkdirp');
var sIndexPath = path.join(__dirname, '..', 'index.js');
var logger;

describe('swa-logger模块测试', function(){
before(function () {
rmDir();
this.timeout(10000);
rmDir().then(() => {
done();
});
});

after(function () {
rmDir();
after(function (done) {
this.timeout(10000);
rmDir().then(() => {
done();
});
});

describe('开发环境测试', function(){
Expand All @@ -27,17 +34,12 @@ describe('swa-logger模块测试', function(){
delete process.env.NODE_ENV;
});

after(function(){
rmDir();
});

beforeEach(function(){
req = httpmock.createRequest({url: '/'});
res = httpmock.createResponse({
eventEmitter: events.EventEmitter
});
req.headers = {};
rmDir();
delete require.cache[sIndexPath];
delete process.env.NODE_SWA_ROOT;
});
Expand All @@ -61,6 +63,15 @@ describe('swa-logger模块测试', function(){
logger = require('../index.js');
});
});

it('模拟创建文件夹失败场景', function(){
process.env.NODE_SWA_ROOT = path.join(__dirname, 'conf-6');
muk(mkdirp, 'sync', () => false);
should.doesNotThrow(function(){
logger = require('../index.js');
});
muk.restore();
});
});

describe('生产环境测试', function(){
Expand All @@ -72,7 +83,7 @@ describe('swa-logger模块测试', function(){
process.env.NODE_ENV = 'production';
});
after(function(){
rmDir();
// rmDir();
});

beforeEach(function(){
Expand All @@ -87,7 +98,7 @@ describe('swa-logger模块测试', function(){
req.body = {'test1': 'value1'};
req.connection = {};

rmDir();
// rmDir();
delete require.cache[sIndexPath];
delete process.env.NODE_SWA_ROOT;
});
Expand Down Expand Up @@ -143,11 +154,27 @@ describe('swa-logger模块测试', function(){
});

function rmDir(){
rimraf(path.join(__dirname, '..', 'logs'), function(){});
rimraf(path.join(__dirname, 'logs'), function(){});
rimraf(path.join(__dirname, 'conf-1', 'logs'), function(){});
rimraf(path.join(__dirname, 'conf-2', 'logs'), function(){});
rimraf(path.join(__dirname, 'conf-3', 'logs'), function(){});
rimraf(path.join(__dirname, 'conf-4', 'logs'), function(){});
rimraf(path.join(__dirname, 'conf-5', 'logs'), function(){});
const logDirs = [
path.join(__dirname, '..', 'logs'),
path.join(__dirname, 'logs'),
path.join(__dirname, 'conf-1', 'logs'),
path.join(__dirname, 'conf-2', 'logs'),
path.join(__dirname, 'conf-3', 'logs'),
path.join(__dirname, 'conf-4', 'logs'),
path.join(__dirname, 'conf-5', 'logs'),
path.join(__dirname, 'conf-6', 'logs'),
];

const promises = [];

delete require.cache[sIndexPath];

logDirs.forEach(path => {

promises.push(new Promise((resolve, reject) => {
rimraf(path, {emfileWait: 5000}, resolve);
}));
});

return Promise.all(promises);
}

0 comments on commit dc99035

Please sign in to comment.