fs.uc 5.2 KB

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