1 | var path = require('path');
|
2 | process.argv.push('--root');
|
3 | process.argv.push(path.join(__dirname, 'config', 'testroot'));
|
4 | var config = require('../lib/config.js');
|
5 | var debug = require('debug')('config test');
|
6 | var assert = require('assert');
|
7 | var beautify = require('../lib/util/beautify.js').js_beautify;
|
8 | var fs = require('fs');
|
9 | var util = require('util');
|
10 |
|
11 | describe('配置管理', 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 |
|
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 |