-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
verify.js
221 lines (174 loc) · 5.94 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
219
220
221
const fs = require('fs')
const path = require('path')
const util = require('util')
const files = require('../filtered_ls/file-list')
function validateModule (modFile, callback) {
const exercise = this
const __ = this.__
const __n = this.__n
const dir = this._testDir
let mod
const error = new Error('testing')
let returned = false
const _callback = callback
let callbackUsed
try {
mod = require(modFile)
} catch (e) {
exercise.emit('fail', __('fail.loadError', { message: e.message, path: path.basename(modFile) }))
return callback(null, false)
}
callback = function () {
returned = true
_callback.apply(this, arguments)
}
function modFileError (txt) {
exercise.emit('fail', __('fail.mod._base', { path: path.basename(modFile), message: txt }))
callback(null, false)
}
// ---- Check that our module file is `module.exports = function () {}`
if (typeof mod !== 'function') {
return modFileError(__('fail.mod.no_export'))
}
exercise.emit('pass', __('pass.singleFunction'))
// ---- Check that the function exported takes 3 arguments
if (mod.length < 3) {
return modFileError(__n('fail.mod.arguments', mod.length))
}
exercise.emit('pass', __n('pass.arguments', mod.length))
// ---- Mock `fs.readdir` and check that an error bubbles back up through the cb
fs.$readdir = fs.readdir
fs.readdir = function (dir, optionalEncoding, callback) {
callback = callback || optionalEncoding
callback(error)
}
function noerr () {
return modFileError(__('fail.mod.missing_error'))
}
callbackUsed = false
try {
mod('/foo/bar/', 'wheee', function (err) {
if (err !== error) {
return noerr()
}
callbackUsed = true
})
} catch (e) {
noerr()
}
if (callbackUsed) {
exercise.emit('pass', __('pass.error'))
}
// ---- Check whether the callback is used at all
setTimeout(function () {
if (returned) {
return
}
if (!callbackUsed) {
return modFileError(__('fail.mod.missing_callback'))
}
exercise.emit('pass', __('pass.callback'))
// replace the mock readdir
fs.readdir = fs.$readdir
callbackUsed = false
// a special follow-up for when we detect they are not passing back
// the right number of elements--the most common case is that they are
// sending in the '.'-prefixed value
function checkWithDot () {
mod(dir, '.dat', function (err, list) {
if (err) {
return modFileError(__('fail.mod.callback_error', { error: util.inspect(err) }))
}
const notexp = files.filter(function (f) { return (/\.dat$/).test(f) })
if (list.length === notexp.length) {
return modFileError(__('fail.mod.dont_use_dot'))
}
exercise.emit('pass', __('pass.dont_use_dot'))
// else ... no idea what went wrong here!
modFileError(__('fail.mod.array_wrong_size'))
})
}
try {
mod(dir, 'md', function (err, list) {
if (err) {
return modFileError(__('fail.mod.callback_error', { error: util.inspect(err) }))
}
// ---- Check that we got the correct number of elements
if (arguments.length < 2) {
return modFileError(__n('fail.mod.callback_arguments', arguments.length))
}
exercise.emit('pass', __('pass.callback_arguments'))
// ---- Check that we got an Array as the second argument
if (!Array.isArray(list)) {
return modFileError(__('fail.mod.missing_array_argument'))
}
exercise.emit('pass', __('pass.array_argument'))
const exp = files.filter(function (f) { return (/\.md$/).test(f) })
const noDotExp = files.filter(function (f) { return (/md$/).test(f) })
let i
// ---- Check for `ext` instead of `.ext`
if (noDotExp.length === list.length) {
return modFileError(__('fail.mod.dotExt'))
}
// ---- Check that we got the expected number of elements in the Array
if (exp.length !== list.length) {
return checkWithDot()
}
exercise.emit('pass', __('pass.dont_use_dot'))
exercise.emit('pass', __('pass.array_size'))
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(__('fail.mod.array_comparison'))
}
}
exercise.emit('pass', __('pass.final'))
// WIN!!
callback()
})
} catch (e) {
return modFileError(__('fail.mod.unexpected', { error: util.inspect(e) }))
}
setTimeout(function () {
if (returned) {
return
}
if (!callbackUsed) {
return modFileError(__('fail.mod.timeout'))
}
}, 300)
}, 300)
}
// find any modules that are required by the submission program
function requires (exercise) {
// rule out these 4 things
const main = path.resolve(process.cwd(), exercise.args[0])
const exec = require.resolve('workshopper-wrappedexec/exec-wrap')
const wrap1 = require.resolve('../my_first_io/wrap')
const wrap2 = require.resolve('./wrap-requires')
return exercise.wrapData.requires.filter(function (m) {
return m !== main && m !== exec && m !== wrap1 && m !== wrap2
})
}
function verifyModuleUsed (callback) {
const required = requires(this)
if (required.length === 0) {
this.emit('fail', this.__('fail.missing_module'))
return callback(null, false)
}
validateModule.call(this, required[required.length - 1], callback)
}
function verify (callback) {
Object.keys(this.wrapData.fsCalls).forEach(function (m) {
if (/Sync$/.test(m)) {
this.emit('fail', this.__('fail.sync', { method: 'fs.' + m + '()' }))
} else {
this.emit('pass', this.__('pass.async', { method: 'fs.' + m + '()' }))
}
}.bind(this))
verifyModuleUsed.call(this, callback)
}
module.exports = verify