Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(make_it_modular): use proxyquire for mock fs #710

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions exercises/make_it_modular/verify.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('fs')
const path = require('path')
const util = require('util')
const files = require('../filtered_ls/file-list')
const proxyquire = require('proxyquire')

function validateModule (modFile, callback) {
const exercise = this
Expand Down Expand Up @@ -48,20 +48,22 @@ function validateModule (modFile, callback) {
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)
}
const mockMod = proxyquire(modFile, {
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) {
mockMod('/foo/bar/', 'wheee', function (err) {
if (err !== error) {
return noerr()
}
Expand Down Expand Up @@ -89,9 +91,6 @@ function validateModule (modFile, 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
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"concat-stream": "^2.0.0",
"duplexer": "^0.1.1",
"hyperquest": "^2.1.3",
"proxyquire": "^2.1.3",
"rimraf": "^3.0.0",
"through2": "^3.0.1",
"through2-map": "^3.0.0",
Expand Down
13 changes: 13 additions & 0 deletions test/make_it_modular/module_valid_03.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { readdir } = require('fs')
const { extname } = require('path')

module.exports = (directory, extension, callback) => {
const isExtension = (file) => extname(file) === `.${extension}`

readdir(directory, (err, files) => {
if (err) return callback(err)

const matchingFiles = files.filter(isExtension)
callback(null, matchingFiles)
})
}
9 changes: 9 additions & 0 deletions test/make_it_modular/valid_03.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mod = require('./module_valid_03')

mod(process.argv[2], process.argv[3], (error, list) => {
if (error) return console.log(error)

list.forEach((entry) => {
console.log(entry)
})
})