1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
let mocklib = global.mocklib,
fs = mocklib.require("fs");
return {
readlink: function(path) {
mocklib.trace_call("fs", "readlink", { path });
return path + "-link";
},
stat: function(path) {
let file = sprintf("fs/stat~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')),
mock = mocklib.read_json_file(file);
if (!mock || mock != mock) {
mocklib.I("No stat result fixture defined for fs.stat() call on %s.", path);
mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file);
if (match(path, /\/$/))
mock = { type: "directory" };
else
mock = { type: "file" };
}
mocklib.trace_call("fs", "stat", { path });
return mock;
},
unlink: function(path) {
printf("fs.unlink() path <%s>\n", path);
return true;
},
popen: (cmdline, mode) => {
let read = (!mode || index(mode, "r") != -1),
path = sprintf("fs/popen~%s.txt", replace(cmdline, /[^A-Za-z0-9_-]+/g, '_')),
mock = mocklib.read_data_file(path);
if (read && !mock) {
mocklib.I("No stdout fixture defined for fs.popen() command %s.", cmdline);
mocklib.I("Provide a mock output through the following text file:\n%s\n", path);
return null;
}
mocklib.trace_call("fs", "popen", { cmdline, mode });
return {
read: function(amount) {
let rv;
switch (amount) {
case "all":
rv = mock;
mock = "";
break;
case "line":
let i = index(mock, "\n");
i = (i > -1) ? i + 1 : mock.length;
rv = substr(mock, 0, i);
mock = substr(mock, i);
break;
default:
let n = +amount;
n = (n > 0) ? n : 0;
rv = substr(mock, 0, n);
mock = substr(mock, n);
break;
}
return rv;
},
write: function() {},
close: function() {},
error: function() {
return null;
}
};
},
open: (fpath, mode) => {
let read = (!mode || index(mode, "r") != -1 || index(mode, "+") != -1),
path = sprintf("fs/open~%s.txt", replace(fpath, /[^A-Za-z0-9_-]+/g, '_')),
mock = read ? mocklib.read_data_file(path) : null;
if (read && !mock) {
mocklib.I("No stdout fixture defined for fs.open() path %s.", fpath);
mocklib.I("Provide a mock output through the following text file:\n%s\n", path);
return null;
}
mocklib.trace_call("fs", "open", { path: fpath, mode });
return {
read: function(amount) {
let rv;
switch (amount) {
case "all":
rv = mock;
mock = "";
break;
case "line":
let i = index(mock, "\n");
i = (i > -1) ? i + 1 : mock.length;
rv = substr(mock, 0, i);
mock = substr(mock, i);
break;
default:
let n = +amount;
n = (n > 0) ? n : 0;
rv = substr(mock, 0, n);
mock = substr(mock, n);
break;
}
return rv;
},
write: function() {},
close: function() {},
error: function() {
return null;
}
};
},
opendir: (path) => {
let file = sprintf("fs/opendir~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')),
mock = mocklib.read_json_file(file),
index = 0;
if (!mock || mock != mock) {
mocklib.I("No stat result fixture defined for fs.opendir() call on %s.", path);
mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file);
mock = [];
}
mocklib.trace_call("fs", "opendir", { path });
return {
read: function() {
return mock[index++];
},
tell: function() {
return index;
},
seek: function(i) {
index = i;
},
close: function() {},
error: function() {
return null;
}
};
},
error: () => "Unspecified error"
};
|