forked from YahooArchive/mojito
/
config-tests.server.js
283 lines (232 loc) · 10.4 KB
/
config-tests.server.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (c) 2011-2012, Yahoo! Inc. All rights reserved.
* Copyrights licensed under the New BSD License.
* See the accompanying LICENSE file for terms.
*/
YUI.add('mojito-addon-rs-config-tests', function(Y, NAME) {
var suite = new YUITest.TestSuite(NAME),
libfs = require('fs'),
libpath = require('path'),
mojitoRoot = libpath.join(__dirname, '../../../../../'),
A = YUITest.Assert,
OA = YUITest.ObjectAssert,
AA = YUITest.ArrayAssert;
function MockRS(config) {
MockRS.superclass.constructor.apply(this, arguments);
}
MockRS.NAME = 'MockResourceStore';
MockRS.ATTRS = {};
Y.extend(MockRS, Y.Base, {
initializer: function(cfg) {
this._config = cfg || {};
},
validateContext: function() {
},
cloneObj: function(o) {
return Y.clone(o);
},
getStaticContext: function() {
return this._config.context || {};
},
findResourceVersionByConvention: function(source, mojitType) {
// no-op
},
parseResourceVersion: function(source, type, subtype, mojitType) {
// no-op
}
});
function readJSON(dir, file) {
var path = libpath.join(dir, file);
var contents = libfs.readFileSync(path, 'utf-8');
return Y.JSON.parse(contents);
}
function cmp(x, y, msg, path) {
if (Y.Lang.isArray(x)) {
A.isArray(x, msg || 'first arg should be an array');
A.isArray(y, msg || 'second arg should be an array');
A.areSame(x.length, y.length, msg || 'arrays are different lengths');
for (var i = 0; i < x.length; i += 1) {
cmp(x[i], y[i], msg);
}
return;
}
if (Y.Lang.isObject(x)) {
A.isObject(x, msg || 'first arg should be an object');
A.isObject(y, msg || 'second arg should be an object');
A.areSame(Object.keys(x).length, Object.keys(y).length, msg || 'object keys are different lengths');
for (var i in x) {
if (x.hasOwnProperty(i)) {
cmp(x[i], y[i], msg);
}
}
return;
}
A.areSame(x, y, msg || 'args should be the same');
}
function makeSource(dir, dirType, subdir, file, isFile) {
var source = {
fs: {
fullPath: libpath.join(dir, subdir, file),
rootDir: dir,
rootType: dirType,
subDir: subdir,
subDirArray: subdir.split('/'),
isFile: isFile,
ext: libpath.extname(file)
},
pkg: {
name: 'unittest',
version: '999.666.999',
depth: 999
}
};
source.fs.basename = libpath.basename(file, source.fs.ext);
return source;
}
suite.add(new YUITest.TestCase({
name: 'config rs addon tests',
'read dimensions': function() {
// from mojito
var fixtures = libpath.join(__dirname, '../../../../fixtures/store');
var store = new MockRS({ root: fixtures });
store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
var have = store.config.getDimensions();
var want = readJSON(mojitoRoot, 'dimensions.json');
cmp(want, have);
// app-specified
fixtures = libpath.join(__dirname, '../../../../fixtures/ycb');
store = new MockRS({ root: fixtures });
store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
have = store.config.getDimensions();
want = readJSON(fixtures, 'dimensions.json');
cmp(want, have);
},
'find config resources': function() {
var fixtures = libpath.join(__dirname, '../../../../fixtures/store');
var store = new MockRS({ root: fixtures });
store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
// skip non-json files
var source = makeSource(fixtures, 'app', '.', 'server.js', true);
var have = store.findResourceVersionByConvention(source, null);
var want = undefined;
cmp(have, want, 'skip non-json files');
// include all json files in the app
source = makeSource(fixtures, 'app', '.', 'x.json', true);
have = store.findResourceVersionByConvention(source, null);
want = { type: 'config' };
cmp(have, want, 'include all json files in the app');
// ... explicitly including package.json
source = makeSource(fixtures, 'app', '.', 'package.json', true);
have = store.findResourceVersionByConvention(source, null);
want = { type: 'config' };
cmp(have, want, 'include package.json in the app');
// exclude all json files in a bundle
source = makeSource(fixtures, 'bundle', '.', 'x.json', true);
have = store.findResourceVersionByConvention(source, null);
want = undefined;
cmp(have, want, 'exclude all json files in a bundle');
// ... explicitly excluding package.json
source = makeSource(fixtures, 'bundle', '.', 'package.json', true);
have = store.findResourceVersionByConvention(source, null);
want = undefined;
cmp(have, want, 'exclude package.json in a bundle');
// include all json files in a mojit
source = makeSource(fixtures, 'mojit', '.', 'x.json', true);
have = store.findResourceVersionByConvention(source, 'foo');
want = { type: 'config' };
cmp(have, want, 'include all json files in a mojit');
// ... except for the 'shared' mojit
source = makeSource(fixtures, 'mojit', '.', 'x.json', true);
have = store.findResourceVersionByConvention(source, 'shared');
want = undefined;
cmp(have, want, 'exclude all json files in the "shared" mojit');
// ... explicitly including package.json
source = makeSource(fixtures, 'mojit', '.', 'package.json', true);
have = store.findResourceVersionByConvention(source, 'shared');
want = { type: 'config' };
cmp(have, want, 'include package.json in the "shared" mojit');
},
'parse found resource': function() {
var fixtures = libpath.join(__dirname, '../../../../fixtures/store');
var store = new MockRS({ root: fixtures });
store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
var source = makeSource(fixtures, 'app', '.', 'application.json', true);
var res = store.parseResourceVersion(source, 'config');
A.isNotUndefined(res);
cmp(res.source, source);
A.areSame('config', res.type);
A.areSame('common', res.affinity);
A.areSame('*', res.selector);
A.areSame('application', res.name);
A.areSame('config--application', res.id);
A.isUndefined(res.mojit);
source = makeSource(fixtures, 'mojit', '.', 'defaults.json', true);
res = store.parseResourceVersion(source, 'config', undefined, 'x');
A.isNotUndefined(res);
cmp(res.source, source);
A.areSame('config', res.type);
A.areSame('common', res.affinity);
A.areSame('*', res.selector);
A.areSame('defaults', res.name);
A.areSame('config--defaults', res.id);
A.areSame('x', res.mojit);
},
'read JSON files': function() {
var fixtures = libpath.join(__dirname, '../../../../fixtures/store');
var store = new MockRS({ root: fixtures });
store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
var path = libpath.join(fixtures, 'application.json');
var have = store.config.readConfigJSON(path);
var want = readJSON(fixtures, 'application.json');
cmp(have, want);
},
'read YCB files': function() {
var fixtures = libpath.join(__dirname, '../../../../fixtures/store');
var store = new MockRS({ root: fixtures });
store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
var path = libpath.join(fixtures, 'application.json');
var have = store.config.readConfigYCB(path, { runtime: 'server' });
var want = {
"mojitDirs": [
"soloMojit"
],
"staticHandling": {
"useRollups": true
},
"testKey1": "testVal1-server",
"testKey2": "testVal2",
"testKey3": "testVal3",
"specs": {
"test1": {
"type": "test_mojit_1"
}
},
"selector": "shelves",
"pathos": "portended"
};
cmp(have, want);
},
'malformed JSON for config file': function() {
var fixtures = libpath.join(__dirname, '../../../../fixtures/badfiles2');
var store = new MockRS({ root: fixtures });
store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
var path = libpath.join(fixtures, 'routes.json');
try {
store.config.readConfigJSON(path);
}
catch (err) {
A.areSame('Error parsing JSON file:', err.message.substr(0, 24));
}
},
'JSON config file not YCB': function() {
var fixtures = libpath.join(__dirname, '../../../../fixtures/badfiles3');
var store = new MockRS({ root: fixtures });
store.plug(Y.mojito.addons.rs.config, { appRoot: fixtures, mojitoRoot: mojitoRoot } );
var path = libpath.join(fixtures, 'routes.json');
var have = store.config.readConfigYCB(path, {});
var want = {};
cmp(have, want);
}
}));
YUITest.TestRunner.add(suite);
}, '0.0.1', {requires: ['base', 'oop', 'addon-rs-config', 'json-parse']});