1 let mocklib = global.mocklib,
2 fs = mocklib.require("fs");
5 readlink: function(path) {
6 mocklib.trace_call("fs", "readlink", { path });
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);
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);
19 if (match(path, /\/$/))
20 mock = { type: "directory" };
22 mock = { type: "file" };
25 mocklib.trace_call("fs", "stat", { path });
30 unlink: function(path) {
31 printf("fs.unlink() path <%s>\n", path);
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);
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);
48 mocklib.trace_call("fs", "popen", { cmdline, mode });
51 read: function(amount) {
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);
70 rv = substr(mock, 0, n);
71 mock = substr(mock, n);
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;
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);
99 mocklib.trace_call("fs", "open", { path: fpath, mode });
102 read: function(amount) {
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;
121 rv = substr(mock, 0, n);
122 mock = substr(mock, n);
129 write: function() {},
130 close: function() {},
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);
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);
149 mocklib.trace_call("fs", "readfile", { path: fpath, limit });
151 return limit ? substr(mock, 0, limit) : mock;
155 let path = sprintf("fs/open~%s.txt", replace(fpath, /[^A-Za-z0-9_-]+/g, '_')),
156 mock = mocklib.read_data_file(path);
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);
169 let file = sprintf("fs/opendir~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')),
170 mock = mocklib.read_json_file(file),
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);
180 mocklib.trace_call("fs", "opendir", { path });
184 return mock[index++];
195 close: function() {},
204 let file = sprintf("fs/glob~%s.json", replace(pattern, /[^A-Za-z0-9_-]+/g, '_')),
205 mock = mocklib.read_json_file(file),
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);
215 mocklib.trace_call("fs", "glob", { pattern });
221 let file = sprintf("fs/opendir~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')),
222 mock = mocklib.read_json_file(file),
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);
232 mocklib.trace_call("fs", "lsdir", { path });
237 error: () => "Unspecified error"