-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
verify.js
218 lines (173 loc) · 6.15 KB
/
verify.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
const fs = require('fs')
, path = require('path')
, util = require('util')
, files = require('../filtered_ls/file-list')
, chalk = require('chalk')
function validateModule (modFile, callback) {
var exercise = this
, dir = this._testDir
, mod
, error = new Error('testing')
, returned = false
, _callback = callback
, callbackUsed
try {
mod = require(modFile)
} catch (e) {
exercise.emit('fail', 'Error loading module: ' + e.message)
return callback(null, false)
}
callback = function () {
returned = true
_callback.apply(this, arguments)
}
function modFileError (txt) {
exercise.emit('fail', 'Your additional module file [' + path.basename(modFile) + '] ' + txt)
callback (null, false)
}
//---- Check that our module file is `module.exports = function () {}`
if (typeof mod != 'function') {
return modFileError(
'does not export a ' + chalk.bold('single function') + '.'
+ 'You must use the `module.exports = function () {}` pattern.'
)
} else {
exercise.emit('pass', 'Additional module file exports a single function')
}
//---- Check that the function exported takes 3 arguments
if (mod.length < 3) {
return modFileError(
'exports a function that takes fewer than ' + chalk.bold('three') + ' arguments.'
+ 'You must accept a directory, a filter and a ' + chalk.bold('callback') + '.'
)
} else {
exercise.emit('pass', 'Additional module file exports a function that takes ' + mod.length + ' arguments')
}
//---- Mock `fs.readdir` and check that an error bubbles back up through the cb
fs.$readdir = fs.readdir
fs.readdir = function (dir, callback) {
callback(error)
}
function noerr () {
modFileError(
'does not appear to pass back an error received from `fs.readdir()`'
+ 'Use the following idiomatic Node.js pattern inside your callback to `fs.readdir()`:'
+ '\n\tif (err)\n\t return callback(err)'
)
}
callbackUsed = false
try {
mod('/foo/bar/', 'wheee', function (err) {
if (err !== error)
return noerr()
callbackUsed = true
})
} catch (e) {
noerr()
}
if (callbackUsed)
exercise.emit('pass', 'Additional module file handles errors properly')
//---- Check whether the callback is used at all
setTimeout(function () {
if (returned)
return
if (!callbackUsed)
return modFileError('did not call the callback argument after an error from fs.readdir()')
exercise.emit('pass', 'Additional module file handles callback argument')
// replace the mock readdir
fs.readdir = fs.$readdir
callbackUsed = false
try {
mod(dir, 'md', function (err, list) {
if (err) {
return modFileError(
'returned an error on its callback:'
+ '\n\t' + util.inspect(err)
)
}
//---- Check that we got the correct number of elements
if (arguments.length < 2) {
return modFileError(
'did not return two arguments on the callback function (expected `null` and an Array of filenames)'
)
}
exercise.emit('pass', 'Additional module file returned two arguments on the callback function')
//---- Check that we got an Array as the second argument
if (!Array.isArray(list)) {
return modFileError(
'did not return an Array object as the second argument of the callback'
)
}
exercise.emit('pass', 'Additional module file returned Array as second argument of the callback')
//---- Check that we got the expected number of elements in the Array
var exp = files.filter(function (f) { return (/\.md$/).test(f) })
, i
if (exp.length !== list.length) {
return modFileError(
'did not return an Array with the correct number of elements as the second argument of the callback'
)
}
exercise.emit('pass', 'Additional module file returned correct number of elements as the second argument of the callback')
callbackUsed = true
//---- Check that the elements are exactly the same as expected (ignoring order)
exp.sort()
list.sort()
for (i = 0; i < exp.length; i++) {
if (list[i] !== exp[i]) {
return modFileError(
'did not return the correct list of files as the second argument of the callback'
)
}
}
exercise.emit('pass', 'Additional module file returned correct list of files as the second argument of the callback')
//WIN!!
callback()
})
} catch (e) {
return modFileError(
'threw an error:'
+ '\n\t' + util.inspect(e)
)
}
setTimeout(function () {
if (returned)
return
if (!callbackUsed)
return modFileError('did not call the callback argument')
}, 300)
}, 300)
}
// find any modules that are required by the submission program
function requires (exercise) {
// rule out these 4 things
var main = path.resolve(process.cwd(), exercise.args[0])
, exec = require.resolve('workshopper-wrappedexec/exec-wrap')
, wrap1 = require.resolve('../my_first_io/wrap')
, wrap2 = require.resolve('./wrap-requires')
return exercise.wrapData.requires.filter(function (m) {
return m != main && m != exec && m != wrap1 && m != wrap2
})
}
function verifyModuleUsed (callback) {
var required = requires(this)
if (required.length === 0) {
this.emit('fail', 'Did not use an additional module file, you must require() a module to help solve this exercise')
return callback(null, false)
}
validateModule.call(this, required[0], callback)
}
function verify (callback) {
var usedSync = false
, usedAsync = false
Object.keys(this.wrapData.fsCalls).forEach(function (m) {
if (/Sync$/.test(m)) {
usedSync = true
this.emit('fail', 'Used synchronous method: fs.' + m + '()')
} else {
usedAsync = true
this.emit('pass', 'Used asynchronous method: fs.' + m + '()')
}
}.bind(this))
verifyModuleUsed.call(this, callback)
}
module.exports = verify