MCPcopy
hub / github.com/GitbookIO/gitbook / createMockFS

Function createMockFS

lib/fs/mock.js:14–93  ·  view source on GitHub ↗

Create a fake filesystem for unit testing GitBook. @param {Map }

(files)

Source from the content-addressed store, hash-verified

12 @param {Map<String:String|Map>}
13*/
14function createMockFS(files) {
15 files = Immutable.fromJS(files);
16 var mtime = new Date();
17
18 function getFile(filePath) {
19 var parts = path.normalize(filePath).split(path.sep);
20 return parts.reduce(function(list, part, i) {
21 if (!list) return null;
22
23 var file;
24
25 if (!part || part === '.') file = list;
26 else file = list.get(part);
27
28 if (!file) return null;
29
30 if (is.string(file)) {
31 if (i === (parts.length - 1)) return file;
32 else return null;
33 }
34
35 return file;
36 }, files);
37 }
38
39 function fsExists(filePath) {
40 return Boolean(getFile(filePath) !== null);
41 }
42
43 function fsReadFile(filePath) {
44 var file = getFile(filePath);
45 if (!is.string(file)) {
46 throw error.FileNotFoundError({
47 filename: filePath
48 });
49 }
50
51 return new Buffer(file, 'utf8');
52 }
53
54 function fsStatFile(filePath) {
55 var file = getFile(filePath);
56 if (!file) {
57 throw error.FileNotFoundError({
58 filename: filePath
59 });
60 }
61
62 return {
63 mtime: mtime
64 };
65 }
66
67 function fsReadDir(filePath) {
68 var dir = getFile(filePath);
69 if (!dir || is.string(dir)) {
70 throw error.FileNotFoundError({
71 filename: filePath

Callers 9

generateMockFunction · 0.85
createMockOutputFunction · 0.85
listAssets.jsFile · 0.85
parseSummary.jsFile · 0.85
parseGlossary.jsFile · 0.85
parseBook.jsFile · 0.85
parseReadme.jsFile · 0.85
parseIgnore.jsFile · 0.85
mock.jsFile · 0.85

Calls

no outgoing calls

Tested by 2

generateMockFunction · 0.68
createMockOutputFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…