This repository has been archived by the owner on Nov 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
config_handler.spec.js
254 lines (181 loc) · 8.08 KB
/
config_handler.spec.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
var config_handler = require("../lib/config_handler.js");
var fs = require("fs");
describe("read the given config", function() {
it("check if the config path is a directory", function() {
spyOn(fs, "stat");
var spyCallback = jasmine.createSpy();
config_handler.readConfig("path/config", spyCallback);
expect(fs.stat).toHaveBeenCalledWith("path/config", jasmine.any(Function));
});
it("read the directory if given config is a directory", function() {
spyOn(fs, "stat").andCallFake(function(config_path, callback) {
return callback(null, { "isDirectory": function() { return true } });
});
spyOn(config_handler, "readConfigDir");
var spyCallback = jasmine.createSpy();
config_handler.readConfig("path/config", spyCallback);
expect(config_handler.readConfigDir).toHaveBeenCalledWith("path/config", spyCallback);
});
it("read the file if given config is a file", function() {
spyOn(fs, "stat").andCallFake(function(config_path, callback) {
return callback(null, { "isDirectory": function() { return false } });
});
spyOn(config_handler, "readConfigFile");
var spyCallback = jasmine.createSpy();
config_handler.readConfig("path/config", spyCallback);
expect(config_handler.readConfigFile).toHaveBeenCalledWith("path/config", spyCallback);
});
it("call the callback with an error if stat returns an error", function() {
spyOn(fs, "stat").andCallFake(function(config_path, callback) {
return callback("error", null);
});
var spyCallback = jasmine.createSpy();
config_handler.readConfig("path/config", spyCallback);
expect(spyCallback).toHaveBeenCalledWith("error", null);
});
});
describe("read the config form a file", function() {
it("read the config file from the file system", function() {
spyOn(fs, "readFile");
var spyCallback = jasmine.createSpy();
config_handler.readConfigFile("path/config", spyCallback);
expect(fs.readFile).toHaveBeenCalledWith("path/config", jasmine.any(Function));
});
it("call the callback with the parsed output of the config file", function() {
var sample_json = {"key": "value"};
spyOn(fs, "readFile").andCallFake(function(config_path, callback) {
return callback(null, new Buffer(JSON.stringify(sample_json)));
});
var spyCallback = jasmine.createSpy();
config_handler.readConfigFile("path/config", spyCallback);
expect(spyCallback).toHaveBeenCalledWith(undefined, sample_json);
});
it("call the callback with the error if an error occurrs when reading the file", function() {
spyOn(fs, "readFile").andCallFake(function(config_path, callback) {
return callback("error", null);
});
var spyCallback = jasmine.createSpy();
config_handler.readConfigFile("path/config", spyCallback);
expect(spyCallback).toHaveBeenCalledWith("error", null);
});
it("call the callback with the error if an error occurrs when parsing the file", function() {
var sample_json = {"key": "value"};
spyOn(fs, "readFile").andCallFake(function(config_path, callback) {
return callback(null, new Buffer(JSON.stringify(sample_json)));
});
spyOn(JSON, "parse").andCallFake(function() {
throw "error";
});
var spyCallback = jasmine.createSpy();
config_handler.readConfigFile("path/config", spyCallback);
expect(spyCallback).toHaveBeenCalledWith("error", undefined);
});
});
describe("read the config from a directory", function() {
it("fetch all files in the given directory", function() {
spyOn(fs, "readdir");
var spyCallback = jasmine.createSpy();
config_handler.readConfigDir("path/config_dir", spyCallback);
expect(fs.readdir).toHaveBeenCalledWith("path/config_dir", jasmine.any(Function));
});
it("call the callback with the error if reading directory gives an error", function() {
spyOn(fs, "readdir").andCallFake(function(dir_path, callback) {
return callback("error", null);
});
var spyCallback = jasmine.createSpy();
config_handler.readConfigDir("path/config_dir", spyCallback);
expect(spyCallback).toHaveBeenCalledWith("error", null);
});
it("read only the config files in given directory", function() {
spyOn(fs, "readdir").andCallFake(function(dir_path, callback) {
return callback(null, ["plugins.json", "subdir", "publish.json", ".hidden_file"]);
});
spyOn(config_handler, "readConfigFile").andCallFake(function(config_file, callback){
return callback(null, { });
});
var spyCallback = jasmine.createSpy();
config_handler.readConfigDir("path/config_dir", spyCallback);
expect(config_handler.readConfigFile.callCount).toEqual(2);
});
it("call the callback with the combined output of each config file", function() {
spyOn(fs, "readdir").andCallFake(function(dir_path, callback) {
return callback(null, [ "plugins.json", "subdir", "publish.json", ".hidden_file" ]);
});
spyOn(config_handler, "readConfigFile").andCallFake(function(config_file, callback){
if (config_file === "path/config_dir/plugins.json") {
return callback(null, { "plugin_name": "plugin_path" });
} else {
return callback(null, { "strategy": "" });
}
});
var spyCallback = jasmine.createSpy();
config_handler.readConfigDir("path/config_dir", spyCallback);
expect(spyCallback).toHaveBeenCalledWith(null, { "plugins": { "plugin_name": "plugin_path" }, "publish": { "strategy": "" } });
});
it("don't nest main config values", function() {
spyOn(fs, "readdir").andCallFake(function(dir_path, callback) {
return callback(null, [ "main.json", "plugins.json", "subdir", "publish.json", ".hidden_file" ]);
});
spyOn(config_handler, "readConfigFile").andCallFake(function(config_file, callback){
if (config_file === "path/config_dir/main.json") {
return callback(null, { "main_key": "main_value" });
} else if (config_file === "path/config_dir/plugins.json") {
return callback(null, { "plugin_name": "plugin_path" });
} else {
return callback(null, { "strategy": "" });
}
});
var spyCallback = jasmine.createSpy();
config_handler.readConfigDir("path/config_dir", spyCallback);
expect(spyCallback).toHaveBeenCalledWith(null, { "main_key": "main_value", "plugins": { "plugin_name": "plugin_path" }, "publish": { "strategy": "" } });
});
});
describe("get config", function() {
it("read the config in given path", function() {
spyOn(config_handler, "readConfig");
var spyCallback = jasmine.createSpy();
config_handler.getConfig("custom_config.json", spyCallback);
expect(config_handler.readConfig).toHaveBeenCalledWith("custom_config.json", jasmine.any(Function));
});
it("read the config directory if user-defined config path is invalid", function() {
spyOn(config_handler, "readConfig").andCallFake(function(config_path, callback) {
if (config_path === "config") {
return callback(null, {});
} else {
return callback("error", null);
}
});
var spyCallback = jasmine.createSpy();
config_handler.getConfig("custom_config.json", spyCallback);
expect(config_handler.readConfig).toHaveBeenCalledWith("config", jasmine.any(Function));
});
it("try to read config.json if there's no config directory or path is given", function() {
spyOn(config_handler, "readConfig").andCallFake(function(config_path, callback) {
if (config_path === "config.json") {
return callback(null, {});
} else {
return callback("error", null);
}
});
var spyCallback = jasmine.createSpy();
config_handler.getConfig("custom_config.json", spyCallback);
expect(config_handler.readConfig).toHaveBeenCalledWith("config.json", jasmine.any(Function));
});
it("extend the user-defined config with default config", function() {
spyOn(config_handler, "readConfig").andCallFake(function(config_path, callback) {
return callback(null, { "server": { "port": 3001 } });
});
config_handler.getConfig("custom_config.json", function(output) {
expect(output.server.port).toEqual(3001);
});
});
it("return the default config if no user-defined config can be found", function() {
spyOn(config_handler, "readConfig").andCallFake(function(config_path, callback) {
return callback("error", null);
});
var spyCallback = jasmine.createSpy();
expect(function() {
config_handler.getConfig("custom_config.json", spyCallback);
}).toThrow();
});
});