-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #391 from cosmos/fabo/fs-mock
Fabo/fs mock
- Loading branch information
Showing
8 changed files
with
201 additions
and
41 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ npm-debug.log | |
npm-debug.log.* | ||
thumbs.db | ||
!.gitkeep | ||
.vscode |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
const { Writable } = require('stream') | ||
const { normalize, sep } = require('path') | ||
|
||
/* | ||
* this mock implements every function (all used in this project for now) in fs-extra so that the file system is just represented by a json file holding file content as strings | ||
*/ | ||
export default function mockFsExtra (fileSystem = {}) { | ||
const fsExtraMock = { | ||
copy: (from, to) => { | ||
let {file} = get(from, fsExtraMock.fs) | ||
if (file === null) { | ||
throwENOENT(from) | ||
} | ||
create(to, fsExtraMock.fs, file) | ||
}, | ||
ensureFile: (path) => { | ||
let {file} = get(path, fsExtraMock.fs) | ||
if (file === null) { | ||
create(path, fsExtraMock.fs) | ||
} | ||
}, | ||
ensureDir: (path) => { | ||
let {file} = get(path, fsExtraMock.fs) | ||
if (file === null) { | ||
create(path, fsExtraMock.fs) | ||
} | ||
}, | ||
createWriteStream: () => new Writable(), | ||
// for simplicity we say if there is a file we can access it | ||
access: (path) => { | ||
let {file} = get(path, fsExtraMock.fs) | ||
if (file === null) { | ||
throwENOENT(path) | ||
return false | ||
} | ||
return !!get(path, fsExtraMock.fs).file | ||
}, | ||
remove: (path) => { | ||
let {parent, name} = get(path, fsExtraMock.fs) | ||
delete parent[name] | ||
}, | ||
readFile: (path) => { | ||
let {file} = get(path, fsExtraMock.fs) | ||
if (!file) { | ||
throwENOENT(path) | ||
} | ||
return file | ||
}, | ||
writeFile: (path, file) => create(path, fsExtraMock.fs, file), | ||
pathExists: (path) => !!get(path, fsExtraMock.fs).file, | ||
exists: (path) => { | ||
let {file} = get(path, fsExtraMock.fs) | ||
return file !== null | ||
} | ||
} | ||
|
||
// all methods are synchronous in tests | ||
Object.keys(fsExtraMock).forEach(key => { | ||
fsExtraMock[key + 'Sync'] = fsExtraMock[key] | ||
}) | ||
|
||
// for debugging | ||
fsExtraMock.MOCKED = true | ||
fsExtraMock.fs = fileSystem | ||
|
||
// strip long content from fs | ||
function fsToString (fs) { | ||
// clone | ||
fs = JSON.parse(JSON.stringify(fs)) | ||
Object.keys(fs).forEach(key => { | ||
if (typeof fs[key] === 'object') { | ||
fs[key] = fsToString(fs[key]) | ||
} else { | ||
fs[key] = '#CONTENT#' | ||
} | ||
}) | ||
return fs | ||
} | ||
|
||
function throwENOENT (path) { | ||
let error = new Error('Path ' + path + ' doesnt exist.\nFS:' + JSON.stringify(fsToString(fileSystem), null, 2)) | ||
error.code = 'ENOENT' | ||
throw error | ||
} | ||
|
||
function get (path, fs) { | ||
path = normalize(path) | ||
let pathArr = path.split(sep).filter(x => x !== '') | ||
let current = pathArr.shift() | ||
|
||
if (fs[current]) { | ||
if (pathArr.length === 0) { | ||
return { | ||
file: fs[current], | ||
name: current, | ||
parent: fs | ||
} | ||
} | ||
return get(pathArr.join('/'), fs[current]) | ||
} | ||
return { | ||
file: null, | ||
name: current, | ||
parent: fs | ||
} | ||
} | ||
|
||
function create (path, fs, file = {}) { | ||
path = normalize(path) | ||
let pathArr = path.split(sep).filter(x => x !== '') | ||
let current = pathArr.shift() | ||
|
||
if (!fs[current]) { | ||
fs[current] = {} | ||
} | ||
if (pathArr.length === 0) { | ||
if (typeof file === 'object') { | ||
// clone object | ||
fs[current] = JSON.parse(JSON.stringify(file)) | ||
} else { | ||
fs[current] = file | ||
} | ||
return true | ||
} | ||
return create(pathArr.join('/'), fs[current], file) | ||
} | ||
|
||
return fsExtraMock | ||
} |
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
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
Oops, something went wrong.