1 let mocklib = global.mocklib, 2 fs = mocklib.require("fs"); 3 4 return { 5 readlink: function(path) { 6 mocklib.trace_call("fs", "readlink", { path }); 7 8 return path; 9 }, 10 11 stat: function(path) { 12 let file = sprintf("fs/stat~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')), 13 mock = mocklib.read_json_file(file); 14 15 if (!mock || mock != mock) { 16 mocklib.I("No stat result fixture defined for fs.stat() call on %s.", path); 17 mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file); 18 19 if (match(path, /\/$/)) 20 mock = { type: "directory" }; 21 else 22 mock = { type: "file" }; 23 } 24 25 mocklib.trace_call("fs", "stat", { path }); 26 27 return mock; 28 }, 29 30 unlink: function(path) { 31 printf("fs.unlink() path <%s>\n", path); 32 33 return true; 34 }, 35 36 popen: (cmdline, mode) => { 37 let read = (!mode || index(mode, "r") != -1), 38 path = sprintf("fs/popen~%s.txt", replace(cmdline, /[^A-Za-z0-9_-]+/g, '_')), 39 mock = mocklib.read_data_file(path); 40 41 if (read && !mock) { 42 mocklib.I("No stdout fixture defined for fs.popen() command %s.", cmdline); 43 mocklib.I("Provide a mock output through the following text file:\n%s\n", path); 44 45 return null; 46 } 47 48 mocklib.trace_call("fs", "popen", { cmdline, mode }); 49 50 return { 51 read: function(amount) { 52 let rv; 53 54 switch (amount) { 55 case "all": 56 rv = mock; 57 mock = ""; 58 break; 59 60 case "line": 61 let i = index(mock, "\n"); 62 i = (i > -1) ? i + 1 : mock.length; 63 rv = substr(mock, 0, i); 64 mock = substr(mock, i); 65 break; 66 67 default: 68 let n = +amount; 69 n = (n > 0) ? n : 0; 70 rv = substr(mock, 0, n); 71 mock = substr(mock, n); 72 break; 73 } 74 75 return rv; 76 }, 77 78 write: function() {}, 79 close: function() {}, 80 81 error: function() { 82 return null; 83 } 84 }; 85 }, 86 87 open: (fpath, mode) => { 88 let read = (!mode || index(mode, "r") != -1 || index(mode, "+") != -1), 89 path = sprintf("fs/open~%s.txt", replace(fpath, /[^A-Za-z0-9_-]+/g, '_')), 90 mock = read ? mocklib.read_data_file(path) : null; 91 92 if (read && !mock) { 93 mocklib.I("No stdout fixture defined for fs.open() path %s.", fpath); 94 mocklib.I("Provide a mock output through the following text file:\n%s\n", path); 95 96 return null; 97 } 98 99 mocklib.trace_call("fs", "open", { path: fpath, mode }); 100 101 return { 102 read: function(amount) { 103 let rv; 104 105 switch (amount) { 106 case "all": 107 rv = mock; 108 mock = ""; 109 break; 110 111 case "line": 112 let i = index(mock, "\n"); 113 i = (i > -1) ? i + 1 : length(mock); 114 rv = substr(mock, 0, i); 115 mock = length(mock) ? substr(mock, i) : null; 116 break; 117 118 default: 119 let n = +amount; 120 n = (n > 0) ? n : 0; 121 rv = substr(mock, 0, n); 122 mock = substr(mock, n); 123 break; 124 } 125 126 return rv; 127 }, 128 129 write: function() {}, 130 close: function() {}, 131 132 error: function() { 133 return null; 134 } 135 }; 136 }, 137 138 readfile: (fpath, limit) => { 139 let path = sprintf("fs/open~%s.txt", replace(fpath, /[^A-Za-z0-9_-]+/g, '_')), 140 mock = mocklib.read_data_file(path); 141 142 if (!mock) { 143 mocklib.I("No stdout fixture defined for fs.readfile() path %s.", fpath); 144 mocklib.I("Provide a mock output through the following text file:\n%s\n", path); 145 146 return null; 147 } 148 149 mocklib.trace_call("fs", "readfile", { path: fpath, limit }); 150 151 return limit ? substr(mock, 0, limit) : mock; 152 }, 153 154 access: (fpath) => { 155 let path = sprintf("fs/open~%s.txt", replace(fpath, /[^A-Za-z0-9_-]+/g, '_')), 156 mock = mocklib.read_data_file(path); 157 158 if (!mock) { 159 mocklib.I("No stdout fixture defined for fs.access() path %s.", fpath); 160 mocklib.I("Provide a mock output through the following text file:\n%s\n", path); 161 162 return false; 163 } 164 165 return true; 166 }, 167 168 opendir: (path) => { 169 let file = sprintf("fs/opendir~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')), 170 mock = mocklib.read_json_file(file), 171 index = 0; 172 173 if (!mock || mock != mock) { 174 mocklib.I("No stat result fixture defined for fs.opendir() call on %s.", path); 175 mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file); 176 177 mock = []; 178 } 179 180 mocklib.trace_call("fs", "opendir", { path }); 181 182 return { 183 read: function() { 184 return mock[index++]; 185 }, 186 187 tell: function() { 188 return index; 189 }, 190 191 seek: function(i) { 192 index = i; 193 }, 194 195 close: function() {}, 196 197 error: function() { 198 return null; 199 } 200 }; 201 }, 202 203 glob: (pattern) => { 204 let file = sprintf("fs/glob~%s.json", replace(pattern, /[^A-Za-z0-9_-]+/g, '_')), 205 mock = mocklib.read_json_file(file), 206 index = 0; 207 208 if (!mock || mock != mock) { 209 mocklib.I("No stat result fixture defined for fs.glob() call on %s.", pattern); 210 mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file); 211 212 mock = []; 213 } 214 215 mocklib.trace_call("fs", "glob", { pattern }); 216 217 return mock; 218 }, 219 220 lsdir: (path) => { 221 let file = sprintf("fs/opendir~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')), 222 mock = mocklib.read_json_file(file), 223 index = 0; 224 225 if (!mock || mock != mock) { 226 mocklib.I("No stat result fixture defined for fs.lsdir() call on %s.", path); 227 mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file); 228 229 mock = []; 230 } 231 232 mocklib.trace_call("fs", "lsdir", { path }); 233 234 return mock; 235 }, 236 237 error: () => "Unspecified error" 238 };
This page was automatically generated by LXR 0.3.1. • OpenWrt