-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
274 lines (235 loc) · 8.03 KB
/
index.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
/**
* Collection of test assertions helpers
*
* It mixes in the native node.js `assert` module. So you can just use this module as a
* drop-in replacement.
*
* @mixes nodejs/assert
* @example
* var assert = require('yeoman-assert');
*/
'use strict';
const fs = require('fs');
const pathExists = fs.existsSync;
const isFunction = obj => typeof obj === 'function';
const isObject = obj => typeof obj === 'object' && obj !== null && obj !== undefined;
function extractMethods(methods) {
return Array.isArray(methods) ? methods : Object.keys(methods).filter(method => isFunction(methods[method]));
}
function convertArgs(args) {
if (args.length > 1) {
return [Array.from(args)];
}
const arg = args[0];
return Array.isArray(arg) ? arg : [arg];
}
function readFile(filename, json) {
const file = fs.readFileSync(filename, 'utf8');
return json ? JSON.parse(file) : file;
}
// Extend the native assert module
const assert = module.exports = require('assert');
/**
* Assert that a file exists
* @param {String} path - path to a file
* @example
* assert.file('templates/user.hbs');
*
* @also
*
* Assert that each files in the array exists
* @param {Array} paths - an array of paths to files
* @example
* assert.file(['templates/user.hbs', 'templates/user/edit.hbs']);
*/
assert.file = function () {
convertArgs(arguments).forEach(file => {
const here = pathExists(file);
assert.ok(here, `${file}, no such file or directory`);
});
};
/**
* Assert that a file doesn't exist
* @param {String} file - path to a file
* @example
* assert.noFile('templates/user.hbs');
*
* @also
*
* Assert that each of an array of files doesn't exist
* @param {Array} pairs - an array of paths to files
* @example
* assert.noFile(['templates/user.hbs', 'templates/user/edit.hbs']);
*/
assert.noFile = function () {
convertArgs(arguments).forEach(file => {
const here = pathExists(file);
assert.ok(!here, `${file} exists`);
});
};
/**
* Assert that a file's content matches a regex or string
* @param {String} file - path to a file
* @param {Regex|String} reg - regex / string that will be used to search the file
* @example
* assert.fileContent('models/user.js', /App\.User = DS\.Model\.extend/);
* assert.fileContent('models/user.js', 'App.User = DS.Model.extend');
*
* @also
*
* Assert that each file in an array of file-regex pairs matches its corresponding regex
* @param {Array} pairs - an array of arrays, where each subarray is a [String, RegExp] pair
* @example
* var arg = [
* [ 'models/user.js', /App\.User = DS\.Model\.extend/ ],
* [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ]
* ]
* assert.fileContent(arg);
*/
assert.fileContent = function () {
convertArgs(arguments).forEach(pair => {
const file = pair[0];
const regex = pair[1];
assert.file(file);
const body = readFile(file);
let match = false;
if (typeof regex === 'string') {
match = body.indexOf(regex) !== -1;
} else {
match = regex.test(body);
}
assert(match, `${file} did not match '${regex}'. Contained:\n\n${body}`);
});
};
/**
* Assert that a file's content is the same as the given string
* @param {String} file - path to a file
* @param {String} expectedContent - the expected content of the file
* @example
* assert.equalsFileContent(
* 'data.js',
* 'const greeting = "Hello";\nexport default { greeting }'
* );
*
* @also
*
* Assert that each file in an array of file-string pairs equals its corresponding string
* @param {Array} pairs - an array of arrays, where each subarray is a [String, String] pair
* @example
* assert.equalsFileContent([
* ['data.js', 'const greeting = "Hello";\nexport default { greeting }'],
* ['user.js', 'export default {\n name: 'Coleman',\n age: 0\n}']
* ]);
*/
assert.equalsFileContent = function () {
convertArgs(arguments).forEach(pair => {
const file = pair[0];
const expectedContent = pair[1];
assert.file(file);
this.textEqual(readFile(file), expectedContent);
});
};
/**
* Assert that a file's content does not match a regex / string
* @param {String} file - path to a file
* @param {Regex|String} reg - regex / string that will be used to search the file
* @example
* assert.noFileContent('models/user.js', /App\.User = DS\.Model\.extend/);
* assert.noFileContent('models/user.js', 'App.User = DS.Model.extend');
*
* @also
*
* Assert that each file in an array of file-regex pairs does not match its corresponding regex
* @param {Array} pairs - an array of arrays, where each subarray is a [String, RegExp] pair
* var arg = [
* [ 'models/user.js', /App\.User \ DS\.Model\.extend/ ],
* [ 'controllers/user.js', /App\.UserController = Ember\.ObjectController\.extend/ ]
* ]
* assert.noFileContent(arg);
*/
assert.noFileContent = function () {
convertArgs(arguments).forEach(pair => {
const file = pair[0];
const regex = pair[1];
assert.file(file);
const body = readFile(file);
if (typeof regex === 'string') {
assert.ok(body.indexOf(regex) === -1, `${file} matched '${regex}'.`);
return;
}
assert.ok(!regex.test(body), `${file} matched '${regex}'.`);
});
};
/**
* Assert that two strings are equal after standardization of newlines
* @param {String} value - a string
* @param {String} expected - the expected value of the string
* @example
* assert.textEqual('I have a yellow cat', 'I have a yellow cat');
*/
assert.textEqual = (value, expected) => {
const eol = str => str.replace(/\r\n/g, '\n');
assert.equal(eol(value), eol(expected));
};
/**
* Assert an Object implements an interface
* @param {Object} subject - subject implementing the façade
* @param {Object|Array} methods - a façace, hash or array of keys to be implemented
*/
assert.implement = (subject, methods) => {
const pass = extractMethods(methods).filter(method => !isFunction(subject[method]));
assert.ok(pass.length === 0, `expected object to implement methods named: ${pass.join(', ')}`);
};
/**
* Assert an Object doesn't implements any method of an interface
* @param {Object} subject - subject not implementing the methods
* @param {Object|Array} methods - hash or array of method names to be implemented
*/
assert.notImplement = (subject, methods) => {
const pass = extractMethods(methods).filter(method => isFunction(subject[method]));
assert.ok(pass.length === 0, `expected object to not implement any methods named: ${pass.join(', ')}`);
};
/**
* Assert an object contains the provided keys
* @param {Object} obj Object that should match the given pattern
* @param {Object} content An object of key/values the object should contains
*/
assert.objectContent = (obj, content) => {
Object.keys(content).forEach(key => {
if (isObject(content[key])) {
assert.objectContent(obj[key], content[key]);
return;
}
assert.equal(obj[key], content[key]);
});
};
/**
* Assert an object does not contain the provided keys
* @param {Object} obj Object that should not match the given pattern
* @param {Object} content An object of key/values the object should not contain
*/
assert.noObjectContent = (obj, content) => {
Object.keys(content).forEach(key => {
if (isObject(content[key])) {
assert.noObjectContent(obj[key], content[key]);
return;
}
assert.notEqual(obj[key], content[key]);
});
};
/**
* Assert a JSON file contains the provided keys
* @param {String} filename
* @param {Object} content An object of key/values the file should contains
*/
assert.JSONFileContent = assert.jsonFileContent = (filename, content) => {
assert.objectContent(readFile(filename, true), content);
};
/**
* Assert a JSON file does not contain the provided keys
* @param {String} filename
* @param {Object} content An object of key/values the file should not contain
*/
assert.noJSONFileContent = assert.noJsonFileContent = (filename, content) => {
assert.noObjectContent(readFile(filename, true), content);
};