3cb6252a777f231a465b7fea1f4ecd57cc92abe2
[project/firewall4.git] / tests / lib / mocklib / fs.uc
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 + "-link";
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 opendir: (path) => {
139 let file = sprintf("fs/opendir~%s.json", replace(path, /[^A-Za-z0-9_-]+/g, '_')),
140 mock = mocklib.read_json_file(file),
141 index = 0;
142
143 if (!mock || mock != mock) {
144 mocklib.I("No stat result fixture defined for fs.opendir() call on %s.", path);
145 mocklib.I("Provide a mock result through the following JSON file:\n%s\n", file);
146
147 mock = [];
148 }
149
150 mocklib.trace_call("fs", "opendir", { path });
151
152 return {
153 read: function() {
154 return mock[index++];
155 },
156
157 tell: function() {
158 return index;
159 },
160
161 seek: function(i) {
162 index = i;
163 },
164
165 close: function() {},
166
167 error: function() {
168 return null;
169 }
170 };
171 },
172
173 error: () => "Unspecified error"
174 };