-
Notifications
You must be signed in to change notification settings - Fork 98
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
Fabo/fs mock #391
Merged
Merged
Fabo/fs mock #391
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6ee6c1a
not working
faboweb c070cb2
Merge branch 'develop' into fabo/fs-mock
faboweb c00bd32
fixed node.js tests
faboweb 506b719
fixed all tests for mock fs
faboweb 43761d5
added comments
faboweb bfac60c
removed annoying console log
faboweb f7bf096
added fs mock description
faboweb 9dccf64
fixed lcd connector tests
faboweb 8ddaf82
ignore vscode folder
faboweb dfcb91f
Merge branch 'develop' into fabo/fs-mock
faboweb bc25dd1
Merge branch 'develop' into fabo/fs-mock
nylira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
// Verwendet IntelliSense zum Ermitteln möglicher Attribute. | ||
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen. | ||
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Jest Tests", | ||
"program": "${workspaceRoot}\\node_modules\\jest\\bin\\jest.js", | ||
"args": [ | ||
"-i" | ||
], | ||
"internalConsoleOptions": "openOnSessionStart" | ||
} | ||
] | ||
} |
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?