Last active
August 29, 2015 13:56
-
-
Save sgade/9238197 to your computer and use it in GitHub Desktop.
mock-fs module test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// endsWidth, see http://stackoverflow.com/questions/280634/endswith-in-javascript | |
String.prototype.endsWith = function(suffix) { | |
return this.indexOf(suffix, this.length - suffix.length) !== -1; | |
}; | |
var fs = require('fs'), | |
path = require('path'), | |
mock = require('mock-fs'); | |
function showText(file) { | |
if ( file.endsWith(".txt") ) { | |
console.log(file, "=>", fs.readFileSync(file, { | |
encoding: 'utf-8' | |
})); | |
} | |
} | |
function readDir(dir) { | |
if ( !dir ) dir = __dirname; | |
var files = fs.readdirSync(dir); | |
console.log(dir, "=>", files); | |
for ( var i = 0; i < files.length; i++ ) { | |
if ( files[i] == "node_modules" ) continue; | |
var subDir = path.join(dir, files[i]) | |
var stat = fs.statSync(subDir); | |
if ( stat.isDirectory() ) { | |
readDir(subDir); | |
} else { | |
showText(subDir); | |
} | |
} | |
} | |
(function main(){ | |
console.log("REAL"); | |
readDir(); | |
console.log("MOCK"); | |
mock({ | |
'mock.txt': 'MOCK!', | |
'home/': { | |
'subfolder': { | |
'welcome.txt': 'Welcome to the mock-fs.' | |
} | |
} | |
}); | |
readDir(); | |
mock.restore(); | |
console.log("REAL"); | |
readDir(); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment