UNPKG

2.1 kBJavaScriptView Raw
1var path = require('path');
2process.argv.push('--root');
3process.argv.push(path.join(__dirname, 'config', 'testroot'));
4var config = require('../lib/config.js');
5var debug = require('debug')('config test');
6var assert = require('assert');
7var beautify = require('../lib/util/beautify.js').js_beautify;
8var fs = require('fs');
9var util = require('util');
10
11describe('配置管理', function () {
12 it('基本功能', function () {
13
14 //测试开始,构造环境
15 var clamRepo = path.join(__dirname, 'config/.clam');
16 if(!fs.existsSync(clamRepo)){
17 fs.mkdirSync(clamRepo);
18 }
19
20 config.set('project', {aa:'bb'});
21 var e = config.get('project');
22 assert.deepEqual(e.aa, 'bb');
23 var info = fs.readFileSync(path.join(__dirname, 'config/.clam/project.json'));
24 info = info.toString().replace(/[\n\r]/g, '');
25 info = JSON.parse(info);
26 assert.deepEqual(info.aa, 'bb');
27 });
28
29 it('获取项目根目录', function () {
30 var root = config.root();
31 var expired = path.join(__dirname, 'config');
32 assert.deepEqual(root, expired);
33 });
34
35 it('文件监听', function () {
36 //先把aa值设置为bb,然后再通过文件修改设置为cc。检查是否可以监听到内容修改
37 config.set('page', {aa:'bb'});
38 var e = config.get('page');
39 assert.deepEqual(e.aa, 'bb');
40 var pageFile = path.join(__dirname, 'config/.clam/page.json');
41
42 config.on('pageChange', function (key) {
43 debug('修改内容%s', key);
44 assert.deepEqual(config.get('page').aa, 'cc');
45 });
46 fs.writeFileSync(pageFile, beautify(JSON.stringify({aa:'cc'})));
47
48
49 //测试完毕,系统清理
50 var clamRepo = path.join(__dirname, 'config/.clam');
51 if (fs.existsSync(clamRepo)) {
52 var files = fs.readdirSync(clamRepo);
53 files.forEach(function (file, i) {
54 fs.unlinkSync(path.join(__dirname, 'config/.clam/', file));
55 });
56 fs.rmdirSync(clamRepo);
57 }
58 });
59});
\No newline at end of file