134673edc70e68f007044ed22c3d6749eaad5ee6
[project/firewall4.git] / tests / lib / mocklib / fs.uc
1 {%
2 let mocklib = global.mocklib,
3 fs = mocklib.require("fs");
4
5 return {
6 readlink: function(path) {
7 mocklib.trace_call("fs", "readlink", { path });
8
9 return path + "-link";
10 },
11
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);
15
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);
19
20 if (match(path, /\/$/))
21 mock = { type: "directory" };
22 else
23 mock = { type: "file" };
24 }
25
26 mocklib.trace_call("fs", "stat", { path });
27
28 return mock;
29 },
30
31 unlink: function(path) {
32 printf("fs.unlink() path <%s>\n", path);
33
34 return true;
35 },
36
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);
41
42 if (read && !mock) {
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);
45
46 return null;
47 }
48
49 mocklib.trace_call("fs", "popen", { cmdline, mode });
50
51 return {
52 read: function(amount) {
53 let rv;
54
55 switch (amount) {
56 case "all":
57 rv = mock;
58 mock = "";
59 break;
60
61 case "line":
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);
66 break;
67
68 default:
69 let n = +amount;
70 n = (n > 0) ? n : 0;
71 rv = substr(mock, 0, n);
72 mock = substr(mock, n);
73 break;
74 }
75
76 return rv;
77 },
78
79 write: function() {},
80 close: function() {},
81
82 error: function() {
83 return null;
84 }
85 };
86 },
87
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;
92
93 if (read && !mock) {
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);
96
97 return null;
98 }
99
100 mocklib.trace_call("fs", "open", { path: fpath, mode });
101
102 return {
103 read: function(amount) {
104 let rv;
105
106 switch (amount) {
107 case "all":
108 rv = mock;
109 mock = "";
110 break;
111
112 case "line":
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);
117 break;
118
119 default:
120 let n = +amount;
121 n = (n > 0) ? n : 0;
122 rv = substr(mock, 0, n);
123 mock = substr(mock, n);
124 break;
125 }
126
127 return rv;
128 },
129
130 write: function() {},
131 close: function() {},
132
133 error: function() {
134 return null;
135 }
136 };
137 },
138
139 error: () => "Unspecified error"
140 };