2 let mocklib = global.mocklib,
3 fs = mocklib.require("fs");
6 readlink: function(path) {
7 mocklib.trace_call("fs", "readlink", { path });
12 stat: function(path) {
13 let file = sprintf("fs/stat~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')),
14 mock = mocklib.read_json_file(file);
16 if (!mock || mock != mock) {
17 mocklib.I("No stat result fixture defined for fs.stat() call on %s.", path);
18 mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file);
20 if (match(path, /\/$/))
21 mock = { type: "directory" };
23 mock = { type: "file" };
26 mocklib.trace_call("fs", "stat", { path });
31 unlink: function(path) {
32 printf("fs.unlink() path <%s>\n", path);
37 popen: (cmdline, mode) => {
38 let read = (!mode || index(mode, "r") != -1),
39 path = sprintf("fs/popen~%s.txt", replace(cmdline, /[^A-Za-z0-9_-]+/g, '_')),
40 mock = mocklib.read_data_file(path);
43 mocklib.I("No stdout fixture defined for fs.popen() command %s.", cmdline);
44 mocklib.I("Provide a mock output through the following text file:\n%s\n", path);
49 mocklib.trace_call("fs", "popen", { cmdline, mode });
52 read: function(amount) {
62 let i = index(mock, "\n");
63 i = (i > -1) ? i + 1 : mock.length;
64 rv = substr(mock, 0, i);
65 mock = substr(mock, i);
71 rv = substr(mock, 0, n);
72 mock = substr(mock, n);
88 open: (fpath, mode) => {
89 let read = (!mode || index(mode, "r") != -1 || index(mode, "+") != -1),
90 path = sprintf("fs/open~%s.txt", replace(fpath, /[^A-Za-z0-9_-]+/g, '_')),
91 mock = read ? mocklib.read_data_file(path) : null;
94 mocklib.I("No stdout fixture defined for fs.open() path %s.", fpath);
95 mocklib.I("Provide a mock output through the following text file:\n%s\n", path);
100 mocklib.trace_call("fs", "open", { path: fpath, mode });
103 read: function(amount) {
113 let i = index(mock, "\n");
114 i = (i > -1) ? i + 1 : mock.length;
115 rv = substr(mock, 0, i);
116 mock = substr(mock, i);
122 rv = substr(mock, 0, n);
123 mock = substr(mock, n);
130 write: function() {},
131 close: function() {},
140 let file = sprintf("fs/opendir~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')),
141 mock = mocklib.read_json_file(file),
144 if (!mock || mock != mock) {
145 mocklib.I("No stat result fixture defined for fs.opendir() call on %s.", path);
146 mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file);
151 mocklib.trace_call("fs", "opendir", { path });
155 return mock[index++];
166 close: function() {},
174 error: () => "Unspecified error"