From 707195e4aa5cd1dd9279c2431685870d53d1589b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Carruitero?= Date: Thu, 18 Jun 2020 19:46:44 -0500 Subject: [PATCH] fix(make_it_modular): use proxyquire for mock fs to avoid fail verification when use destructuring assignment --- exercises/make_it_modular/verify.js | 21 ++++++++++----------- package.json | 1 + test/make_it_modular/module_valid_03.js | 13 +++++++++++++ test/make_it_modular/valid_03.js | 9 +++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 test/make_it_modular/module_valid_03.js create mode 100644 test/make_it_modular/valid_03.js diff --git a/exercises/make_it_modular/verify.js b/exercises/make_it_modular/verify.js index ec7aaf33..091d8644 100644 --- a/exercises/make_it_modular/verify.js +++ b/exercises/make_it_modular/verify.js @@ -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 @@ -48,12 +48,14 @@ 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')) @@ -61,7 +63,7 @@ function validateModule (modFile, callback) { callbackUsed = false try { - mod('/foo/bar/', 'wheee', function (err) { + mockMod('/foo/bar/', 'wheee', function (err) { if (err !== error) { return noerr() } @@ -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 diff --git a/package.json b/package.json index 1addd8c8..caf62f0a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/make_it_modular/module_valid_03.js b/test/make_it_modular/module_valid_03.js new file mode 100644 index 00000000..de1d42a4 --- /dev/null +++ b/test/make_it_modular/module_valid_03.js @@ -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) + }) +} diff --git a/test/make_it_modular/valid_03.js b/test/make_it_modular/valid_03.js new file mode 100644 index 00000000..b1eea7bd --- /dev/null +++ b/test/make_it_modular/valid_03.js @@ -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) + }) +})