tests: implement fs.opendir() mock interface
authorJo-Philipp Wich <jo@mein.io>
Sat, 5 Feb 2022 23:29:54 +0000 (00:29 +0100)
committerJo-Philipp Wich <jo@mein.io>
Sat, 5 Feb 2022 23:33:04 +0000 (00:33 +0100)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
tests/lib/mocklib/fs.uc

index 134673edc70e68f007044ed22c3d6749eaad5ee6..97521cc461bbd4efe331634d28357f32e23b7834 100644 (file)
                        };
                },
 
+               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"
        };