/
config-loader.unit.js
157 lines (123 loc) · 4.99 KB
/
config-loader.unit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var configLoader = require('../lib/config-loader')
, sh = require('shelljs')
, path = require('path')
, fs = require('fs')
, db = require('../lib/db').create({name: 'test-db', host: 'localhost', port: 27017})
, Server = require('../lib/server')
, Collection = require('../lib/resources/collection')
, Files = require('../lib/resources/files')
, InternalResources = require('../lib/resources/internal-resources')
, sinon = require('sinon')
, basepath = './test/support/proj';
describe('config-loader', function() {
beforeEach(function() {
if (fs.existsSync(basepath)) {
sh.rm('-rf', basepath);
}
sh.mkdir('-p', basepath);
this.server = new Server();
this.sinon = sinon.sandbox.create();
});
afterEach(function() {
this.sinon.restore();
});
describe('.loadConfig()', function() {
it('should load resources', function(done) {
this.timeout(10000);
sh.mkdir('-p', path.join(basepath, 'resources/foo'));
sh.mkdir('-p', path.join(basepath, 'resources/bar'));
JSON.stringify({type: "Collection", val: 1}).to(path.join(basepath, 'resources/foo/config.json'));
JSON.stringify({type: "Collection", val: 2}).to(path.join(basepath, 'resources/bar/config.json'));
configLoader.loadConfig(basepath, this.server, function(err, resources) {
if (err) return done(err);
expect(resources).to.have.length(4);
expect(resources.filter(function(r) { return r.name == 'foo';})).to.have.length(1);
expect(resources.filter(function(r) { return r.name == 'bar';})).to.have.length(1);
done();
});
});
it('should return a set of resource instances', function(done) {
sh.mkdir('-p', path.join(basepath, 'resources/foo'));
JSON.stringify({type: "Collection", properties: {}}).to(path.join(basepath, 'resources/foo/config.json'));
configLoader.loadConfig(basepath, {db: db}, function(err, resourceList) {
expect(resourceList).to.have.length(3);
expect(resourceList[0].config.properties).to.be.a('object');
expect(resourceList[0] instanceof Collection).to.equal(true);
done(err);
});
});
it('should add internal resources', function(done) {
sh.mkdir('-p', path.join(basepath, 'resources'));
configLoader.loadConfig(basepath, {}, function(err, resourceList) {
if (err) return done(err);
expect(resourceList).to.have.length(2);
expect(resourceList[0] instanceof Files).to.equal(true);
expect(resourceList[1] instanceof InternalResources).to.equal(true);
done(err);
});
});
it('should not attempt to load files', function(done) {
sh.mkdir('-p', path.join(basepath, 'resources'));
('').to(path.join(basepath, 'resources/.DS_STORE'));
configLoader.loadConfig(basepath, {}, function(err, resourceList) {
if (err) return done(err);
done();
});
});
it('should use public_dir option if available', function(done) {
sh.mkdir('-p', path.join(basepath, 'resources'));
configLoader.loadConfig(basepath, {}, function(err, resourceList) {
if (err) return done(err);
expect(resourceList[0].config.public).to.equal('./public');
});
var opts = {};
opts.options = {};
opts.options.public_dir = 'test';
configLoader.loadConfig(basepath, opts, function(err, resourceList) {
if (err) return done(err);
expect(resourceList[0].config.public).to.equal('test');
});
done();
});
it('should use env options path if exists', function(done) {
sh.mkdir('-p', path.join(basepath, 'resources'));
var public_dir = basepath + '/test';
var opts = {};
opts.options = {};
opts.options.public_dir = public_dir;
opts.options.env = 'dev';
sh.mkdir('-p', public_dir + '-dev');
configLoader.loadConfig(basepath, opts, function(err, resourceList) {
if (err) return done(err);
expect(resourceList[0].config.public).to.equal(public_dir + '-dev');
done();
});
});
it('should read directories only once on multiple server.route requests', function (done) {
sh.mkdir('-p', path.join(basepath, 'resources'));
var server = new Server({ server_dir: basepath });
var callsLeft = 20;
function next() {
if (callsLeft == 0) {
expect(fs.readdir.callCount).to.equal(1);
return done();
}
callsLeft--;
server.route(req, res);
}
var originalLoadConfig = configLoader.loadConfig;
this.sinon.stub(configLoader, 'loadConfig').callsFake(function (basepath, server, fn) {
originalLoadConfig(basepath, server, function () {
// intercepting this call so that we can call this sequentially
next();
fn.apply(this, arguments);
});
});
var req = { url: 'foo', headers: {} };
var res = { body: 'bar' };
var fs = require('fs');
sinon.spy(fs, 'readdir');
next();
});
});
});